Added Junits for POLICY-SDK-APP controllers
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / DashboardController.java
index aedb943..d1483a2 100644 (file)
@@ -45,29 +45,28 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.json.JSONObject;
+import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.common.logging.flexlogger.Logger;
 import org.onap.policy.dao.SystemLogDbDao;
 import org.onap.policy.model.PDPGroupContainer;
 import org.onap.policy.rest.XACMLRestProperties;
 import org.onap.policy.rest.dao.CommonClassDao;
-import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
-import org.openecomp.portalsdk.core.web.support.JsonMessage;
+import org.onap.policy.xacml.api.XACMLErrorConstants;
+import org.onap.policy.xacml.api.pap.OnapPDP;
+import org.onap.policy.xacml.api.pap.OnapPDPGroup;
+import org.onap.portalsdk.core.controller.RestrictedBaseController;
+import org.onap.portalsdk.core.web.support.JsonMessage;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 
-import org.onap.policy.common.logging.flexlogger.FlexLogger;
-import org.onap.policy.common.logging.flexlogger.Logger;
-import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
-import org.onap.policy.xacml.api.XACMLErrorConstants;
-import org.onap.policy.xacml.api.pap.OnapPDP;
-import org.onap.policy.xacml.api.pap.OnapPDPGroup;
-
 import com.att.research.xacml.api.pap.PAPException;
 import com.att.research.xacml.api.pap.PDP;
 import com.att.research.xacml.api.pap.PDPGroup;
 import com.att.research.xacml.api.pap.PDPPolicy;
 import com.att.research.xacml.util.XACMLProperties;
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
 import com.fasterxml.jackson.annotation.PropertyAccessor;
 import com.fasterxml.jackson.databind.ObjectMapper;
 
@@ -75,12 +74,19 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 @RequestMapping({"/"})
 public class DashboardController  extends RestrictedBaseController{
        private static final Logger policyLogger = FlexLogger.getLogger(DashboardController.class);
-       @Autowired
-       SystemLogDbDao systemDAO;
-
        @Autowired
        CommonClassDao commonClassDao;
+       
+       @Autowired
+       SystemLogDbDao systemDAO;
 
+       public void setCommonClassDao(CommonClassDao commonClassDao) {
+               this.commonClassDao = commonClassDao;
+       }
+       public void setSystemLogDbDao(SystemLogDbDao systemDAO){
+               this.systemDAO = systemDAO;
+       }
+        
        private int pdpCount;
        private PDPGroupContainer pdpConatiner;
        private ArrayList<Object> pdpStatusData;
@@ -195,7 +201,6 @@ public class DashboardController  extends RestrictedBaseController{
                        PolicyController controller = getPolicyControllerInstance();
                        Set<OnapPDPGroup> groups = controller.getPapEngine().getOnapPDPGroups();
                        if (groups == null) {
-                               papStatus = "UNKNOWN";
                                throw new PAPException("PAP not running");
                        }else {
                                papStatus = "IS_OK";
@@ -282,14 +287,10 @@ public class DashboardController  extends RestrictedBaseController{
        private long getRequestCounts(String host, int port, String jmxAttribute) {
 
                policyLogger.debug("Create an RMI connector client and connect it to the JMX connector server");
-               HashMap map = new HashMap();
-               map = null;
-               JMXConnector jmxConnection;
-               try {
-                       jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map);
+               HashMap map = null;
+               try (JMXConnector jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map)){
                        jmxConnection.connect();
                        Object o = jmxConnection.getMBeanServerConnection().getAttribute(new ObjectName("PdpRest:type=PdpRestMonitor"), jmxAttribute);
-                       jmxConnection.close();
                        policyLogger.debug("pdpEvaluationNA value retreived: " + o);
                        return (long) o;
                } catch (MalformedURLException e) {
@@ -322,10 +323,10 @@ public class DashboardController  extends RestrictedBaseController{
         */
        private void addPolicyToTable() {
                policyActivityData = new ArrayList<>();
-               String policyID = null;
-               int policyFireCount = 0;
+               String policyID;
+               int policyFireCount;
                Map<String, String> policyMap = new HashMap<>();
-               Object policyList = null;
+               Object policyList;
                //get list of policy
 
                for (PDPGroup group : this.pdpConatiner.getGroups()){
@@ -349,14 +350,12 @@ public class DashboardController  extends RestrictedBaseController{
                                        for (String policyKeyValue : splitPolicy){
                                                policyID = urnPolicyID(policyKeyValue);
                                                policyFireCount = countPolicyID(policyKeyValue);
-                                               if (policyID != null ){
-                                                       if (policyMap.containsKey(policyID)){
-                                                               JSONObject object = new JSONObject();
-                                                               object.put("policyId", policyMap.get(policyID));
-                                                               object.put("fireCount", policyFireCount);
-                                                               object.put("system", pdp.getId());
-                                                               policyActivityData.add(object);
-                                                       }
+                                               if (policyID != null && policyMap.containsKey(policyID)){
+                                                       JSONObject object = new JSONObject();
+                                                       object.put("policyId", policyMap.get(policyID));
+                                                       object.put("fireCount", policyFireCount);
+                                                       object.put("system", pdp.getId());
+                                                       policyActivityData.add(object);
                                                }
                                        }
                                }else {
@@ -384,14 +383,10 @@ public class DashboardController  extends RestrictedBaseController{
        @SuppressWarnings({ "rawtypes", "unchecked" })
        private Object getPolicy(String host, int port, String jmxAttribute){
                policyLogger.debug("Create an RMI connector client and connect it to the JMX connector server for Policy: " + host);
-               HashMap map = new HashMap();
-               map = null;
-               JMXConnector jmxConnection;
-               try {
-                       jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map);
+               HashMap map = null;
+               try (JMXConnector jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map)) {
                        jmxConnection.connect();
                        Object o = jmxConnection.getMBeanServerConnection().getAttribute(new ObjectName("PdpRest:type=PdpRestMonitor"), "policyMap");
-                       jmxConnection.close();
                        policyLogger.debug("policyMap value retreived: " + o);
                        return  o;
                } catch (MalformedURLException e) {
@@ -416,16 +411,15 @@ public class DashboardController  extends RestrictedBaseController{
        }
 
        private static String urnPolicyID(String line){
-               String[]  splitLine = line.toString().split("=");
+               String[]  splitLine = line.split("=");
                String removeSpaces = splitLine[0].replaceAll("\\s+", "");
                return removeSpaces.replace("{", "");
        }
 
        private static Integer countPolicyID(String line){
-               String[]  splitLine = line.toString().split("=");
+               String[]  splitLine = line.split("=");
                String sCount = splitLine[1].replace("}", "");
-               int intCount = Integer.parseInt(sCount);
-               return intCount;
+               return Integer.parseInt(sCount);
        }
 
 }