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.SdncOscA1Client.AdapterRequest;
40 import org.oransc.policyagent.clients.SdncOscA1Client.AdapterResponse;
41 import org.oransc.policyagent.repository.Policy;
42 import org.springframework.web.reactive.function.client.WebClientResponseException;
44 import reactor.core.publisher.Mono;
45 import reactor.test.StepVerifier;
47 @ExtendWith(MockitoExtension.class)
48 public class SdncOscA1ClientTest {
49 private static final String CONTROLLER_USERNAME = "username";
50 private static final String CONTROLLER_PASSWORD = "password";
51 private static final String RIC_1_URL = "RicUrl";
52 private static final String GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy";
53 private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy";
54 private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy";
55 private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus";
56 private static final String POLICY_TYPE_1_ID = "type1";
57 private static final String POLICY_1_ID = "policy1";
58 private static final String POLICY_2_ID = "policy2";
59 private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}";
61 SdncOscA1Client clientUnderTest;
63 AsyncRestClient asyncRestClientMock;
67 asyncRestClientMock = mock(AsyncRestClient.class);
68 clientUnderTest = new SdncOscA1Client(A1ClientHelper.createRic(RIC_1_URL).getConfig(), CONTROLLER_USERNAME,
69 CONTROLLER_PASSWORD, asyncRestClientMock);
73 public void testGetPolicyTypeIdentities() {
74 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
75 assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
76 assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
79 private String policiesUrl() {
80 return RIC_1_URL + "/A1-P/v1/policies";
84 return SdncOscA1Client.gson;
88 public void testGetPolicyIdentities() {
90 List<String> policyIds = Arrays.asList(POLICY_1_ID, POLICY_2_ID);
91 AdapterResponse output = ImmutableAdapterResponse.builder() //
92 .body(gson().toJson(policyIds)) //
96 String policyIdsResp = gson().toJson(output);
97 whenAsyncPostThenReturn(Mono.just(policyIdsResp));
99 List<String> returned = clientUnderTest.getPolicyIdentities().block();
100 assertEquals(2, returned.size(), "");
102 AdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
103 .nearRtRicUrl(policiesUrl()) //
105 String expInput = A1ClientHelper.createInputJsonString(expectedParams);
106 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
107 CONTROLLER_PASSWORD);
112 public void testGetValidPolicyType() {
113 String policyType = clientUnderTest.getPolicyTypeSchema("").block();
114 assertEquals("{}", policyType, "");
118 public void testPutPolicyValidResponse() {
119 whenPostReturnOkResponse();
121 String returned = clientUnderTest
122 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
124 assertEquals("OK", returned, "");
125 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
126 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
127 .nearRtRicUrl(expUrl) //
128 .body(POLICY_JSON_VALID) //
130 String expInput = A1ClientHelper.createInputJsonString(expectedInputParams);
132 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
136 public void testPutPolicyRejected() {
137 final String policyJson = "{}";
138 AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() //
140 .httpStatus(400) // ERROR
143 String resp = gson().toJson(adapterResponse);
144 whenAsyncPostThenReturn(Mono.just(resp));
146 Mono<String> returnedMono = clientUnderTest
147 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID));
149 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
150 AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() //
151 .nearRtRicUrl(expUrl) //
154 String expRequest = A1ClientHelper.createInputJsonString(expRequestParams);
155 verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME,
156 CONTROLLER_PASSWORD);
157 StepVerifier.create(returnedMono)
158 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
162 public void testDeletePolicy() {
163 whenPostReturnOkResponse();
165 String returned = clientUnderTest
166 .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
168 assertEquals("OK", returned, "");
169 final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
170 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
171 .nearRtRicUrl(expUrl) //
173 String expInput = A1ClientHelper.createInputJsonString(expectedInputParams);
175 verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME,
176 CONTROLLER_PASSWORD);
180 public void testGetStatus() {
181 whenPostReturnOkResponse();
183 Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID);
185 String returnedStatus = clientUnderTest.getPolicyStatus(policy).block();
187 assertEquals("OK", returnedStatus, "unexpeted status");
189 final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status";
190 AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
191 .nearRtRicUrl(expUrl) //
193 String expInput = A1ClientHelper.createInputJsonString(expectedInputParams);
195 verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME,
196 CONTROLLER_PASSWORD);
199 private void whenPostReturnOkResponse() {
200 AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() //
205 String resp = gson().toJson(adapterResponse);
206 whenAsyncPostThenReturn(Mono.just(resp));
209 private OngoingStubbing<Mono<String>> whenAsyncPostThenReturn(Mono<String> response) {
210 return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString()))
211 .thenReturn(response);