fc0eba35326a4b7d5bcd14ae7447b52a73300b37
[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.nio.charset.StandardCharsets;
24 import java.time.Duration;
25 import java.util.List;
26 import java.util.Vector;
27
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;
35
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
38 import reactor.core.publisher.MonoSink;
39
40 public class MockA1Client implements A1Client {
41     Policies policies = new Policies();
42     private final PolicyTypes policyTypes;
43     private final Duration asynchDelay;
44
45     public MockA1Client(PolicyTypes policyTypes, Duration asynchDelay) {
46         this.policyTypes = policyTypes;
47         this.asynchDelay = asynchDelay;
48     }
49
50     @Override
51     public Mono<List<String>> getPolicyTypeIdentities() {
52         List<String> result = new Vector<>();
53         for (PolicyType p : this.policyTypes.getAll()) {
54             result.add(p.name());
55         }
56         return mono(result);
57     }
58
59     @Override
60     public Mono<List<String>> getPolicyIdentities() {
61         Vector<String> result = new Vector<>();
62         for (Policy policy : policies.getAll()) {
63             result.add(policy.id());
64         }
65
66         return mono(result);
67     }
68
69     @Override
70     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
71         try {
72             return mono(this.policyTypes.getType(policyTypeId).schema());
73         } catch (Exception e) {
74             return Mono.error(e);
75         }
76     }
77
78     @Override
79     public Mono<String> putPolicy(Policy p) {
80         this.policies.put(p);
81         return mono("OK");
82
83     }
84
85     @Override
86     public Mono<String> deletePolicy(Policy policy) {
87         this.policies.remove(policy);
88         return mono("OK");
89     }
90
91     public Policies getPolicies() {
92         return this.policies;
93     }
94
95     @Override
96     public Mono<A1ProtocolType> getProtocolVersion() {
97         return mono(A1ProtocolType.STD_V1_1);
98     }
99
100     @Override
101     public Flux<String> deleteAllPolicies() {
102         this.policies.clear();
103         return mono("OK") //
104             .flatMapMany(Flux::just);
105     }
106
107     @Override
108     public Mono<String> getPolicyStatus(Policy policy) {
109         return mono("OK");
110     }
111
112     private <T> Mono<T> mono(T value) {
113         if (this.asynchDelay.isZero()) {
114             return Mono.just(value);
115         } else {
116             return Mono.create(monoSink -> asynchResponse(monoSink, value));
117         }
118     }
119
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);
125     }
126
127     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
128     private void sleep() {
129         try {
130             Thread.sleep(this.asynchDelay.toMillis());
131         } catch (InterruptedException e) {
132             e.printStackTrace();
133         }
134     }
135
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);
140         });
141         thread.start();
142     }
143
144 }