0fa5be4f7d4a3b5b7a281d94e607c59c8399fae4
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / utils / MockA1Client.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.policyagent.utils;
22
23 import java.time.Duration;
24 import java.util.List;
25 import java.util.Vector;
26
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;
32
33 import reactor.core.publisher.Flux;
34 import reactor.core.publisher.Mono;
35 import reactor.core.publisher.MonoSink;
36
37 public class MockA1Client implements A1Client {
38     Policies policies = new Policies();
39     private final PolicyTypes policyTypes;
40     private final Duration asynchDelay;
41
42     public MockA1Client(PolicyTypes policyTypes, Duration asynchDelay) {
43         this.policyTypes = policyTypes;
44         this.asynchDelay = asynchDelay;
45     }
46
47     @Override
48     public Mono<List<String>> getPolicyTypeIdentities() {
49         List<String> result = new Vector<>();
50         for (PolicyType p : this.policyTypes.getAll()) {
51             result.add(p.name());
52         }
53         return mono(result);
54     }
55
56     @Override
57     public Mono<List<String>> getPolicyIdentities() {
58         Vector<String> result = new Vector<>();
59         for (Policy policy : policies.getAll()) {
60             result.add(policy.id());
61         }
62
63         return mono(result);
64     }
65
66     @Override
67     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
68         try {
69             return mono(this.policyTypes.getType(policyTypeId).schema());
70         } catch (Exception e) {
71             return Mono.error(e);
72         }
73     }
74
75     @Override
76     public Mono<String> putPolicy(Policy p) {
77         this.policies.put(p);
78         return mono("OK");
79
80     }
81
82     @Override
83     public Mono<String> deletePolicy(Policy policy) {
84         this.policies.remove(policy);
85         return mono("OK");
86     }
87
88     public Policies getPolicies() {
89         return this.policies;
90     }
91
92     @Override
93     public Mono<A1ProtocolType> getProtocolVersion() {
94         return mono(A1ProtocolType.STD_V1_1);
95     }
96
97     @Override
98     public Flux<String> deleteAllPolicies() {
99         this.policies.clear();
100         return mono("OK") //
101             .flatMapMany(Flux::just);
102     }
103
104     @Override
105     public Mono<String> getPolicyStatus(Policy policy) {
106         return mono("OK");
107     }
108
109     private <T> Mono<T> mono(T value) {
110         if (this.asynchDelay.isZero()) {
111             return Mono.just(value);
112         } else {
113             return Mono.create(monoSink -> asynchResponse(monoSink, value));
114         }
115     }
116
117     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
118     private void sleep() {
119         try {
120             Thread.sleep(this.asynchDelay.toMillis());
121         } catch (InterruptedException e) {
122             e.printStackTrace();
123         }
124     }
125
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);
130         });
131         thread.start();
132     }
133
134 }