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;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.Optional;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.junit.jupiter.MockitoExtension;
39 import org.mockito.stubbing.OngoingStubbing;
40 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
41 import org.oransc.policyagent.clients.SdncOscA1Client.AdapterOutput;
42 import org.oransc.policyagent.configuration.ControllerConfig;
43 import org.oransc.policyagent.configuration.ImmutableControllerConfig;
44 import org.oransc.policyagent.repository.Policy;
45 import org.oransc.policyagent.repository.Ric;
46 import org.springframework.web.reactive.function.client.WebClientResponseException;
48 import reactor.core.publisher.Mono;
49 import reactor.test.StepVerifier;
51 @ExtendWith(MockitoExtension.class)
52 public class SdncOscA1ClientTest {
53 private static final String CONTROLLER_USERNAME = "username";
54 private static final String CONTROLLER_PASSWORD = "password";
55 private static final String RIC_1_URL = "RicUrl";
56 private static final String GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy";
57 private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy";
58 private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy";
59 private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus";
60 private static final String POLICY_TYPE_1_ID = "type1";
61 private static final String POLICY_1_ID = "policy1";
62 private static final String POLICY_2_ID = "policy2";
63 private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}";
65 SdncOscA1Client clientUnderTest;
67 AsyncRestClient asyncRestClientMock;
69 private ControllerConfig controllerConfig() {
70 return ImmutableControllerConfig.builder() //
72 .baseUrl("baseUrl") //
73 .password(CONTROLLER_PASSWORD) //
74 .userName(CONTROLLER_USERNAME) //
80 asyncRestClientMock = mock(AsyncRestClient.class);
81 Ric ric = A1ClientHelper.createRic(RIC_1_URL);
83 clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), controllerConfig(),
88 public void testGetPolicyTypeIdentities_STD() {
89 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
90 assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
91 assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
95 public void testGetPolicyTypeIdentities_OSC() {
96 clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, //
97 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
98 controllerConfig(), asyncRestClientMock);
100 String response = createResponse(Arrays.asList(POLICY_TYPE_1_ID));
101 whenAsyncPostThenReturn(Mono.just(response));
103 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
104 assertEquals(1, policyTypeIds.size(), "");
105 assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0), "");
107 String expUrl = RIC_1_URL + "/a1-p/policytypes";
108 ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
109 .nearRtRicUrl(expUrl) //
111 String expInput = SdncJsonHelper.createInputJsonString(expectedParams);
112 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
113 CONTROLLER_PASSWORD);
117 private String policiesUrl() {
118 return RIC_1_URL + "/A1-P/v1/policies";
121 private Gson gson() {
122 return SdncOscA1Client.gson;
125 private String createResponse(Object body) {
126 AdapterOutput output = ImmutableAdapterOutput.builder() //
127 .body(gson().toJson(body)) //
130 return SdncJsonHelper.createOutputJsonString(output);
134 public void testGetPolicyIdentities() {
136 String policyIdsResp = createResponse(Arrays.asList(POLICY_1_ID, POLICY_2_ID));
137 whenAsyncPostThenReturn(Mono.just(policyIdsResp));
139 List<String> returned = clientUnderTest.getPolicyIdentities().block();
140 assertEquals(2, returned.size(), "");
142 ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
143 .nearRtRicUrl(policiesUrl()) //
145 String expInput = SdncJsonHelper.createInputJsonString(expectedParams);
146 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
147 CONTROLLER_PASSWORD);
152 public void testGetValidPolicyType() {
153 String policyType = clientUnderTest.getPolicyTypeSchema("").block();
154 assertEquals("{}", policyType, "");
158 public void testPutPolicyValidResponse() {
159 whenPostReturnOkResponse();
161 String returned = clientUnderTest
162 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
164 assertEquals("OK", returned, "");
165 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
166 ImmutableAdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
167 .nearRtRicUrl(expUrl) //
168 .body(POLICY_JSON_VALID) //
170 String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
172 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
176 public void testPutPolicyRejected() {
177 final String policyJson = "{}";
178 AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
180 .httpStatus(400) // ERROR
183 String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
184 whenAsyncPostThenReturn(Mono.just(resp));
186 Mono<String> returnedMono = clientUnderTest
187 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID));
189 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
190 ImmutableAdapterRequest expRequestParams = ImmutableAdapterRequest.builder() //
191 .nearRtRicUrl(expUrl) //
194 String expRequest = SdncJsonHelper.createInputJsonString(expRequestParams);
195 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME,
196 CONTROLLER_PASSWORD);
197 StepVerifier.create(returnedMono)
198 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
202 public void testDeletePolicy() {
203 whenPostReturnOkResponse();
205 String returned = clientUnderTest
206 .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
208 assertEquals("OK", returned, "");
209 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
210 ImmutableAdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
211 .nearRtRicUrl(expUrl) //
213 String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
215 verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME,
216 CONTROLLER_PASSWORD);
220 public void testGetStatus() {
221 whenPostReturnOkResponse();
223 Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID);
225 String returnedStatus = clientUnderTest.getPolicyStatus(policy).block();
227 assertEquals("OK", returnedStatus, "unexpected status");
229 final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status";
230 ImmutableAdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
231 .nearRtRicUrl(expUrl) //
233 String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
235 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME,
236 CONTROLLER_PASSWORD);
240 public void testGetVersion() {
241 whenPostReturnOkResponse();
242 A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block();
243 assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion, "");
245 whenPostReturnOkResponseNoBody();
246 returnedVersion = clientUnderTest.getProtocolVersion().block();
247 assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion, "");
250 private void whenPostReturnOkResponse() {
251 AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
256 String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
257 whenAsyncPostThenReturn(Mono.just(resp));
260 private void whenPostReturnOkResponseNoBody() {
261 AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
263 .body(Optional.empty()) //
266 String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
267 whenAsyncPostThenReturn(Mono.just(resp));
270 private OngoingStubbing<Mono<String>> whenAsyncPostThenReturn(Mono<String> response) {
271 return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString()))
272 .thenReturn(response);