2 * ========================LICENSE_START=================================
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
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;
30 import java.util.Vector;
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;
43 import reactor.core.publisher.Flux;
44 import reactor.core.publisher.Mono;
45 import reactor.core.publisher.MonoSink;
47 public class MockA1Client implements A1Client {
49 private final PolicyTypes policyTypes;
52 private Duration asynchDelay;
55 private String errorInject;
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);
66 public Mono<List<String>> getPolicyTypeIdentities() {
67 List<String> result = new Vector<>();
68 for (PolicyType p : this.policyTypes.getAll()) {
69 result.add(p.getId());
75 public Mono<List<String>> getPolicyIdentities() {
76 Vector<String> result = new Vector<>();
77 for (Policy policy : policies.getAll()) {
78 result.add(policy.getId());
85 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
87 return mono(this.policyTypes.getType(policyTypeId).getSchema());
88 } catch (Exception e) {
94 public Mono<String> putPolicy(Policy p) {
101 public Mono<String> deletePolicy(Policy policy) {
102 this.policies.remove(policy);
106 public Policies getPolicies() {
107 return this.policies;
111 public Mono<A1ProtocolType> getProtocolVersion() {
112 return mono(A1ProtocolType.STD_V1_1);
116 public Flux<String> deleteAllPolicies(Set<String> excludePolicyId) {
117 this.policies.clear();
119 .flatMapMany(Flux::just);
123 public Mono<String> getPolicyStatus(Policy policy) {
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));
133 if (this.errorInject != null) {
134 res = res.flatMap(x -> monoError(this.errorInject, HttpStatus.BAD_GATEWAY));
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);
147 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
148 private void sleep() {
150 Thread.sleep(this.asynchDelay.toMillis());
151 } catch (InterruptedException e) {
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);