2 * ============LICENSE_START=======================================================
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
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;
30 import javax.servlet.http.HttpServletRequest;
31 import org.apache.commons.io.IOUtils;
32 import org.apache.commons.lang3.ArrayUtils;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.springframework.mock.web.MockHttpServletResponse;
37 import org.springframework.mock.web.MockMultipartFile;
38 import org.springframework.mock.web.MockMultipartHttpServletRequest;
39 import org.springframework.util.FileCopyUtils;
41 public class ExportAndImportDecisionBlackListEntriesTest {
43 private HttpServletRequest request;
44 private MockHttpServletResponse response;
48 public void setUp() throws Exception {
49 request = mock(HttpServletRequest.class);
50 response = new MockHttpServletResponse();
54 public void testExportBlackList() throws IOException {
55 ClassLoader classLoader = getClass().getClassLoader();
56 jsonString = IOUtils.toString(classLoader.getResourceAsStream("DecisionPolicyData.txt"));
57 try (BufferedReader reader = new BufferedReader(new StringReader(jsonString))) {
58 Mockito.when(request.getReader()).thenReturn(reader);
59 ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries();
60 controller.exportBlackList(request, response);
61 assertTrue("".equals(response.getContentAsString()));
62 } catch (Exception e) {
63 fail("Not expecting Exception while Exporting BlackListEntries.");
68 public void testImportBlackList() throws Exception {
69 byte[] fileContent = FileCopyUtils
70 .copyToByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream("BlackList.xls"));
72 MockMultipartFile file = new MockMultipartFile("BlackList.xls",
73 Thread.currentThread().getContextClassLoader().getResourceAsStream("BlackList.xls"));
75 MockMultipartHttpServletRequest req = new MockMultipartHttpServletRequest();
76 req.setMethod("POST");
78 String boundary = "JPnJUN6FOo0qLySf-__r_RY1nQE7QOXXJ_nLK1s";
79 req.setContentType("multipart/form-data; boundary=" + boundary);
81 String start = "--" + boundary + "\r\n Content-Disposition: form-data; name=\"file\"; filename=\""
82 + "BlackList.xls" + "\"\r\n" + "Content-type: " + "application/vnd.ms-excel" + "\r\n\r\n";
84 String end = "\r\n--" + boundary + "--";
85 req.setContent(ArrayUtils.addAll(start.getBytes(), ArrayUtils.addAll(fileContent, end.getBytes())));
86 req.addHeader("name", "BlackList.xls");
88 ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries();
89 MockHttpServletResponse resp = new MockHttpServletResponse();
90 controller.importBlackListFile(req, resp);
91 assertTrue(resp.getContentAsString().contains("data"));