2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.policyagent.utils;
23 import java.nio.charset.StandardCharsets;
24 import java.time.Duration;
25 import java.util.List;
26 import java.util.Vector;
28 import org.oransc.policyagent.clients.A1Client;
29 import org.oransc.policyagent.repository.Policies;
30 import org.oransc.policyagent.repository.Policy;
31 import org.oransc.policyagent.repository.PolicyType;
32 import org.oransc.policyagent.repository.PolicyTypes;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.web.reactive.function.client.WebClientResponseException;
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
38 import reactor.core.publisher.MonoSink;
40 public class MockA1Client implements A1Client {
41 Policies policies = new Policies();
42 private final PolicyTypes policyTypes;
43 private final Duration asynchDelay;
45 public MockA1Client(PolicyTypes policyTypes, Duration asynchDelay) {
46 this.policyTypes = policyTypes;
47 this.asynchDelay = asynchDelay;
51 public Mono<List<String>> getPolicyTypeIdentities() {
52 List<String> result = new Vector<>();
53 for (PolicyType p : this.policyTypes.getAll()) {
60 public Mono<List<String>> getPolicyIdentities() {
61 Vector<String> result = new Vector<>();
62 for (Policy policy : policies.getAll()) {
63 result.add(policy.id());
70 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
72 return mono(this.policyTypes.getType(policyTypeId).schema());
73 } catch (Exception e) {
79 public Mono<String> putPolicy(Policy p) {
86 public Mono<String> deletePolicy(Policy policy) {
87 this.policies.remove(policy);
91 public Policies getPolicies() {
96 public Mono<A1ProtocolType> getProtocolVersion() {
97 return mono(A1ProtocolType.STD_V1_1);
101 public Flux<String> deleteAllPolicies() {
102 this.policies.clear();
104 .flatMapMany(Flux::just);
108 public Mono<String> getPolicyStatus(Policy policy) {
112 private <T> Mono<T> mono(T value) {
113 if (this.asynchDelay.isZero()) {
114 return Mono.just(value);
116 return Mono.create(monoSink -> asynchResponse(monoSink, value));
120 Mono<String> monoError(String responseBody, HttpStatus status) {
121 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
122 WebClientResponseException a1Exception = new WebClientResponseException(status.value(),
123 status.getReasonPhrase(), null, responseBodyBytes, StandardCharsets.UTF_8, null);
124 return Mono.error(a1Exception);
127 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
128 private void sleep() {
130 Thread.sleep(this.asynchDelay.toMillis());
131 } catch (InterruptedException e) {
136 private <T> void asynchResponse(MonoSink<T> callback, T str) {
137 Thread thread = new Thread(() -> {
138 sleep(); // Simulate a network delay
139 callback.success(str);