2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.policy.pap.xacml.rest.controller;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
26 import java.io.ByteArrayOutputStream;
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;
34 import javax.servlet.ServletException;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
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;
46 public class DictionaryImportControllerTest extends Mockito {
48 private static Logger logger = FlexLogger.getLogger(DictionaryImportController.class);
50 private static CommonClassDao commonClassDao;
51 private HttpServletRequest request = null;
52 private HttpServletResponse response = null;
53 private DictionaryImportController controller = null;
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();
67 public void testIsValidDictionaryName() {
68 DictionaryImportController cotroller = new DictionaryImportController();
70 assertTrue(!cotroller.isValidDictionaryName("wrong-name"));
72 assertTrue(cotroller.isValidDictionaryName("ActionList"));
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) {
116 when(request.getParameter("dictionaryName")).thenReturn("WrongName");
117 controller.importDictionaryData(request, response);
118 assertTrue(HttpServletResponse.SC_BAD_REQUEST == response.getStatus());
120 when(request.getParameter("dictionaryName")).thenReturn("");
121 controller.importDictionaryData(request, response);
122 assertTrue(HttpServletResponse.SC_BAD_REQUEST == response.getStatus());
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());
130 public static byte[] getBytes(InputStream is) throws IOException {
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();