Format java POLICY-SDK-APP
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / ExportAndImportDecisionBlackListEntriesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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.controller;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.mock;
26
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.StringReader;
30
31 import javax.servlet.http.HttpServletRequest;
32
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;
42
43 public class ExportAndImportDecisionBlackListEntriesTest {
44
45     private HttpServletRequest request;
46     private MockHttpServletResponse response;
47     String jsonString;
48
49     @Before
50     public void setUp() throws Exception {
51         request = mock(HttpServletRequest.class);
52         response = new MockHttpServletResponse();
53     }
54
55     @Test
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.");
66         }
67     }
68
69     @Test
70     public void testImportBlackList() throws Exception {
71         byte[] fileContent = FileCopyUtils
72                 .copyToByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream("BlackList.xls"));
73
74         MockMultipartFile file = new MockMultipartFile("BlackList.xls",
75                 Thread.currentThread().getContextClassLoader().getResourceAsStream("BlackList.xls"));
76
77         MockMultipartHttpServletRequest req = new MockMultipartHttpServletRequest();
78         req.setMethod("POST");
79
80         String boundary = "JPnJUN6FOo0qLySf-__r_RY1nQE7QOXXJ_nLK1s";
81         req.setContentType("multipart/form-data; boundary=" + boundary);
82
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";
85
86         String end = "\r\n--" + boundary + "--";
87         req.setContent(ArrayUtils.addAll(start.getBytes(), ArrayUtils.addAll(fileContent, end.getBytes())));
88         req.addHeader("name", "BlackList.xls");
89         req.addFile(file);
90         ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries();
91         MockHttpServletResponse resp = new MockHttpServletResponse();
92         controller.importBlackListFile(req, resp);
93         assertTrue(resp.getContentAsString().contains("data"));
94
95     }
96 }