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.controller;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.mock;
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.StringReader;
31 import javax.servlet.http.HttpServletRequest;
33 import org.apache.commons.io.IOUtils;
34 import org.apache.commons.lang3.ArrayUtils;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.springframework.mock.web.MockHttpServletResponse;
39 import org.springframework.mock.web.MockMultipartFile;
40 import org.springframework.mock.web.MockMultipartHttpServletRequest;
41 import org.springframework.util.FileCopyUtils;
43 public class ExportAndImportDecisionBlackListEntriesTest {
45 private HttpServletRequest request;
46 private MockHttpServletResponse response;
50 public void setUp() throws Exception {
51 request = mock(HttpServletRequest.class);
52 response = new MockHttpServletResponse();
56 public void testExportBlackList() throws IOException {
57 ClassLoader classLoader = getClass().getClassLoader();
58 jsonString = IOUtils.toString(classLoader.getResourceAsStream("DecisionPolicyData.txt"));
59 try (BufferedReader reader = new BufferedReader(new StringReader(jsonString))) {
60 Mockito.when(request.getReader()).thenReturn(reader);
61 ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries();
62 controller.exportBlackList(request, response);
63 assertTrue("".equals(response.getContentAsString()));
64 } catch (Exception e) {
65 fail("Not expecting Exception while Exporting BlackListEntries.");
70 public void testImportBlackList() throws Exception {
71 byte[] fileContent = FileCopyUtils
72 .copyToByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream("BlackList.xls"));
74 MockMultipartFile file = new MockMultipartFile("BlackList.xls",
75 Thread.currentThread().getContextClassLoader().getResourceAsStream("BlackList.xls"));
77 MockMultipartHttpServletRequest req = new MockMultipartHttpServletRequest();
78 req.setMethod("POST");
80 String boundary = "JPnJUN6FOo0qLySf-__r_RY1nQE7QOXXJ_nLK1s";
81 req.setContentType("multipart/form-data; boundary=" + boundary);
83 String start = "--" + boundary + "\r\n Content-Disposition: form-data; name=\"file\"; filename=\""
84 + "BlackList.xls" + "\"\r\n" + "Content-type: " + "application/vnd.ms-excel" + "\r\n\r\n";
86 String end = "\r\n--" + boundary + "--";
87 req.setContent(ArrayUtils.addAll(start.getBytes(), ArrayUtils.addAll(fileContent, end.getBytes())));
88 req.addHeader("name", "BlackList.xls");
90 ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries();
91 MockHttpServletResponse resp = new MockHttpServletResponse();
92 controller.importBlackListFile(req, resp);
93 assertTrue(resp.getContentAsString().contains("data"));