2 * ========================LICENSE_START=================================
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
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.onap.ccsdk.oran.a1policymanagementservice.utils;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
26 import java.nio.charset.StandardCharsets;
27 import java.time.Duration;
28 import java.util.List;
29 import java.util.Vector;
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;
42 import reactor.core.publisher.Flux;
43 import reactor.core.publisher.Mono;
44 import reactor.core.publisher.MonoSink;
46 public class MockA1Client implements A1Client {
48 private final PolicyTypes policyTypes;
51 private Duration asynchDelay;
54 private String errorInject;
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);
65 public Mono<List<String>> getPolicyTypeIdentities() {
66 List<String> result = new Vector<>();
67 for (PolicyType p : this.policyTypes.getAll()) {
68 result.add(p.getId());
74 public Mono<List<String>> getPolicyIdentities() {
75 Vector<String> result = new Vector<>();
76 for (Policy policy : policies.getAll()) {
77 result.add(policy.getId());
84 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
86 return mono(this.policyTypes.getType(policyTypeId).getSchema());
87 } catch (Exception e) {
93 public Mono<String> putPolicy(Policy p) {
100 public Mono<String> deletePolicy(Policy policy) {
101 this.policies.remove(policy);
105 public Policies getPolicies() {
106 return this.policies;
110 public Mono<A1ProtocolType> getProtocolVersion() {
111 return mono(A1ProtocolType.STD_V1_1);
115 public Flux<String> deleteAllPolicies() {
116 this.policies.clear();
118 .flatMapMany(Flux::just);
122 public Mono<String> getPolicyStatus(Policy policy) {
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));
132 if (this.errorInject != null) {
133 res = res.flatMap(x -> monoError(this.errorInject, HttpStatus.BAD_GATEWAY));
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);
146 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
147 private void sleep() {
149 Thread.sleep(this.asynchDelay.toMillis());
150 } catch (InterruptedException e) {
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);