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=========================================================
20 package org.onap.policy.controller;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.mock;
26 import java.io.BufferedReader;
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.StringReader;
35 import javax.servlet.ReadListener;
36 import javax.servlet.ServletInputStream;
37 import javax.servlet.http.HttpServletRequest;
39 import org.apache.commons.io.IOUtils;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mockito;
43 import org.springframework.mock.web.MockHttpServletRequest;
44 import org.springframework.mock.web.MockHttpServletResponse;
46 public class ExportAndImportDecisionBlackListEntriesTest {
48 private HttpServletRequest request;
49 private MockHttpServletResponse response;
53 public void setUp() throws Exception {
54 request = mock(HttpServletRequest.class);
55 response = new MockHttpServletResponse();
59 public void testExportBlackList() throws IOException{
60 ClassLoader classLoader = getClass().getClassLoader();
61 jsonString = IOUtils.toString(classLoader.getResourceAsStream("DecisionPolicyData.txt"));
62 try(BufferedReader reader = new BufferedReader(new StringReader(jsonString))){
63 Mockito.when(request.getReader()).thenReturn(reader);
64 ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries();
65 controller.exportBlackList(request, response);
66 assertTrue("".equals(response.getContentAsString()));
68 fail("Not expecting Exception while Exporting BlackListEntries.");
73 public void testImportBlackList() throws Exception{
74 MockHttpServletRequest request = new MockHttpServletRequest();
75 ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries();
76 File file = new File("src/test/resources/BlackList.xls");
77 try(FileInputStream targetStream = new FileInputStream(file)){
78 ExportAndImportDecisionBlackListEntriesTest testController = Mockito.mock(ExportAndImportDecisionBlackListEntriesTest.class);
79 ServletInputStream inputStream = testController.getInputStream(getBytes(targetStream));
80 Mockito.when(request.getInputStream()).thenReturn(inputStream);
81 String boundary = "===" + System.currentTimeMillis() + "===";
82 request.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
83 request.addHeader("name", "BlackList.xls");
84 controller.importBlackListFile(request, response);
85 assertTrue(response.getContentAsString().contains("data"));
87 fail("Not expecting Exception while importing BlackListEntries.");
91 public static byte[] getBytes(InputStream is) throws IOException {
95 ByteArrayOutputStream bos = new ByteArrayOutputStream();
97 while ((len = is.read(buf, 0, size)) != -1)
98 bos.write(buf, 0, len);
99 buf = bos.toByteArray();
103 public ServletInputStream getInputStream(byte[] body) throws IOException {
104 final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
105 ServletInputStream servletInputStream = new ServletInputStream() {
106 public int read() throws IOException {
107 return byteArrayInputStream.read();
111 public boolean isFinished() {
116 public boolean isReady() {
121 public void setReadListener(ReadListener readListener) {
124 return servletInputStream;