diff options
Diffstat (limited to 'datarouter-prov')
7 files changed, 310 insertions, 348 deletions
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java index 852321a9..c08bce57 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java @@ -24,20 +24,6 @@ package org.onap.dmaap.datarouter.provisioning.beans;
-import java.io.InvalidObjectException;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
@@ -45,6 +31,11 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; import org.onap.dmaap.datarouter.provisioning.utils.JSONUtilities;
import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
+import java.io.InvalidObjectException;
+import java.sql.*;
+import java.util.*;
+import java.util.Date;
+
/**
* The representation of a Feed. Feeds can be retrieved from the DB, or stored/updated in the DB.
*
@@ -81,13 +72,13 @@ public class Feed extends Syncable { try {
DB db = new DB();
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select COUNT(*) from FEEDS where FEEDID = " + id);
- if (rs.next()) {
- count = rs.getInt(1);
+ try(Statement stmt = conn.createStatement()) {
+ try(ResultSet rs = stmt.executeQuery("select COUNT(*) from FEEDS where FEEDID = " + id)) {
+ if (rs.next()) {
+ count = rs.getInt(1);
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
@@ -131,13 +122,13 @@ public class Feed extends Syncable { DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select count(*) from FEEDS where DELETED = 0");
- if (rs.next()) {
- count = rs.getInt(1);
+ try(Statement stmt = conn.createStatement()) {
+ try (ResultSet rs = stmt.executeQuery("select count(*) from FEEDS where DELETED = 0")) {
+ if (rs.next()) {
+ count = rs.getInt(1);
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
intlogger.info("countActiveFeeds: " + e.getMessage());
@@ -152,13 +143,13 @@ public class Feed extends Syncable { DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select MAX(feedid) from FEEDS");
- if (rs.next()) {
- max = rs.getInt(1);
+ try(Statement stmt = conn.createStatement()) {
+ try (ResultSet rs = stmt.executeQuery("select MAX(feedid) from FEEDS")) {
+ if (rs.next()) {
+ max = rs.getInt(1);
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
intlogger.info("getMaxFeedID: " + e.getMessage());
@@ -173,40 +164,39 @@ public class Feed extends Syncable { DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select * from FEEDS");
- while (rs.next()) {
- Feed feed = new Feed(rs);
- map.put(feed.getFeedid(), feed);
- }
- rs.close();
+ try(Statement stmt = conn.createStatement()) {
+ try(ResultSet rs = stmt.executeQuery("select * from FEEDS")) {
+ while (rs.next()) {
+ Feed feed = new Feed(rs);
+ map.put(feed.getFeedid(), feed);
+ }
+ }
- String sql = "select * from FEED_ENDPOINT_IDS";
- rs = stmt.executeQuery(sql);
- while (rs.next()) {
- int id = rs.getInt("FEEDID");
- Feed feed = map.get(id);
- if (feed != null) {
- FeedEndpointID epi = new FeedEndpointID(rs);
- Collection<FeedEndpointID> ecoll = feed.getAuthorization().getEndpoint_ids();
- ecoll.add(epi);
+ String sql = "select * from FEED_ENDPOINT_IDS";
+ try(ResultSet rs = stmt.executeQuery(sql)){
+ while (rs.next()) {
+ int id = rs.getInt("FEEDID");
+ Feed feed = map.get(id);
+ if (feed != null) {
+ FeedEndpointID epi = new FeedEndpointID(rs);
+ Collection<FeedEndpointID> ecoll = feed.getAuthorization().getEndpoint_ids();
+ ecoll.add(epi);
+ }
+ }
}
- }
- rs.close();
- sql = "select * from FEED_ENDPOINT_ADDRS";
- rs = stmt.executeQuery(sql);
- while (rs.next()) {
- int id = rs.getInt("FEEDID");
- Feed feed = map.get(id);
- if (feed != null) {
- Collection<String> acoll = feed.getAuthorization().getEndpoint_addrs();
- acoll.add(rs.getString("ADDR"));
+ sql = "select * from FEED_ENDPOINT_ADDRS";
+ try(ResultSet rs = stmt.executeQuery(sql)) {
+ while (rs.next()) {
+ int id = rs.getInt("FEEDID");
+ Feed feed = map.get(id);
+ if (feed != null) {
+ Collection<String> acoll = feed.getAuthorization().getEndpoint_addrs();
+ acoll.add(rs.getString("ADDR"));
+ }
+ }
}
}
- rs.close();
-
- stmt.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
@@ -231,16 +221,16 @@ public class Feed extends Syncable { DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- PreparedStatement ps = conn.prepareStatement(sql);
- if (sql.indexOf('?') >= 0)
- ps.setString(1, val);
- ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- String t = rs.getString(1);
- list.add(t.trim());
+ try(PreparedStatement ps = conn.prepareStatement(sql)) {
+ if (sql.indexOf('?') >= 0)
+ ps.setString(1, val);
+ try(ResultSet rs = ps.executeQuery()) {
+ while (rs.next()) {
+ String t = rs.getString(1);
+ list.add(t.trim());
+ }
+ }
}
- rs.close();
- ps.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
@@ -254,30 +244,30 @@ public class Feed extends Syncable { try {
DB db = new DB();
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- if (rs.next()) {
- feed = new Feed(rs);
- rs.close();
-
- sql = "select * from FEED_ENDPOINT_IDS where FEEDID = " + feed.feedid;
- rs = stmt.executeQuery(sql);
- Collection<FeedEndpointID> ecoll = feed.getAuthorization().getEndpoint_ids();
- while (rs.next()) {
- FeedEndpointID epi = new FeedEndpointID(rs);
- ecoll.add(epi);
+ try (Statement stmt = conn.createStatement()) {
+ try (ResultSet rs = stmt.executeQuery(sql)) {
+ if (rs.next()) {
+ feed = new Feed(rs);
+ }
}
- rs.close();
-
- sql = "select * from FEED_ENDPOINT_ADDRS where FEEDID = " + feed.feedid;
- rs = stmt.executeQuery(sql);
- Collection<String> acoll = feed.getAuthorization().getEndpoint_addrs();
- while (rs.next()) {
- acoll.add(rs.getString("ADDR"));
+ if (feed != null) {
+ sql = "select * from FEED_ENDPOINT_IDS where FEEDID = " + feed.feedid;
+ try (ResultSet rs = stmt.executeQuery(sql)) {
+ Collection<FeedEndpointID> ecoll = feed.getAuthorization().getEndpoint_ids();
+ while (rs.next()) {
+ FeedEndpointID epi = new FeedEndpointID(rs);
+ ecoll.add(epi);
+ }
+ }
+ sql = "select * from FEED_ENDPOINT_ADDRS where FEEDID = " + feed.feedid;
+ try (ResultSet rs = stmt.executeQuery(sql)) {
+ Collection<String> acoll = feed.getAuthorization().getEndpoint_addrs();
+ while (rs.next()) {
+ acoll.add(rs.getString("ADDR"));
+ }
+ }
}
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
@@ -546,7 +536,9 @@ public class Feed extends Syncable { e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -557,17 +549,8 @@ public class Feed extends Syncable { @Override
public synchronized boolean doInsert(Connection c) {
boolean rv = true;
-// PreparedStatement ps = null;
try {
if (feedid == -1) {
-// // Get the next feedid
-// String sql = "insert into FEEDS_UNIQUEID (FEEDID) values (0)";
-// ps = c.prepareStatement(sql, new String[] { "FEEDID" });
-// ps.execute();
-// ResultSet rs = ps.getGeneratedKeys();
-// rs.first();
-// setFeedid(rs.getInt(1));
- // No feed ID assigned yet, so assign the next available one
setFeedid(next_feedid++);
}
// In case we insert a feed from synchronization
@@ -577,54 +560,48 @@ public class Feed extends Syncable { // Create FEED_ENDPOINT_IDS rows
FeedAuthorization auth = getAuthorization();
String sql = "insert into FEED_ENDPOINT_IDS values (?, ?, ?)";
- PreparedStatement ps2 = c.prepareStatement(sql);
- for (FeedEndpointID fid : auth.getEndpoint_ids()) {
- ps2.setInt(1, feedid);
- ps2.setString(2, fid.getId());
- ps2.setString(3, fid.getPassword());
- ps2.executeUpdate();
+ try(PreparedStatement ps2 = c.prepareStatement(sql)) {
+ for (FeedEndpointID fid : auth.getEndpoint_ids()) {
+ ps2.setInt(1, feedid);
+ ps2.setString(2, fid.getId());
+ ps2.setString(3, fid.getPassword());
+ ps2.executeUpdate();
+ }
}
- ps2.close();
// Create FEED_ENDPOINT_ADDRS rows
sql = "insert into FEED_ENDPOINT_ADDRS values (?, ?)";
- ps2 = c.prepareStatement(sql);
- for (String t : auth.getEndpoint_addrs()) {
- ps2.setInt(1, feedid);
- ps2.setString(2, t);
- ps2.executeUpdate();
+ try(PreparedStatement ps2 = c.prepareStatement(sql)) {
+ for (String t : auth.getEndpoint_addrs()) {
+ ps2.setInt(1, feedid);
+ ps2.setString(2, t);
+ ps2.executeUpdate();
+ }
}
- ps2.close();
// Finally, create the FEEDS row
sql = "insert into FEEDS (FEEDID, NAME, VERSION, DESCRIPTION, AUTH_CLASS, PUBLISHER, SELF_LINK, PUBLISH_LINK, SUBSCRIBE_LINK, LOG_LINK, DELETED, SUSPENDED,BUSINESS_DESCRIPTION, GROUPID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?)";
- ps2 = c.prepareStatement(sql);
- ps2.setInt(1, feedid);
- ps2.setString(2, getName());
- ps2.setString(3, getVersion());
- ps2.setString(4, getDescription());
- ps2.setString(5, getAuthorization().getClassification());
- ps2.setString(6, getPublisher());
- ps2.setString(7, getLinks().getSelf());
- ps2.setString(8, getLinks().getPublish());
- ps2.setString(9, getLinks().getSubscribe());
- ps2.setString(10, getLinks().getLog());
- ps2.setBoolean(11, isDeleted());
- ps2.setBoolean(12, isSuspended());
- ps2.setString(13, getBusiness_description()); // New field is added - Groups feature Rally:US708102 - 1610
- ps2.setInt(14, groupid); //New field is added - Groups feature Rally:US708115 - 1610
- ps2.executeUpdate();
- ps2.close();
+ try(PreparedStatement ps2 = c.prepareStatement(sql)) {
+ ps2.setInt(1, feedid);
+ ps2.setString(2, getName());
+ ps2.setString(3, getVersion());
+ ps2.setString(4, getDescription());
+ ps2.setString(5, getAuthorization().getClassification());
+ ps2.setString(6, getPublisher());
+ ps2.setString(7, getLinks().getSelf());
+ ps2.setString(8, getLinks().getPublish());
+ ps2.setString(9, getLinks().getSubscribe());
+ ps2.setString(10, getLinks().getLog());
+ ps2.setBoolean(11, isDeleted());
+ ps2.setBoolean(12, isSuspended());
+ ps2.setString(13, getBusiness_description()); // New field is added - Groups feature Rally:US708102 - 1610
+ ps2.setInt(14, groupid); //New field is added - Groups feature Rally:US708115 - 1610
+ ps2.executeUpdate();
+ }
} catch (SQLException e) {
rv = false;
intlogger.warn("PROV0005 doInsert: " + e.getMessage());
e.printStackTrace();
-// } finally {
-// try {
-// ps.close();
-// } catch (SQLException e) {
-// e.printStackTrace();
-// }
}
return rv;
}
@@ -741,7 +718,9 @@ public class Feed extends Syncable { e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -789,4 +768,9 @@ public class Feed extends Syncable { public String toString() {
return "FEED: feedid=" + feedid + ", name=" + name + ", version=" + version;
}
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(feedid, groupid, name, version, description, business_description, authorization, publisher, links, deleted, suspended, last_mod, created_date);
+ }
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java index a021a60e..a460d647 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java @@ -29,10 +29,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
import org.apache.log4j.Logger;
import org.json.JSONObject;
@@ -99,14 +96,14 @@ public class Group extends Syncable { DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- Group group = new Group(rs);
- list.add(group);
+ try(Statement stmt = conn.createStatement()) {
+ try(ResultSet rs = stmt.executeQuery(sql)) {
+ while (rs.next()) {
+ Group group = new Group(rs);
+ list.add(group);
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
@@ -120,13 +117,13 @@ public class Group extends Syncable { DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select MAX(groupid) from GROUPS");
- if (rs.next()) {
- max = rs.getInt(1);
+ try(Statement stmt = conn.createStatement()) {
+ try(ResultSet rs = stmt.executeQuery("select MAX(groupid) from GROUPS")) {
+ if (rs.next()) {
+ max = rs.getInt(1);
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
intlogger.info("getMaxSubID: " + e.getMessage());
@@ -142,14 +139,14 @@ public class Group extends Syncable { DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- int groupid = rs.getInt("groupid");
- //list.add(URLUtilities.generateSubscriptionURL(groupid));
+ try(Statement stmt = conn.createStatement()) {
+ try(ResultSet rs = stmt.executeQuery(sql)) {
+ while (rs.next()) {
+ int groupid = rs.getInt("groupid");
+
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
@@ -168,13 +165,13 @@ public class Group extends Syncable { DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select count(*) from SUBSCRIPTIONS");
- if (rs.next()) {
- count = rs.getInt(1);
+ try(Statement stmt = conn.createStatement()) {
+ try(ResultSet rs = stmt.executeQuery("select count(*) from SUBSCRIPTIONS")) {
+ if (rs.next()) {
+ count = rs.getInt(1);
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
intlogger.warn("PROV0008 countActiveSubscriptions: " + e.getMessage());
@@ -351,7 +348,9 @@ public class Group extends Syncable { e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -379,7 +378,9 @@ public class Group extends Syncable { e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -402,7 +403,9 @@ public class Group extends Syncable { e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -440,4 +443,9 @@ public class Group extends Syncable { public String toString() {
return "GROUP: groupid=" + groupid;
}
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(groupid, authid, name, description, classification, members, last_mod);
+ }
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java index 0de57df2..a4ed60a2 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRoute.java @@ -85,18 +85,18 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- int seq = rs.getInt("SEQUENCE");
- int feedid = rs.getInt("FEEDID");
- String user = rs.getString("USERID");
- String subnet = rs.getString("SUBNET");
- int nodeset = rs.getInt("NODESET");
- set.add(new IngressRoute(seq, feedid, user, subnet, nodeset));
+ try(Statement stmt = conn.createStatement()) {
+ try(ResultSet rs = stmt.executeQuery(sql)) {
+ while (rs.next()) {
+ int seq = rs.getInt("SEQUENCE");
+ int feedid = rs.getInt("FEEDID");
+ String user = rs.getString("USERID");
+ String subnet = rs.getString("SUBNET");
+ int nodeset = rs.getInt("NODESET");
+ set.add(new IngressRoute(seq, feedid, user, subnet, nodeset));
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
@@ -128,13 +128,13 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- if (rs.next()) {
- rv = rs.getInt("MAX");
+ try(Statement stmt = conn.createStatement()) {
+ try(ResultSet rs = stmt.executeQuery(sql)) {
+ if (rs.next()) {
+ rv = rs.getInt("MAX");
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
@@ -162,20 +162,22 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> ps.setInt(1, feedid);
ps.setString(2, user);
ps.setString(3, subnet);
- ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- int seq = rs.getInt("SEQUENCE");
- int nodeset = rs.getInt("NODESET");
- v = new IngressRoute(seq, feedid, user, subnet, nodeset);
+ try(ResultSet rs = ps.executeQuery()) {
+ if (rs.next()) {
+ int seq = rs.getInt("SEQUENCE");
+ int nodeset = rs.getInt("NODESET");
+ v = new IngressRoute(seq, feedid, user, subnet, nodeset);
+ }
}
- rs.close();
ps.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -191,33 +193,26 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> */
public static Collection<IngressRoute> getIngressRoute(int seq) {
Collection<IngressRoute> rv = new ArrayList<IngressRoute>();
- PreparedStatement ps = null;
try {
DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
String sql = "select FEEDID, USERID, SUBNET, NODESET from INGRESS_ROUTES where SEQUENCE = ?";
- ps = conn.prepareStatement(sql);
- ps.setInt(1, seq);
- ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- int feedid = rs.getInt("FEEDID");
- String user = rs.getString("USERID");
- String subnet = rs.getString("SUBNET");
- int nodeset = rs.getInt("NODESET");
- rv.add(new IngressRoute(seq, feedid, user, subnet, nodeset));
+ try(PreparedStatement ps = conn.prepareStatement(sql)) {
+ ps.setInt(1, seq);
+ try(ResultSet rs = ps.executeQuery()) {
+ while (rs.next()) {
+ int feedid = rs.getInt("FEEDID");
+ String user = rs.getString("USERID");
+ String subnet = rs.getString("SUBNET");
+ int nodeset = rs.getInt("NODESET");
+ rv.add(new IngressRoute(seq, feedid, user, subnet, nodeset));
+ }
+ }
}
- rs.close();
- ps.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
- } finally {
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
}
return rv;
}
@@ -386,31 +381,23 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> private Collection<String> readNodes() {
Collection<String> set = new TreeSet<String>();
- PreparedStatement ps = null;
try {
DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
String sql = "select NODEID from NODESETS where SETID = ?";
- ps = conn.prepareStatement(sql);
- ps.setInt(1, nodelist);
- ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- int id = rs.getInt("NODEID");
- set.add(lookupNodeID(id));
+ try(PreparedStatement ps = conn.prepareStatement(sql)) {
+ ps.setInt(1, nodelist);
+ try(ResultSet rs = ps.executeQuery()) {
+ while (rs.next()) {
+ int id = rs.getInt("NODEID");
+ set.add(lookupNodeID(id));
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
- } finally {
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
}
return set;
}
@@ -441,7 +428,9 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -482,7 +471,9 @@ public class IngressRoute extends NodeClass implements Comparable<IngressRoute> e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java index f50043a8..00eb6a26 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRoute.java @@ -29,6 +29,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
@@ -60,16 +61,16 @@ public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select FROMNODE, TONODE, VIANODE from NETWORK_ROUTES");
- while (rs.next()) {
- int fromnode = rs.getInt("FROMNODE");
- int tonode = rs.getInt("TONODE");
- int vianode = rs.getInt("VIANODE");
- set.add(new NetworkRoute(fromnode, tonode, vianode));
+ try(Statement stmt = conn.createStatement()) {
+ try(ResultSet rs = stmt.executeQuery("select FROMNODE, TONODE, VIANODE from NETWORK_ROUTES")) {
+ while (rs.next()) {
+ int fromnode = rs.getInt("FROMNODE");
+ int tonode = rs.getInt("TONODE");
+ int vianode = rs.getInt("VIANODE");
+ set.add(new NetworkRoute(fromnode, tonode, vianode));
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
@@ -129,7 +130,9 @@ public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -157,7 +160,9 @@ public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -183,7 +188,9 @@ public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -214,6 +221,11 @@ public class NetworkRoute extends NodeClass implements Comparable<NetworkRoute> }
@Override
+ public int hashCode() {
+ return Objects.hash(fromnode, tonode, vianode);
+ }
+
+ @Override
public int compareTo(NetworkRoute o) {
if (this.fromnode == o.fromnode) {
if (this.tonode == o.tonode)
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java index 8e9d5bfd..3e8c90b4 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java @@ -28,10 +28,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
import org.apache.log4j.Logger;
import org.json.JSONObject;
@@ -93,15 +90,15 @@ public class Parameters extends Syncable { DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- String sql = "select * from PARAMETERS";
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- Parameters p = new Parameters(rs);
- coll.add(p);
+ try(Statement stmt = conn.createStatement()) {
+ String sql = "select * from PARAMETERS";
+ try(ResultSet rs = stmt.executeQuery(sql)) {
+ while (rs.next()) {
+ Parameters p = new Parameters(rs);
+ coll.add(p);
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
@@ -121,14 +118,14 @@ public class Parameters extends Syncable { DB db = new DB();
@SuppressWarnings("resource")
Connection conn = db.getConnection();
- Statement stmt = conn.createStatement();
- String sql = "select KEYNAME, VALUE from PARAMETERS where KEYNAME = '" + k + "'";
- ResultSet rs = stmt.executeQuery(sql);
- if (rs.next()) {
- v = new Parameters(rs);
+ try(Statement stmt = conn.createStatement()) {
+ String sql = "select KEYNAME, VALUE from PARAMETERS where KEYNAME = '" + k + "'";
+ try(ResultSet rs = stmt.executeQuery(sql)) {
+ if (rs.next()) {
+ v = new Parameters(rs);
+ }
+ }
}
- rs.close();
- stmt.close();
db.release(conn);
} catch (SQLException e) {
e.printStackTrace();
@@ -191,7 +188,9 @@ public class Parameters extends Syncable { e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -216,7 +215,9 @@ public class Parameters extends Syncable { e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -240,7 +241,9 @@ public class Parameters extends Syncable { e.printStackTrace();
} finally {
try {
- ps.close();
+ if(ps!=null) {
+ ps.close();
+ }
} catch (SQLException e) {
e.printStackTrace();
}
@@ -266,6 +269,11 @@ public class Parameters extends Syncable { }
@Override
+ public int hashCode() {
+ return Objects.hash(keyname, value);
+ }
+
+ @Override
public String toString() {
return "PARAM: keyname=" + keyname + ", value=" + value;
}
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java index 5f6b7ae3..591dcc3d 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java @@ -33,43 +33,35 @@ import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER; import java.io.File; import java.net.InetAddress; -import java.util.HashMap; -import java.util.Map; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.junit.BeforeClass; +import org.junit.AfterClass; import org.mockito.Mock; -import org.onap.dmaap.datarouter.authz.AuthorizationResponse; -import org.onap.dmaap.datarouter.authz.Authorizer; + import org.onap.dmaap.datarouter.provisioning.beans.Deleteable; -import org.onap.dmaap.datarouter.provisioning.beans.Feed; import org.onap.dmaap.datarouter.provisioning.beans.Insertable; import org.onap.dmaap.datarouter.provisioning.beans.LogRecord; -import org.onap.dmaap.datarouter.provisioning.beans.NodeClass; -import org.onap.dmaap.datarouter.provisioning.beans.Parameters; -import org.onap.dmaap.datarouter.provisioning.beans.Subscription; import org.onap.dmaap.datarouter.provisioning.beans.Updateable; -import org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader; -import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(LogRecord.class) -@SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed", - "org.onap.dmaap.datarouter.provisioning.beans.Parameters", - "org.onap.dmaap.datarouter.provisioning.beans.NodeClass", - "org.onap.dmaap.datarouter.provisioning.beans.Subscription", - "org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader"}) public class InternalServletTest extends DrServletTestBase { - + private static EntityManagerFactory emf; + private static EntityManager em; private InternalServlet internalServlet; @Mock @@ -78,11 +70,25 @@ public class InternalServletTest extends DrServletTestBase { @Mock private HttpServletResponse response; + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("dr-unit-tests"); + em = emf.createEntityManager(); + System.setProperty( + "org.onap.dmaap.datarouter.provserver.properties", + "src/test/resources/h2Database.properties"); + } + + @AfterClass + public static void tearDownClass() { + em.clear(); + em.close(); + emf.close(); + } + @Before public void setUp() throws Exception { - super.setUp(); internalServlet = new InternalServlet(); - setAuthoriserToReturnRequestIsAuthorized(); setUpValidAuthorisedRequest(); } @@ -90,8 +96,6 @@ public class InternalServletTest extends DrServletTestBase { public void Given_Request_Is_HTTP_GET_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { when(request.getRemoteAddr()).thenReturn("127.100.0.3"); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); - internalServlet.doGet(request, response); verify(response) .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); @@ -108,7 +112,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Request_Succeeds() throws Exception { + public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/halt"); when(request.isSecure()).thenReturn(false); when(request.getRemoteAddr()).thenReturn("127.0.0.1"); @@ -117,7 +121,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Request_Succeeds() + public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/fetchProv"); when(request.isSecure()).thenReturn(false); @@ -126,7 +130,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Request_Succeeds() throws Exception { + public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/prov"); when(request.getQueryString()).thenReturn(null); setPokerToNotCreateTimers(); @@ -137,7 +141,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Request_Succeeds() throws Exception { + public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/logs/"); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); @@ -146,7 +150,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Request_Succeeds() + public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/logs/TestFile"); internalServlet.doGet(request, response); @@ -168,9 +172,8 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Then_Request_Succeeds() throws Exception { + when(request.getPathInfo()).thenReturn("/api/DELIVERY_MAX_RETRY_INTERVAL"); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); internalServlet.doGet(request, response); @@ -178,10 +181,9 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Request_Succeeds() + public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/drlogs/"); - mockLogfileLoader(); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); internalServlet.doGet(request, response); @@ -189,7 +191,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated() + public void Given_Request_Is_HTTP_GET_With_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/incorrect/"); internalServlet.doGet(request, response); @@ -209,13 +211,11 @@ public class InternalServletTest extends DrServletTestBase { @Test public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerSuccess(); setPokerToNotCreateTimers(); - mockProvisioningParametersChanged(); internalServlet.doPut(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } @@ -223,8 +223,7 @@ public class InternalServletTest extends DrServletTestBase { @Test public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_And_Update_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerFailure(); @@ -255,13 +254,11 @@ public class InternalServletTest extends DrServletTestBase { @Test public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerSuccess(); setPokerToNotCreateTimers(); - mockProvisioningParametersChanged(); internalServlet.doDelete(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } @@ -269,8 +266,7 @@ public class InternalServletTest extends DrServletTestBase { @Test public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_And_Delete_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerFailure(); @@ -293,20 +289,17 @@ public class InternalServletTest extends DrServletTestBase { throws Exception { when(request.getRemoteAddr()).thenReturn("127.100.0.3"); internalServlet.doPost(request, response); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); verify(response) .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); } @Test public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(true); + when(request.getPathInfo()).thenReturn("/api/key"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerSuccess(); setPokerToNotCreateTimers(); - mockProvisioningParametersChanged(); internalServlet.doPost(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } @@ -315,7 +308,6 @@ public class InternalServletTest extends DrServletTestBase { public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_And_Insert_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(true); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerFailure(); @@ -352,7 +344,6 @@ public class InternalServletTest extends DrServletTestBase { File testDir = new File("unit-test-logs/spool"); testDir.mkdirs(); testDir.deleteOnExit(); - mockLogfileLoader(); internalServlet.doPost(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_CREATED)); } @@ -386,14 +377,6 @@ public class InternalServletTest extends DrServletTestBase { .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } - private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException { - AuthorizationResponse authResponse = mock(AuthorizationResponse.class); - Authorizer authorizer = mock(Authorizer.class); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true); - when(authorizer.decide(request)).thenReturn(authResponse); - when(authResponse.isAuthorized()).thenReturn(true); - } - private void setUpValidAuthorisedRequest() throws Exception { setUpValidSecurityOnHttpRequest(); setBehalfHeader("Stub_Value"); @@ -424,16 +407,6 @@ public class InternalServletTest extends DrServletTestBase { FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true); } - private void setParametersToNotContactDb(boolean isPost) { - PowerMockito.mockStatic(Parameters.class); - Parameters parameters = mock(Parameters.class); - if (isPost) { - PowerMockito.when(Parameters.getParameter(anyString())).thenReturn(null); - } else { - PowerMockito.when(Parameters.getParameter(anyString())).thenReturn(parameters); - } - } - private InternalServlet internalServerSuccess() { InternalServlet internalServlet = new InternalServlet() { @@ -469,20 +442,4 @@ public class InternalServletTest extends DrServletTestBase { }; return internalServlet; } - - private void mockProvisioningParametersChanged() throws IllegalAccessException { - PowerMockito.mockStatic(Feed.class); - PowerMockito.mockStatic(Subscription.class); - PowerMockito.when(Feed.countActiveFeeds()).thenReturn(0); - PowerMockito.when(Subscription.countActiveSubscriptions()).thenReturn(0); - Map<String, Integer> map = new HashMap<>(); - FieldUtils.writeDeclaredStaticField(NodeClass.class, "map", map, true); - } - - private void mockLogfileLoader() { - PowerMockito.mockStatic(LogfileLoader.class); - LogfileLoader logfileLoader = mock(LogfileLoader.class); - when(logfileLoader.getBitSet()).thenReturn(new RLEBitSet()); - PowerMockito.when(LogfileLoader.getLoader()).thenReturn(logfileLoader); - } } diff --git a/datarouter-prov/src/test/resources/h2Database.properties b/datarouter-prov/src/test/resources/h2Database.properties index 11f13810..336af0e2 100755 --- a/datarouter-prov/src/test/resources/h2Database.properties +++ b/datarouter-prov/src/test/resources/h2Database.properties @@ -25,4 +25,6 @@ org.onap.dmaap.datarouter.db.driver = org.h2.Driver org.onap.dmaap.datarouter.db.url = jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 org.onap.dmaap.datarouter.provserver.isaddressauthenabled = true -org.onap.dmaap.datarouter.provserver.https.relaxation = false
\ No newline at end of file +org.onap.dmaap.datarouter.provserver.https.relaxation = false +org.onap.dmaap.datarouter.provserver.accesslog.dir = unit-test-logs +org.onap.dmaap.datarouter.provserver.spooldir = unit-test-logs/spool
\ No newline at end of file |