Dockerize the frontend of controlpanel
[portal/nonrtric-controlpanel.git] / webapp-backend / src / test / java / org / oransc / portal / nonrtric / controlpanel / policyagentapi / PolicyAgentApiImplTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
6  * Modifications Copyright (C) 2020 Nordix Foundation
7  * %%
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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===================================
20  */
21 package org.oransc.portal.nonrtric.controlpanel.policyagentapi;
22
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;
29
30 import com.google.gson.GsonBuilder;
31 import com.google.gson.reflect.TypeToken;
32
33 import java.lang.reflect.Type;
34 import java.nio.charset.StandardCharsets;
35 import java.util.Arrays;
36 import java.util.List;
37
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;
48
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";
62
63     private static com.google.gson.Gson gson = new GsonBuilder() //
64         .serializeNulls() //
65         .create(); //
66
67     PolicyAgentApiImpl apiUnderTest;
68
69     AsyncRestClient restClient;
70
71     @BeforeEach
72     void init() {
73         restClient = mock(AsyncRestClient.class);
74         apiUnderTest = new PolicyAgentApiImpl(restClient);
75     }
76
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));
80     }
81
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));
85     }
86
87     @Test
88     void testGetAllPolicyTypesFailure() {
89         whenGetReturnFailure(URL_POLICY_SCHEMAS, HttpStatus.NOT_FOUND, "");
90         ResponseEntity<String> returnedResp = apiUnderTest.getAllPolicyTypes();
91         assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
92     }
93
94     @Test
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\":{}}");
100
101         ResponseEntity<String> resp = apiUnderTest.getAllPolicyTypes();
102         assertTrue(resp.getBody().contains("\"name\":\"type1\""));
103         assertEquals(HttpStatus.OK, resp.getStatusCode());
104     }
105
106     @Test
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);
110
111         ResponseEntity<String> returnedResp = apiUnderTest.getAllPolicyTypes();
112
113         assertTrue(returnedResp.getBody().contains("Exception"));
114         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, returnedResp.getStatusCode());
115     }
116
117     private String urlPolicyInstances(String type) {
118         return "/v2/policies?policytype_id=" + type;
119     }
120
121     @Test
122     void testGetPolicyInstancesForTypeFailure() {
123         whenGetReturnFailure(urlPolicyInstances(POLICY_TYPE_1_ID), HttpStatus.NOT_FOUND, "");
124
125         ResponseEntity<String> returnedResp = apiUnderTest.getPolicyInstancesForType(POLICY_TYPE_1_ID);
126
127         assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
128     }
129
130     @Test
131     void testGetPolicyInstancesForTypeSuccessValidJson() {
132         String policyInstances = "{\"policy_ids\":[{\"id\":\"policy1\"}]}";
133         String policyInstancesJson = parsePolicyInstancesJson("[{\"id\":\"policy1\"}]");
134
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());
139     }
140
141     @Test
142     void testGetPolicyInstancesForTypeSuccessInvalidJson() {
143         String policyInstances = Arrays.asList(POLICY_1_INVALID).toString();
144
145         whenGetReturnOK(urlPolicyInstances(POLICY_TYPE_1_ID), HttpStatus.OK, policyInstances);
146
147         ResponseEntity<String> returnedResp = apiUnderTest.getPolicyInstancesForType(POLICY_TYPE_1_ID);
148
149         assertTrue(returnedResp.getBody().contains("Exception"));
150         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, returnedResp.getStatusCode());
151     }
152
153     private String urlPolicyInstance(String id) {
154         return "/v2/policies/" + id;
155     }
156
157     @Test
158     void testGetPolicyInstance() {
159         whenGetReturnOK(urlPolicyInstance(POLICY_1_ID), HttpStatus.OK, POLICY_1_VALID);
160
161         ResponseEntity<Object> returnedResp = apiUnderTest.getPolicyInstance(POLICY_1_ID);
162
163         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
164         assertEquals(POLICY_1_VALID, returnedResp.getBody());
165
166     }
167
168     private String urlPutPolicy() {
169         return "/v2/policies/";
170     }
171
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));
175     }
176
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));
181     }
182
183     @Test
184     void testPutPolicyFailure() {
185         String url = urlPutPolicy();
186
187         PolicyInfo i = ImmutablePolicyInfo.builder() //
188             .id(POLICY_1_ID) //
189             .type(POLICY_TYPE_1_ID) //
190             .ric(RIC_1_ID) //
191             .json(POLICY_1_VALID) //
192             .service("") //
193             .lastModified("") //
194             .build(); //
195
196         String jsonStr = gson.toJson(i, PolicyInfo.class);
197         whenPutReturnFailure(url, jsonStr, HttpStatus.NOT_FOUND, CLIENT_ERROR_MESSAGE);
198
199         ResponseEntity<String> returnedResp =
200             apiUnderTest.putPolicy(POLICY_TYPE_1_ID, POLICY_1_ID, POLICY_1_VALID, RIC_1_ID);
201
202         assertTrue(returnedResp.getBody().contains(CLIENT_ERROR_MESSAGE));
203         assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
204     }
205
206     @Test
207     void testPutPolicySuccess() {
208         String url = urlPutPolicy();
209         PolicyInfo i = ImmutablePolicyInfo.builder() //
210             .id(POLICY_1_ID) //
211             .type(POLICY_TYPE_1_ID) //
212             .ric(RIC_1_ID) //
213             .json(POLICY_1_VALID) //
214             .service("") //
215             .lastModified("") //
216             .build(); //
217
218         String jsonStr = gson.toJson(i, PolicyInfo.class);
219
220         whenPutReturnOK(url, jsonStr, HttpStatus.OK, POLICY_1_VALID);
221
222         ResponseEntity<String> returnedResp =
223             apiUnderTest.putPolicy(POLICY_TYPE_1_ID, POLICY_1_ID, POLICY_1_VALID, RIC_1_ID);
224
225         assertNull(returnedResp.getBody());
226         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
227     }
228
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));
232     }
233
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));
238     }
239
240     private String deletePolicyUrl(String id) {
241         return "/v2/policies/" + id;
242     }
243
244     @Test
245     void testDeletePolicyFailure() {
246         whenDeleteReturnFailure(deletePolicyUrl(POLICY_1_ID), HttpStatus.NOT_FOUND, CLIENT_ERROR_MESSAGE);
247
248         ResponseEntity<String> returnedResp = apiUnderTest.deletePolicy(POLICY_1_ID);
249
250         assertTrue(returnedResp.getBody().contains(CLIENT_ERROR_MESSAGE));
251         assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
252     }
253
254     @Test
255     void testDeletePolicySuccess() {
256         whenDeleteReturnOK(deletePolicyUrl(POLICY_1_ID), HttpStatus.OK);
257         ResponseEntity<String> returnedResp = apiUnderTest.deletePolicy(POLICY_1_ID);
258
259         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
260     }
261
262     private String urlRicInfo(String typeName) {
263         return "/v2/rics?policytype_id=" + typeName;
264     }
265
266     @Test
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);
271
272         ResponseEntity<String> resp = apiUnderTest.getRicsSupportingType(POLICY_TYPE_1_ID);
273
274         assertEquals(HttpStatus.OK, resp.getStatusCode());
275         assertEquals("[\"ric1\"]", resp.getBody());
276     }
277
278     @Test
279     void testGetRicsSupportingTypeInvalidJson() {
280         String rics = Arrays.asList(RIC_1_INFO_INVALID).toString();
281
282         this.whenGetReturnOK(urlRicInfo(POLICY_TYPE_1_ID), HttpStatus.OK, rics);
283
284         ResponseEntity<String> returnedResp = apiUnderTest.getRicsSupportingType(POLICY_TYPE_1_ID);
285
286         assertTrue(returnedResp.getBody().contains("Exception"));
287         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, returnedResp.getStatusCode());
288     }
289
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);
296         }
297         return gson.toJson(policyInstances);
298     }
299 }