More unit test coverage and code cleanup
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / Parameters.java
index 9e7071b..78ed96d 100644 (file)
 \r
 package org.onap.dmaap.datarouter.provisioning.beans;\r
 \r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
 import java.sql.Connection;\r
 import java.sql.PreparedStatement;\r
 import java.sql.ResultSet;\r
 import java.sql.SQLException;\r
 import java.sql.Statement;\r
-import java.util.*;\r
-\r
-import com.att.eelf.configuration.EELFLogger;\r
-import com.att.eelf.configuration.EELFManager;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.Objects;\r
 import org.json.JSONObject;\r
 import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
 \r
@@ -67,7 +70,7 @@ public class Parameters extends Syncable {
     public static final String DELIVERY_RETRY_RATIO = "DELIVERY_RETRY_RATIO";\r
     public static final String DELIVERY_MAX_AGE = "DELIVERY_MAX_AGE";\r
     public static final String THROTTLE_FILTER = "THROTTLE_FILTER";\r
-    public static final String STATIC_ROUTING_NODES = "STATIC_ROUTING_NODES"; //Adding new param for static Routing - Rally:US664862-1610\r
+    public static final String STATIC_ROUTING_NODES = "STATIC_ROUTING_NODES";\r
 \r
     private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");\r
     private static final String SQLEXCEPTION = "SQLException: ";\r
@@ -75,13 +78,23 @@ public class Parameters extends Syncable {
     private String keyname;\r
     private String value;\r
 \r
+    public Parameters(String k, String v) {\r
+        this.keyname = k;\r
+        this.value = v;\r
+    }\r
+\r
+    public Parameters(ResultSet rs) throws SQLException {\r
+        this.keyname = rs.getString("KEYNAME");\r
+        this.value = rs.getString("VALUE");\r
+    }\r
+\r
     /**\r
      * Get all parameters in the DB as a Map.\r
      *\r
      * @return the Map of keynames/values from the DB.\r
      */\r
     public static Map<String, String> getParameters() {\r
-        Map<String, String> props = new HashMap<String, String>();\r
+        Map<String, String> props = new HashMap<>();\r
         for (Parameters p : getParameterCollection()) {\r
             props.put(p.getKeyname(), p.getValue());\r
         }\r
@@ -89,23 +102,20 @@ public class Parameters extends Syncable {
     }\r
 \r
     public static Collection<Parameters> getParameterCollection() {\r
-        Collection<Parameters> coll = new ArrayList<Parameters>();\r
-        try {\r
-            DB db = new DB();\r
-            @SuppressWarnings("resource")\r
-            Connection conn = db.getConnection();\r
-            try (Statement stmt = conn.createStatement()) {\r
-                String sql = "select * from PARAMETERS";\r
-                try (ResultSet rs = stmt.executeQuery(sql)) {\r
-                    while (rs.next()) {\r
-                        Parameters p = new Parameters(rs);\r
-                        coll.add(p);\r
-                    }\r
+        Collection<Parameters> coll = new ArrayList<>();\r
+        DB db = new DB();\r
+        String sql = "select * from PARAMETERS";\r
+        try (Connection conn = db.getConnection();\r
+                Statement stmt = conn.createStatement()) {\r
+            try (ResultSet rs = stmt.executeQuery(sql)) {\r
+                while (rs.next()) {\r
+                    Parameters p = new Parameters(rs);\r
+                    coll.add(p);\r
                 }\r
             }\r
             db.release(conn);\r
         } catch (SQLException e) {\r
-            intlogger.error(SQLEXCEPTION + e.getMessage());\r
+            intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
         }\r
         return coll;\r
     }\r
@@ -118,40 +128,23 @@ public class Parameters extends Syncable {
      */\r
     public static Parameters getParameter(String k) {\r
         Parameters v = null;\r
-        try {\r
-            DB db = new DB();\r
-            @SuppressWarnings("resource")\r
-            Connection conn = db.getConnection();\r
-            try (PreparedStatement stmt = conn\r
-                    .prepareStatement("select KEYNAME, VALUE from PARAMETERS where KEYNAME = ?")) {\r
-                stmt.setString(1, k);\r
-                try (ResultSet rs = stmt.executeQuery()) {\r
-                    if (rs.next()) {\r
-                        v = new Parameters(rs);\r
-                    }\r
+        DB db = new DB();\r
+        String sql = "select KEYNAME, VALUE from PARAMETERS where KEYNAME = ?";\r
+        try (Connection conn = db.getConnection();\r
+                PreparedStatement stmt = conn.prepareStatement(sql)) {\r
+            stmt.setString(1, k);\r
+            try (ResultSet rs = stmt.executeQuery()) {\r
+                if (rs.next()) {\r
+                    v = new Parameters(rs);\r
                 }\r
             }\r
             db.release(conn);\r
         } catch (SQLException e) {\r
-            intlogger.error(SQLEXCEPTION + e.getMessage());\r
+            intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
         }\r
         return v;\r
     }\r
 \r
-    public Parameters() {\r
-        this("", "");\r
-    }\r
-\r
-    public Parameters(String k, String v) {\r
-        this.keyname = k;\r
-        this.value = v;\r
-    }\r
-\r
-    public Parameters(ResultSet rs) throws SQLException {\r
-        this.keyname = rs.getString("KEYNAME");\r
-        this.value = rs.getString("VALUE");\r
-    }\r
-\r
     public String getKeyname() {\r
         return keyname;\r
     }\r
@@ -175,25 +168,14 @@ public class Parameters extends Syncable {
     @Override\r
     public boolean doInsert(Connection c) {\r
         boolean rv = true;\r
-        PreparedStatement ps = null;\r
-        try {\r
-            // Create the SUBSCRIPTIONS row\r
-            String sql = "insert into PARAMETERS values (?, ?)";\r
-            ps = c.prepareStatement(sql);\r
+        String sql = "insert into PARAMETERS values (?, ?)";\r
+        try (PreparedStatement ps = c.prepareStatement(sql)) {\r
             ps.setString(1, getKeyname());\r
             ps.setString(2, getValue());\r
             ps.execute();\r
         } catch (SQLException e) {\r
             rv = false;\r
             intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);\r
-        } finally {\r
-            try {\r
-                if (ps != null) {\r
-                    ps.close();\r
-                }\r
-            } catch (SQLException e) {\r
-                intlogger.error(SQLEXCEPTION + e.getMessage());\r
-            }\r
         }\r
         return rv;\r
     }\r
@@ -201,25 +183,14 @@ public class Parameters extends Syncable {
     @Override\r
     public boolean doUpdate(Connection c) {\r
         boolean rv = true;\r
-        PreparedStatement ps = null;\r
-        try {\r
-            // Update the PARAMETERS row\r
-            String sql = "update PARAMETERS set VALUE = ? where KEYNAME = ?";\r
-            ps = c.prepareStatement(sql);\r
+        String sql = "update PARAMETERS set VALUE = ? where KEYNAME = ?";\r
+        try (PreparedStatement ps = c.prepareStatement(sql)) {\r
             ps.setString(1, getValue());\r
             ps.setString(2, getKeyname());\r
             ps.executeUpdate();\r
         } catch (SQLException e) {\r
             rv = false;\r
             intlogger.warn("PROV0006 doUpdate: " + e.getMessage(),e);\r
-        } finally {\r
-            try {\r
-                if (ps != null) {\r
-                    ps.close();\r
-                }\r
-            } catch (SQLException e) {\r
-                intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
-            }\r
         }\r
         return rv;\r
     }\r
@@ -227,24 +198,13 @@ public class Parameters extends Syncable {
     @Override\r
     public boolean doDelete(Connection c) {\r
         boolean rv = true;\r
-        PreparedStatement ps = null;\r
-        try {\r
-            // Create the SUBSCRIPTIONS row\r
-            String sql = "delete from PARAMETERS where KEYNAME = ?";\r
-            ps = c.prepareStatement(sql);\r
+        String sql = "delete from PARAMETERS where KEYNAME = ?";\r
+        try (PreparedStatement ps = c.prepareStatement(sql)) {\r
             ps.setString(1, getKeyname());\r
             ps.execute();\r
         } catch (SQLException e) {\r
             rv = false;\r
             intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);\r
-        } finally {\r
-            try {\r
-                if (ps != null) {\r
-                    ps.close();\r
-                }\r
-            } catch (SQLException e) {\r
-                intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
-            }\r
         }\r
         return rv;\r
     }\r
@@ -260,13 +220,7 @@ public class Parameters extends Syncable {
             return false;\r
         }\r
         Parameters of = (Parameters) obj;\r
-        if (!keyname.equals(of.keyname)) {\r
-            return false;\r
-        }\r
-        if (!value.equals(of.value)) {\r
-            return false;\r
-        }\r
-        return true;\r
+        return (!value.equals(of.value)) && (!keyname.equals(of.keyname));\r
     }\r
 \r
     @Override\r