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.time.Duration;
24 import java.util.List;
25 import java.util.Vector;
27 import org.oransc.policyagent.clients.A1Client;
28 import org.oransc.policyagent.repository.Policies;
29 import org.oransc.policyagent.repository.Policy;
30 import org.oransc.policyagent.repository.PolicyType;
31 import org.oransc.policyagent.repository.PolicyTypes;
33 import reactor.core.publisher.Flux;
34 import reactor.core.publisher.Mono;
35 import reactor.core.publisher.MonoSink;
37 public class MockA1Client implements A1Client {
38 Policies policies = new Policies();
39 private final PolicyTypes policyTypes;
40 private final Duration asynchDelay;
42 public MockA1Client(PolicyTypes policyTypes, Duration asynchDelay) {
43 this.policyTypes = policyTypes;
44 this.asynchDelay = asynchDelay;
48 public Mono<List<String>> getPolicyTypeIdentities() {
49 List<String> result = new Vector<>();
50 for (PolicyType p : this.policyTypes.getAll()) {
57 public Mono<List<String>> getPolicyIdentities() {
58 Vector<String> result = new Vector<>();
59 for (Policy policy : policies.getAll()) {
60 result.add(policy.id());
67 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
69 return mono(this.policyTypes.getType(policyTypeId).schema());
70 } catch (Exception e) {
76 public Mono<String> putPolicy(Policy p) {
83 public Mono<String> deletePolicy(Policy policy) {
84 this.policies.remove(policy);
88 public Policies getPolicies() {
93 public Mono<A1ProtocolType> getProtocolVersion() {
94 return mono(A1ProtocolType.STD_V1_1);
98 public Flux<String> deleteAllPolicies() {
99 this.policies.clear();
101 .flatMapMany(Flux::just);
105 public Mono<String> getPolicyStatus(Policy policy) {
109 private <T> Mono<T> mono(T value) {
110 if (this.asynchDelay.isZero()) {
111 return Mono.just(value);
113 return Mono.create(monoSink -> asynchResponse(monoSink, value));
117 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
118 private void sleep() {
120 Thread.sleep(this.asynchDelay.toMillis());
121 } catch (InterruptedException e) {
126 private <T> void asynchResponse(MonoSink<T> callback, T str) {
127 Thread thread = new Thread(() -> {
128 sleep(); // Simulate a network delay
129 callback.success(str);