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.clients.SdncOscA1Client.AdapterRequest;
43 import org.oransc.policyagent.configuration.ControllerConfig;
44 import org.oransc.policyagent.configuration.ImmutableControllerConfig;
45 import org.oransc.policyagent.repository.Policy;
46 import org.oransc.policyagent.repository.Ric;
47 import org.springframework.web.reactive.function.client.WebClientResponseException;
49 import reactor.core.publisher.Mono;
50 import reactor.test.StepVerifier;
52 @ExtendWith(MockitoExtension.class)
53 public class SdncOscA1ClientTest {
54 private static final String CONTROLLER_USERNAME = "username";
55 private static final String CONTROLLER_PASSWORD = "password";
56 private static final String RIC_1_URL = "RicUrl";
57 private static final String GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy";
58 private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy";
59 private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy";
60 private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus";
61 private static final String POLICY_TYPE_1_ID = "type1";
62 private static final String POLICY_1_ID = "policy1";
63 private static final String POLICY_2_ID = "policy2";
64 private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}";
66 SdncOscA1Client clientUnderTest;
68 AsyncRestClient asyncRestClientMock;
70 private ControllerConfig controllerConfig() {
71 return ImmutableControllerConfig.builder() //
73 .baseUrl("baseUrl") //
74 .password(CONTROLLER_PASSWORD) //
75 .userName(CONTROLLER_USERNAME) //
81 asyncRestClientMock = mock(AsyncRestClient.class);
82 Ric ric = A1ClientHelper.createRic(RIC_1_URL);
84 clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), controllerConfig(),
89 public void testGetPolicyTypeIdentities_STD() {
90 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
91 assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
92 assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
96 public void testGetPolicyTypeIdentities_OSC() {
97 clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, //
98 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
99 controllerConfig(), asyncRestClientMock);
101 String response = createResponse(Arrays.asList(POLICY_TYPE_1_ID));
102 whenAsyncPostThenReturn(Mono.just(response));
104 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
105 assertEquals(1, policyTypeIds.size(), "");
106 assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0), "");
108 String expUrl = RIC_1_URL + "/a1-p/policytypes";
109 ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
110 .nearRtRicUrl(expUrl) //
112 String expInput = SdncJsonHelper.createInputJsonString(expectedParams);
113 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
114 CONTROLLER_PASSWORD);
118 private String policiesUrl() {
119 return RIC_1_URL + "/A1-P/v1/policies";
122 private Gson gson() {
123 return SdncOscA1Client.gson;
126 private String createResponse(Object body) {
127 AdapterOutput output = ImmutableAdapterOutput.builder() //
128 .body(gson().toJson(body)) //
131 return SdncJsonHelper.createOutputJsonString(output);
135 public void testGetPolicyIdentities() {
137 String policyIdsResp = createResponse(Arrays.asList(POLICY_1_ID, POLICY_2_ID));
138 whenAsyncPostThenReturn(Mono.just(policyIdsResp));
140 List<String> returned = clientUnderTest.getPolicyIdentities().block();
141 assertEquals(2, returned.size(), "");
143 ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
144 .nearRtRicUrl(policiesUrl()) //
146 String expInput = SdncJsonHelper.createInputJsonString(expectedParams);
147 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
148 CONTROLLER_PASSWORD);
153 public void testGetValidPolicyType() {
154 String policyType = clientUnderTest.getPolicyTypeSchema("").block();
155 assertEquals("{}", policyType, "");
159 public void testPutPolicyValidResponse() {
160 whenPostReturnOkResponse();
162 String returned = clientUnderTest
163 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
165 assertEquals("OK", returned, "");
166 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
167 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
168 .nearRtRicUrl(expUrl) //
169 .body(POLICY_JSON_VALID) //
171 String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
173 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
177 public void testPutPolicyRejected() {
178 final String policyJson = "{}";
179 AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
181 .httpStatus(400) // ERROR
184 String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
185 whenAsyncPostThenReturn(Mono.just(resp));
187 Mono<String> returnedMono = clientUnderTest
188 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID));
189 StepVerifier.create(returnedMono) //
190 .expectSubscription() //
191 .expectErrorMatches(t -> t instanceof WebClientResponseException) //
194 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
195 AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() //
196 .nearRtRicUrl(expUrl) //
199 String expRequest = SdncJsonHelper.createInputJsonString(expRequestParams);
200 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME,
201 CONTROLLER_PASSWORD);
202 StepVerifier.create(returnedMono)
203 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
207 public void testDeletePolicy() {
208 whenPostReturnOkResponse();
210 String returned = clientUnderTest
211 .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
213 assertEquals("OK", returned, "");
214 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
215 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
216 .nearRtRicUrl(expUrl) //
218 String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
220 verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME,
221 CONTROLLER_PASSWORD);
225 public void testGetStatus() {
226 whenPostReturnOkResponse();
228 Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID);
230 String returnedStatus = clientUnderTest.getPolicyStatus(policy).block();
232 assertEquals("OK", returnedStatus, "unexpected status");
234 final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status";
235 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
236 .nearRtRicUrl(expUrl) //
238 String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
240 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME,
241 CONTROLLER_PASSWORD);
245 public void testGetVersion() {
246 whenPostReturnOkResponse();
247 A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block();
248 assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion, "");
250 whenPostReturnOkResponseNoBody();
251 returnedVersion = clientUnderTest.getProtocolVersion().block();
252 assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion, "");
255 private void whenPostReturnOkResponse() {
256 AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
261 String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
262 whenAsyncPostThenReturn(Mono.just(resp));
265 private void whenPostReturnOkResponseNoBody() {
266 AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
268 .body(Optional.empty()) //
271 String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
272 whenAsyncPostThenReturn(Mono.just(resp));
275 private OngoingStubbing<Mono<String>> whenAsyncPostThenReturn(Mono<String> response) {
276 return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString()))
277 .thenReturn(response);