Added support for https
[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 public class PolicyAgentApiImplTest {
50     private static final String URL_POLICY_SCHEMAS = "/policy_schemas";
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     public 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     public 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     public void testGetAllPolicyTypesSuccessValidJson() {
96         String policyTypes = Arrays.asList(POLICY_TYPE_1_VALID, POLICY_TYPE_2_VALID).toString();
97
98         whenGetReturnOK(URL_POLICY_SCHEMAS, HttpStatus.OK, policyTypes);
99
100         ResponseEntity<String> resp = apiUnderTest.getAllPolicyTypes();
101         assertTrue(resp.getBody().contains("\"name\":\"type1\""));
102         assertEquals(HttpStatus.OK, resp.getStatusCode());
103     }
104
105     @Test
106     public void testGetAllPolicyTypesSuccessInvalidJson() {
107         String policyTypes = Arrays.asList(POLICY_TYPE_1_INVALID, POLICY_TYPE_2_VALID).toString();
108         whenGetReturnOK(URL_POLICY_SCHEMAS, HttpStatus.OK, policyTypes);
109
110         ResponseEntity<String> returnedResp = apiUnderTest.getAllPolicyTypes();
111
112         assertTrue(returnedResp.getBody().contains("Exception"));
113         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, returnedResp.getStatusCode());
114     }
115
116     private String urlPolicyInstances(String type) {
117         return "/policies?type=" + type;
118     }
119
120     @Test
121     public void testGetPolicyInstancesForTypeFailure() {
122         whenGetReturnFailure(urlPolicyInstances(POLICY_TYPE_1_ID), HttpStatus.NOT_FOUND, "");
123
124         ResponseEntity<String> returnedResp = apiUnderTest.getPolicyInstancesForType(POLICY_TYPE_1_ID);
125
126         assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
127     }
128
129     @Test
130     public void testGetPolicyInstancesForTypeSuccessValidJson() {
131         String policyInstances = Arrays.asList(POLICY_1_VALID).toString();
132         String policyInstancesJson = parsePolicyInstancesJson(policyInstances);
133
134         whenGetReturnOK(urlPolicyInstances(POLICY_TYPE_1_ID), HttpStatus.OK, policyInstances);
135
136         ResponseEntity<String> returnedResp = apiUnderTest.getPolicyInstancesForType(POLICY_TYPE_1_ID);
137
138         assertEquals(returnedResp.getBody(), policyInstancesJson);
139         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
140     }
141
142     @Test
143     public void testGetPolicyInstancesForTypeSuccessInvalidJson() {
144         String policyInstances = Arrays.asList(POLICY_1_INVALID).toString();
145
146         whenGetReturnOK(urlPolicyInstances(POLICY_TYPE_1_ID), HttpStatus.OK, policyInstances);
147
148         ResponseEntity<String> returnedResp = apiUnderTest.getPolicyInstancesForType(POLICY_TYPE_1_ID);
149
150         assertTrue(returnedResp.getBody().contains("Exception"));
151         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, returnedResp.getStatusCode());
152     }
153
154     private String urlPolicyInstance(String id) {
155         return "/policy?id=" + id;
156     }
157
158     @Test
159     public void testGetPolicyInstance() {
160         whenGetReturnOK(urlPolicyInstance(POLICY_1_ID), HttpStatus.OK, POLICY_1_VALID);
161
162         ResponseEntity<Object> returnedResp = apiUnderTest.getPolicyInstance(POLICY_1_ID);
163
164         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
165         assertEquals(POLICY_1_VALID, returnedResp.getBody());
166
167     }
168
169     private String urlPutPolicy(String type, String id, String ric) {
170         return "/policy?type=" + type + "&id=" + id + "&ric=" + ric + "&service=controlpanel";
171     }
172
173     private void whenPutReturnOK(String url, String putBody, HttpStatus status, String body) {
174         ResponseEntity<String> ret = new ResponseEntity<>(body, status);
175         when(restClient.putForEntity(eq(url), eq(putBody))).thenReturn(Mono.just(ret));
176     }
177
178     private void whenPutReturnFailure(String url, String putBody, HttpStatus status, String body) {
179         HttpServerErrorException e =
180             new HttpServerErrorException(status, body, body.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
181         when(restClient.putForEntity(eq(url), eq(putBody))).thenReturn(Mono.error(e));
182     }
183
184     @Test
185     public void testPutPolicyFailure() {
186         String url = urlPutPolicy(POLICY_TYPE_1_ID, POLICY_1_ID, RIC_1_ID);
187         whenPutReturnFailure(url, POLICY_1_VALID, HttpStatus.NOT_FOUND, CLIENT_ERROR_MESSAGE);
188
189         ResponseEntity<String> returnedResp =
190             apiUnderTest.putPolicy(POLICY_TYPE_1_ID, POLICY_1_ID, POLICY_1_VALID, RIC_1_ID);
191
192         assertTrue(returnedResp.getBody().contains(CLIENT_ERROR_MESSAGE));
193         assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
194     }
195
196     @Test
197     public void testPutPolicySuccess() {
198         String url = urlPutPolicy(POLICY_TYPE_1_ID, POLICY_1_ID, RIC_1_ID);
199         whenPutReturnOK(url, POLICY_1_VALID, HttpStatus.OK, POLICY_1_VALID);
200
201         ResponseEntity<String> returnedResp =
202             apiUnderTest.putPolicy(POLICY_TYPE_1_ID, POLICY_1_ID, POLICY_1_VALID, RIC_1_ID);
203
204         assertNull(returnedResp.getBody());
205         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
206     }
207
208     private void whenDeleteReturnOK(String url, HttpStatus status) {
209         ResponseEntity<String> ret = new ResponseEntity<>(status);
210         when(restClient.deleteForEntity(eq(url))).thenReturn(Mono.just(ret));
211     }
212
213     private void whenDeleteReturnFailure(String url, HttpStatus status, String body) {
214         HttpServerErrorException e =
215             new HttpServerErrorException(status, body, body.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
216         when(restClient.deleteForEntity(eq(url))).thenReturn(Mono.error(e));
217     }
218
219     private String deletePolicyUrl(String id) {
220         return "/policy?id=" + id;
221     }
222
223     @Test
224     public void testDeletePolicyFailure() {
225         whenDeleteReturnFailure(deletePolicyUrl(POLICY_1_ID), HttpStatus.NOT_FOUND, CLIENT_ERROR_MESSAGE);
226
227         ResponseEntity<String> returnedResp = apiUnderTest.deletePolicy(POLICY_1_ID);
228
229         assertTrue(returnedResp.getBody().contains(CLIENT_ERROR_MESSAGE));
230         assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
231     }
232
233     @Test
234     public void testDeletePolicySuccess() {
235         whenDeleteReturnOK(deletePolicyUrl(POLICY_1_ID), HttpStatus.OK);
236         ResponseEntity<String> returnedResp = apiUnderTest.deletePolicy(POLICY_1_ID);
237
238         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
239     }
240
241     private String urlRicInfo(String typeName) {
242         return "/rics?policyType=" + typeName;
243     }
244
245     @Test
246     public void testGetRicsSupportingTypeValidJson() {
247         String rics = Arrays.asList(RIC_1_INFO_VALID).toString();
248
249         this.whenGetReturnOK(urlRicInfo(POLICY_TYPE_1_ID), HttpStatus.OK, rics);
250
251         ResponseEntity<String> resp = apiUnderTest.getRicsSupportingType(POLICY_TYPE_1_ID);
252
253         assertEquals(HttpStatus.OK, resp.getStatusCode());
254         assertEquals("[\"ric1\"]", resp.getBody());
255     }
256
257     @Test
258     public void testGetRicsSupportingTypeInvalidJson() {
259         String rics = Arrays.asList(RIC_1_INFO_INVALID).toString();
260
261         this.whenGetReturnOK(urlRicInfo(POLICY_TYPE_1_ID), HttpStatus.OK, rics);
262
263         ResponseEntity<String> returnedResp = apiUnderTest.getRicsSupportingType(POLICY_TYPE_1_ID);
264
265         assertTrue(returnedResp.getBody().contains("Exception"));
266         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, returnedResp.getStatusCode());
267     }
268
269     private String parsePolicyInstancesJson(String inputString) {
270         Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {}.getType();
271         List<PolicyInfo> rspParsed = gson.fromJson(inputString, listType);
272         PolicyInstances policyInstances = new PolicyInstances();
273         for (PolicyInfo policy : rspParsed) {
274             policyInstances.add(policy);
275         }
276         return gson.toJson(policyInstances);
277     }
278 }