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.repository.Policy;
43 import org.oransc.policyagent.repository.Ric;
44 import org.springframework.web.reactive.function.client.WebClientResponseException;
46 import reactor.core.publisher.Mono;
47 import reactor.test.StepVerifier;
49 @ExtendWith(MockitoExtension.class)
50 public class SdncOscA1ClientTest {
51 private static final String CONTROLLER_USERNAME = "username";
52 private static final String CONTROLLER_PASSWORD = "password";
53 private static final String RIC_1_URL = "RicUrl";
54 private static final String GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy";
55 private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy";
56 private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy";
57 private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus";
58 private static final String POLICY_TYPE_1_ID = "type1";
59 private static final String POLICY_1_ID = "policy1";
60 private static final String POLICY_2_ID = "policy2";
61 private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}";
63 SdncOscA1Client clientUnderTest;
65 AsyncRestClient asyncRestClientMock;
69 asyncRestClientMock = mock(AsyncRestClient.class);
70 Ric ric = A1ClientHelper.createRic(RIC_1_URL);
71 clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), CONTROLLER_USERNAME,
72 CONTROLLER_PASSWORD, asyncRestClientMock);
76 public void testGetPolicyTypeIdentities_STD() {
77 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
78 assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
79 assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
83 public void testGetPolicyTypeIdentities_OSC() {
84 clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, //
85 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
86 CONTROLLER_USERNAME, CONTROLLER_PASSWORD, asyncRestClientMock);
88 String response = createResponse(Arrays.asList(POLICY_TYPE_1_ID));
89 whenAsyncPostThenReturn(Mono.just(response));
91 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
92 assertEquals(1, policyTypeIds.size(), "");
93 assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0), "");
95 String expUrl = RIC_1_URL + "/a1-p/policytypes";
96 AdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
97 .nearRtRicUrl(expUrl) //
99 String expInput = A1ClientHelper.createInputJsonString(expectedParams);
100 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
101 CONTROLLER_PASSWORD);
105 private String policiesUrl() {
106 return RIC_1_URL + "/A1-P/v1/policies";
109 private Gson gson() {
110 return SdncOscA1Client.gson;
113 private String createResponse(Object obj) {
114 AdapterResponse output = ImmutableAdapterResponse.builder() //
115 .body(gson().toJson(obj)) //
119 return gson().toJson(output);
123 public void testGetPolicyIdentities() {
125 String policyIdsResp = createResponse(Arrays.asList(POLICY_1_ID, POLICY_2_ID));
126 whenAsyncPostThenReturn(Mono.just(policyIdsResp));
128 List<String> returned = clientUnderTest.getPolicyIdentities().block();
129 assertEquals(2, returned.size(), "");
131 AdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
132 .nearRtRicUrl(policiesUrl()) //
134 String expInput = A1ClientHelper.createInputJsonString(expectedParams);
135 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
136 CONTROLLER_PASSWORD);
141 public void testGetValidPolicyType() {
142 String policyType = clientUnderTest.getPolicyTypeSchema("").block();
143 assertEquals("{}", policyType, "");
147 public void testPutPolicyValidResponse() {
148 whenPostReturnOkResponse();
150 String returned = clientUnderTest
151 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
153 assertEquals("OK", returned, "");
154 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
155 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
156 .nearRtRicUrl(expUrl) //
157 .body(POLICY_JSON_VALID) //
159 String expInput = A1ClientHelper.createInputJsonString(expectedInputParams);
161 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
165 public void testPutPolicyRejected() {
166 final String policyJson = "{}";
167 AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() //
169 .httpStatus(400) // ERROR
172 String resp = gson().toJson(adapterResponse);
173 whenAsyncPostThenReturn(Mono.just(resp));
175 Mono<String> returnedMono = clientUnderTest
176 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID));
178 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
179 AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() //
180 .nearRtRicUrl(expUrl) //
183 String expRequest = A1ClientHelper.createInputJsonString(expRequestParams);
184 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME,
185 CONTROLLER_PASSWORD);
186 StepVerifier.create(returnedMono)
187 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
191 public void testDeletePolicy() {
192 whenPostReturnOkResponse();
194 String returned = clientUnderTest
195 .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
197 assertEquals("OK", returned, "");
198 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
199 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
200 .nearRtRicUrl(expUrl) //
202 String expInput = A1ClientHelper.createInputJsonString(expectedInputParams);
204 verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME,
205 CONTROLLER_PASSWORD);
209 public void testGetStatus() {
210 whenPostReturnOkResponse();
212 Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID);
214 String returnedStatus = clientUnderTest.getPolicyStatus(policy).block();
216 assertEquals("OK", returnedStatus, "unexpeted status");
218 final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status";
219 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
220 .nearRtRicUrl(expUrl) //
222 String expInput = A1ClientHelper.createInputJsonString(expectedInputParams);
224 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME,
225 CONTROLLER_PASSWORD);
228 private void whenPostReturnOkResponse() {
229 AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() //
234 String resp = gson().toJson(adapterResponse);
235 whenAsyncPostThenReturn(Mono.just(resp));
238 private OngoingStubbing<Mono<String>> whenAsyncPostThenReturn(Mono<String> response) {
239 return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString()))
240 .thenReturn(response);