package org.onap.policy.controller;
import com.google.gson.Gson;
+
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.springframework.web.bind.annotation.RequestMethod;
-
/**
* This class is used to import and export the black list entries which were used in the Decision Blacklist Guard YAML
* Policy.
List<String> blackLists = adapter.getYamlparams().getBlackList();
HSSFSheet sheet = workBook.createSheet("BlackList");
HSSFRow headingRow = sheet.createRow(0);
- headingRow.createCell(0).setCellValue("Action");
- headingRow.createCell(1).setCellValue("BlackListEntry");
+ headingRow.createCell(0).setCellValue(ACTION);
+ headingRow.createCell(1).setCellValue(BLACKLISTENTRY);
short rowNo = 1;
for (Object object : blackLists) {
String formatedDate = dateFormat.format(date);
String fileName = "BlackList_Scope_" + adapter.getDomainDir() + "_Name_" + adapter.getPolicyName()
- + "_Version_" + root.get("version").toString() + "_Date_" + formatedDate + ".xls";
+ + "_Version_" + root.get("version").toString() + "_Date_" + formatedDate + ".xls";
String deleteCheckPath = tmpFile + File.separator + fileName;
File deleteCheck = new File(deleteCheckPath);
out.write(jsonResposne.toString());
} catch (Exception e) {
policyLogger.error(
- XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Exception Occured while Exporting BlackList Entries" , e);
+ XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Exception Occured while Exporting BlackList Entries", e);
}
}
*
* @param request the HTTP request contains file upload stream form GUI.
* @param response the response is send to the GUI after reading the file input stream.
- * @throws FileUploadException throws fileUpload Exception.
- * @throws IOException throws IO Exceptions.
*/
@RequestMapping(value = {"/policycreation/importBlackListForDecisionPolicy"}, method = {RequestMethod.POST})
- public void importBlackListFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
- List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
- List<String> errorLogs = new ArrayList<>();
- Gson mapper = new Gson();
- errorLogs.add("error");
- Map<String, Object> model = new HashMap<>();
- if (items.isEmpty()) {
- errorLogs.add("The File doesn't have any content and it is invalid.");
- model.put(BLACKLISTENTRIESDATA, errorLogs);
- } else {
- readItems(items, errorLogs, model);
+ public void importBlackListFile(HttpServletRequest request, HttpServletResponse response) {
+ try {
+ List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
+ List<String> errorLogs = new ArrayList<>();
+ Gson mapper = new Gson();
+ errorLogs.add("error");
+ Map<String, Object> model = new HashMap<>();
+ if (items.isEmpty()) {
+ errorLogs.add("The File doesn't have any content and it is invalid.");
+ model.put(BLACKLISTENTRIESDATA, errorLogs);
+ } else {
+ readItems(items, errorLogs, model);
+ }
+ JsonMessage msg = new JsonMessage(mapper.toJson(model));
+ JSONObject jsonResposne = new JSONObject(msg);
+ response.getWriter().write(jsonResposne.toString());
+ } catch (FileUploadException | IOException e) {
+ policyLogger.error("Exception Occured while importing the BlackListEntry", e);
}
- JsonMessage msg = new JsonMessage(mapper.toJson(model));
- JSONObject jsonResposne = new JSONObject(msg);
- response.getWriter().write(jsonResposne.toString());
}
/**
* @param model Map which stores key value (blacklist and append list data)
* @throws Exception throws exception if it is not .xls format
*/
- private void readItems(List<FileItem> items, List<String> errorLogs, Map<String, Object> model) throws Exception {
+ private void readItems(List<FileItem> items, List<String> errorLogs, Map<String, Object> model) throws IOException {
Map<String, InputStream> files = new HashMap<>();
FileItem item = items.get(0);
String error = "Error Occured While Reading File. Please check the format of the file.";
errorLogs.add(error);
model.put(BLACKLISTENTRIESDATA, errorLogs);
- policyLogger.error(error , e);
+ policyLogger.error(error, e);
}
}
ReturnBlackList returnList = readBlackListCell(cell, lineNo, errorLogs);
blEntry = returnList.getEntryValue();
blackListCheck = returnList.isEntryCheck();
- actionEntry = returnList.getActionValue();
}
lineNo++;
}
}
/**
- *
* This method is used to read the BlackList cell entry.
*
* @param cell reading the blacklist entry cell.
public class ExportAndImportDecisionBlackListEntriesTest {
- private HttpServletRequest request;
- private MockHttpServletResponse response;
- String jsonString;
-
- @Before
- public void setUp() throws Exception {
- request = mock(HttpServletRequest.class);
- response = new MockHttpServletResponse();
- }
-
- @Test
- public void testExportBlackList() throws IOException{
- ClassLoader classLoader = getClass().getClassLoader();
- jsonString = IOUtils.toString(classLoader.getResourceAsStream("DecisionPolicyData.txt"));
- try(BufferedReader reader = new BufferedReader(new StringReader(jsonString))){
- Mockito.when(request.getReader()).thenReturn(reader);
- ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries();
- controller.exportBlackList(request, response);
- assertTrue("".equals(response.getContentAsString()));
- }catch(Exception e){
- fail("Not expecting Exception while Exporting BlackListEntries.");
- }
- }
-
- @Test
- public void testImportBlackList() throws Exception{
- MockHttpServletRequest request = new MockHttpServletRequest();
- ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries();
- File file = new File("src/test/resources/BlackList.xls");
- try(FileInputStream targetStream = new FileInputStream(file)){
- ExportAndImportDecisionBlackListEntriesTest testController = Mockito.mock(ExportAndImportDecisionBlackListEntriesTest.class);
- ServletInputStream inputStream = testController.getInputStream(getBytes(targetStream));
- Mockito.when(request.getInputStream()).thenReturn(inputStream);
- String boundary = "===" + System.currentTimeMillis() + "===";
- request.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
- request.addHeader("name", "BlackList.xls");
- controller.importBlackListFile(request, response);
- assertTrue(response.getContentAsString().contains("data"));
- }catch(Exception e){
- fail("Not expecting Exception while importing BlackListEntries.");
- }
- }
-
- public static byte[] getBytes(InputStream is) throws IOException {
- int len;
- int size = 1024;
- byte[] buf;
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- buf = new byte[size];
- while ((len = is.read(buf, 0, size)) != -1)
- bos.write(buf, 0, len);
- buf = bos.toByteArray();
- return buf;
- }
-
- public ServletInputStream getInputStream(byte[] body) throws IOException {
+ private HttpServletRequest request;
+ private MockHttpServletResponse response;
+ String jsonString;
+
+ @Before
+ public void setUp() throws Exception {
+ request = mock(HttpServletRequest.class);
+ response = new MockHttpServletResponse();
+ }
+
+ @Test
+ public void testExportBlackList() throws IOException{
+ ClassLoader classLoader = getClass().getClassLoader();
+ jsonString = IOUtils.toString(classLoader.getResourceAsStream("DecisionPolicyData.txt"));
+ try(BufferedReader reader = new BufferedReader(new StringReader(jsonString))){
+ Mockito.when(request.getReader()).thenReturn(reader);
+ ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries();
+ controller.exportBlackList(request, response);
+ assertTrue("".equals(response.getContentAsString()));
+ }catch(Exception e){
+ fail("Not expecting Exception while Exporting BlackListEntries.");
+ }
+ }
+
+ @Test
+ public void testImportBlackList() throws Exception{
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries();
+ File file = new File("src/test/resources/BlackList.xls");
+ try(FileInputStream targetStream = new FileInputStream(file)){
+ ExportAndImportDecisionBlackListEntriesTest testController = Mockito.mock(ExportAndImportDecisionBlackListEntriesTest.class);
+ ServletInputStream inputStream = testController.getInputStream(getBytes(targetStream));
+ Mockito.when(request.getInputStream()).thenReturn(inputStream);
+ String boundary = "===" + System.currentTimeMillis() + "===";
+ request.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
+ request.addHeader("name", "BlackList.xls");
+ controller.importBlackListFile(request, response);
+ assertTrue(response.getContentAsString().contains("data"));
+ }catch(Exception e){
+ fail("Not expecting Exception while importing BlackListEntries.");
+ }
+ }
+
+ public static byte[] getBytes(InputStream is) throws IOException {
+ int len;
+ int size = 1024;
+ byte[] buf;
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ buf = new byte[size];
+ while ((len = is.read(buf, 0, size)) != -1)
+ bos.write(buf, 0, len);
+ buf = bos.toByteArray();
+ return buf;
+ }
+
+ public ServletInputStream getInputStream(byte[] body) throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
ServletInputStream servletInputStream = new ServletInputStream() {
public int read() throws IOException {
return byteArrayInputStream.read();
}
- @Override
- public boolean isFinished() {
- return false;
- }
+ @Override
+ public boolean isFinished() {
+ return false;
+ }
- @Override
- public boolean isReady() {
- return false;
- }
+ @Override
+ public boolean isReady() {
+ return false;
+ }
- @Override
- public void setReadListener(ReadListener readListener) {
- }
+ @Override
+ public void setReadListener(ReadListener readListener) {
+ }
};
return servletInputStream;
}