From 39db5ecce3151ad42bc0c5433d14b2a81887a4ef Mon Sep 17 00:00:00 2001 From: uj426b Date: Thu, 7 May 2020 10:32:26 -0400 Subject: Fix Policy import/export on gui Issue-ID: POLICY-2545 Change-Id: I25fe8c415a0c9a9b15c97feb93c2ce3e9c975ed2 Signed-off-by: uj426b --- .../onap/policy/admin/PolicyManagerServlet.java | 30 +++++---- .../PolicyExportAndImportController.java | 73 ++++++++++++---------- .../Editor/js/controllers/policyManager.js | 3 +- .../policy/admin/PolicyManagerServletTest.java | 8 +-- .../PolicyExportAndImportControllerTest.java | 11 +--- 5 files changed, 67 insertions(+), 58 deletions(-) diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java index d7d6e7829..a23a0e821 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java @@ -25,7 +25,6 @@ package org.onap.policy.admin; import com.att.research.xacml.util.XACMLProperties; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; - import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; @@ -49,7 +48,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; - import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonReader; @@ -61,11 +59,11 @@ import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.elasticsearch.common.Strings; import org.json.JSONArray; @@ -266,20 +264,24 @@ public class PolicyManagerServlet extends HttpServlet { private void uploadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException { try { Map files = new HashMap<>(); - + String resp = null; List items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { // Process form file field (input type="file"). files.put(item.getName(), item.getInputStream()); - processFormFile(request, item); + resp = processFormFile(request, item, response); } } - JSONObject responseJsonObject; - responseJsonObject = this.success(); response.setContentType(CONTENTTYPE); PrintWriter out = response.getWriter(); + JSONObject responseJsonObject; + if (!StringUtils.isBlank(resp)) { + responseJsonObject = this.error("Import Issue " + resp); + } else { + responseJsonObject = this.success(); + } out.print(responseJsonObject); out.flush(); } catch (Exception e) { @@ -288,23 +290,27 @@ public class PolicyManagerServlet extends HttpServlet { } } - private void processFormFile(HttpServletRequest request, FileItem item) { + private String processFormFile(HttpServletRequest request, FileItem item, HttpServletResponse response) { String newFile; + String outPutResp = null; if (item.getName().endsWith(".xls") && item.getSize() <= getFileSizeLimit()) { File file = new File(item.getName()); try (OutputStream outputStream = new FileOutputStream(file)) { - copyStream(item.getInputStream(), outputStream); + IOUtils.copy(item.getInputStream(), outputStream); newFile = file.toString(); PolicyExportAndImportController importController = new PolicyExportAndImportController(); - importController.importRepositoryFile(newFile, request); + return importController.importRepositoryFile(newFile, request); } catch (Exception e) { LOGGER.error("Upload error : " + e); } } else if (!item.getName().endsWith(".xls")) { - LOGGER.error("Non .xls filetype uploaded: " + item.getName()); + outPutResp = "Non .xls filetype uploaded: " + item.getName(); + LOGGER.error(outPutResp); } else { // uploaded file size is greater than allowed - LOGGER.error("Upload file size limit exceeded! File size (Bytes) is: " + item.getSize()); + outPutResp = "Upload file size limit exceeded! File size (Bytes) is: " + item.getSize(); + LOGGER.error(outPutResp); } + return outPutResp; } protected long copyStream(InputStream inputStream, OutputStream outputStream) throws IOException { diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyExportAndImportController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyExportAndImportController.java index 651ee7e3a..cf7e62b46 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyExportAndImportController.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyExportAndImportController.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ package org.onap.policy.controller; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; - import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; @@ -36,11 +35,10 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Set; - import javax.script.SimpleBindings; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - +import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -69,6 +67,7 @@ import org.onap.policy.utils.UserUtils.Pair; import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.portalsdk.core.controller.RestrictedBaseController; import org.onap.portalsdk.core.web.support.UserUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @@ -116,6 +115,11 @@ public class PolicyExportAndImportController extends RestrictedBaseController { // Empty constructor } + @Autowired + private PolicyExportAndImportController(CommonClassDao commonClassDao) { + PolicyExportAndImportController.commonClassDao = commonClassDao; + } + /** * This is for downloading existing policy. * @@ -257,7 +261,7 @@ public class PolicyExportAndImportController extends RestrictedBaseController { * @return JSONObject * @throws IOException error out */ - public JSONObject importRepositoryFile(String file, HttpServletRequest request) throws IOException { + public String importRepositoryFile(String file, HttpServletRequest request) throws IOException { boolean configExists = false; boolean actionExists = false; String configName = null; @@ -276,7 +280,7 @@ public class PolicyExportAndImportController extends RestrictedBaseController { HSSFWorkbook workbook = new HSSFWorkbook(excelFile)) { Sheet datatypeSheet = workbook.getSheetAt(0); Iterator rowIterator = datatypeSheet.iterator(); - + String sendResult = null; while (rowIterator.hasNext()) { finalColumn = false; PolicyEntity policyEntity = new PolicyEntity(); @@ -291,6 +295,7 @@ public class PolicyExportAndImportController extends RestrictedBaseController { int bodySize = 0; int setBodySize = 0; boolean configurationBodySet = false; + boolean errorFlag = false; while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); if (policyName.equalsIgnoreCase(getCellHeaderName(cell))) { @@ -314,13 +319,18 @@ public class PolicyExportAndImportController extends RestrictedBaseController { configurationDataEntity = populateConfigurationDataEntity(policyEntity, configurationDataEntity, cell); actionBodyEntity = populateActionBodyObject(policyEntity, actionBodyEntity, cell); - JSONObject response = validatRequiredValue(policyEntity, body, finalColumn, configurationBodySet); - if (response != null) { - return response; + String response = null; + response = validatRequiredValue(policyEntity, body, finalColumn, configurationBodySet); + if (!StringUtils.isBlank(response)) { + sendResult = sendResult + "\n" + response; + errorFlag = true; + } + if (!StringUtils.isBlank(response) && !rowIterator.hasNext()) { + return sendResult; } savePolicyEntiies(finalColumn, configurationBodySet, configurationDataEntity, policyEntity, controller, roles, userInfo, scopes, configName, userId, configExists, actionExists, - actionBodyEntity, body); + actionBodyEntity, body, errorFlag); } } @@ -365,43 +375,40 @@ public class PolicyExportAndImportController extends RestrictedBaseController { * @return String */ public String validatMatchRequiredFields(String policyName, String jsonString) { - + String errorMsg = ""; try { JSONObject jsonObject = new JSONObject(jsonString); - String configName = jsonObject.getString("configName"); - String uuid = jsonObject.getString("uuid"); - String erorMsg = validConfigName(configName); - if (erorMsg != null) { - return erorMsg; + String confErorMsg = validConfigName(jsonObject.getString("configName")); + if (!confErorMsg.isEmpty()) { + errorMsg = errorMsg + "\n POLICY :" + policyName + " at " + confErorMsg; } - erorMsg = validUuid(uuid); - if (erorMsg != null) { - return erorMsg; + String uuidErorMsg = validUuid(jsonObject.getString("uuid")); + if (!uuidErorMsg.isEmpty()) { + errorMsg = errorMsg + "\n POLICY :" + policyName + " at " + uuidErorMsg; } - String location = jsonObject.getString("location"); - erorMsg = validLocation(location); - if (erorMsg != null) { - return erorMsg; + String locErorMsg = validLocation(jsonObject.getString("location")); + if (!locErorMsg.isEmpty()) { + errorMsg = errorMsg + "\n POLICY :" + policyName + " at " + locErorMsg; } - String policyScope = jsonObject.getString("policyScope"); - erorMsg = validPolicyScope(policyScope); - if (erorMsg != null) { - return erorMsg; + String pScopeErorMsg = validPolicyScope(jsonObject.getString("policyScope")); + if (!pScopeErorMsg.isEmpty()) { + errorMsg = errorMsg + "\n POLICY :" + policyName + " at " + pScopeErorMsg; } String msVersion = jsonObject.getString("version"); String msService = jsonObject.getString("service"); if (!isAttributeObjectFound(msService, msVersion)) { - return POLICY + policyName + " MS Service: " + msService + " and MS Version: " + msVersion + NOTVALID; + errorMsg = errorMsg + "\n POLICY :" + policyName + " at MS Service: " + msService + " and MS Version: " + + msVersion + NOTVALID; } } catch (Exception e) { logger.error("Exception Occured While validating required fields", e); } - return null; + return errorMsg; } - private JSONObject validatRequiredValue(PolicyEntity policyEntity, StringBuilder body, boolean finalColumn, + private String validatRequiredValue(PolicyEntity policyEntity, StringBuilder body, boolean finalColumn, boolean configurationBodySet) { if (finalColumn && configurationBodySet && (policyEntity.getPolicyName().contains(CONFIG_MS))) { String errorMsg = validatMatchRequiredFields(policyEntity.getPolicyName(), body.toString()); @@ -409,7 +416,7 @@ public class PolicyExportAndImportController extends RestrictedBaseController { logger.error("errorMsg => " + errorMsg); JSONObject response = new JSONObject(); response.append("error", errorMsg); - return response; + return errorMsg; } } return null; @@ -751,9 +758,9 @@ public class PolicyExportAndImportController extends RestrictedBaseController { private void savePolicyEntiies(boolean finalColumn, boolean configurationBodySet, ConfigurationDataEntity configurationDataEntity, PolicyEntity policyEntity, PolicyController controller, List roles, UserInfo userInfo, Set scopes, String configName, String userId, - boolean configExists, boolean actionExists, ActionBodyEntity actionBodyEntity, StringBuilder body) { + boolean configExists, boolean actionExists, ActionBodyEntity actionBodyEntity, StringBuilder body, boolean errorFlagSent) { - if (finalColumn && configurationBodySet) { + if (finalColumn && configurationBodySet && !errorFlagSent) { configurationDataEntity.setConfigBody(body.toString()); String scope = policyEntity.getScope().replace(".", File.separator); String query = "FROM PolicyEntity where policyName = :policyName and scope = :policyScope"; diff --git a/POLICY-SDK-APP/src/main/webapp/app/policyApp/policy-models/Editor/js/controllers/policyManager.js b/POLICY-SDK-APP/src/main/webapp/app/policyApp/policy-models/Editor/js/controllers/policyManager.js index c4ec75ce1..0cf655398 100644 --- a/POLICY-SDK-APP/src/main/webapp/app/policyApp/policy-models/Editor/js/controllers/policyManager.js +++ b/POLICY-SDK-APP/src/main/webapp/app/policyApp/policy-models/Editor/js/controllers/policyManager.js @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine * ================================================================================ - * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017, 2019-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -301,6 +301,7 @@ app.controller('PolicyManagerController', [ $scope.uploadFiles = function() { $scope.policyUploader.upload($scope.uploadFileList, $scope.policyNavigator.currentPath).then(function() { $scope.policyNavigator.refresh(); + Notification.success('Policy Import Complete'); $scope.modal('uploadfile', true); }, function(data) { var errorMsg = data.result && data.result.error || 'Error Occured while Uploading....'; diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java index 50b806a37..e349d53ac 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java @@ -27,7 +27,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; - import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -855,6 +854,7 @@ public class PolicyManagerServletTest extends Mockito { } }; HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class); + HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class); FileItem mockFileItem = Mockito.mock(FileItem.class); InputStream mockInputStream = Mockito.mock(InputStream.class); @@ -862,18 +862,18 @@ public class PolicyManagerServletTest extends Mockito { when(mockFileItem.getInputStream()).thenReturn(mockInputStream); when(mockFileItem.getSize()).thenReturn(fileSizeLimit + 1); - Whitebox.invokeMethod(servlet, "processFormFile", mockRequest, mockFileItem); + Whitebox.invokeMethod(servlet, "processFormFile", mockRequest, mockFileItem, mockResp); verify(mockFileItem, atLeast(1)).getName(); verify(mockFileItem, atLeast(1)).getSize(); when(mockFileItem.getName()).thenReturn("testFileName.txt"); - Whitebox.invokeMethod(servlet, "processFormFile", mockRequest, mockFileItem); + Whitebox.invokeMethod(servlet, "processFormFile", mockRequest, mockFileItem, mockResp); verify(mockFileItem, atLeast(1)).getName(); when(mockFileItem.getSize()).thenReturn(fileSizeLimit); when(mockFileItem.getName()).thenReturn("testFileName.xls"); when(mockFileItem.getInputStream()).thenThrow(IOException.class); - Whitebox.invokeMethod(servlet, "processFormFile", mockRequest, mockFileItem); + Whitebox.invokeMethod(servlet, "processFormFile", mockRequest, mockFileItem, mockResp); verify(mockFileItem, atLeast(1)).getName(); verify(mockFileItem, atLeast(1)).getInputStream(); verify(mockFileItem, atLeast(1)).getSize(); diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyExportAndImportControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyExportAndImportControllerTest.java index 4b348cf2b..37145655c 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyExportAndImportControllerTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/PolicyExportAndImportControllerTest.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -26,18 +26,13 @@ import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; - import com.mockrunner.mock.web.MockHttpServletRequest; import com.mockrunner.mock.web.MockHttpServletResponse; - import java.io.File; import java.io.IOException; import java.util.Collections; - import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - -import org.json.JSONObject; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -106,7 +101,7 @@ public class PolicyExportAndImportControllerTest { // Test negative case String file = new File(classLoader.getResource("Config_BRMS_Raw_TestBRMSRawPolicy.1.xml").getFile()) .getAbsolutePath(); - JSONObject json = controller.importRepositoryFile(file, request); + String json = controller.importRepositoryFile(file, request); assertNull(json); // Another negative case -- cgit 1.2.3-korg