Merge "JUnit additions for ONAP-PAP-REST"
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / DictionaryImportControllerTest.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
25 import java.io.ByteArrayOutputStream;
26 import java.io.File;
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;
32
33 import javax.servlet.ServletException;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36
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;
44
45 public class DictionaryImportControllerTest  extends Mockito{
46         
47         private static Logger logger = FlexLogger.getLogger(DictionaryImportController.class);
48
49         private static CommonClassDao commonClassDao;
50         private HttpServletRequest request = null;
51         private HttpServletResponse response = null;
52         private DictionaryImportController controller = null;
53         
54         @Before
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();
63         }
64         
65         @Test
66         public void testIsValidDictionaryName(){
67                 DictionaryImportController cotroller = new DictionaryImportController();
68                 //test invalid name
69                 assertTrue(!cotroller.isValidDictionaryName("wrong-name"));
70                 //test valid name
71                 assertTrue(cotroller.isValidDictionaryName("ActionList"));
72         }
73         
74         @Test
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("ClosedLoopService.csv");
82                 fileNames.add("ClosedLoopSite.csv");
83                 fileNames.add("VarbindDictionary.csv");
84                 fileNames.add("BRMSParamDictionary.csv");
85                 fileNames.add("BRMSControllerDictionary.csv");
86                 fileNames.add("BRMSDependencyDictionary.csv");
87                 fileNames.add("PrefixList.csv");
88                 fileNames.add("SecurityZone.csv");
89                 fileNames.add("ServiceList.csv");
90                 fileNames.add("ServiceGroup.csv");
91                 fileNames.add("AddressGroup.csv");
92                 fileNames.add("ProtocolList.csv");
93                 fileNames.add("TermList.csv");
94                 fileNames.add("SearchCriteria.csv");
95                 fileNames.add("VNFType.csv");
96                 fileNames.add("VSCLAction.csv");
97                 fileNames.add("PEPOptions.csv");
98                 fileNames.add("Settings.csv");
99                 fileNames.add("Zone.csv");
100                 fileNames.add("ActionList.csv");
101                 for(int i =0; i < fileNames.size(); i++){
102                         File file = new File("src/test/resources/dictionaryImport/"+fileNames.get(i));
103                         try(FileInputStream targetStream = new FileInputStream(file)){
104                                 PushPolicyControllerTest pushController = new PushPolicyControllerTest();
105                                 when(request.getInputStream()).thenReturn(pushController.getInputStream(getBytes(targetStream)));
106                                 when(request.getParameter("userId")).thenReturn("demo");
107                                 when(request.getParameter("dictionaryName")).thenReturn(fileNames.get(i));
108                                 controller.importDictionaryData(request, response);
109                                 assertTrue(HttpServletResponse.SC_OK == response.getStatus());
110                         } catch (IOException e) {
111                                 fail();
112                         }
113                 }
114                 when(request.getParameter("dictionaryName")).thenReturn("WrongName");
115                 controller.importDictionaryData(request, response);
116                 assertTrue(HttpServletResponse.SC_OK == response.getStatus());
117                 
118                 when(request.getParameter("dictionaryName")).thenReturn("");
119                 controller.importDictionaryData(request, response);
120                 assertTrue(HttpServletResponse.SC_BAD_REQUEST == response.getStatus());
121                 
122                 when(request.getInputStream()).thenReturn(null);
123                 when(request.getParameter("dictionaryName")).thenReturn("Attribute.csv");
124                 controller.importDictionaryData(request, response);
125                 assertTrue(HttpServletResponse.SC_INTERNAL_SERVER_ERROR == response.getStatus());
126         }
127         
128         public static byte[] getBytes(InputStream is) throws IOException {
129                 int len;
130                 int size = 1024;
131                 byte[] buf;
132                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
133                 buf = new byte[size];
134                 while ((len = is.read(buf, 0, size)) != -1)
135                         bos.write(buf, 0, len);
136                 buf = bos.toByteArray();
137                 return buf;
138         }
139 }