3472b0df5cc7bfbe879b0523800ac15b1231fe05
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / ClosedLoopDictionaryControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.controller;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.io.BufferedReader;
30 import java.io.StringReader;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import javax.servlet.http.HttpServletRequest;
35
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.onap.policy.common.logging.flexlogger.FlexLogger;
40 import org.onap.policy.common.logging.flexlogger.Logger;
41 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
42 import org.onap.policy.rest.dao.CommonClassDao;
43 import org.onap.policy.rest.jpa.ClosedLoopD2Services;
44 import org.onap.policy.rest.jpa.ClosedLoopSite;
45 import org.onap.policy.rest.jpa.PEPOptions;
46 import org.onap.policy.rest.jpa.UserInfo;
47 import org.onap.policy.rest.jpa.VNFType;
48 import org.onap.policy.rest.jpa.VSCLAction;
49 import org.onap.policy.rest.jpa.VarbindDictionary;
50 import org.springframework.mock.web.MockHttpServletResponse;
51
52 public class ClosedLoopDictionaryControllerTest {
53
54     private static Logger logger = FlexLogger.getLogger(ClosedLoopDictionaryControllerTest.class);
55     private static CommonClassDao commonClassDao;
56     private String jsonString = null;
57     private HttpServletRequest request = null;
58     private ClosedLoopDictionaryController controller = null;
59     private MockHttpServletResponse response = null;
60     private UserInfo userInfo;
61     private List<String> data;
62
63     @Before
64     public void setUp() throws Exception {
65         logger.info("setUp: Entering");
66         commonClassDao = Mockito.mock(CommonClassDao.class);
67
68         data = new ArrayList<>();
69         data.add("Test");
70
71         userInfo = new UserInfo();
72         userInfo.setUserLoginId("Test");
73         userInfo.setUserName("Test");
74
75         doNothing().when(commonClassDao).delete(new VSCLAction());
76         doNothing().when(commonClassDao).save(new VSCLAction());
77
78         controller = new ClosedLoopDictionaryController();
79         controller.setCommonClassDao(commonClassDao);
80
81         request = Mockito.mock(HttpServletRequest.class);
82         response = new MockHttpServletResponse();
83         new DictionaryUtils(commonClassDao);
84         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
85         mock(DictionaryUtils.class);
86         logger.info("setUp: exit");
87     }
88
89     @Test
90     public void testGetVSCLActionDictionaryByNameEntityData() {
91         when(commonClassDao.getDataByColumn(VSCLAction.class, "vsclaction")).thenReturn(data);
92         controller.getVSCLActionDictionaryByNameEntityData(response);
93         try {
94             assertTrue(response.getContentAsString() != null
95                     && response.getContentAsString().contains("vsclActionDictionaryDatas"));
96         } catch (Exception e) {
97             fail();
98             logger.error(e.getMessage(), e);
99         }
100     }
101
102     @Test
103     public void testGetVSCLActionDictionaryEntityData() {
104         when(commonClassDao.getData(VSCLAction.class)).thenReturn(new ArrayList<>());
105         controller.getVSCLActionDictionaryEntityData(response);
106         try {
107             assertTrue(response.getContentAsString() != null
108                     && response.getContentAsString().contains("vsclActionDictionaryDatas"));
109         } catch (Exception e) {
110             fail();
111             logger.error(e.getMessage(), e);
112         }
113     }
114
115     @Test
116     public void testGetVNFTypeDictionaryByNameEntityData() {
117         when(commonClassDao.getDataByColumn(VNFType.class, "vnftype")).thenReturn(data);
118         controller.getVNFTypeDictionaryByNameEntityData(response);
119         try {
120             assertTrue(response.getContentAsString() != null
121                     && response.getContentAsString().contains("vnfTypeDictionaryDatas"));
122         } catch (Exception e) {
123             fail();
124             logger.error(e.getMessage(), e);
125         }
126     }
127
128     @Test
129     public void testGetVNFTypeDictionaryEntityData() {
130         when(commonClassDao.getData(VNFType.class)).thenReturn(new ArrayList<>());
131         controller.getVNFTypeDictionaryEntityData(response);
132         try {
133             assertTrue(response.getContentAsString() != null
134                     && response.getContentAsString().contains("vnfTypeDictionaryDatas"));
135         } catch (Exception e) {
136             fail();
137             logger.error(e.getMessage(), e);
138         }
139     }
140
141     @Test
142     public void testGetPEPOptionsDictionaryByNameEntityData() {
143         when(commonClassDao.getDataByColumn(PEPOptions.class, "pepName")).thenReturn(data);
144         controller.getPEPOptionsDictionaryByNameEntityData(response);
145         try {
146             assertTrue(response.getContentAsString() != null
147                     && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
148         } catch (Exception e) {
149             fail();
150             logger.error(e.getMessage(), e);
151         }
152     }
153
154     @Test
155     public void testGetPEPOptionsDictionaryEntityData() {
156         when(commonClassDao.getData(PEPOptions.class)).thenReturn(new ArrayList<>());
157         controller.getPEPOptionsDictionaryEntityData(response);
158         try {
159             assertTrue(response.getContentAsString() != null
160                     && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
161         } catch (Exception e) {
162             fail();
163             logger.error(e.getMessage(), e);
164         }
165     }
166
167     @Test
168     public void testGetVarbindDictionaryByNameEntityData() {
169         when(commonClassDao.getDataByColumn(VarbindDictionary.class, "varbindName")).thenReturn(data);
170         controller.getVarbindDictionaryByNameEntityData(response);
171         try {
172             assertTrue(response.getContentAsString() != null
173                     && response.getContentAsString().contains("varbindDictionaryDatas"));
174         } catch (Exception e) {
175             fail();
176             logger.error(e.getMessage(), e);
177         }
178     }
179
180     @Test
181     public void testGetVarbindDictionaryEntityData() {
182         when(commonClassDao.getData(VarbindDictionary.class)).thenReturn(new ArrayList<>());
183         controller.getVarbindDictionaryEntityData(response);
184         try {
185             assertTrue(response.getContentAsString() != null
186                     && response.getContentAsString().contains("varbindDictionaryDatas"));
187         } catch (Exception e) {
188             fail();
189             logger.error(e.getMessage(), e);
190         }
191     }
192
193     @Test
194     public void testGetClosedLoopServiceDictionaryByNameEntityData() {
195         when(commonClassDao.getDataByColumn(ClosedLoopD2Services.class, "serviceName")).thenReturn(data);
196         controller.getClosedLoopServiceDictionaryByNameEntityData(response);
197         try {
198             assertTrue(response.getContentAsString() != null
199                     && response.getContentAsString().contains("closedLoopServiceDictionaryDatas"));
200         } catch (Exception e) {
201             fail();
202             logger.error(e.getMessage(), e);
203         }
204     }
205
206     @Test
207     public void testGetClosedLoopServiceDictionaryEntityData() {
208         when(commonClassDao.getData(ClosedLoopD2Services.class)).thenReturn(new ArrayList<>());
209         controller.getClosedLoopServiceDictionaryEntityData(response);
210         try {
211             assertTrue(response.getContentAsString() != null
212                     && response.getContentAsString().contains("closedLoopServiceDictionaryDatas"));
213         } catch (Exception e) {
214             fail();
215             logger.error(e.getMessage(), e);
216         }
217     }
218
219     @Test
220     public void testGetClosedLoopSiteDictionaryByNameEntityData() {
221         when(commonClassDao.getDataByColumn(ClosedLoopSite.class, "siteName")).thenReturn(data);
222         controller.getClosedLoopSiteDictionaryByNameEntityData(response);
223         try {
224             assertTrue(response.getContentAsString() != null
225                     && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
226         } catch (Exception e) {
227             fail();
228             logger.error(e.getMessage(), e);
229         }
230     }
231
232     @Test
233     public void testGetClosedLoopSiteDictionaryEntityData() {
234         when(commonClassDao.getData(ClosedLoopSite.class)).thenReturn(new ArrayList<>());
235         controller.getClosedLoopSiteDictionaryEntityData(response);
236         try {
237             assertTrue(response.getContentAsString() != null
238                     && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
239         } catch (Exception e) {
240             fail();
241             logger.error(e.getMessage(), e);
242         }
243     }
244
245     @Test
246     public void testSaveVSCLAction() {
247         jsonString =
248                 "{\"userid\":\"demo\",\"vsclActionDictionaryData\":{\"description\":\"test\",\"vsclaction\":\"Test\"}}";
249         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
250             when(request.getReader()).thenReturn(br);
251             controller.saveVSCLAction(request, response);
252             assertTrue(response.getContentAsString() != null
253                     && response.getContentAsString().contains("vsclActionDictionaryDatas"));
254         } catch (Exception e) {
255             logger.error("Exception" + e);
256         }
257     }
258
259     @Test
260     public void testUpdateVSCLAction() {
261         jsonString =
262                 "{\"userid\":\"demo\",\"vsclActionDictionaryData\":{\"id\":1,\"description\":\"test\",\"vsclaction\":\"Test\"}}";
263         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
264             when(request.getReader()).thenReturn(br);
265             controller.saveVSCLAction(request, response);
266             assertTrue(response.getContentAsString() != null
267                     && response.getContentAsString().contains("vsclActionDictionaryDatas"));
268         } catch (Exception e) {
269             logger.error("Exception" + e);
270         }
271     }
272
273     @Test
274     public void testRemoveVSCLAction() {
275         jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"vsclaction\":\"Test\"}}";
276         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
277             when(request.getReader()).thenReturn(br);
278             controller.removeVSCLAction(request, response);
279             assertTrue(response.getContentAsString() != null
280                     && response.getContentAsString().contains("vsclActionDictionaryDatas"));
281         } catch (Exception e) {
282             logger.error("Exception" + e);
283         }
284     }
285
286     @Test
287     public void testSaveVnfType() {
288         jsonString = "{\"userid\":\"demo\",\"vnfTypeDictionaryData\":{\"description\":\"test\",\"vnftype\":\"Test\"}}";
289         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
290             when(request.getReader()).thenReturn(br);
291             controller.saveVnfType(request, response);
292             assertTrue(response.getContentAsString() != null
293                     && response.getContentAsString().contains("vnfTypeDictionaryData"));
294         } catch (Exception e) {
295             logger.error("Exception" + e);
296         }
297     }
298
299     @Test
300     public void testUpdateVnfType() {
301         jsonString =
302                 "{\"userid\":\"demo\",\"vnfTypeDictionaryData\":{\"id\":1,\"description\":\"test\",\"vnftype\":\"Test\"}}";
303         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
304             when(request.getReader()).thenReturn(br);
305             controller.saveVnfType(request, response);
306             assertTrue(response.getContentAsString() != null
307                     && response.getContentAsString().contains("vnfTypeDictionaryData"));
308         } catch (Exception e) {
309             logger.error("Exception" + e);
310         }
311     }
312
313     @Test
314     public void testRemoveVnfType() {
315         jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"vnftype\":\"Test\"}}";
316         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
317             when(request.getReader()).thenReturn(br);
318             controller.removeVnfType(request, response);
319             assertTrue(response.getContentAsString() != null
320                     && response.getContentAsString().contains("vnfTypeDictionaryData"));
321         } catch (Exception e) {
322             logger.error("Exception" + e);
323         }
324     }
325
326     @Test
327     public void testSavePEPOptions() {
328         jsonString =
329                 "{\"pepOptionsDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"pepName\":\"Test\"},\"userid\":\"demo\"}";
330         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
331             when(request.getReader()).thenReturn(br);
332             controller.savePEPOptions(request, response);
333             assertTrue(response.getContentAsString() != null
334                     && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
335         } catch (Exception e) {
336             logger.error("Exception" + e);
337         }
338     }
339
340     @Test
341     public void testUpdatePEPOptions() {
342         jsonString =
343                 "{\"pepOptionsDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"pepName\":\"Test\",\"id\":1},\"userid\":\"demo\"}";
344         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
345             when(request.getReader()).thenReturn(br);
346             controller.savePEPOptions(request, response);
347             assertTrue(response.getContentAsString() != null
348                     && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
349         } catch (Exception e) {
350             logger.error("Exception" + e);
351         }
352     }
353
354     @Test
355     public void testRemovePEPOptions() {
356         jsonString =
357                 "{\"data\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"pepName\":\"Test\"},\"userid\":\"demo\"}";
358         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
359             when(request.getReader()).thenReturn(br);
360             controller.removePEPOptions(request, response);
361             assertTrue(response.getContentAsString() != null
362                     && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
363         } catch (Exception e) {
364             logger.error("Exception" + e);
365         }
366     }
367
368     @Test
369     public void testSaveServiceType() {
370         jsonString =
371                 "{\"userid\":\"demo\",\"closedLoopServiceDictionaryData\":{\"description\":\"test\",\"serviceName\":\"Test\",\"id\":1}}";
372         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
373             when(request.getReader()).thenReturn(br);
374             controller.saveServiceType(request, response);
375             assertTrue(response.getContentAsString() != null
376                     && response.getContentAsString().contains("closedLoopServiceDictionaryData"));
377         } catch (Exception e) {
378             logger.error("Exception" + e);
379         }
380     }
381
382     @Test
383     public void testUpdateServiceType() {
384         jsonString =
385                 "{\"userid\":\"demo\",\"closedLoopServiceDictionaryData\":{\"id\":1,\"description\":\"test\",\"serviceName\":\"Test\"}}";
386         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
387             when(request.getReader()).thenReturn(br);
388             controller.saveServiceType(request, response);
389             assertTrue(response.getContentAsString() != null
390                     && response.getContentAsString().contains("closedLoopServiceDictionaryData"));
391         } catch (Exception e) {
392             logger.error("Exception" + e);
393         }
394     }
395
396     @Test
397     public void testRemoveServiceType() {
398         jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"serviceName\":\"Test\"}}";
399         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
400             when(request.getReader()).thenReturn(br);
401             controller.removeServiceType(request, response);
402             assertTrue(response.getContentAsString() != null
403                     && response.getContentAsString().contains("closedLoopServiceDictionaryData"));
404         } catch (Exception e) {
405             logger.error("Exception" + e);
406         }
407     }
408
409     @Test
410     public void testSaveSiteType() {
411         jsonString =
412                 "{\"userid\":\"demo\",\"closedLoopSiteDictionaryData\":{\"description\":\"test\",\"siteName\":\"Test\"}}";
413         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
414             when(request.getReader()).thenReturn(br);
415             controller.saveSiteType(request, response);
416             assertTrue(response.getContentAsString() != null
417                     && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
418         } catch (Exception e) {
419             logger.error("Exception" + e);
420         }
421     }
422
423     @Test
424     public void testUpdateSiteType() {
425         jsonString =
426                 "{\"userid\":\"demo\",\"closedLoopSiteDictionaryData\":{\"id\":1,\"description\":\"test\",\"siteName\":\"Test\"}}";
427         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
428             when(request.getReader()).thenReturn(br);
429             controller.saveSiteType(request, response);
430             assertTrue(response.getContentAsString() != null
431                     && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
432         } catch (Exception e) {
433             logger.error("Exception" + e);
434         }
435     }
436
437     @Test
438     public void testRemoveSiteType() {
439         jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"siteName\":\"Test\"}}";
440         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
441             when(request.getReader()).thenReturn(br);
442             controller.removeSiteType(request, response);
443             assertTrue(response.getContentAsString() != null
444                     && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
445         } catch (Exception e) {
446             logger.error("Exception" + e);
447         }
448     }
449
450     @Test
451     public void testSaveVarbind() {
452         jsonString =
453                 "{\"userid\":\"demo\",\"varbindDictionaryData\":{\"description\":\"test\",\"varbindName\":\"Test\"}}";
454         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
455             when(request.getReader()).thenReturn(br);
456             controller.saveVarbind(request, response);
457             assertTrue(response.getContentAsString() != null
458                     && response.getContentAsString().contains("varbindDictionaryDatas"));
459         } catch (Exception e) {
460             logger.error("Exception" + e);
461         }
462     }
463
464     @Test
465     public void testUpdateVarbind() {
466         jsonString =
467                 "{\"userid\":\"demo\",\"varbindDictionaryData\":{\"id\":1,\"description\":\"test\",\"varbindName\":\"Test\"}}";
468         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
469             when(request.getReader()).thenReturn(br);
470             controller.saveVarbind(request, response);
471             assertTrue(response.getContentAsString() != null
472                     && response.getContentAsString().contains("varbindDictionaryDatas"));
473         } catch (Exception e) {
474             logger.error("Exception" + e);
475         }
476     }
477
478     @Test
479     public void testRemoveVarbind() {
480         jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"varbindName\":\"Test\"}}";
481         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
482             when(request.getReader()).thenReturn(br);
483             controller.removeVarbind(request, response);
484             assertTrue(response.getContentAsString() != null
485                     && response.getContentAsString().contains("varbindDictionaryDatas"));
486         } catch (Exception e) {
487             logger.error("Exception" + e);
488         }
489     }
490 }