6a28c271ad444cd8292a2ec01515ef4544f0352e
[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("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) {
112                                 fail();
113                         }
114                 }
115                 when(request.getParameter("dictionaryName")).thenReturn("WrongName");
116                 controller.importDictionaryData(request, response);
117                 assertTrue(HttpServletResponse.SC_BAD_REQUEST == response.getStatus());
118                 
119                 when(request.getParameter("dictionaryName")).thenReturn("");
120                 controller.importDictionaryData(request, response);
121                 assertTrue(HttpServletResponse.SC_BAD_REQUEST == response.getStatus());
122                 
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());
127         }
128         
129         public static byte[] getBytes(InputStream is) throws IOException {
130                 int len;
131                 int size = 1024;
132                 byte[] buf;
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();
138                 return buf;
139         }
140 }