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;
 
  25 import java.io.ByteArrayOutputStream;
 
  27 import java.io.FileInputStream;
 
  28 import java.io.IOException;
 
  29 import java.io.InputStream;
 
  30 import java.util.ArrayList;
 
  31 import java.util.List;
 
  33 import javax.servlet.ServletException;
 
  34 import javax.servlet.http.HttpServletRequest;
 
  35 import javax.servlet.http.HttpServletResponse;
 
  37 import org.junit.Before;
 
  38 import org.junit.Test;
 
  39 import org.mockito.Mockito;
 
  40 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 
  41 import org.onap.policy.common.logging.flexlogger.Logger;
 
  42 import org.onap.policy.rest.dao.CommonClassDao;
 
  43 import org.springframework.mock.web.MockHttpServletResponse;
 
  45 public class DictionaryImportControllerTest extends Mockito{
 
  47         private static Logger logger = FlexLogger.getLogger(DictionaryImportController.class);
 
  49         private static CommonClassDao commonClassDao;
 
  50         private HttpServletRequest request = null;
 
  51         private HttpServletResponse response = null;
 
  52         private DictionaryImportController controller = null;
 
  55         public void setUp() throws Exception {
 
  56                 logger.info("setUp: Entering");
 
  57                 commonClassDao = Mockito.mock(CommonClassDao.class);
 
  58                 doNothing().when(commonClassDao).save(new Object());
 
  59                 controller = new DictionaryImportController(); 
 
  60                 new DictionaryImportController(commonClassDao);
 
  61                 request = Mockito.mock(HttpServletRequest.class);
 
  62                 response =  new MockHttpServletResponse();      
 
  66         public void testIsValidDictionaryName(){
 
  67                 DictionaryImportController cotroller = new DictionaryImportController();
 
  69                 assertTrue(!cotroller.isValidDictionaryName("wrong-name"));
 
  71                 assertTrue(cotroller.isValidDictionaryName("ActionList"));
 
  75         public void testImportDictionaryData() throws ServletException, IOException{
 
  76                 List<String> fileNames = new ArrayList<>();
 
  77                 fileNames.add("Attribute.csv");
 
  78                 fileNames.add("ActionPolicyDictionary.csv");
 
  79                 fileNames.add("OnapName.csv");
 
  80                 fileNames.add("MSPolicyDictionary.csv");
 
  81                 fileNames.add("OptimizationPolicyDictionary.csv");
 
  82                 fileNames.add("ClosedLoopService.csv");
 
  83                 fileNames.add("ClosedLoopSite.csv");
 
  84                 fileNames.add("VarbindDictionary.csv");
 
  85                 fileNames.add("BRMSParamDictionary.csv");
 
  86                 fileNames.add("BRMSControllerDictionary.csv");
 
  87                 fileNames.add("BRMSDependencyDictionary.csv");
 
  88                 fileNames.add("PrefixList.csv");
 
  89                 fileNames.add("SecurityZone.csv");
 
  90                 fileNames.add("ServiceList.csv");
 
  91                 fileNames.add("ServiceGroup.csv");
 
  92                 fileNames.add("AddressGroup.csv");
 
  93                 fileNames.add("ProtocolList.csv");
 
  94                 fileNames.add("TermList.csv");
 
  95                 fileNames.add("SearchCriteria.csv");
 
  96                 fileNames.add("VNFType.csv");
 
  97                 fileNames.add("VSCLAction.csv");
 
  98                 fileNames.add("PEPOptions.csv");
 
  99                 fileNames.add("Settings.csv");
 
 100                 fileNames.add("Zone.csv");
 
 101                 fileNames.add("ActionList.csv");
 
 102                 for(int i =0; i < fileNames.size(); i++){
 
 103                         File file = new File("src/test/resources/dictionaryImport/"+fileNames.get(i));
 
 104                         try(FileInputStream targetStream = new FileInputStream(file)){
 
 105                                 PushPolicyControllerTest pushController = new PushPolicyControllerTest();
 
 106                                 when(request.getInputStream()).thenReturn(pushController.getInputStream(getBytes(targetStream)));
 
 107                                 when(request.getParameter("userId")).thenReturn("demo");
 
 108                                 when(request.getParameter("dictionaryName")).thenReturn(fileNames.get(i));
 
 109                                 controller.importDictionaryData(request, response);
 
 110                                 assertTrue(HttpServletResponse.SC_OK == response.getStatus());
 
 111                         } catch (IOException e) {
 
 115                 when(request.getParameter("dictionaryName")).thenReturn("WrongName");
 
 116                 controller.importDictionaryData(request, response);
 
 117                 assertTrue(HttpServletResponse.SC_BAD_REQUEST == response.getStatus());
 
 119                 when(request.getParameter("dictionaryName")).thenReturn("");
 
 120                 controller.importDictionaryData(request, response);
 
 121                 assertTrue(HttpServletResponse.SC_BAD_REQUEST == response.getStatus());
 
 123                 when(request.getInputStream()).thenReturn(null);
 
 124                 when(request.getParameter("dictionaryName")).thenReturn("Attribute.csv");
 
 125                 controller.importDictionaryData(request, response);
 
 126                 assertTrue(HttpServletResponse.SC_INTERNAL_SERVER_ERROR == response.getStatus());
 
 129         public static byte[] getBytes(InputStream is) throws IOException {
 
 133                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
 134                 buf = new byte[size];
 
 135                 while ((len = is.read(buf, 0, size)) != -1)
 
 136                         bos.write(buf, 0, len);
 
 137                 buf = bos.toByteArray();