91b762c5bb4f4b5b2dc78a6476e714b22a31b542
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
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.onap.ccsdk.oran.a1policymanagementservice.utils;
22
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
25
26 import java.nio.charset.StandardCharsets;
27 import java.time.Duration;
28 import java.util.List;
29 import java.util.Set;
30 import java.util.Vector;
31
32 import lombok.Setter;
33
34 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client;
35 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
36 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
37 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
38 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
39 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.web.reactive.function.client.WebClientResponseException;
42
43 import reactor.core.publisher.Flux;
44 import reactor.core.publisher.Mono;
45 import reactor.core.publisher.MonoSink;
46
47 public class MockA1Client implements A1Client {
48     Policies policies;
49     private final PolicyTypes policyTypes;
50
51     @Setter
52     private Duration asynchDelay;
53
54     @Setter
55     private String errorInject;
56
57     public MockA1Client(String ricId, ApplicationConfig appConfig, PolicyTypes policyTypes, Duration asynchDelay) {
58         this.policyTypes = policyTypes;
59         this.asynchDelay = asynchDelay;
60         ApplicationConfig cfg = spy(appConfig);
61         when(cfg.getVardataDirectory()).thenReturn(null);
62         this.policies = new Policies(cfg);
63     }
64
65     @Override
66     public Mono<List<String>> getPolicyTypeIdentities() {
67         List<String> result = new Vector<>();
68         for (PolicyType p : this.policyTypes.getAll()) {
69             result.add(p.getId());
70         }
71         return mono(result);
72     }
73
74     @Override
75     public Mono<List<String>> getPolicyIdentities() {
76         Vector<String> result = new Vector<>();
77         for (Policy policy : policies.getAll()) {
78             result.add(policy.getId());
79         }
80
81         return mono(result);
82     }
83
84     @Override
85     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
86         try {
87             return mono(this.policyTypes.getType(policyTypeId).getSchema());
88         } catch (Exception e) {
89             return Mono.error(e);
90         }
91     }
92
93     @Override
94     public Mono<String> putPolicy(Policy p) {
95         this.policies.put(p);
96         return mono("OK");
97
98     }
99
100     @Override
101     public Mono<String> deletePolicy(Policy policy) {
102         this.policies.remove(policy);
103         return mono("OK");
104     }
105
106     public Policies getPolicies() {
107         return this.policies;
108     }
109
110     @Override
111     public Mono<A1ProtocolType> getProtocolVersion() {
112         return mono(A1ProtocolType.STD_V1_1);
113     }
114
115     @Override
116     public Flux<String> deleteAllPolicies(Set<String> excludePolicyId) {
117         this.policies.clear();
118         return mono("OK") //
119                 .flatMapMany(Flux::just);
120     }
121
122     @Override
123     public Mono<String> getPolicyStatus(Policy policy) {
124         return mono("OK");
125     }
126
127     private <T> Mono<T> mono(T value) {
128         Mono<T> res = Mono.just(value);
129         if (!this.asynchDelay.isZero()) {
130             res = Mono.create(monoSink -> asynchResponse(monoSink, value));
131         }
132
133         if (this.errorInject != null) {
134             res = res.flatMap(x -> monoError(this.errorInject, HttpStatus.BAD_GATEWAY));
135         }
136
137         return res;
138     }
139
140     public static <T> Mono<T> monoError(String responseBody, HttpStatus status) {
141         byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
142         WebClientResponseException a1Exception = new WebClientResponseException(status.value(),
143                 status.getReasonPhrase(), null, responseBodyBytes, StandardCharsets.UTF_8, null);
144         return Mono.error(a1Exception);
145     }
146
147     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
148     private void sleep() {
149         try {
150             Thread.sleep(this.asynchDelay.toMillis());
151         } catch (InterruptedException e) {
152             e.printStackTrace();
153         }
154     }
155
156     private <T> void asynchResponse(MonoSink<T> callback, T str) {
157         Thread thread = new Thread(() -> {
158             sleep(); // Simulate a network delay
159             callback.success(str);
160         });
161         thread.start();
162     }
163
164 }