aboutsummaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
authorTomek Kaminski <tomasz.kaminski@nokia.com>2019-03-21 12:34:02 +0100
committerTomek Kaminski <tomasz.kaminski@nokia.com>2019-03-25 09:13:32 +0100
commited283717c1d63873fd93cb5c5446cf44ca93d018 (patch)
treefb999d8ef9cea2daf62b3614a9504685851d354e /src/main
parent31cbc922a7971a5f20d5790a1fbbd6b38a49af13 (diff)
Fix Resources Jersey tests
-fix for tests: DR_SubResourceTest.DelTest DR_SubResourceTest.PutTest MR_ClusterResourceTest.DelTest TopicResourceTest.DelTest -DataBaseClass refactor -Rewriten tests for DR_SubResource Change-Id: I484ad1b18504e0eacc1aae4d8e3b6773e075fa63 Issue-ID: DMAAP-1117 Signed-off-by: Tomek Kaminski <tomasz.kaminski@nokia.com>
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/onap/dmaap/dbcapi/database/DatabaseClass.java437
-rw-r--r--src/main/java/org/onap/dmaap/dbcapi/model/DR_Sub.java47
-rw-r--r--src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java12
-rw-r--r--src/main/java/org/onap/dmaap/dbcapi/service/DR_SubService.java4
4 files changed, 301 insertions, 199 deletions
diff --git a/src/main/java/org/onap/dmaap/dbcapi/database/DatabaseClass.java b/src/main/java/org/onap/dmaap/dbcapi/database/DatabaseClass.java
index e2004bd..c853782 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/database/DatabaseClass.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/database/DatabaseClass.java
@@ -32,201 +32,252 @@ import org.onap.dmaap.dbcapi.util.DmaapConfig;
import org.onap.dmaap.dbcapi.util.Singleton;
+public class DatabaseClass extends BaseLoggingClass {
+ private static Singleton<Dmaap> dmaap;
+ private static Map<String, DcaeLocation> dcaeLocations;
+ private static Map<String, DR_Node> dr_nodes;
+ private static Map<String, DR_Pub> dr_pubs;
+ private static Map<String, DR_Sub> dr_subs;
+ private static Map<String, MR_Client> mr_clients;
+ private static Map<String, MR_Cluster> mr_clusters;
+ private static Map<String, Feed> feeds;
+ private static Map<String, Topic> topics;
+ private static Map<String, MirrorMaker> mirrors;
-public class DatabaseClass extends BaseLoggingClass {
-
- private static Singleton<Dmaap> dmaap;
- private static Map<String, DcaeLocation> dcaeLocations;
- private static Map<String, DR_Node> dr_nodes;
- private static Map<String, DR_Pub> dr_pubs;
- private static Map<String, DR_Sub> dr_subs;
- private static Map<String, MR_Client> mr_clients;
- private static Map<String, MR_Cluster> mr_clusters;
- private static Map<String, Feed> feeds;
- private static Map<String, Topic> topics;
- private static Map<String, MirrorMaker> mirrors;
-
- private static long lastTime = 0L;
-
-
-
- private static class MirrorTopicsHandler implements DBFieldHandler.SqlOp {
- public Object get(ResultSet rs, int index) throws Exception {
- String val = rs.getString(index);
- if (val == null) {
- return(null);
- }
- List<String> rv = new ArrayList<>();
- for (String s: val.split(",")) {
- rv.add(new String(s));
- }
- return(rv);
- }
- public void set(PreparedStatement ps, int index, Object val) throws Exception {
- if (val == null) {
- ps.setString(index, null);
- return;
- }
- @SuppressWarnings("unchecked")
- List<String> xv = (List<String>)val;
- StringBuilder sb = new StringBuilder();
- String sep = "";
- for (Object o: xv) {
- String rv = (String)o;
- sb.append(sep).append(DBFieldHandler.fesc(rv));
- sep = ",";
- }
- ps.setString(index, sb.toString());
- }
- }
- private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
- public Object get(ResultSet rs, int index) throws Exception {
- int val = rs.getInt(index);
-
- return (ReplicationType.valueOf(val));
- }
- public void set(PreparedStatement ps, int index, Object val) throws Exception {
- if (val == null) {
- ps.setInt(index, 0);
- return;
- }
- @SuppressWarnings("unchecked")
- ReplicationType rep = (ReplicationType) val;
- ps.setInt(index, rep.getValue());
- }
- }
- public static Singleton<Dmaap> getDmaap() {
- return dmaap;
- }
-
-
-
- public static Map<String, DcaeLocation> getDcaeLocations() {
- return dcaeLocations;
- }
-
- public static Map<String, DR_Node> getDr_nodes() {
- return dr_nodes;
- }
-
- public static Map<String, DR_Sub> getDr_subs() {
- return dr_subs;
- }
- public static Map<String, DR_Pub> getDr_pubs() {
- return dr_pubs;
- }
-
- public static Map<String, MR_Client> getMr_clients() {
- return mr_clients;
- }
-
-
- public static Map<String, MR_Cluster> getMr_clusters() {
- return mr_clusters;
- }
-
- public static Map<String, Feed> getFeeds() {
- return feeds;
- }
- public static Map<String, Topic> getTopics() {
- return topics;
- }
- public static Map<String, MirrorMaker> getMirrorMakers() {
- return mirrors;
- }
-
- static {
- try {
- appLogger.info( "begin static initialization");
- appLogger.info( "initializing dmaap" );
- DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
- if ("true".equalsIgnoreCase(p.getProperty("UsePGSQL", "false"))) {
- appLogger.info("Data from database");
- try {
- LoadSchema.upgrade();
- } catch (Exception e) {
- appLogger.warn("Problem updating DB schema", e);
- }
- try {
- dmaap = new DBSingleton<>(Dmaap.class, "dmaap");
- dcaeLocations = new DBMap<>(DcaeLocation.class, "dcae_location", "dcae_location_name");
- dr_nodes = new DBMap<>(DR_Node.class, "dr_node", "fqdn");
- dr_pubs = new DBMap<>(DR_Pub.class, "dr_pub", "pub_id");
- dr_subs = new DBMap<>(DR_Sub.class, "dr_sub", "sub_id");
- mr_clients = new DBMap<>(MR_Client.class, "mr_client", "mr_client_id");
- mr_clusters = new DBMap<>(MR_Cluster.class, "mr_cluster", "dcae_location_name");
- feeds = new DBMap<>(Feed.class, "feed", "feed_id");
- TableHandler.setSpecialCase("topic", "replication_case", new TopicReplicationTypeHandler());
- topics = new DBMap<>(Topic.class, "topic", "fqtn");
- TableHandler.setSpecialCase("mirror_maker", "topics", new MirrorTopicsHandler());
- mirrors = new DBMap<>(MirrorMaker.class, "mirror_maker", "mm_name");
- } catch (Exception e) {
- errorLogger.error("Error initializing database access " + e, e);
- System.exit(1);
- }
- } else {
- appLogger.info("Data from memory");
- dmaap = new Singleton<Dmaap>() {
- private Dmaap dmaap;
- public void remove() {
- dmaap = null;
- }
- public void init(Dmaap val) {
- if (dmaap == null) {
- dmaap = val;
- }
- }
- public Dmaap get() {
- return(dmaap);
- }
- public void update(Dmaap nd) {
- dmaap.setVersion(nd.getVersion());
- dmaap.setTopicNsRoot(nd.getTopicNsRoot());
- dmaap.setDmaapName(nd.getDmaapName());
- dmaap.setDrProvUrl(nd.getDrProvUrl());
- dmaap.setBridgeAdminTopic(nd.getBridgeAdminTopic());
- dmaap.setLoggingUrl(nd.getLoggingUrl());
- dmaap.setNodeKey(nd.getNodeKey());
- dmaap.setAccessKeyOwner(nd.getAccessKeyOwner());
- }
- };
- dcaeLocations = new HashMap<>();
- dr_nodes = new HashMap<>();
- dr_pubs = new HashMap<>();
- dr_subs = new HashMap<>();
- mr_clients = new HashMap<>();
- mr_clusters = new HashMap<>();
- feeds = new HashMap<>();
- topics = new HashMap<>();
- mirrors = new HashMap<>();
- }
- dmaap.init(new Dmaap("0", "", "", "", "", "", "", ""));
- // force initial read from DB, if it exists
- @SuppressWarnings("unused")
- Dmaap dmx = dmaap.get();
-
- // old code in this spot would read from properties file as part of init.
- // but all those properties are now set via /dmaap API
-
- } catch (Exception e) {
- errorLogger.error("Error", e);
- errorLogger.error(DmaapbcLogMessageEnum.DB_UPDATE_ERROR, e.getMessage());
- }
-
- }
-
- public static synchronized String getNextClientId() {
-
- long id = System.currentTimeMillis();
- if ( id <= lastTime ) {
- id = lastTime + 1;
- }
- lastTime = id;
- return Long.toString(id);
- }
-
-
+ private static long lastTime = 0L;
+ private static DBType databaseType;
+
+ private enum DBType {
+ PGSQL, MEMORY
+ }
+
+ public static Singleton<Dmaap> getDmaap() {
+ return dmaap;
+ }
+
+
+ public static Map<String, DcaeLocation> getDcaeLocations() {
+ return dcaeLocations;
+ }
+
+ public static Map<String, DR_Node> getDr_nodes() {
+ return dr_nodes;
+ }
+
+ public static Map<String, DR_Sub> getDr_subs() {
+ return dr_subs;
+ }
+
+ public static Map<String, DR_Pub> getDr_pubs() {
+ return dr_pubs;
+ }
+
+ public static Map<String, MR_Client> getMr_clients() {
+ return mr_clients;
+ }
+
+
+ public static Map<String, MR_Cluster> getMr_clusters() {
+ return mr_clusters;
+ }
+
+ public static Map<String, Feed> getFeeds() {
+ return feeds;
+ }
+
+ public static Map<String, Topic> getTopics() {
+ return topics;
+ }
+
+ public static Map<String, MirrorMaker> getMirrorMakers() {
+ return mirrors;
+ }
+
+ static {
+ try {
+ appLogger.info("begin static initialization");
+ appLogger.info("initializing dmaap");
+ determineDatabaseType();
+
+ switch (databaseType) {
+ case PGSQL:
+ databaseResourceInit();
+ break;
+ case MEMORY:
+ inMemoryResourceInit();
+ break;
+ }
+
+ dmaap.init(new Dmaap("0", "", "", "", "", "", "", ""));
+ // force initial read from DB, if it exists
+ @SuppressWarnings("unused")
+ Dmaap dmx = dmaap.get();
+
+ // old code in this spot would read from properties file as part of init.
+ // but all those properties are now set via /dmaap API
+
+ } catch (Exception e) {
+ errorLogger.error("Error", e);
+ errorLogger.error(DmaapbcLogMessageEnum.DB_UPDATE_ERROR, e.getMessage());
+ }
+
+ }
+
+ public static synchronized String getNextClientId() {
+
+ long id = System.currentTimeMillis();
+ if (id <= lastTime) {
+ id = lastTime + 1;
+ }
+ lastTime = id;
+ return Long.toString(id);
+ }
+
+ public static synchronized void clearDatabase() {
+ switch (databaseType) {
+ case PGSQL:
+ try {
+ initDatabase();
+ } catch (Exception e) {
+ errorLogger.error("Error initializing database access " + e, e);
+ }
+ break;
+ case MEMORY:
+ initMemoryDatabase();
+ break;
+ }
+ }
+
+ private static void inMemoryResourceInit() {
+ appLogger.info("Data from memory");
+ dmaap = new Singleton<Dmaap>() {
+ private Dmaap dmaap;
+
+ public void remove() {
+ dmaap = null;
+ }
+
+ public void init(Dmaap val) {
+ if (dmaap == null) {
+ dmaap = val;
+ }
+ }
+
+ public Dmaap get() {
+ return (dmaap);
+ }
+
+ public void update(Dmaap nd) {
+ dmaap.setVersion(nd.getVersion());
+ dmaap.setTopicNsRoot(nd.getTopicNsRoot());
+ dmaap.setDmaapName(nd.getDmaapName());
+ dmaap.setDrProvUrl(nd.getDrProvUrl());
+ dmaap.setBridgeAdminTopic(nd.getBridgeAdminTopic());
+ dmaap.setLoggingUrl(nd.getLoggingUrl());
+ dmaap.setNodeKey(nd.getNodeKey());
+ dmaap.setAccessKeyOwner(nd.getAccessKeyOwner());
+ }
+ };
+ initMemoryDatabase();
+ }
+
+ private static void databaseResourceInit() {
+ appLogger.info("Data from database");
+ try {
+ LoadSchema.upgrade();
+ } catch (Exception e) {
+ appLogger.warn("Problem updating DB schema", e);
+ }
+ try {
+ dmaap = new DBSingleton<>(Dmaap.class, "dmaap");
+ TableHandler.setSpecialCase("topic", "replication_case", new TopicReplicationTypeHandler());
+ TableHandler.setSpecialCase("mirror_maker", "topics", new MirrorTopicsHandler());
+ initDatabase();
+ } catch (Exception e) {
+ errorLogger.error("Error initializing database access " + e, e);
+ System.exit(1);
+ }
+ }
+
+ private static class MirrorTopicsHandler implements DBFieldHandler.SqlOp {
+
+ public Object get(ResultSet rs, int index) throws Exception {
+ String val = rs.getString(index);
+ if (val == null) {
+ return (null);
+ }
+ List<String> rv = new ArrayList<>();
+ for (String s : val.split(",")) {
+ rv.add(new String(s));
+ }
+ return (rv);
+ }
+
+ public void set(PreparedStatement ps, int index, Object val) throws Exception {
+ if (val == null) {
+ ps.setString(index, null);
+ return;
+ }
+ @SuppressWarnings("unchecked")
+ List<String> xv = (List<String>) val;
+ StringBuilder sb = new StringBuilder();
+ String sep = "";
+ for (Object o : xv) {
+ String rv = (String) o;
+ sb.append(sep).append(DBFieldHandler.fesc(rv));
+ sep = ",";
+ }
+ ps.setString(index, sb.toString());
+ }
+ }
+
+ private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
+
+ public Object get(ResultSet rs, int index) throws Exception {
+ int val = rs.getInt(index);
+
+ return (ReplicationType.valueOf(val));
+ }
+
+ public void set(PreparedStatement ps, int index, Object val) throws Exception {
+ if (val == null) {
+ ps.setInt(index, 0);
+ return;
+ }
+ @SuppressWarnings("unchecked")
+ ReplicationType rep = (ReplicationType) val;
+ ps.setInt(index, rep.getValue());
+ }
+ }
+
+ private static void initMemoryDatabase() {
+ dcaeLocations = new HashMap<>();
+ dr_nodes = new HashMap<>();
+ dr_pubs = new HashMap<>();
+ dr_subs = new HashMap<>();
+ mr_clients = new HashMap<>();
+ mr_clusters = new HashMap<>();
+ feeds = new HashMap<>();
+ topics = new HashMap<>();
+ mirrors = new HashMap<>();
+ }
+ private static void initDatabase() throws Exception {
+ dcaeLocations = new DBMap<>(DcaeLocation.class, "dcae_location", "dcae_location_name");
+ dr_nodes = new DBMap<>(DR_Node.class, "dr_node", "fqdn");
+ dr_pubs = new DBMap<>(DR_Pub.class, "dr_pub", "pub_id");
+ dr_subs = new DBMap<>(DR_Sub.class, "dr_sub", "sub_id");
+ mr_clients = new DBMap<>(MR_Client.class, "mr_client", "mr_client_id");
+ mr_clusters = new DBMap<>(MR_Cluster.class, "mr_cluster", "dcae_location_name");
+ feeds = new DBMap<>(Feed.class, "feed", "feed_id");
+ topics = new DBMap<>(Topic.class, "topic", "fqtn");
+ mirrors = new DBMap<>(MirrorMaker.class, "mirror_maker", "mm_name");
+ }
+ private static void determineDatabaseType() {
+ DmaapConfig dmaapConfig = (DmaapConfig) DmaapConfig.getConfig();
+ String isPgSQLset = dmaapConfig.getProperty("UsePGSQL", "false");
+ databaseType = isPgSQLset.equalsIgnoreCase("true") ? DBType.PGSQL : DBType.MEMORY;
+ }
}
diff --git a/src/main/java/org/onap/dmaap/dbcapi/model/DR_Sub.java b/src/main/java/org/onap/dmaap/dbcapi/model/DR_Sub.java
index 9d30021..8ac0880 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/model/DR_Sub.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/model/DR_Sub.java
@@ -331,7 +331,7 @@ public class DR_Sub extends DmaapObject {
@Override
public String toString() {
- String rc = String.format ( "DR_Sub: {dcaeLocationName=%s username=%s userpwd=%s feedId=%s deliveryURL=%s logURL=%s subid=%s use100=%s suspended=%s owner=%s}",
+ return String.format ( "DR_Sub: {dcaeLocationName=%s username=%s userpwd=%s feedId=%s deliveryURL=%s logURL=%s subid=%s use100=%s suspended=%s owner=%s}",
dcaeLocationName,
username,
userpwd,
@@ -343,6 +343,49 @@ public class DR_Sub extends DmaapObject {
suspended,
owner
);
- return rc;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ DR_Sub drSub = (DR_Sub) o;
+
+ if (use100 != drSub.use100) {
+ return false;
+ }
+ if (suspended != drSub.suspended) {
+ return false;
+ }
+ if (!dcaeLocationName.equals(drSub.dcaeLocationName)) {
+ return false;
+ }
+ if (!username.equals(drSub.username)) {
+ return false;
+ }
+ if (!userpwd.equals(drSub.userpwd)) {
+ return false;
+ }
+ if (!feedId.equals(drSub.feedId)) {
+ return false;
+ }
+ return subId.equals(drSub.subId);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = dcaeLocationName.hashCode();
+ result = 31 * result + username.hashCode();
+ result = 31 * result + userpwd.hashCode();
+ result = 31 * result + feedId.hashCode();
+ result = 31 * result + subId.hashCode();
+ result = 31 * result + (use100 ? 1 : 0);
+ result = 31 * result + (suspended ? 1 : 0);
+ return result;
}
}
diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java
index b74d5a1..d5a96de 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java
@@ -22,7 +22,9 @@
package org.onap.dmaap.dbcapi.resources;
+import com.google.common.collect.Iterables;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import javax.ws.rs.Consumes;
@@ -107,11 +109,17 @@ public class DR_SubResource extends BaseLoggingClass {
}
// if we found a FeedName instead of a FeedId then try to look it up.
List<Feed> nfeeds = feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals");
- if ( nfeeds.size() != 1 ) {
+ if ( nfeeds.isEmpty() ) {
+ resp.setCode(Status.NOT_FOUND.getStatusCode());
+ resp.setFields("feedName");
+ return responseBuilder.error(resp.getErr());
+ } else if (nfeeds.size() > 1) {
logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() );
+ resp.setCode(Status.CONFLICT.getStatusCode());
+ resp.setFields("feedName");
return responseBuilder.error(resp.getErr());
}
- fnew = nfeeds.get(0);
+ fnew = Iterables.getOnlyElement(nfeeds);
}
try {
diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/DR_SubService.java b/src/main/java/org/onap/dmaap/dbcapi/service/DR_SubService.java
index 9d233a5..c14b1fd 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/service/DR_SubService.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/service/DR_SubService.java
@@ -209,8 +209,8 @@ public class DR_SubService extends BaseLoggingClass {
server, subid, sub.getUsername(), sub.getUserpwd());
String links = String.format( "\"links\": {\"feed\": \"https://dr-prov/feedlog/%s\", \"self\": \"https://dr-prov/sub/%s\", \"log\": \"https://dr-prov/sublog/%s\" }",
sub.getFeedId(),
- sub.getSubId(),
- sub.getSubId() );
+ subid,
+ subid );
ret += links + "}";
logger.info( "DR_SubService:simulateResp=" + ret);