More unit test coverage and code cleanup
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / Group.java
index da682d7..4d6b0ee 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.io.InvalidObjectException;\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.Date;\r
+import java.util.List;\r
+import java.util.Objects;\r
 import org.json.JSONObject;\r
 import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
 \r
@@ -44,8 +47,9 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB;
  */\r
 public class Group extends Syncable {\r
 \r
+    private static final String GROUP_ID_CONST = "groupid";\r
     private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");\r
-    private static int next_groupid = getMaxGroupID() + 1;\r
+    private static int nextGroupid = getMaxGroupID() + 1;\r
     private static final String SQLEXCEPTION = "SQLException: ";\r
 \r
     private int groupid;\r
@@ -56,6 +60,60 @@ public class Group extends Syncable {
     private String members;\r
     private Date last_mod;\r
 \r
+    public Group() {\r
+        this("", "", "");\r
+    }\r
+\r
+    public Group(String name, String desc, String members) {\r
+        this.groupid = -1;\r
+        this.authid = "";\r
+        this.name = name;\r
+        this.description = desc;\r
+        this.members = members;\r
+        this.classification = "";\r
+        this.last_mod = new Date();\r
+    }\r
+\r
+\r
+    public Group(ResultSet rs) throws SQLException {\r
+        this.groupid = rs.getInt("GROUPID");\r
+        this.authid = rs.getString("AUTHID");\r
+        this.name = rs.getString("NAME");\r
+        this.description = rs.getString("DESCRIPTION");\r
+        this.classification = rs.getString("CLASSIFICATION");\r
+        this.members = rs.getString("MEMBERS");\r
+        this.last_mod = rs.getDate("LAST_MOD");\r
+    }\r
+\r
+\r
+    public Group(JSONObject jo) throws InvalidObjectException {\r
+        this("", "", "");\r
+        try {\r
+            // The JSONObject is assumed to contain a vnd.dmaap-dr.group representation\r
+            this.groupid = jo.optInt(GROUP_ID_CONST, -1);\r
+            String gname = jo.getString("name");\r
+            String gdescription = jo.getString("description");\r
+\r
+            this.authid = jo.getString("authid");\r
+            this.name = gname;\r
+            this.description = gdescription;\r
+            this.classification = jo.getString("classification");\r
+            this.members = jo.getString("members");\r
+\r
+            if (gname.length() > 50) {\r
+                throw new InvalidObjectException("Group name is too long");\r
+            }\r
+            if (gdescription.length() > 256) {\r
+                throw new InvalidObjectException("Group Description is too long");\r
+            }\r
+        } catch (InvalidObjectException e) {\r
+            throw e;\r
+        } catch (Exception e) {\r
+            intlogger.warn("Invalid JSON: " + e.getMessage(), e);\r
+            throw new InvalidObjectException("Invalid JSON: " + e.getMessage());\r
+        }\r
+    }\r
+\r
 \r
     public static Group getGroupMatching(Group gup) {\r
         String sql = String.format(\r
@@ -82,7 +140,7 @@ public class Group extends Syncable {
         return list.size() > 0 ? list.get(0) : null;\r
     }\r
 \r
-    public static Group getGroupByAuthId(String id) {\r
+    static Group getGroupByAuthId(String id) {\r
         String sql = "select * from GROUPS where AUTHID = '" + id + "'";\r
         List<Group> list = getGroupsForSQL(sql);\r
         return list.size() > 0 ? list.get(0) : null;\r
@@ -93,7 +151,7 @@ public class Group extends Syncable {
     }\r
 \r
     private static List<Group> getGroupsForSQL(String sql) {\r
-        List<Group> list = new ArrayList<Group>();\r
+        List<Group> list = new ArrayList<>();\r
         try {\r
             DB db = new DB();\r
             @SuppressWarnings("resource")\r
@@ -113,7 +171,7 @@ public class Group extends Syncable {
         return list;\r
     }\r
 \r
-    public static int getMaxGroupID() {\r
+    private static int getMaxGroupID() {\r
         int max = 0;\r
         try {\r
             DB db = new DB();\r
@@ -133,60 +191,6 @@ public class Group extends Syncable {
         return max;\r
     }\r
 \r
-    public Group() {\r
-        this("", "", "");\r
-    }\r
-\r
-    public Group(String name, String desc, String members) {\r
-        this.groupid = -1;\r
-        this.authid = "";\r
-        this.name = name;\r
-        this.description = desc;\r
-        this.members = members;\r
-        this.classification = "";\r
-        this.last_mod = new Date();\r
-    }\r
-\r
-\r
-    public Group(ResultSet rs) throws SQLException {\r
-        this.groupid = rs.getInt("GROUPID");\r
-        this.authid = rs.getString("AUTHID");\r
-        this.name = rs.getString("NAME");\r
-        this.description = rs.getString("DESCRIPTION");\r
-        this.classification = rs.getString("CLASSIFICATION");\r
-        this.members = rs.getString("MEMBERS");\r
-        this.last_mod = rs.getDate("LAST_MOD");\r
-    }\r
-\r
-\r
-    public Group(JSONObject jo) throws InvalidObjectException {\r
-        this("", "", "");\r
-        try {\r
-            // The JSONObject is assumed to contain a vnd.dmaap-dr.group representation\r
-            this.groupid = jo.optInt("groupid", -1);\r
-            String gname = jo.getString("name");\r
-            String gdescription = jo.getString("description");\r
-\r
-            this.authid = jo.getString("authid");\r
-            this.name = gname;\r
-            this.description = gdescription;\r
-            this.classification = jo.getString("classification");\r
-            this.members = jo.getString("members");\r
-\r
-            if (gname.length() > 50) {\r
-                throw new InvalidObjectException("Group name is too long");\r
-            }\r
-            if (gdescription.length() > 256) {\r
-                throw new InvalidObjectException("Group Description is too long");\r
-            }\r
-        } catch (InvalidObjectException e) {\r
-            throw e;\r
-        } catch (Exception e) {\r
-            intlogger.warn("Invalid JSON: " + e.getMessage(), e);\r
-            throw new InvalidObjectException("Invalid JSON: " + e.getMessage());\r
-        }\r
-    }\r
-\r
     public int getGroupid() {\r
         return groupid;\r
     }\r
@@ -242,7 +246,7 @@ public class Group extends Syncable {
     @Override\r
     public JSONObject asJSONObject() {\r
         JSONObject jo = new JSONObject();\r
-        jo.put("groupid", groupid);\r
+        jo.put(GROUP_ID_CONST, groupid);\r
         jo.put("authid", authid);\r
         jo.put("name", name);\r
         jo.put("description", description);\r
@@ -259,11 +263,11 @@ public class Group extends Syncable {
         try {\r
             if (groupid == -1) {\r
                 // No feed ID assigned yet, so assign the next available one\r
-                setGroupid(next_groupid++);\r
+                setGroupid(nextGroupid++);\r
             }\r
             // In case we insert a gropup from synchronization\r
-            if (groupid > next_groupid) {\r
-                next_groupid = groupid + 1;\r
+            if (groupid > nextGroupid) {\r
+                nextGroupid = groupid + 1;\r
             }\r
 \r
             // Create the GROUPS row\r