Added Junits for ONAP-PAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / ActionPolicyDictionaryController.java
index 85890fc..02b0707 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP-PAP-REST
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 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.
@@ -22,8 +22,6 @@ package org.onap.policy.pap.xacml.rest.controller;
 
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.io.UnsupportedEncodingException;
-import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -40,6 +38,7 @@ import org.onap.policy.pap.xacml.rest.util.JsonMessage;
 import org.onap.policy.rest.dao.CommonClassDao;
 import org.onap.policy.rest.jpa.ActionPolicyDict;
 import org.onap.policy.rest.jpa.UserInfo;
+import org.onap.policy.utils.PolicyUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
@@ -56,25 +55,33 @@ public class ActionPolicyDictionaryController {
        private static final Logger LOGGER  = FlexLogger.getLogger(ActionPolicyDictionaryController.class);
 
        private static CommonClassDao commonClassDao;
-       
+       private static String utf8 = "UTF-8";
+       private static String attributeName = "attributeName";
        @Autowired
        public ActionPolicyDictionaryController(CommonClassDao commonClassDao){
                ActionPolicyDictionaryController.commonClassDao = commonClassDao;
        }
        
-       public ActionPolicyDictionaryController(){}
+       public void setCommonClassDao(CommonClassDao commonClassDao){
+               ActionPolicyDictionaryController.commonClassDao = commonClassDao;
+       }
+       /*
+        * This is an empty constructor
+        */     
+       public ActionPolicyDictionaryController(){
+               
+       }
 
        public UserInfo getUserInfo(String loginId){
-               UserInfo name = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
-               return name;    
+               return (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
        }
 
        @RequestMapping(value={"/get_ActionPolicyDictDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
-       public void getActionEntitybyName(HttpServletRequest request, HttpServletResponse response){
+       public void getActionEntitybyName(HttpServletResponse response){
                try{
                        Map<String, Object> model = new HashMap<>();
                        ObjectMapper mapper = new ObjectMapper();
-                       model.put("actionPolicyDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(ActionPolicyDict.class, "attributeName")));
+                       model.put("actionPolicyDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(ActionPolicyDict.class, attributeName)));
                        JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
                        JSONObject j = new JSONObject(msg);
                        response.getWriter().write(j.toString());
@@ -85,7 +92,7 @@ public class ActionPolicyDictionaryController {
        }
 
        @RequestMapping(value={"/get_ActionPolicyDictData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
-       public void getActionPolicyDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
+       public void getActionPolicyDictionaryEntityData(HttpServletResponse response){
                try{
                        Map<String, Object> model = new HashMap<>();
                        ObjectMapper mapper = new ObjectMapper();
@@ -104,13 +111,13 @@ public class ActionPolicyDictionaryController {
        }
 
        @RequestMapping(value={"/action_dictionary/save_ActionDict"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
-       public ModelAndView saveActionPolicyDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException   {
+       public ModelAndView saveActionPolicyDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException   {
                try {
                        boolean duplicateflag = false;
                        boolean isFakeUpdate = false;
                        boolean fromAPI = false;
 
-                       if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
+                       if (request.getParameter("apiflag")!=null && ("api").equalsIgnoreCase(request.getParameter("apiflag"))) {
                                fromAPI = true;
                        }
                        ObjectMapper mapper = new ObjectMapper();
@@ -121,16 +128,15 @@ public class ActionPolicyDictionaryController {
                        String userId = null;
 
                        if(fromAPI) {
-                               actionPolicyDict = (ActionPolicyDict)mapper.readValue(root.get("dictionaryFields").toString(), ActionPolicyDict.class);
-                               adapter = (ActionAdapter)mapper.readValue(root.get("dictionaryFields").toString(), ActionAdapter.class);
+                               actionPolicyDict = mapper.readValue(root.get("dictionaryFields").toString(), ActionPolicyDict.class);
+                               adapter = mapper.readValue(root.get("dictionaryFields").toString(), ActionAdapter.class);
                                userId = "API";
 
                                //check if update operation or create, get id for data to be updated and update attributeData
-                               if (request.getParameter("operation").equals("update")) {
-                                       List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(actionPolicyDict.getAttributeName(), "attributeName", ActionPolicyDict.class);
-                                       int id = 0;
+                               if (("update").equals(request.getParameter("operation"))) {
+                                       List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(actionPolicyDict.getAttributeName(), attributeName, ActionPolicyDict.class);
                                        ActionPolicyDict data = (ActionPolicyDict) duplicateData.get(0);
-                                       id = data.getId();
+                                       int id = data.getId();
                                        if(id==0){
                                                isFakeUpdate=true;
                                                actionPolicyDict.setId(1);
@@ -140,29 +146,28 @@ public class ActionPolicyDictionaryController {
                                        actionPolicyDict.setUserCreatedBy(this.getUserInfo(userId));
                                }
                        } else {
-                               actionPolicyDict = (ActionPolicyDict)mapper.readValue(root.get("actionPolicyDictionaryData").toString(), ActionPolicyDict.class);
+                               actionPolicyDict = mapper.readValue(root.get("actionPolicyDictionaryData").toString(), ActionPolicyDict.class);
                                adapter = mapper.readValue(root.get("actionPolicyDictionaryData").toString(), ActionAdapter.class);
                                userId = root.get("userid").textValue();
                        }
-                       String header = "";
+                       StringBuilder header = new StringBuilder();
                        int counter = 0;
-                       if(adapter.getHeaders().size() > 0){
+                       if(!adapter.getHeaders().isEmpty()){
                                for(Object attribute : adapter.getHeaders()){
                                        if(attribute instanceof LinkedHashMap<?, ?>){
                                                String key = ((LinkedHashMap<?, ?>) attribute).get("option").toString();
                                                String value = ((LinkedHashMap<?, ?>) attribute).get("number").toString();
                                                if(counter>0){
-                                                       header = header + ":";
+                                                       header.append(":");
                                                }
-                                               header = header + key + "=";
-                                               header = header + value;
+                                               header.append(key).append("=").append(value);
                                                counter ++;
                                        }
                                }
                        }
-                       actionPolicyDict.setHeader(header);
+                       actionPolicyDict.setHeader(header.toString());
                        if(actionPolicyDict.getId() == 0){
-                               List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(actionPolicyDict.getAttributeName(), "attributeName", ActionPolicyDict.class);
+                               List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(actionPolicyDict.getAttributeName(), attributeName, ActionPolicyDict.class);
                                if(!duplicateData.isEmpty()){
                                        duplicateflag = true;
                                }else{
@@ -186,7 +191,7 @@ public class ActionPolicyDictionaryController {
                        }
 
                        if (fromAPI) {
-                               if (responseString!=null && !responseString.equals("Duplicate")) {
+                               if (responseString!=null && !("Duplicate").equals(responseString)) {
                                        if(isFakeUpdate) {
                                                responseString = "Exists";
                                        } else {
@@ -198,9 +203,9 @@ public class ActionPolicyDictionaryController {
                                result.setViewName(responseString);
                                return result;
                        } else {
-                               response.setCharacterEncoding("UTF-8");
+                               response.setCharacterEncoding(utf8);
                                response.setContentType("application / json");
-                               request.setCharacterEncoding("UTF-8"); 
+                               request.setCharacterEncoding(utf8); 
 
                                PrintWriter out = response.getWriter();
                                JSONObject j = new JSONObject("{actionPolicyDictionaryDatas: " + responseString + "}");
@@ -210,54 +215,51 @@ public class ActionPolicyDictionaryController {
                        }
                }
                catch (Exception e){
-                       LOGGER.error(e.getMessage(),e);
-                       response.setCharacterEncoding("UTF-8");
-                       request.setCharacterEncoding("UTF-8");
+                       LOGGER.error(e);
+                       response.setCharacterEncoding(utf8);
+                       request.setCharacterEncoding(utf8);
                        PrintWriter out = response.getWriter();
-                       out.write(e.getMessage());
+                       out.write(PolicyUtils.CATCH_EXCEPTION);
                }
                return null;
        }
 
        @RequestMapping(value={"/action_dictionary/remove_actionPolicyDict"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
-       public ModelAndView removeActionPolicyDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
+       public void removeActionPolicyDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
                try{
                        ObjectMapper mapper = new ObjectMapper();
                        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                        JsonNode root = mapper.readTree(request.getReader());
-                       ActionPolicyDict actionPolicyDict = (ActionPolicyDict)mapper.readValue(root.get("data").toString(), ActionPolicyDict.class);
+                       ActionPolicyDict actionPolicyDict = mapper.readValue(root.get("data").toString(), ActionPolicyDict.class);
                        commonClassDao.delete(actionPolicyDict);
-                       response.setCharacterEncoding("UTF-8");
+                       response.setCharacterEncoding(utf8);
                        response.setContentType("application / json");
-                       request.setCharacterEncoding("UTF-8");
+                       request.setCharacterEncoding(utf8);
 
                        PrintWriter out = response.getWriter();
 
                        String responseString = mapper.writeValueAsString(ActionPolicyDictionaryController.commonClassDao.getData(ActionPolicyDict.class));
                        JSONObject j = new JSONObject("{actionPolicyDictionaryDatas: " + responseString + "}");
                        out.write(j.toString());
-
-                       return null;
                }
                catch (Exception e){
-                       LOGGER.error(e.getMessage(),e);
-                       response.setCharacterEncoding("UTF-8");
-                       request.setCharacterEncoding("UTF-8");
+                       LOGGER.error(e);
+                       response.setCharacterEncoding(utf8);
+                       request.setCharacterEncoding(utf8);
                        PrintWriter out = response.getWriter();
                        out.write(e.getMessage());
                }
-               return null;
        }
 }
 
 class ActionAdapter{
-       private ArrayList<Object> headers;
+       private List<Object> headers;
 
-       public ArrayList<Object> getHeaders() {
+       public List<Object> getHeaders() {
                return headers;
        }
 
-       public void setHeaders(ArrayList<Object> headers) {
+       public void setHeaders(List<Object> headers) {
                this.headers = headers;
        }
 }