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