2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
6 * Modifications Copyright (C) 2020 Nordix Foundation
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ========================LICENSE_END===================================
21 package org.oransc.portal.nonrtric.controlpanel.policyagentapi;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import com.google.gson.GsonBuilder;
31 import com.google.gson.reflect.TypeToken;
33 import java.lang.reflect.Type;
34 import java.nio.charset.StandardCharsets;
35 import java.util.Arrays;
36 import java.util.List;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.oransc.portal.nonrtric.controlpanel.model.ImmutablePolicyInfo;
41 import org.oransc.portal.nonrtric.controlpanel.model.PolicyInfo;
42 import org.oransc.portal.nonrtric.controlpanel.model.PolicyInstances;
43 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.client.HttpServerErrorException;
47 import reactor.core.publisher.Mono;
49 class PolicyAgentApiImplTest {
50 private static final String URL_POLICY_SCHEMAS = "/v2/policy-types";
51 private static final String POLICY_TYPE_1_ID = "type1";
52 private static final String POLICY_TYPE_1_VALID = "{\"title\":\"type1\"}";
53 private static final String POLICY_TYPE_1_INVALID = "\"title\":\"type1\"}";
54 private static final String POLICY_TYPE_2_VALID = "{\"title\":\"type2\"}";
55 private static final String POLICY_1_ID = "policy1";
56 private static final String POLICY_1_VALID = "{\"policyId\":\"policy1\"}";
57 private static final String POLICY_1_INVALID = "\"policyId\":\"policy1\"}";
58 private static final String RIC_1_ID = "ric1";
59 private static final String RIC_1_INFO_VALID = "{\"ricName\":\"ric1\",\"policyTypes\":[\"type1\"]}";
60 private static final String RIC_1_INFO_INVALID = "{\"ricName\":\"ric1\",\"policyTypes\":\"type1\"]}";
61 private static final String CLIENT_ERROR_MESSAGE = "XXXXXXX";
63 private static com.google.gson.Gson gson = new GsonBuilder() //
67 PolicyAgentApiImpl apiUnderTest;
69 AsyncRestClient restClient;
73 restClient = mock(AsyncRestClient.class);
74 apiUnderTest = new PolicyAgentApiImpl(restClient);
77 private void whenGetReturnOK(String url, HttpStatus status, String body) {
78 ResponseEntity<String> ret = new ResponseEntity<>(body, status);
79 when(restClient.getForEntity(eq(url))).thenReturn(Mono.just(ret));
82 private void whenGetReturnFailure(String url, HttpStatus status, String body) {
83 HttpServerErrorException e = new HttpServerErrorException(status, body);
84 when(restClient.getForEntity(eq(url))).thenReturn(Mono.error(e));
88 void testGetAllPolicyTypesFailure() {
89 whenGetReturnFailure(URL_POLICY_SCHEMAS, HttpStatus.NOT_FOUND, "");
90 ResponseEntity<String> returnedResp = apiUnderTest.getAllPolicyTypes();
91 assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
95 void testGetAllPolicyTypesSuccessValidJson() {
96 String returnValue = "{\"policytype_ids\": [\"type1\",\"type2\"]}";
97 whenGetReturnOK(URL_POLICY_SCHEMAS, HttpStatus.OK, returnValue);
98 whenGetReturnOK(URL_POLICY_SCHEMAS + "/type1", HttpStatus.OK, "{\"policy_schema\":{}}");
99 whenGetReturnOK(URL_POLICY_SCHEMAS + "/type2", HttpStatus.OK, "{\"policy_schema\":{}}");
101 ResponseEntity<String> resp = apiUnderTest.getAllPolicyTypes();
102 assertTrue(resp.getBody().contains("\"name\":\"type1\""));
103 assertEquals(HttpStatus.OK, resp.getStatusCode());
107 void testGetAllPolicyTypesSuccessInvalidJson() {
108 String policyTypes = Arrays.asList(POLICY_TYPE_1_INVALID, POLICY_TYPE_2_VALID).toString();
109 whenGetReturnOK(URL_POLICY_SCHEMAS, HttpStatus.OK, policyTypes);
111 ResponseEntity<String> returnedResp = apiUnderTest.getAllPolicyTypes();
113 assertTrue(returnedResp.getBody().contains("Exception"));
114 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, returnedResp.getStatusCode());
117 private String urlPolicyInstances(String type) {
118 return "/v2/policies?policytype_id=" + type;
122 void testGetPolicyInstancesForTypeFailure() {
123 whenGetReturnFailure(urlPolicyInstances(POLICY_TYPE_1_ID), HttpStatus.NOT_FOUND, "");
125 ResponseEntity<String> returnedResp = apiUnderTest.getPolicyInstancesForType(POLICY_TYPE_1_ID);
127 assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
131 void testGetPolicyInstancesForTypeSuccessValidJson() {
132 String policyInstances = "{\"policy_ids\":[{\"id\":\"policy1\"}]}";
133 String policyInstancesJson = parsePolicyInstancesJson("[{\"id\":\"policy1\"}]");
135 whenGetReturnOK(urlPolicyInstances(POLICY_TYPE_1_ID), HttpStatus.OK, policyInstances);
136 ResponseEntity<String> returnedResp = apiUnderTest.getPolicyInstancesForType(POLICY_TYPE_1_ID);
137 assertEquals(returnedResp.getBody(), policyInstancesJson);
138 assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
142 void testGetPolicyInstancesForTypeSuccessInvalidJson() {
143 String policyInstances = Arrays.asList(POLICY_1_INVALID).toString();
145 whenGetReturnOK(urlPolicyInstances(POLICY_TYPE_1_ID), HttpStatus.OK, policyInstances);
147 ResponseEntity<String> returnedResp = apiUnderTest.getPolicyInstancesForType(POLICY_TYPE_1_ID);
149 assertTrue(returnedResp.getBody().contains("Exception"));
150 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, returnedResp.getStatusCode());
153 private String urlPolicyInstance(String id) {
154 return "/v2/policies/" + id;
158 void testGetPolicyInstance() {
159 whenGetReturnOK(urlPolicyInstance(POLICY_1_ID), HttpStatus.OK, POLICY_1_VALID);
161 ResponseEntity<Object> returnedResp = apiUnderTest.getPolicyInstance(POLICY_1_ID);
163 assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
164 assertEquals(POLICY_1_VALID, returnedResp.getBody());
168 private String urlPutPolicy() {
169 return "/v2/policies/";
172 private void whenPutReturnOK(String url, String putBody, HttpStatus status, String body) {
173 ResponseEntity<String> ret = new ResponseEntity<>(body, status);
174 when(restClient.putForEntity(eq(url), eq(putBody))).thenReturn(Mono.just(ret));
177 private void whenPutReturnFailure(String url, String putBody, HttpStatus status, String body) {
178 HttpServerErrorException e =
179 new HttpServerErrorException(status, body, body.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
180 when(restClient.putForEntity(eq(url), eq(putBody))).thenReturn(Mono.error(e));
184 void testPutPolicyFailure() {
185 String url = urlPutPolicy();
187 PolicyInfo i = ImmutablePolicyInfo.builder() //
189 .type(POLICY_TYPE_1_ID) //
191 .json(POLICY_1_VALID) //
196 String jsonStr = gson.toJson(i, PolicyInfo.class);
197 whenPutReturnFailure(url, jsonStr, HttpStatus.NOT_FOUND, CLIENT_ERROR_MESSAGE);
199 ResponseEntity<String> returnedResp =
200 apiUnderTest.putPolicy(POLICY_TYPE_1_ID, POLICY_1_ID, POLICY_1_VALID, RIC_1_ID);
202 assertTrue(returnedResp.getBody().contains(CLIENT_ERROR_MESSAGE));
203 assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
207 void testPutPolicySuccess() {
208 String url = urlPutPolicy();
209 PolicyInfo i = ImmutablePolicyInfo.builder() //
211 .type(POLICY_TYPE_1_ID) //
213 .json(POLICY_1_VALID) //
218 String jsonStr = gson.toJson(i, PolicyInfo.class);
220 whenPutReturnOK(url, jsonStr, HttpStatus.OK, POLICY_1_VALID);
222 ResponseEntity<String> returnedResp =
223 apiUnderTest.putPolicy(POLICY_TYPE_1_ID, POLICY_1_ID, POLICY_1_VALID, RIC_1_ID);
225 assertNull(returnedResp.getBody());
226 assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
229 private void whenDeleteReturnOK(String url, HttpStatus status) {
230 ResponseEntity<String> ret = new ResponseEntity<>(status);
231 when(restClient.deleteForEntity(eq(url))).thenReturn(Mono.just(ret));
234 private void whenDeleteReturnFailure(String url, HttpStatus status, String body) {
235 HttpServerErrorException e =
236 new HttpServerErrorException(status, body, body.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
237 when(restClient.deleteForEntity(eq(url))).thenReturn(Mono.error(e));
240 private String deletePolicyUrl(String id) {
241 return "/v2/policies/" + id;
245 void testDeletePolicyFailure() {
246 whenDeleteReturnFailure(deletePolicyUrl(POLICY_1_ID), HttpStatus.NOT_FOUND, CLIENT_ERROR_MESSAGE);
248 ResponseEntity<String> returnedResp = apiUnderTest.deletePolicy(POLICY_1_ID);
250 assertTrue(returnedResp.getBody().contains(CLIENT_ERROR_MESSAGE));
251 assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
255 void testDeletePolicySuccess() {
256 whenDeleteReturnOK(deletePolicyUrl(POLICY_1_ID), HttpStatus.OK);
257 ResponseEntity<String> returnedResp = apiUnderTest.deletePolicy(POLICY_1_ID);
259 assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
262 private String urlRicInfo(String typeName) {
263 return "/v2/rics?policytype_id=" + typeName;
267 void testGetRicsSupportingTypeValidJson() {
268 String rics = Arrays.asList(RIC_1_INFO_VALID).toString();
269 String returnVal = "{\"rics\":" + rics + "}";
270 this.whenGetReturnOK(urlRicInfo(POLICY_TYPE_1_ID), HttpStatus.OK, returnVal);
272 ResponseEntity<String> resp = apiUnderTest.getRicsSupportingType(POLICY_TYPE_1_ID);
274 assertEquals(HttpStatus.OK, resp.getStatusCode());
275 assertEquals("[\"ric1\"]", resp.getBody());
279 void testGetRicsSupportingTypeInvalidJson() {
280 String rics = Arrays.asList(RIC_1_INFO_INVALID).toString();
282 this.whenGetReturnOK(urlRicInfo(POLICY_TYPE_1_ID), HttpStatus.OK, rics);
284 ResponseEntity<String> returnedResp = apiUnderTest.getRicsSupportingType(POLICY_TYPE_1_ID);
286 assertTrue(returnedResp.getBody().contains("Exception"));
287 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, returnedResp.getStatusCode());
290 private String parsePolicyInstancesJson(String inputString) {
291 Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {}.getType();
292 List<PolicyInfo> rspParsed = gson.fromJson(inputString, listType);
293 PolicyInstances policyInstances = new PolicyInstances();
294 for (PolicyInfo policy : rspParsed) {
295 policyInstances.add(policy);
297 return gson.toJson(policyInstances);