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.clients;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
29 import com.google.gson.Gson;
30 import com.google.gson.JsonElement;
33 import java.io.IOException;
35 import java.nio.file.Files;
36 import java.util.Arrays;
37 import java.util.List;
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.api.extension.ExtendWith;
41 import org.mockito.Mock;
42 import org.mockito.junit.jupiter.MockitoExtension;
43 import org.mockito.stubbing.OngoingStubbing;
44 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType;
45 import org.onap.ccsdk.oran.a1policymanagementservice.clients.CcsdkA1AdapterClient.AdapterOutput;
46 import org.onap.ccsdk.oran.a1policymanagementservice.clients.CcsdkA1AdapterClient.AdapterRequest;
47 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig;
48 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
49 import org.springframework.http.HttpStatus;
50 import org.springframework.web.reactive.function.client.WebClientResponseException;
52 import reactor.core.publisher.Mono;
53 import reactor.test.StepVerifier;
55 @ExtendWith(MockitoExtension.class)
56 class CcsdkA1AdapterClientTest {
57 private static final String CONTROLLER_USERNAME = "username";
58 private static final String CONTROLLER_PASSWORD = "password";
59 private static final String RIC_1_URL = "RicUrl";
60 private static final String GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy";
61 private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy";
62 private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy";
63 private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus";
64 private static final String POLICY_TYPE_1_ID = "type1";
65 private static final String POLICY_1_ID = "policy1";
66 private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}";
68 CcsdkA1AdapterClient clientUnderTest;
71 AsyncRestClient asyncRestClientMock;
73 private ControllerConfig controllerConfig() {
74 return ControllerConfig.builder() //
76 .baseUrl("baseUrl") //
77 .password(CONTROLLER_PASSWORD) //
78 .userName(CONTROLLER_USERNAME) //
83 void createClientWithWrongProtocol_thenErrorIsThrown() {
84 AsyncRestClient asyncRestClient = new AsyncRestClient("", null, null, new SecurityContext(""));
85 assertThrows(IllegalArgumentException.class, () -> {
86 new CcsdkA1AdapterClient(A1ProtocolType.STD_V1_1, null, null, asyncRestClient);
91 void getPolicyTypeIdentities_STD_V1() {
92 clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, //
93 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
94 controllerConfig(), asyncRestClientMock);
95 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
96 assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
97 assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
100 private void testGetPolicyTypeIdentities(A1ProtocolType protocolType, String expUrl) {
101 clientUnderTest = new CcsdkA1AdapterClient(protocolType, //
102 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
103 controllerConfig(), asyncRestClientMock);
105 String response = createOkResponseWithBody(Arrays.asList(POLICY_TYPE_1_ID));
106 whenAsyncPostThenReturn(Mono.just(response));
108 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
110 assertEquals(1, policyTypeIds.size());
111 assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0));
113 AdapterRequest expectedParams = new AdapterRequest(expUrl, null);
115 String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams);
116 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
117 CONTROLLER_PASSWORD);
121 void getPolicyTypeIdentities_OSC() {
122 testGetPolicyTypeIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, RIC_1_URL + "/a1-p/policytypes");
126 void getPolicyTypeIdentities_STD_V2() {
127 testGetPolicyTypeIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, RIC_1_URL + "/A1-P/v2/policytypes");
131 void getTypeSchema_STD_V1() {
133 clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, //
134 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
135 controllerConfig(), asyncRestClientMock);
137 String policyType = clientUnderTest.getPolicyTypeSchema("").block();
139 assertEquals("{}", policyType);
142 private void testGetTypeSchema(A1ProtocolType protocolType, String expUrl, String policyTypeId,
143 String getSchemaResponseFile) throws IOException {
144 clientUnderTest = new CcsdkA1AdapterClient(protocolType, //
145 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
146 controllerConfig(), asyncRestClientMock);
148 String ricResponse = loadFile(getSchemaResponseFile);
149 JsonElement elem = gson().fromJson(ricResponse, JsonElement.class);
150 String responseFromController = createOkResponseWithBody(elem);
151 whenAsyncPostThenReturn(Mono.just(responseFromController));
153 String response = clientUnderTest.getPolicyTypeSchema(policyTypeId).block();
155 JsonElement respJson = gson().fromJson(response, JsonElement.class);
156 assertEquals(policyTypeId, respJson.getAsJsonObject().get("title").getAsString(),
157 "title should be updated to contain policyType ID");
159 AdapterRequest expectedParams = new AdapterRequest(expUrl, null);
161 String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams);
163 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
164 CONTROLLER_PASSWORD);
168 void getTypeSchema_OSC() throws IOException {
169 String expUrl = RIC_1_URL + "/a1-p/policytypes/policyTypeId";
170 testGetTypeSchema(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrl, "policyTypeId",
171 "test_osc_get_schema_response.json");
175 void getTypeSchema_STD_V2() throws IOException {
176 String expUrl = RIC_1_URL + "/A1-P/v2/policytypes/policyTypeId";
177 testGetTypeSchema(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrl, "policyTypeId",
178 "test_oran_get_schema_response.json");
182 void parseJsonArrayOfString() {
183 // One integer and one string
184 String inputString = "[1, \"1\" ]";
186 List<String> result = A1AdapterJsonHelper.parseJsonArrayOfString(inputString).collectList().block();
187 assertEquals(2, result.size());
188 assertEquals("1", result.get(0));
189 assertEquals("1", result.get(1));
192 private void getPolicyIdentities(A1ProtocolType protocolType, String... expUrls) {
193 clientUnderTest = new CcsdkA1AdapterClient(protocolType, //
194 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
195 controllerConfig(), asyncRestClientMock);
196 String resp = createOkResponseWithBody(Arrays.asList("xxx"));
197 whenAsyncPostThenReturn(Mono.just(resp));
199 List<String> returned = clientUnderTest.getPolicyIdentities().block();
201 assertEquals(1, returned.size());
202 for (String expUrl : expUrls) {
203 AdapterRequest expectedParams = new AdapterRequest(expUrl, null);
205 String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams);
206 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
207 CONTROLLER_PASSWORD);
212 void getPolicyIdentities_STD_V1() {
213 String expUrl = RIC_1_URL + "/A1-P/v1/policies";
214 getPolicyIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, expUrl);
218 void getPolicyIdentities_STD_V2() {
219 String expUrlPolicies = RIC_1_URL + "/A1-P/v2/policytypes";
220 String expUrlInstances = RIC_1_URL + "/A1-P/v2/policytypes/xxx/policies";
221 getPolicyIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrlPolicies, expUrlInstances);
225 void getPolicyIdentities_OSC() {
226 String expUrlTypes = RIC_1_URL + "/a1-p/policytypes";
227 String expUrlInstances = RIC_1_URL + "/a1-p/policytypes/xxx/policies";
228 getPolicyIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrlTypes, expUrlInstances);
231 private void putPolicy(A1ProtocolType protocolType, String expUrl) {
232 clientUnderTest = new CcsdkA1AdapterClient(protocolType, //
233 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
234 controllerConfig(), asyncRestClientMock);
236 whenPostReturnOkResponse();
238 String returned = clientUnderTest
239 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
242 assertEquals("OK", returned);
243 AdapterRequest expectedInputParams = new AdapterRequest(expUrl, POLICY_JSON_VALID);
244 String expInput = A1AdapterJsonHelper.createInputJsonString(expectedInputParams);
246 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
251 void putPolicy_OSC() {
252 String expUrl = RIC_1_URL + "/a1-p/policytypes/type1/policies/policy1";
253 putPolicy(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrl);
257 void putPolicy_STD_V1() {
258 String expUrl = RIC_1_URL + "/A1-P/v1/policies/policy1";
259 putPolicy(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, expUrl);
263 void putPolicy_STD_V2() {
265 RIC_1_URL + "/A1-P/v2/policytypes/type1/policies/policy1?notificationDestination=https://test.com";
266 putPolicy(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrl);
270 void postRejected() {
271 clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, //
272 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
273 controllerConfig(), asyncRestClientMock);
275 final String policyJson = "{}";
276 AdapterOutput adapterOutput = new AdapterOutput(HttpStatus.BAD_REQUEST.value(), "NOK");
278 String resp = A1AdapterJsonHelper.createOutputJsonString(adapterOutput);
279 whenAsyncPostThenReturn(Mono.just(resp));
281 Mono<String> returnedMono = clientUnderTest
282 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID));
283 StepVerifier.create(returnedMono) //
284 .expectSubscription() //
285 .expectErrorMatches(t -> t instanceof WebClientResponseException) //
288 StepVerifier.create(returnedMono).expectErrorMatches(throwable -> {
289 return throwable instanceof WebClientResponseException;
293 private void deleteAllPolicies(A1ProtocolType protocolType, String expUrl) {
294 clientUnderTest = new CcsdkA1AdapterClient(protocolType, //
295 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
296 controllerConfig(), asyncRestClientMock);
297 String resp = createOkResponseWithBody(Arrays.asList("xxx"));
298 whenAsyncPostThenReturn(Mono.just(resp));
300 clientUnderTest.deleteAllPolicies().blockLast();
302 AdapterRequest expectedParams = new AdapterRequest(expUrl, null);
304 String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams);
305 verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME,
306 CONTROLLER_PASSWORD);
310 void deleteAllPolicies_STD_V2() {
311 String expUrl1 = RIC_1_URL + "/A1-P/v2/policytypes/xxx/policies/xxx";
312 deleteAllPolicies(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrl1);
316 void deleteAllPolicies_STD_V1() {
317 String expUrl1 = RIC_1_URL + "/A1-P/v1/policies/xxx";
318 deleteAllPolicies(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, expUrl1);
322 void deleteAllPolicies_OSC() {
323 String expUrl1 = RIC_1_URL + "/a1-p/policytypes/xxx/policies/xxx";
324 deleteAllPolicies(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrl1);
328 void getVersion_OSC() {
329 clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, // Version irrelevant here
330 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
331 controllerConfig(), asyncRestClientMock);
333 whenAsyncPostThenReturn(Mono.error(new Exception("Error"))).thenReturn(Mono.just(createOkResponseString(true)));
335 A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block();
337 assertEquals(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, returnedVersion);
341 void testGetStatus() {
342 clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, //
343 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
344 controllerConfig(), asyncRestClientMock);
345 whenPostReturnOkResponse();
347 Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID);
349 String response = clientUnderTest.getPolicyStatus(policy).block();
350 assertEquals("OK", response);
352 String expUrl = RIC_1_URL + "/A1-P/v2/policytypes/type1/policies/policy1/status";
353 AdapterRequest expectedParams = new AdapterRequest(expUrl, null);
355 String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams);
356 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME,
357 CONTROLLER_PASSWORD);
361 private Gson gson() {
362 return CcsdkA1AdapterClient.gson;
365 private String loadFile(String fileName) throws IOException {
366 ClassLoader loader = Thread.currentThread().getContextClassLoader();
367 URL url = loader.getResource(fileName);
368 File file = new File(url.getFile());
369 return new String(Files.readAllBytes(file.toPath()));
372 private void whenPostReturnOkResponse() {
373 whenAsyncPostThenReturn(Mono.just(createOkResponseString(true)));
376 void whenPostReturnOkResponseNoBody() {
377 whenAsyncPostThenReturn(Mono.just(createOkResponseString(false)));
380 private String createOkResponseWithBody(Object body) {
381 AdapterOutput output = new AdapterOutput(HttpStatus.OK.value(), gson().toJson(body));
382 return A1AdapterJsonHelper.createOutputJsonString(output);
385 private String createOkResponseString(boolean withBody) {
386 String body = withBody ? HttpStatus.OK.name() : null;
387 AdapterOutput output = new AdapterOutput(HttpStatus.OK.value(), body);
388 return A1AdapterJsonHelper.createOutputJsonString(output);
391 private OngoingStubbing<Mono<String>> whenAsyncPostThenReturn(Mono<String> response) {
392 return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString()))
393 .thenReturn(response);