More unit test coverage and code cleanup
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / EgressRoute.java
index e766e70..a78a9c1 100644 (file)
@@ -24,6 +24,8 @@
 \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
@@ -32,9 +34,6 @@ import java.sql.Statement;
 import java.util.Objects;\r
 import java.util.SortedSet;\r
 import java.util.TreeSet;\r
-\r
-import com.att.eelf.configuration.EELFLogger;\r
-import com.att.eelf.configuration.EELFManager;\r
 import org.json.JSONObject;\r
 import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
 \r
@@ -47,10 +46,22 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB;
 public class EgressRoute extends NodeClass implements Comparable<EgressRoute> {\r
 \r
     private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");\r
-    private static final String SQLEXCEPTION = "SQLException: ";\r
     private final int subid;\r
     private final int nodeid;\r
 \r
+    public EgressRoute(int subid, int nodeid) {\r
+        this.subid = subid;\r
+        this.nodeid = nodeid;\r
+        // Note: unlike for Feeds, it subscriptions can be removed from the tables, so it is\r
+        // possible that an orphan ERT entry can exist if a sub is removed.\r
+        //        if (Subscription.getSubscriptionById(subid) == null)\r
+        //            throw new IllegalArgumentException("No such subscription: "+subid);\r
+    }\r
+\r
+    public EgressRoute(int subid, String node) {\r
+        this(subid, lookupNodeName(node));\r
+    }\r
+\r
     /**\r
      * Get a set of all Egress Routes in the DB.  The set is sorted according to the natural sorting order of the routes\r
      * (based on the subscription ID in each route).\r
@@ -59,27 +70,30 @@ public class EgressRoute extends NodeClass implements Comparable<EgressRoute> {
      */\r
     public static SortedSet<EgressRoute> getAllEgressRoutes() {\r
         SortedSet<EgressRoute> set = new TreeSet<>();\r
-        try {\r
-            DB db = new DB();\r
-            @SuppressWarnings("resource")\r
-            Connection conn = db.getConnection();\r
+        DB db = new DB();\r
+        String sql = "select SUBID, NODEID from EGRESS_ROUTES";\r
+        try (Connection conn = db.getConnection()) {\r
             try (Statement stmt = conn.createStatement()) {\r
-                try (ResultSet rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES")) {\r
-                    while (rs.next()) {\r
-                        int subid = rs.getInt("SUBID");\r
-                        int nodeid = rs.getInt("NODEID");\r
-                        set.add(new EgressRoute(subid, nodeid));\r
-                    }\r
+                try (ResultSet rs = stmt.executeQuery(sql)) {\r
+                    addEgressRouteToSet(set, rs);\r
                 }\r
+            } finally {\r
+                db.release(conn);\r
             }\r
-\r
-            db.release(conn);\r
         } catch (SQLException e) {\r
             intlogger.error("PROV0008 EgressRoute.getAllEgressRoutes: " + e.getMessage(), e);\r
         }\r
         return set;\r
     }\r
 \r
+    private static void addEgressRouteToSet(SortedSet<EgressRoute> set, ResultSet rs) throws SQLException {\r
+        while (rs.next()) {\r
+            int subid = rs.getInt("SUBID");\r
+            int nodeid = rs.getInt("NODEID");\r
+            set.add(new EgressRoute(subid, nodeid));\r
+        }\r
+    }\r
+\r
     /**\r
      * Get a single Egress Route for the subscription <i>sub</i>.\r
      *\r
@@ -88,69 +102,35 @@ public class EgressRoute extends NodeClass implements Comparable<EgressRoute> {
      */\r
     public static EgressRoute getEgressRoute(int sub) {\r
         EgressRoute v = null;\r
-        PreparedStatement ps = null;\r
-        try {\r
-            DB db = new DB();\r
-            @SuppressWarnings("resource")\r
-            Connection conn = db.getConnection();\r
-            String sql = "select NODEID from EGRESS_ROUTES where SUBID = ?";\r
-            ps = conn.prepareStatement(sql);\r
+        DB db = new DB();\r
+        String sql = "select NODEID from EGRESS_ROUTES where SUBID = ?";\r
+        try (Connection conn = db.getConnection();\r
+                PreparedStatement ps = conn.prepareStatement(sql)) {\r
             ps.setInt(1, sub);\r
             try (ResultSet rs = ps.executeQuery()) {\r
                 if (rs.next()) {\r
                     int node = rs.getInt("NODEID");\r
                     v = new EgressRoute(sub, node);\r
                 }\r
+            } finally {\r
+                db.release(conn);\r
             }\r
-            ps.close();\r
-            db.release(conn);\r
         } catch (SQLException e) {\r
             intlogger.error("PROV0009 EgressRoute.getEgressRoute: " + 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 v;\r
     }\r
 \r
-    public EgressRoute(int subid, int nodeid) {\r
-        this.subid = subid;\r
-        this.nodeid = nodeid;\r
-// Note: unlike for Feeds, it subscriptions can be removed from the tables, so it is\r
-// possible that an orphan ERT entry can exist if a sub is removed.\r
-//        if (Subscription.getSubscriptionById(subid) == null)\r
-//            throw new IllegalArgumentException("No such subscription: "+subid);\r
-    }\r
-\r
-    public EgressRoute(int subid, String node) {\r
-        this(subid, lookupNodeName(node));\r
-    }\r
-\r
     @Override\r
     public boolean doDelete(Connection c) {\r
         boolean rv = true;\r
-        PreparedStatement ps = null;\r
-        try {\r
-            String sql = "delete from EGRESS_ROUTES where SUBID = ?";\r
-            ps = c.prepareStatement(sql);\r
+        String sql = "delete from EGRESS_ROUTES where SUBID = ?";\r
+        try (PreparedStatement ps = c.prepareStatement(sql)) {\r
             ps.setInt(1, subid);\r
             ps.execute();\r
         } catch (SQLException e) {\r
             rv = false;\r
             intlogger.error("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
@@ -158,11 +138,9 @@ public class EgressRoute extends NodeClass implements Comparable<EgressRoute> {
     @Override\r
     public boolean doInsert(Connection c) {\r
         boolean rv = false;\r
-        PreparedStatement ps = null;\r
-        try {\r
+        String sql = "insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)";\r
+        try (PreparedStatement ps = c.prepareStatement(sql)) {\r
             // Create the NETWORK_ROUTES row\r
-            String sql = "insert into EGRESS_ROUTES (SUBID, NODEID) values (?, ?)";\r
-            ps = c.prepareStatement(sql);\r
             ps.setInt(1, this.subid);\r
             ps.setInt(2, this.nodeid);\r
             ps.execute();\r
@@ -170,14 +148,6 @@ public class EgressRoute extends NodeClass implements Comparable<EgressRoute> {
             rv = true;\r
         } catch (SQLException e) {\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(), e);\r
-            }\r
         }\r
         return rv;\r
     }\r
@@ -185,24 +155,14 @@ public class EgressRoute extends NodeClass implements Comparable<EgressRoute> {
     @Override\r
     public boolean doUpdate(Connection c) {\r
         boolean rv = true;\r
-        PreparedStatement ps = null;\r
-        try {\r
-            String sql = "update EGRESS_ROUTES set NODEID = ? where SUBID = ?";\r
-            ps = c.prepareStatement(sql);\r
+        String sql = "update EGRESS_ROUTES set NODEID = ? where SUBID = ?";\r
+        try (PreparedStatement ps = c.prepareStatement(sql)) {\r
             ps.setInt(1, nodeid);\r
             ps.setInt(2, subid);\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