Resolved the Sonar issues
[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 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 package org.onap.policy.controller;
21
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.mock;
25
26 import java.io.BufferedReader;
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.StringReader;
34
35 import javax.servlet.ReadListener;
36 import javax.servlet.ServletInputStream;
37 import javax.servlet.http.HttpServletRequest;
38
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;
45
46 public class ExportAndImportDecisionBlackListEntriesTest {
47
48     private HttpServletRequest request;
49     private MockHttpServletResponse response;
50     String jsonString;
51     
52     @Before
53     public void setUp() throws Exception {
54         request = mock(HttpServletRequest.class);       
55         response =  new MockHttpServletResponse();
56     }
57     
58     @Test
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()));
67         }catch(Exception e){
68             fail("Not expecting Exception while Exporting BlackListEntries.");
69         }
70     }
71     
72     @Test
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"));
86         }catch(Exception e){
87             fail("Not expecting Exception while importing BlackListEntries.");
88         }
89     }
90     
91     public static byte[] getBytes(InputStream is) throws IOException {
92         int len;
93         int size = 1024;
94         byte[] buf;
95         ByteArrayOutputStream bos = new ByteArrayOutputStream();
96         buf = new byte[size];
97         while ((len = is.read(buf, 0, size)) != -1)
98             bos.write(buf, 0, len);
99         buf = bos.toByteArray();
100         return buf;
101     }
102     
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(); 
108             }
109
110             @Override
111             public boolean isFinished() {
112                 return false;
113             }
114
115             @Override
116             public boolean isReady() {
117                 return false;
118             }
119
120             @Override
121             public void setReadListener(ReadListener readListener) {
122             } 
123         }; 
124         return servletInputStream; 
125     } 
126 }