2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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.oransc.policyagent.clients;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.Mockito.mock;
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;
38 import java.util.Optional;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.api.extension.ExtendWith;
43 import org.mockito.junit.jupiter.MockitoExtension;
44 import org.mockito.stubbing.OngoingStubbing;
45 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
46 import org.oransc.policyagent.clients.SdncOscA1Client.AdapterOutput;
47 import org.oransc.policyagent.clients.SdncOscA1Client.AdapterRequest;
48 import org.oransc.policyagent.configuration.ControllerConfig;
49 import org.oransc.policyagent.configuration.ImmutableControllerConfig;
50 import org.oransc.policyagent.repository.Policy;
51 import org.oransc.policyagent.repository.Ric;
52 import org.springframework.web.reactive.function.client.WebClientResponseException;
54 import reactor.core.publisher.Mono;
55 import reactor.test.StepVerifier;
57 @ExtendWith(MockitoExtension.class)
58 public class SdncOscA1ClientTest {
59 private static final String CONTROLLER_USERNAME = "username";
60 private static final String CONTROLLER_PASSWORD = "password";
61 private static final String RIC_1_URL = "RicUrl";
62 private static final String GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy";
63 private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy";
64 private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy";
65 private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus";
66 private static final String POLICY_TYPE_1_ID = "type1";
67 private static final String POLICY_1_ID = "policy1";
68 private static final String POLICY_2_ID = "policy2";
69 private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}";
71 SdncOscA1Client clientUnderTest;
73 AsyncRestClient asyncRestClientMock;
75 private ControllerConfig controllerConfig() {
76 return ImmutableControllerConfig.builder() //
78 .baseUrl("baseUrl") //
79 .password(CONTROLLER_PASSWORD) //
80 .userName(CONTROLLER_USERNAME) //
86 asyncRestClientMock = mock(AsyncRestClient.class);
87 Ric ric = A1ClientHelper.createRic(RIC_1_URL);
89 clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), controllerConfig(),
94 public void testGetPolicyTypeIdentities_STD() {
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");
101 public void testGetPolicyTypeIdentities_OSC() {
102 clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, //
103 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
104 controllerConfig(), asyncRestClientMock);
106 String response = createResponse(Arrays.asList(POLICY_TYPE_1_ID));
107 whenAsyncPostThenReturn(Mono.just(response));
109 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
110 assertEquals(1, policyTypeIds.size(), "");
111 assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0), "");
113 String expUrl = RIC_1_URL + "/a1-p/policytypes";
114 ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
115 .nearRtRicUrl(expUrl) //
117 String expInput = SdncJsonHelper.createInputJsonString(expectedParams);
118 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
119 CONTROLLER_PASSWORD);
122 private String loadFile(String fileName) throws IOException {
123 ClassLoader loader = Thread.currentThread().getContextClassLoader();
124 URL url = loader.getResource(fileName);
125 File file = new File(url.getFile());
126 return new String(Files.readAllBytes(file.toPath()));
130 public void testGetTypeSchema_OSC() throws IOException {
131 clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, //
132 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
133 controllerConfig(), asyncRestClientMock);
135 String ricResponse = loadFile("test_osc_get_schema_response.json");
136 JsonElement elem = gson().fromJson(ricResponse, JsonElement.class);
137 String responseFromController = createResponse(elem);
138 whenAsyncPostThenReturn(Mono.just(responseFromController));
140 String response = clientUnderTest.getPolicyTypeSchema("policyTypeId").block();
141 JsonElement respJson = gson().fromJson(response, JsonElement.class);
142 assertEquals("policyTypeId", respJson.getAsJsonObject().get("title").getAsString(),
143 "title should be updated to contain policyType ID");
147 void parseJsonArrayOfString() {
148 // One integer and one string
149 String inputString = "[1, \"1\" ]";
151 List<String> result = SdncJsonHelper.parseJsonArrayOfString(inputString).collectList().block();
152 assertEquals(2, result.size(), "");
153 assertEquals("1", result.get(0), "");
154 assertEquals("1", result.get(1), "");
157 private String policiesUrl() {
158 return RIC_1_URL + "/A1-P/v1/policies";
161 private Gson gson() {
162 return SdncOscA1Client.gson;
165 private String createResponse(Object body) {
166 AdapterOutput output = ImmutableAdapterOutput.builder() //
167 .body(gson().toJson(body)) //
170 return SdncJsonHelper.createOutputJsonString(output);
174 public void testGetPolicyIdentities() {
176 String policyIdsResp = createResponse(Arrays.asList(POLICY_1_ID, POLICY_2_ID));
177 whenAsyncPostThenReturn(Mono.just(policyIdsResp));
179 List<String> returned = clientUnderTest.getPolicyIdentities().block();
180 assertEquals(2, returned.size(), "");
182 ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
183 .nearRtRicUrl(policiesUrl()) //
185 String expInput = SdncJsonHelper.createInputJsonString(expectedParams);
186 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
187 CONTROLLER_PASSWORD);
192 public void testGetValidPolicyType() {
193 String policyType = clientUnderTest.getPolicyTypeSchema("").block();
194 assertEquals("{}", policyType, "");
198 public void testPutPolicyValidResponse() {
199 whenPostReturnOkResponse();
201 String returned = clientUnderTest
202 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
204 assertEquals("OK", returned, "");
205 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
206 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
207 .nearRtRicUrl(expUrl) //
208 .body(POLICY_JSON_VALID) //
210 String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
212 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
216 public void testPutPolicyRejected() {
217 final String policyJson = "{}";
218 AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
220 .httpStatus(400) // ERROR
223 String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
224 whenAsyncPostThenReturn(Mono.just(resp));
226 Mono<String> returnedMono = clientUnderTest
227 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID));
228 StepVerifier.create(returnedMono) //
229 .expectSubscription() //
230 .expectErrorMatches(t -> t instanceof WebClientResponseException) //
233 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
234 AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() //
235 .nearRtRicUrl(expUrl) //
238 String expRequest = SdncJsonHelper.createInputJsonString(expRequestParams);
239 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME,
240 CONTROLLER_PASSWORD);
241 StepVerifier.create(returnedMono)
242 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
246 public void testDeletePolicy() {
247 whenPostReturnOkResponse();
249 String returned = clientUnderTest
250 .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
252 assertEquals("OK", returned, "");
253 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
254 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
255 .nearRtRicUrl(expUrl) //
257 String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
259 verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME,
260 CONTROLLER_PASSWORD);
264 public void testGetStatus() {
265 whenPostReturnOkResponse();
267 Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID);
269 String returnedStatus = clientUnderTest.getPolicyStatus(policy).block();
271 assertEquals("OK", returnedStatus, "unexpected status");
273 final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status";
274 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
275 .nearRtRicUrl(expUrl) //
277 String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
279 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME,
280 CONTROLLER_PASSWORD);
284 public void testGetVersion() {
285 whenPostReturnOkResponse();
286 A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block();
287 assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion, "");
289 whenPostReturnOkResponseNoBody();
290 returnedVersion = clientUnderTest.getProtocolVersion().block();
291 assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion, "");
294 private void whenPostReturnOkResponse() {
295 AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
300 String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
301 whenAsyncPostThenReturn(Mono.just(resp));
304 private void whenPostReturnOkResponseNoBody() {
305 AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
307 .body(Optional.empty()) //
310 String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
311 whenAsyncPostThenReturn(Mono.just(resp));
314 private OngoingStubbing<Mono<String>> whenAsyncPostThenReturn(Mono<String> response) {
315 return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString()))
316 .thenReturn(response);