Adding code coverage reduce duplicate lines
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / PolicyExportAndImportController.java
index bb6f38b..5365af1 100644 (file)
 package org.onap.policy.controller;
 
 
+import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -54,10 +56,10 @@ import org.onap.policy.rest.jpa.PolicyEditorScopes;
 import org.onap.policy.rest.jpa.PolicyEntity;
 import org.onap.policy.rest.jpa.PolicyVersion;
 import org.onap.policy.rest.jpa.UserInfo;
+import org.onap.policy.utils.UserUtils.Pair;
 import org.onap.policy.xacml.api.XACMLErrorConstants;
-import org.openecomp.policy.model.Roles;
-import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
-import org.openecomp.portalsdk.core.web.support.UserUtils;
+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;
@@ -113,12 +115,14 @@ public class PolicyExportAndImportController extends RestrictedBaseController {
                PolicyExportAndImportController.commonClassDao = commonClassDao;
        }
 
-       public PolicyExportAndImportController(){}
+       public PolicyExportAndImportController(){
+               // Empty constructor
+       }
 
        @RequestMapping(value={"/policy_download/exportPolicy.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
-       public void exportPolicy(HttpServletRequest request, HttpServletResponse response) throws Exception{
+       public void exportPolicy(HttpServletRequest request, HttpServletResponse response) throws IOException{
                try{
-                       String file = null;
+                       String file;
                        selectedPolicy = new ArrayList<>();
                        ObjectMapper mapper = new ObjectMapper();
                        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@@ -192,7 +196,7 @@ public class PolicyExportAndImportController extends RestrictedBaseController {
                        request.setCharacterEncoding("UTF-8");
 
                        PrintWriter out = response.getWriter();
-                       String successMap = file.toString().substring(file.toString().lastIndexOf("webapps")+8);
+                       String successMap = file.substring(file.lastIndexOf("webapps")+8);
                        String responseString = mapper.writeValueAsString(successMap);
                        JSONObject j = new JSONObject("{data: " + responseString + "}");
                        out.write(j.toString());
@@ -202,40 +206,28 @@ public class PolicyExportAndImportController extends RestrictedBaseController {
        }
 
        //Policy Import 
-       public JSONObject importRepositoryFile(String file, HttpServletRequest request) throws Exception{
+       public JSONObject importRepositoryFile(String file, HttpServletRequest request) throws IOException{
                boolean configExists = false;
                boolean actionExists = false;
                String configName = null;
-               String scope = null;
-               boolean finalColumn = false;
+               String scope;
+               boolean finalColumn;
                PolicyController controller = policyController != null ? getPolicyController() : new PolicyController();
                String userId = UserUtils.getUserSession(request).getOrgUserId();
                UserInfo userInfo = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", userId);
 
-               //Check if the Role and Scope Size are Null get the values from db. 
                List<Object> userRoles = controller.getRoles(userId);
-               roles = new ArrayList<>();
-               scopes = new HashSet<>();
-               for(Object role: userRoles){
-                       Roles userRole = (Roles) role;
-                       roles.add(userRole.getRole());
-                       if(userRole.getScope() != null){
-                               if(userRole.getScope().contains(",")){
-                                       String[] multipleScopes = userRole.getScope().split(",");
-                                       for(int i =0; i < multipleScopes.length; i++){
-                                               scopes.add(multipleScopes[i]);
-                                       }
-                               }else{
-                                       scopes.add(userRole.getScope());
-                               }               
-                       }
-               }
+               Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+               roles = pair.u;
+               scopes = pair.t;
+               
                FileInputStream excelFile = new FileInputStream(new File(file));
                workbook = new HSSFWorkbook(excelFile);
                Sheet datatypeSheet = workbook.getSheetAt(0);
                Iterator<Row> rowIterator = datatypeSheet.iterator();
 
                while (rowIterator.hasNext()) {
+                       finalColumn = false;
                        policyEntity = new PolicyEntity();
                        configurationDataEntity = new ConfigurationDataEntity();
                        actionBodyEntity = new ActionBodyEntity();
@@ -247,19 +239,19 @@ public class PolicyExportAndImportController extends RestrictedBaseController {
                        Iterator<Cell> cellIterator = currentRow.cellIterator();
                        while (cellIterator.hasNext()) {
                                Cell cell = cellIterator.next();
-                               if (getCellHeaderName(cell).equalsIgnoreCase("policyName")) {
+                               if ("policyName".equalsIgnoreCase(getCellHeaderName(cell))) {
                                        policyEntity.setPolicyName(cell.getStringCellValue());
                                }
-                               if (getCellHeaderName(cell).equalsIgnoreCase("scope")) {
+                               if ("scope".equalsIgnoreCase(getCellHeaderName(cell))) {
                                        policyEntity.setScope(cell.getStringCellValue());
                                }
-                               if (getCellHeaderName(cell).equalsIgnoreCase("policyData")) {
+                               if ("policyData".equalsIgnoreCase(getCellHeaderName(cell))) {
                                        policyEntity.setPolicyData(cell.getStringCellValue());
                                }
-                               if (getCellHeaderName(cell).equalsIgnoreCase("description")) {
+                               if ("description".equalsIgnoreCase(getCellHeaderName(cell))) {
                                        policyEntity.setDescription(cell.getStringCellValue());
                                }
-                               if (getCellHeaderName(cell).equalsIgnoreCase("configurationbody")) {
+                               if ("configurationbody".equalsIgnoreCase(getCellHeaderName(cell))) {
                                        if(policyEntity.getPolicyName().contains("Config_")){
                                                configExists = true;
                                                configurationDataEntity.setConfigBody(cell.getStringCellValue());
@@ -268,7 +260,7 @@ public class PolicyExportAndImportController extends RestrictedBaseController {
                                                actionBodyEntity.setActionBody(cell.getStringCellValue());
                                        }       
                                }
-                               if (getCellHeaderName(cell).equalsIgnoreCase("configurationName")) {
+                               if ("configurationName".equalsIgnoreCase(getCellHeaderName(cell))) {
                                        finalColumn = true;
                                        configName = cell.getStringCellValue();
                                        if(policyEntity.getPolicyName().contains("Config_")){
@@ -308,11 +300,11 @@ public class PolicyExportAndImportController extends RestrictedBaseController {
                                        }
                                        if (roles.contains(ADMIN) || roles.contains(EDITOR)) {
                                                if(scopes.isEmpty()){
-                                                       //return error("No Scopes has been Assigned to the User. Please, Contact Super-Admin");
+                                                       logger.error("No Scopes has been Assigned to the User. Please, Contact Super-Admin");
                                                }else{
                                                        //1. if Role contains admin, then check if parent scope has role admin, if not don't create a scope and add to list.
                                                        if(roles.contains(ADMIN)){
-                                                               String scopeCheck = scope.substring(0, scope.lastIndexOf("."));
+                                                               String scopeCheck = scope.substring(0, scope.lastIndexOf('.'));
                                                                if(scopes.contains(scopeCheck)){
                                                                        PolicyEditorScopes policyEditorScopeEntity = new PolicyEditorScopes();
                                                                        policyEditorScopeEntity.setScopeName(scope);
@@ -342,12 +334,26 @@ public class PolicyExportAndImportController extends RestrictedBaseController {
                                                configurationDataEntity.setCreatedBy(userId);
                                                configurationDataEntity.setModifiedBy(userId);
                                                commonClassDao.save(configurationDataEntity);
+                                               try(FileWriter fw = new FileWriter(PolicyController.getConfigHome() + File.separator + configName)){
+                                                       BufferedWriter bw = new BufferedWriter(fw);
+                                                       bw.write(configurationDataEntity.getConfigBody());
+                                                       bw.close();
+                                               } catch (IOException e) {
+                                                       logger.error("Exception Occured While cloning the configuration file",e);
+                                               }
                                        }
                                        if(actionExists){
                                                actionBodyEntity.setDeleted(false);
                                                actionBodyEntity.setCreatedBy(userId);
                                                actionBodyEntity.setModifiedBy(userId);
                                                commonClassDao.save(actionBodyEntity);
+                                               try(FileWriter fw = new FileWriter(PolicyController.getActionHome() + File.separator + actionBodyEntity.getActionBodyName())) {
+                                                       BufferedWriter bw = new BufferedWriter(fw);
+                                                       bw.write(actionBodyEntity.getActionBody());
+                                                       bw.close();
+                                               } catch (IOException e) {
+                                                       logger.error("Exception Occured While cloning the configuration file",e);
+                                               }
                                        }
                                        if(configName != null){
                                                if(configName.contains("Config_")){
@@ -365,8 +371,8 @@ public class PolicyExportAndImportController extends RestrictedBaseController {
                                        
                                        policyVersion = new PolicyVersion();
                                        String policyName = policyEntity.getPolicyName().replace(".xml", "");
-                                       int version = Integer.parseInt(policyName.substring(policyName.lastIndexOf(".")+1));
-                                       policyName = policyName.substring(0, policyName.lastIndexOf("."));
+                                       int version = Integer.parseInt(policyName.substring(policyName.lastIndexOf('.')+1));
+                                       policyName = policyName.substring(0, policyName.lastIndexOf('.'));
                                        
                                        policyVersion.setPolicyName(scope.replace(".", File.separator) + File.separator + policyName);
                                        policyVersion.setActiveVersion(version);
@@ -382,7 +388,6 @@ public class PolicyExportAndImportController extends RestrictedBaseController {
 
        //return the column header name value
        private String getCellHeaderName(Cell cell){
-               String cellHeaderName = cell.getSheet().getRow(0).getCell(cell.getColumnIndex()).getRichStringCellValue().toString();
-               return cellHeaderName;
+               return cell.getSheet().getRow(0).getCell(cell.getColumnIndex()).getRichStringCellValue().toString();
        }
 }