2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.pap.xacml.rest.controller;
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.when;
27 import java.io.BufferedReader;
28 import java.io.StringReader;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.List;
33 import javax.servlet.http.HttpServletRequest;
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.rest.dao.CommonClassDao;
41 import org.onap.policy.rest.jpa.ClosedLoopD2Services;
42 import org.onap.policy.rest.jpa.ClosedLoopSite;
43 import org.onap.policy.rest.jpa.PEPOptions;
44 import org.onap.policy.rest.jpa.UserInfo;
45 import org.onap.policy.rest.jpa.VNFType;
46 import org.onap.policy.rest.jpa.VSCLAction;
47 import org.onap.policy.rest.jpa.VarbindDictionary;
48 import org.springframework.mock.web.MockHttpServletResponse;
50 public class ClosedLoopDictionaryControllerTest {
52 private static Logger logger = FlexLogger.getLogger(ClosedLoopDictionaryControllerTest.class);
53 private static CommonClassDao commonClassDao;
54 private String jsonString = null;
55 private HttpServletRequest request = null;
56 private ClosedLoopDictionaryController controller = null;
57 private MockHttpServletResponse response = null;
58 private UserInfo userInfo;
59 private List<String> data;
62 public void setUp() throws Exception {
63 logger.info("setUp: Entering");
64 commonClassDao = Mockito.mock(CommonClassDao.class);
66 data = new ArrayList<>();
69 userInfo = new UserInfo();
70 userInfo.setUserLoginId("Test");
71 userInfo.setUserName("Test");
73 doNothing().when(commonClassDao).delete(new VSCLAction());
74 doNothing().when(commonClassDao).save(new VSCLAction());
76 controller = new ClosedLoopDictionaryController();
77 controller.setCommonClassDao(commonClassDao);
79 request = Mockito.mock(HttpServletRequest.class);
80 response = new MockHttpServletResponse();
81 logger.info("setUp: exit");
84 public List<Object> testVSCLAction(){
85 List<Object> objectData = new ArrayList<>();
87 VSCLAction data = new VSCLAction();
89 assertTrue(1 == data.getId());
90 data.setVsclaction("Test");
91 assertTrue("Test".equals(data.getVsclaction()));
92 data.setDescription("Test");
93 assertTrue("Test".equals(data.getDescription()));
94 data.setCreatedDate(new Date());
95 assertTrue(data.getCreatedDate()!= null);
96 data.setModifiedDate(new Date());
97 assertTrue(data.getModifiedDate()!= null);
98 data.setUserCreatedBy(userInfo);
99 assertTrue(data.getUserCreatedBy()!= null);
100 data.setUserModifiedBy(userInfo);
101 assertTrue(data.getUserModifiedBy()!= null);
102 objectData.add(data);
107 public List<Object> testVNFType(){
108 List<Object> objectData = new ArrayList<>();
110 VNFType data = new VNFType();
112 assertTrue(1 == data.getId());
113 data.setVnftype("Test");
114 assertTrue("Test".equals(data.getVnftype()));
115 data.setDescription("Test");
116 assertTrue("Test".equals(data.getDescription()));
117 data.setCreatedDate(new Date());
118 assertTrue(data.getCreatedDate()!= null);
119 data.setModifiedDate(new Date());
120 assertTrue(data.getModifiedDate()!= null);
121 data.setUserCreatedBy(userInfo);
122 assertTrue(data.getUserCreatedBy()!= null);
123 data.setUserModifiedBy(userInfo);
124 assertTrue(data.getUserModifiedBy()!= null);
125 objectData.add(data);
130 public List<Object> testPEPOptions(){
131 List<Object> objectData = new ArrayList<>();
133 PEPOptions data = new PEPOptions();
135 assertTrue(1 == data.getId());
136 data.setPepName("Test");
137 assertTrue("Test".equals(data.getPepName()));
138 data.setDescription("Test");
139 assertTrue("Test".equals(data.getDescription()));
140 data.setCreatedDate(new Date());
141 assertTrue(data.getCreatedDate()!= null);
142 data.setModifiedDate(new Date());
143 assertTrue(data.getModifiedDate()!= null);
144 data.setUserCreatedBy(userInfo);
145 assertTrue(data.getUserCreatedBy()!= null);
146 data.setUserModifiedBy(userInfo);
147 assertTrue(data.getUserModifiedBy()!= null);
148 objectData.add(data);
153 public List<Object> testVarbindDictionary(){
154 List<Object> objectData = new ArrayList<>();
156 VarbindDictionary data = new VarbindDictionary();
158 assertTrue(1 == data.getId());
159 data.setVarbindName("Test");
160 assertTrue("Test".equals(data.getVarbindName()));
161 data.setVarbindDescription("Test");
162 assertTrue("Test".equals(data.getVarbindDescription()));
163 data.setVarbindOID("Test");
164 assertTrue("Test".equals(data.getVarbindOID()));
165 data.setCreatedDate(new Date());
166 assertTrue(data.getCreatedDate()!= null);
167 data.setModifiedDate(new Date());
168 assertTrue(data.getModifiedDate()!= null);
169 data.setUserCreatedBy(userInfo);
170 assertTrue(data.getUserCreatedBy()!= null);
171 data.setUserModifiedBy(userInfo);
172 assertTrue(data.getUserModifiedBy()!= null);
173 objectData.add(data);
178 public List<Object> testClosedLoopD2Services(){
179 List<Object> objectData = new ArrayList<>();
181 ClosedLoopD2Services data = new ClosedLoopD2Services();
183 assertTrue(1 == data.getId());
184 data.setServiceName("Test");
185 assertTrue("Test".equals(data.getServiceName()));
186 data.setDescription("Test");
187 assertTrue("Test".equals(data.getDescription()));
188 data.setCreatedDate(new Date());
189 assertTrue(data.getCreatedDate()!= null);
190 data.setModifiedDate(new Date());
191 assertTrue(data.getModifiedDate()!= null);
192 data.setUserCreatedBy(userInfo);
193 assertTrue(data.getUserCreatedBy()!= null);
194 data.setUserModifiedBy(userInfo);
195 assertTrue(data.getUserModifiedBy()!= null);
196 objectData.add(data);
202 public List<Object> testClosedLoopSite(){
203 List<Object> objectData = new ArrayList<>();
205 ClosedLoopSite data = new ClosedLoopSite();
207 assertTrue(1 == data.getId());
208 data.setSiteName("Test");
209 assertTrue("Test".equals(data.getSiteName()));
210 data.setDescription("Test");
211 assertTrue("Test".equals(data.getDescription()));
212 data.setCreatedDate(new Date());
213 assertTrue(data.getCreatedDate()!= null);
214 data.setModifiedDate(new Date());
215 assertTrue(data.getModifiedDate()!= null);
216 data.setUserCreatedBy(userInfo);
217 assertTrue(data.getUserCreatedBy()!= null);
218 data.setUserModifiedBy(userInfo);
219 assertTrue(data.getUserModifiedBy()!= null);
220 objectData.add(data);
226 public void testGetVSCLActionDictionaryByNameEntityData(){
227 when(commonClassDao.getDataByColumn(VSCLAction.class, "vsclaction")).thenReturn(data);
228 controller.getVSCLActionDictionaryByNameEntityData(response);
230 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vsclActionDictionaryDatas"));
231 } catch (Exception e) {
233 logger.error(e.getMessage(),e);
238 public void testGetVSCLActionDictionaryEntityData(){
239 when(commonClassDao.getData(VSCLAction.class)).thenReturn(testVSCLAction());
240 controller.getVSCLActionDictionaryEntityData(response);
242 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vsclActionDictionaryDatas"));
243 } catch (Exception e) {
245 logger.error(e.getMessage(),e);
250 public void testGetVNFTypeDictionaryByNameEntityData(){
251 when(commonClassDao.getDataByColumn(VNFType.class, "vnftype")).thenReturn(data);
252 controller.getVNFTypeDictionaryByNameEntityData(response);
254 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vnfTypeDictionaryDatas"));
255 } catch (Exception e) {
257 logger.error(e.getMessage(),e);
262 public void testGetVNFTypeDictionaryEntityData(){
263 when(commonClassDao.getData(VNFType.class)).thenReturn(testVNFType());
264 controller.getVNFTypeDictionaryEntityData(response);
266 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vnfTypeDictionaryDatas"));
267 } catch (Exception e) {
269 logger.error(e.getMessage(),e);
274 public void testGetPEPOptionsDictionaryByNameEntityData(){
275 when(commonClassDao.getDataByColumn(PEPOptions.class, "pepName")).thenReturn(data);
276 controller.getPEPOptionsDictionaryByNameEntityData(response);
278 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
279 } catch (Exception e) {
281 logger.error(e.getMessage(),e);
286 public void testGetPEPOptionsDictionaryEntityData(){
287 when(commonClassDao.getData(PEPOptions.class)).thenReturn(testPEPOptions());
288 controller.getPEPOptionsDictionaryEntityData(response);
290 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
291 } catch (Exception e) {
293 logger.error(e.getMessage(),e);
298 public void testGetVarbindDictionaryByNameEntityData(){
299 when(commonClassDao.getDataByColumn(VarbindDictionary.class, "varbindName")).thenReturn(data);
300 controller.getVarbindDictionaryByNameEntityData(response);
302 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("varbindDictionaryDatas"));
303 } catch (Exception e) {
305 logger.error(e.getMessage(),e);
310 public void testGetVarbindDictionaryEntityData(){
311 when(commonClassDao.getData(VarbindDictionary.class)).thenReturn(testVarbindDictionary());
312 controller.getVarbindDictionaryEntityData(response);
314 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("varbindDictionaryDatas"));
315 } catch (Exception e) {
317 logger.error(e.getMessage(),e);
322 public void testGetClosedLoopServiceDictionaryByNameEntityData(){
323 when(commonClassDao.getDataByColumn(ClosedLoopD2Services.class, "serviceName")).thenReturn(data);
324 controller.getClosedLoopServiceDictionaryByNameEntityData(response);
326 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopServiceDictionaryDatas"));
327 } catch (Exception e) {
329 logger.error(e.getMessage(),e);
334 public void testGetClosedLoopServiceDictionaryEntityData(){
335 when(commonClassDao.getData(ClosedLoopD2Services.class)).thenReturn(testClosedLoopD2Services());
336 controller.getClosedLoopServiceDictionaryEntityData(response);
338 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopServiceDictionaryDatas"));
339 } catch (Exception e) {
341 logger.error(e.getMessage(),e);
346 public void testGetClosedLoopSiteDictionaryByNameEntityData(){
347 when(commonClassDao.getDataByColumn(ClosedLoopSite.class, "siteName")).thenReturn(data);
348 controller.getClosedLoopSiteDictionaryByNameEntityData(response);
350 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
351 } catch (Exception e) {
353 logger.error(e.getMessage(),e);
358 public void testGetClosedLoopSiteDictionaryEntityData(){
359 when(commonClassDao.getData(ClosedLoopSite.class)).thenReturn(testClosedLoopSite());
360 controller.getClosedLoopSiteDictionaryEntityData(response);
362 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
363 } catch (Exception e) {
365 logger.error(e.getMessage(),e);
370 public void testSaveVSCLAction(){
371 jsonString = "{\"userid\":\"demo\",\"vsclActionDictionaryData\":{\"description\":\"test\",\"vsclaction\":\"Test\"}}";
372 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
373 when(request.getReader()).thenReturn(br);
374 controller.saveVSCLAction(request, response);
375 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vsclActionDictionaryDatas"));
377 logger.error("Exception"+ e);
382 public void testUpdateVSCLAction(){
383 jsonString = "{\"userid\":\"demo\",\"vsclActionDictionaryData\":{\"id\":1,\"description\":\"test\",\"vsclaction\":\"Test\"}}";
384 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
385 when(request.getReader()).thenReturn(br);
386 controller.saveVSCLAction(request, response);
387 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vsclActionDictionaryDatas"));
389 logger.error("Exception"+ e);
394 public void testRemoveVSCLAction(){
395 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"vsclaction\":\"Test\"}}";
396 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
397 when(request.getReader()).thenReturn(br);
398 controller.removeVSCLAction(request, response);
399 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vsclActionDictionaryDatas"));
401 logger.error("Exception"+ e);
406 public void testSaveVnfType(){
407 jsonString = "{\"userid\":\"demo\",\"vnfTypeDictionaryData\":{\"description\":\"test\",\"vnftype\":\"Test\"}}";
408 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
409 when(request.getReader()).thenReturn(br);
410 controller.saveVnfType(request, response);
411 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vnfTypeDictionaryData"));
413 logger.error("Exception"+ e);
418 public void testUpdateVnfType(){
419 jsonString = "{\"userid\":\"demo\",\"vnfTypeDictionaryData\":{\"id\":1,\"description\":\"test\",\"vnftype\":\"Test\"}}";
420 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
421 when(request.getReader()).thenReturn(br);
422 controller.saveVnfType(request, response);
423 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vnfTypeDictionaryData"));
425 logger.error("Exception"+ e);
430 public void testRemoveVnfType(){
431 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"vnftype\":\"Test\"}}";
432 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
433 when(request.getReader()).thenReturn(br);
434 controller.removeVnfType(request, response);
435 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vnfTypeDictionaryData"));
437 logger.error("Exception"+ e);
442 public void testSavePEPOptions(){
443 jsonString = "{\"pepOptionsDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"pepName\":\"Test\"},\"userid\":\"demo\"}";
444 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
445 when(request.getReader()).thenReturn(br);
446 controller.savePEPOptions(request, response);
447 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
449 logger.error("Exception"+ e);
454 public void testUpdatePEPOptions(){
455 jsonString = "{\"pepOptionsDictionaryData\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"pepName\":\"Test\",\"id\":1},\"userid\":\"demo\"}";
456 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
457 when(request.getReader()).thenReturn(br);
458 controller.savePEPOptions(request, response);
459 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
461 logger.error("Exception"+ e);
466 public void testRemovePEPOptions(){
467 jsonString = "{\"data\":{\"attributes\":[{\"$$hashKey\":\"object:257\",\"id\":\"choice1\",\"number\":\"12\",\"option\":\"test\"}],\"description\":\"test\",\"pepName\":\"Test\"},\"userid\":\"demo\"}";
468 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
469 when(request.getReader()).thenReturn(br);
470 controller.removePEPOptions(request, response);
471 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
473 logger.error("Exception"+ e);
478 public void testSaveServiceType(){
479 jsonString = "{\"userid\":\"demo\",\"closedLoopServiceDictionaryData\":{\"description\":\"test\",\"serviceName\":\"Test\",\"id\":1}}";
480 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
481 when(request.getReader()).thenReturn(br);
482 controller.saveServiceType(request, response);
483 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopServiceDictionaryData"));
485 logger.error("Exception"+ e);
490 public void testUpdateServiceType(){
491 jsonString = "{\"userid\":\"demo\",\"closedLoopServiceDictionaryData\":{\"id\":1,\"description\":\"test\",\"serviceName\":\"Test\"}}";
492 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
493 when(request.getReader()).thenReturn(br);
494 controller.saveServiceType(request, response);
495 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopServiceDictionaryData"));
497 logger.error("Exception"+ e);
502 public void testRemoveServiceType(){
503 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"serviceName\":\"Test\"}}";
504 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
505 when(request.getReader()).thenReturn(br);
506 controller.removeServiceType(request, response);
507 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopServiceDictionaryData"));
509 logger.error("Exception"+ e);
514 public void testSaveSiteType(){
515 jsonString = "{\"userid\":\"demo\",\"closedLoopSiteDictionaryData\":{\"description\":\"test\",\"siteName\":\"Test\"}}";
516 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
517 when(request.getReader()).thenReturn(br);
518 controller.saveSiteType(request, response);
519 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
521 logger.error("Exception"+ e);
526 public void testUpdateSiteType(){
527 jsonString = "{\"userid\":\"demo\",\"closedLoopSiteDictionaryData\":{\"id\":1,\"description\":\"test\",\"siteName\":\"Test\"}}";
528 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
529 when(request.getReader()).thenReturn(br);
530 controller.saveSiteType(request, response);
531 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
533 logger.error("Exception"+ e);
538 public void testRemoveSiteType(){
539 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"siteName\":\"Test\"}}";
540 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
541 when(request.getReader()).thenReturn(br);
542 controller.removeSiteType(request, response);
543 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
545 logger.error("Exception"+ e);
550 public void testSaveVarbind(){
551 jsonString = "{\"userid\":\"demo\",\"varbindDictionaryData\":{\"description\":\"test\",\"varbindName\":\"Test\"}}";
552 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
553 when(request.getReader()).thenReturn(br);
554 controller.saveVarbind(request, response);
555 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("varbindDictionaryDatas"));
557 logger.error("Exception"+ e);
562 public void testUpdateVarbind(){
563 jsonString = "{\"userid\":\"demo\",\"varbindDictionaryData\":{\"id\":1,\"description\":\"test\",\"varbindName\":\"Test\"}}";
564 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
565 when(request.getReader()).thenReturn(br);
566 controller.saveVarbind(request, response);
567 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("varbindDictionaryDatas"));
569 logger.error("Exception"+ e);
574 public void testRemoveVarbind(){
575 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"varbindName\":\"Test\"}}";
576 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
577 when(request.getReader()).thenReturn(br);
578 controller.removeVarbind(request, response);
579 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("varbindDictionaryDatas"));
581 logger.error("Exception"+ e);