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