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.mock;
 
  26 import static org.mockito.Mockito.when;
 
  28 import java.io.BufferedReader;
 
  29 import java.io.StringReader;
 
  30 import java.util.ArrayList;
 
  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.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;
 
  51 public class ClosedLoopDictionaryControllerTest {
 
  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;
 
  63     public void setUp() throws Exception {
 
  64         logger.info("setUp: Entering");
 
  65         commonClassDao = Mockito.mock(CommonClassDao.class);
 
  67         data = new ArrayList<>();
 
  70         userInfo = new UserInfo();
 
  71         userInfo.setUserLoginId("Test");
 
  72         userInfo.setUserName("Test");
 
  74         doNothing().when(commonClassDao).delete(new VSCLAction());
 
  75         doNothing().when(commonClassDao).save(new VSCLAction());
 
  77         controller = new ClosedLoopDictionaryController();
 
  78         controller.setCommonClassDao(commonClassDao);
 
  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");
 
  89     public void testGetVSCLActionDictionaryByNameEntityData(){
 
  90         when(commonClassDao.getDataByColumn(VSCLAction.class, "vsclaction")).thenReturn(data);
 
  91         controller.getVSCLActionDictionaryByNameEntityData(response);
 
  93             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vsclActionDictionaryDatas"));
 
  94         } catch (Exception e) {
 
  96             logger.error(e.getMessage(),e);
 
 101     public void testGetVSCLActionDictionaryEntityData(){
 
 102         when(commonClassDao.getData(VSCLAction.class)).thenReturn(new ArrayList<>());
 
 103         controller.getVSCLActionDictionaryEntityData(response);
 
 105             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vsclActionDictionaryDatas"));
 
 106         } catch (Exception e) {
 
 108             logger.error(e.getMessage(),e);
 
 113     public void testGetVNFTypeDictionaryByNameEntityData(){
 
 114         when(commonClassDao.getDataByColumn(VNFType.class, "vnftype")).thenReturn(data);
 
 115         controller.getVNFTypeDictionaryByNameEntityData(response);
 
 117             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vnfTypeDictionaryDatas"));
 
 118         } catch (Exception e) {
 
 120             logger.error(e.getMessage(),e);
 
 125     public void testGetVNFTypeDictionaryEntityData(){
 
 126         when(commonClassDao.getData(VNFType.class)).thenReturn(new ArrayList<>());
 
 127         controller.getVNFTypeDictionaryEntityData(response);
 
 129             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("vnfTypeDictionaryDatas"));
 
 130         } catch (Exception e) {
 
 132             logger.error(e.getMessage(),e);
 
 137     public void testGetPEPOptionsDictionaryByNameEntityData(){
 
 138         when(commonClassDao.getDataByColumn(PEPOptions.class, "pepName")).thenReturn(data);
 
 139         controller.getPEPOptionsDictionaryByNameEntityData(response);
 
 141             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
 
 142         } catch (Exception e) {
 
 144             logger.error(e.getMessage(),e);
 
 149     public void testGetPEPOptionsDictionaryEntityData(){
 
 150         when(commonClassDao.getData(PEPOptions.class)).thenReturn(new ArrayList<>());
 
 151         controller.getPEPOptionsDictionaryEntityData(response);
 
 153             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("pepOptionsDictionaryDatas"));
 
 154         } catch (Exception e) {
 
 156             logger.error(e.getMessage(),e);
 
 161     public void testGetVarbindDictionaryByNameEntityData(){
 
 162         when(commonClassDao.getDataByColumn(VarbindDictionary.class, "varbindName")).thenReturn(data);
 
 163         controller.getVarbindDictionaryByNameEntityData(response);
 
 165             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("varbindDictionaryDatas"));
 
 166         } catch (Exception e) {
 
 168             logger.error(e.getMessage(),e);
 
 173     public void testGetVarbindDictionaryEntityData(){
 
 174         when(commonClassDao.getData(VarbindDictionary.class)).thenReturn(new ArrayList<>());
 
 175         controller.getVarbindDictionaryEntityData(response);
 
 177             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("varbindDictionaryDatas"));
 
 178         } catch (Exception e) {
 
 180             logger.error(e.getMessage(),e);
 
 185     public void testGetClosedLoopServiceDictionaryByNameEntityData(){
 
 186         when(commonClassDao.getDataByColumn(ClosedLoopD2Services.class, "serviceName")).thenReturn(data);
 
 187         controller.getClosedLoopServiceDictionaryByNameEntityData(response);
 
 189             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopServiceDictionaryDatas"));
 
 190         } catch (Exception e) {
 
 192             logger.error(e.getMessage(),e);
 
 197     public void testGetClosedLoopServiceDictionaryEntityData(){
 
 198         when(commonClassDao.getData(ClosedLoopD2Services.class)).thenReturn(new ArrayList<>());
 
 199         controller.getClosedLoopServiceDictionaryEntityData(response);
 
 201             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopServiceDictionaryDatas"));
 
 202         } catch (Exception e) {
 
 204             logger.error(e.getMessage(),e);
 
 209     public void testGetClosedLoopSiteDictionaryByNameEntityData(){
 
 210         when(commonClassDao.getDataByColumn(ClosedLoopSite.class, "siteName")).thenReturn(data);
 
 211         controller.getClosedLoopSiteDictionaryByNameEntityData(response);
 
 213             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
 
 214         } catch (Exception e) {
 
 216             logger.error(e.getMessage(),e);
 
 221     public void testGetClosedLoopSiteDictionaryEntityData(){
 
 222         when(commonClassDao.getData(ClosedLoopSite.class)).thenReturn(new ArrayList<>());
 
 223         controller.getClosedLoopSiteDictionaryEntityData(response);
 
 225             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("closedLoopSiteDictionaryDatas"));
 
 226         } catch (Exception e) {
 
 228             logger.error(e.getMessage(),e);
 
 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"));
 
 240             logger.error("Exception"+ e);
 
 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"));
 
 252             logger.error("Exception"+ e);
 
 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"));
 
 264             logger.error("Exception"+ e);
 
 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"));
 
 276             logger.error("Exception"+ e);
 
 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"));
 
 288             logger.error("Exception"+ e);
 
 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"));
 
 300             logger.error("Exception"+ e);
 
 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"));
 
 312             logger.error("Exception"+ e);
 
 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"));
 
 324             logger.error("Exception"+ e);
 
 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"));
 
 336             logger.error("Exception"+ e);
 
 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"));
 
 348             logger.error("Exception"+ e);
 
 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"));
 
 360             logger.error("Exception"+ e);
 
 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"));
 
 372             logger.error("Exception"+ e);
 
 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"));
 
 384             logger.error("Exception"+ e);
 
 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"));
 
 396             logger.error("Exception"+ e);
 
 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"));
 
 408             logger.error("Exception"+ e);
 
 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"));
 
 420             logger.error("Exception"+ e);
 
 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"));
 
 432             logger.error("Exception"+ e);
 
 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"));
 
 444             logger.error("Exception"+ e);