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 synchronized (this.policyTypes) {
50 List<String> result = new Vector<>();
51 for (PolicyType p : this.policyTypes.getAll()) {
59 public Mono<List<String>> getPolicyIdentities() {
60 synchronized (this.policies) {
61 Vector<String> result = new Vector<>();
62 for (Policy policy : policies.getAll()) {
63 result.add(policy.id());
71 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
73 return mono(this.policyTypes.getType(policyTypeId).schema());
74 } catch (Exception e) {
80 public Mono<String> putPolicy(Policy p) {
87 public Mono<String> deletePolicy(Policy policy) {
88 this.policies.remove(policy);
92 public Policies getPolicies() {
97 public Mono<A1ProtocolType> getProtocolVersion() {
98 return mono(A1ProtocolType.STD_V1_1);
102 public Flux<String> deleteAllPolicies() {
103 this.policies.clear();
105 .flatMapMany(Flux::just);
109 public Mono<String> getPolicyStatus(Policy policy) {
113 private <T> Mono<T> mono(T value) {
114 if (this.asynchDelay.isZero()) {
115 return Mono.just(value);
117 return Mono.create(monoSink -> asynchResponse(monoSink, value));
121 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
122 private void sleep() {
124 Thread.sleep(this.asynchDelay.toMillis());
125 } catch (InterruptedException e) {
130 private <T> void asynchResponse(MonoSink<T> callback, T str) {
131 Thread thread = new Thread(() -> {
132 sleep(); // Simulate a network delay
133 callback.success(str);