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