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;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.junit.jupiter.MockitoExtension;
38 import org.mockito.stubbing.OngoingStubbing;
39 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
40 import org.oransc.policyagent.clients.SdncOscA1Client.AdapterRequest;
41 import org.oransc.policyagent.clients.SdncOscA1Client.AdapterResponse;
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 AdapterRequest 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 obj) {
126 AdapterResponse output = ImmutableAdapterResponse.builder() //
127 .body(gson().toJson(obj)) //
131 return gson().toJson(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 AdapterRequest 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 AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() //
181 .httpStatus(400) // ERROR
184 String resp = gson().toJson(adapterResponse);
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));
190 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
191 AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() //
192 .nearRtRicUrl(expUrl) //
195 String expRequest = SdncJsonHelper.createInputJsonString(expRequestParams);
196 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME,
197 CONTROLLER_PASSWORD);
198 StepVerifier.create(returnedMono)
199 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
203 public void testDeletePolicy() {
204 whenPostReturnOkResponse();
206 String returned = clientUnderTest
207 .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
209 assertEquals("OK", returned, "");
210 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
211 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
212 .nearRtRicUrl(expUrl) //
214 String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
216 verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME,
217 CONTROLLER_PASSWORD);
221 public void testGetStatus() {
222 whenPostReturnOkResponse();
224 Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID);
226 String returnedStatus = clientUnderTest.getPolicyStatus(policy).block();
228 assertEquals("OK", returnedStatus, "unexpected status");
230 final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status";
231 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
232 .nearRtRicUrl(expUrl) //
234 String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
236 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME,
237 CONTROLLER_PASSWORD);
241 public void testGetVersion() {
242 whenPostReturnOkResponse();
243 A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block();
244 assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion, "");
247 private void whenPostReturnOkResponse() {
248 AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() //
253 String resp = gson().toJson(adapterResponse);
254 whenAsyncPostThenReturn(Mono.just(resp));
257 private OngoingStubbing<Mono<String>> whenAsyncPostThenReturn(Mono<String> response) {
258 return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString()))
259 .thenReturn(response);