WrapperEncryptionTool sonar fixes 41/31441/5
authorJakub Dudycz <jakub.dudycz@nokia.com>
Mon, 12 Feb 2018 15:21:04 +0000 (16:21 +0100)
committerPatrick Brady <pb071s@att.com>
Mon, 12 Feb 2018 21:27:33 +0000 (21:27 +0000)
Change-Id: I975902bd2bea4cac6b0ae5ac38ad226bb99c6b58
Issue-ID: APPC-614
Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
appc-config/appc-encryption-tool/provider/src/main/java/org/onap/appc/encryptiontool/wrapper/WrapperEncryptionTool.java
appc-config/appc-encryption-tool/provider/src/test/java/org/onap/appc/encryptiontool/TestEncryptionTool.java

index 78acd89..2fc2df6 100644 (file)
@@ -26,16 +26,23 @@ package org.onap.appc.encryptiontool.wrapper;
 
 import java.util.Iterator;
 import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.apache.commons.lang.StringUtils;
 
 public class WrapperEncryptionTool {
 
     private static final Logger log = LoggerFactory.getLogger(WrapperEncryptionTool.class);
+    private static final String USER_PARAM = "user";
+    private static final String PASS_PARAM = "password";
+    private static final String URL_PARAM = "url";
+    private static final String PORT_PARAM = "port";
+
+    private WrapperEncryptionTool() {
+    }
 
     public static void main(String[] args) {
-        String vnf_type = args[0];
+        String vnfType = args[0];
         String protocol = args[1];
         String user = args[2];
         String password = args[3];
@@ -45,13 +52,13 @@ public class WrapperEncryptionTool {
 
         if (StringUtils.isNotBlank(user)) {
             log.info("ERROR-USER can not be null");
-        return;
+            return;
         }
         if (StringUtils.isNotBlank(password)) {
             log.info("ERROR-PASSWORD can not be null");
             return;
         }
-        if (StringUtils.isNotBlank(protocol) || StringUtils.isNotBlank(vnf_type) || StringUtils.isNotBlank(action)) {
+        if (StringUtils.isNotBlank(protocol) || StringUtils.isNotBlank(vnfType) || StringUtils.isNotBlank(action)) {
             log.info("ERROR-PROTOCOL ,Action and VNF-TYPE both can not be null");
             return;
         }
@@ -59,42 +66,29 @@ public class WrapperEncryptionTool {
         EncryptionTool et = EncryptionTool.getInstance();
         String enPass = et.encrypt(password);
 
-        if ((protocol != null && !protocol.isEmpty())) {
-            updateProperties(user, vnf_type, enPass, action, port, url, protocol);
-            return;
+        if (StringUtils.isNotBlank(protocol)) {
+            updateProperties(user, vnfType, enPass, action, port, url, protocol);
         }
-
     }
 
-    public static void updateProperties(String user, String vnf_type, String password, String action, String port,
-            String url, String protocol) {
+    public static void updateProperties(String user, String vnfType, String password, String action, String port,
+        String url, String protocol) {
         try {
             log.info("Received Inputs protocol:%s User:%s vnfType:%s action:%surl:%s port:%s ", protocol, user,
-                    vnf_type, action, url, port);
+                vnfType, action, url, port);
             String property = protocol;
-            if (!StringUtils.isNotBlank(vnf_type)) {
-
-                if (!StringUtils.isNotBlank(protocol)) {
-                    if (!StringUtils.isNotBlank(action)) {
-
-                        property = vnf_type + "." + protocol + "." + action;
-
-                    }
-
-                } else {
-                    property = vnf_type;
-                }
-
-            } else {
-
-                if (!StringUtils.isNotBlank(protocol)) {
-                    property = protocol;
-
+            if (StringUtils.isBlank(vnfType)) {
+                if (StringUtils.isBlank(protocol) && StringUtils.isBlank(action)) {
+                    property = vnfType + "." + protocol + "." + action;
+                } else if (StringUtils.isNotBlank(protocol)){
+                    property = vnfType;
                 }
+            } else if (StringUtils.isBlank(protocol)){
+                property = protocol;
             }
 
             PropertiesConfiguration conf = new PropertiesConfiguration(
-                    System.getenv("APPC_CONFIG_DIR")+"/appc_southbound.properties");
+                System.getenv("APPC_CONFIG_DIR") + "/appc_southbound.properties");
 
             if (conf.subset(property) != null) {
 
@@ -104,73 +98,68 @@ public class WrapperEncryptionTool {
                         String key = it.next();
                         log.info("key---value pairs");
                         log.info(property + "." + key + "------" + conf.getProperty(property + "." + key));
-                        if ((property + "." + key).contains("user")) {
-                            if (user != null && !user.isEmpty())
-                            conf.setProperty(property + "." + key, user);
-                        }
-
-                        if ((property + "." + key).contains("password")) {
-                            if (password != null && !password.isEmpty())
-                                conf.setProperty(property + "." + key, password);
-                        }
-
-                        if ((property + "." + key).contains("port")) {
-                            if (port != null && !port.isEmpty())
-                                conf.setProperty(property + "." + key, port);
-                        }
-                        if ((property + "." + key).contains("url")) {
-                            if (url != null && !url.isEmpty())
-                                conf.setProperty(property + "." + key, url);
-                        }
-
+                        resolveProperty(user, password, port, url, property, conf, key);
                     }
                 } else {
-                    if (conf.containsKey(property + "." + "user")) {
-                       if (user != null && !user.isEmpty())
-                        conf.setProperty(property + "." + "user", user);
-                    } else {
-                        conf.addProperty(property + "." + "user", user);
-                    }
-
-
-                    if (conf.containsKey(property + "." + "password")) {
-                        if (password != null && !password.isEmpty())
-                            conf.setProperty(property + "." + "password", password);
-                    } else {
-                        conf.addProperty(property + "." + "password", password);
-                    }
-
-
-                    if (conf.containsKey(property + "." + "port")) {
-                        if (port != null && !port.isEmpty())
-                            conf.setProperty(property + "." + "port", port);
-                    } else {
-                        if (port != null && !port.isEmpty())
-                            conf.addProperty(property + "." + "port", port);
-                    }
-
-                    if (conf.containsKey(property + "." + "url")) {
-                        if (url != null && !url.isEmpty())
-                            conf.setProperty(property + "." + "url", url);
-
-                    } else {
-
-                        conf.addProperty(property + "." + "url", url);
-                    }
+                    resolvePropertyAction(user, password, port, url, property, conf);
                 }
-
             }
             conf.save();
-
         } catch (Exception e) {
             log.debug("Caught Exception", e);
             log.info("Caught exception", e);
             log.info("APPC-MESSAGE:" + e.getMessage());
 
+        } finally {
+            System.exit(0);
+        }
+    }
+
+    private static void resolvePropertyAction(String user, String password, String port, String url, String property,
+        PropertiesConfiguration conf) {
+        if (containsParam(user, property, conf, USER_PARAM)) {
+            conf.setProperty(property + "." + USER_PARAM, user);
+        } else {
+            conf.addProperty(property + "." + USER_PARAM, user);
         }
-         finally{
-    System.exit(0);
+        if (containsParam(user, property, conf, PASS_PARAM)) {
+            conf.setProperty(property + "." + PASS_PARAM, password);
+        } else {
+            conf.addProperty(property + "." + PASS_PARAM, password);
         }
+        if (containsParam(user, property, conf, PORT_PARAM)) {
+            conf.setProperty(property + "." + PORT_PARAM, port);
+        } else if (port != null && !port.isEmpty()) {
+            conf.addProperty(property + "." + PORT_PARAM, port);
+        }
+        if (containsParam(user, property, conf, URL_PARAM)) {
+            conf.setProperty(property + "." + URL_PARAM, url);
+        } else {
+            conf.addProperty(property + "." + URL_PARAM, url);
+        }
+    }
+
+    private static void resolveProperty(String user, String password, String port, String url, String property,
+        PropertiesConfiguration conf, String key) {
+        if (contains(user, property, key, USER_PARAM)) {
+            conf.setProperty(property + "." + key, user);
+        }
+        if (contains(user, property, key, PASS_PARAM)) {
+            conf.setProperty(property + "." + key, password);
+        }
+        if (contains(user, property, key, PORT_PARAM)) {
+            conf.setProperty(property + "." + key, port);
+        }
+        if (contains(user, property, key, URL_PARAM)) {
+            conf.setProperty(property + "." + key, url);
+        }
+    }
+
+    private static boolean containsParam(String var, String property, PropertiesConfiguration conf, String param) {
+        return StringUtils.isNotBlank(var) && conf.containsKey(property + "." + param);
+    }
 
+    private static boolean contains(String var, String property, String key, String param) {
+        return StringUtils.isNotBlank(var) && (property + "." + key).contains(param);
     }
 }
index 4f21ae5..16ff38e 100644 (file)
@@ -82,8 +82,7 @@ public class TestEncryptionTool {
         
     //@Test(expected=Exception.class)
     public void testupdateProperties() throws Exception{
-        WrapperEncryptionTool et =  new WrapperEncryptionTool();
-        et.updateProperties("testuser2", "", "abc3", "", "22", "testhost1", "Ansible");
+        WrapperEncryptionTool.updateProperties("testuser2", "", "abc3", "", "22", "testhost1", "Ansible");
             
     }