From 8506f0b261083e945488e869803bcd5ab8b7ea84 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Wed, 13 Mar 2019 21:02:14 +0000 Subject: add message router publisher initial api and implementation of message router publisher Change-Id: Ic69d013bae8ab8c842e6ecd2eef41e10649d1009 Issue-ID: CCSDK-1163 Signed-off-by: Smokowski, Kevin (ks6305) --- .../publisher/client/impl/ClientImpl.java | 33 ++++++++++++++++++++++ .../resources/OSGI-INF/blueprint/blueprint.xml | 16 +++++++++++ 2 files changed, 49 insertions(+) create mode 100755 message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java create mode 100755 message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml (limited to 'message-router/publisher/sample.client/src') diff --git a/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java b/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java new file mode 100755 index 000000000..dbf49fe94 --- /dev/null +++ b/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java @@ -0,0 +1,33 @@ +package org.onap.ccsdk.messagerouter.publisher.client.impl; + +import org.onap.ccsdk.messagerouter.publisher.api.PublisherApi; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClientImpl { + private static final Logger logger = LoggerFactory.getLogger(ClientImpl.class); + private String topic; + private PublisherApi publisher; + + public void setPublisher(PublisherApi publisherApi) { + this.publisher = publisherApi; + } + + public void setTopic(String topic) { + this.topic = topic; + } + + public ClientImpl() { + + } + + public void init() { + for (int i = 0; i < 5; i++) { + String body = "{\"hello\":\"world " + String.valueOf(Math.random()) + "\"}"; + logger.error("Loop iteration " + i + " sending body " + body + " to the topic " + topic); + Boolean result = publisher.publish(topic, body); + logger.error("Loop iteration " + i + " returned the boolean value " + result); + } + } + +} \ No newline at end of file diff --git a/message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100755 index 000000000..c44a68adc --- /dev/null +++ b/message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + \ No newline at end of file -- cgit 1.2.3-korg From 6a91fa496b5464a9f96b43851968eee4c837d80c Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Thu, 28 Mar 2019 14:45:52 +0000 Subject: message router publisher changes resolve message router publisher namespace issues Resolve issues loading message-router in odlsli container Change-Id: Ib36bced43129c735c61d5ff7bb0cf29362bb7779 Issue-ID: CCSDK-1186 Signed-off-by: Smokowski, Kevin (ks6305) --- features/ccsdk-sli-adaptors-all/pom.xml | 2 +- .../messagerouter/publisher/api/PublisherApi.java | 5 - .../messagerouter/publisher/api/PublisherApi.java | 5 + .../features/ccsdk-messagerouter-publisher/pom.xml | 30 +++++ .../features-messagerouter-publisher/pom.xml | 33 +++++ message-router/publisher/features/pom.xml | 81 ++++------- message-router/publisher/installer/pom.xml | 2 +- .../publisher/provider/impl/PublisherApiImpl.java | 149 --------------------- .../publisher/provider/impl/PublisherApiImpl.java | 149 +++++++++++++++++++++ .../resources/OSGI-INF/blueprint/blueprint.xml | 6 +- .../publisher/client/impl/ClientImpl.java | 33 ----- .../publisher/client/impl/ClientImpl.java | 33 +++++ .../resources/OSGI-INF/blueprint/blueprint.xml | 6 +- 13 files changed, 286 insertions(+), 248 deletions(-) delete mode 100755 message-router/publisher/api/src/main/java/org/onap/ccsdk/messagerouter/publisher/api/PublisherApi.java create mode 100755 message-router/publisher/api/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/api/PublisherApi.java create mode 100755 message-router/publisher/features/ccsdk-messagerouter-publisher/pom.xml create mode 100755 message-router/publisher/features/features-messagerouter-publisher/pom.xml mode change 100755 => 100644 message-router/publisher/features/pom.xml delete mode 100755 message-router/publisher/provider/src/main/java/org/onap/ccsdk/messagerouter/publisher/provider/impl/PublisherApiImpl.java create mode 100755 message-router/publisher/provider/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImpl.java delete mode 100755 message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java create mode 100755 message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java (limited to 'message-router/publisher/sample.client/src') diff --git a/features/ccsdk-sli-adaptors-all/pom.xml b/features/ccsdk-sli-adaptors-all/pom.xml index 59966087a..e68a59bfc 100644 --- a/features/ccsdk-sli-adaptors-all/pom.xml +++ b/features/ccsdk-sli-adaptors-all/pom.xml @@ -82,7 +82,7 @@ ${project.groupId}.messagerouter - publisher.features + ccsdk-messagerouter-publisher ${project.version} xml features diff --git a/message-router/publisher/api/src/main/java/org/onap/ccsdk/messagerouter/publisher/api/PublisherApi.java b/message-router/publisher/api/src/main/java/org/onap/ccsdk/messagerouter/publisher/api/PublisherApi.java deleted file mode 100755 index 6d4bddcde..000000000 --- a/message-router/publisher/api/src/main/java/org/onap/ccsdk/messagerouter/publisher/api/PublisherApi.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.onap.ccsdk.messagerouter.publisher.api; - -public interface PublisherApi { - public Boolean publish(String topic, String body); -} diff --git a/message-router/publisher/api/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/api/PublisherApi.java b/message-router/publisher/api/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/api/PublisherApi.java new file mode 100755 index 000000000..3551e0441 --- /dev/null +++ b/message-router/publisher/api/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/api/PublisherApi.java @@ -0,0 +1,5 @@ +package org.onap.ccsdk.sli.adaptors.messagerouter.publisher.api; + +public interface PublisherApi { + public Boolean publish(String topic, String body); +} diff --git a/message-router/publisher/features/ccsdk-messagerouter-publisher/pom.xml b/message-router/publisher/features/ccsdk-messagerouter-publisher/pom.xml new file mode 100755 index 000000000..939fb7a77 --- /dev/null +++ b/message-router/publisher/features/ccsdk-messagerouter-publisher/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + single-feature-parent + 1.2.2-SNAPSHOT + + + org.onap.ccsdk.sli.adaptors.messagerouter + ccsdk-messagerouter-publisher + 0.4.2-SNAPSHOT + feature + + + + ${project.groupId} + publisher.api + ${project.version} + + + ${project.groupId} + publisher.provider + ${project.version} + + + + + diff --git a/message-router/publisher/features/features-messagerouter-publisher/pom.xml b/message-router/publisher/features/features-messagerouter-publisher/pom.xml new file mode 100755 index 000000000..eae146702 --- /dev/null +++ b/message-router/publisher/features/features-messagerouter-publisher/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + feature-repo-parent + 1.2.2-SNAPSHOT + + + + org.onap.ccsdk.sli.adaptors.messagerouter + features-messagerouter-publisher + 0.4.2-SNAPSHOT + feature + + ccsdk-sli-adaptors :: messagerouter :: ${project.artifactId} + + + ${project.version} + + + + + ${project.groupId} + ccsdk-messagerouter-publisher + ${project.version} + xml + features + + + + diff --git a/message-router/publisher/features/pom.xml b/message-router/publisher/features/pom.xml old mode 100755 new mode 100644 index 7b1c7bed6..45e1f5d86 --- a/message-router/publisher/features/pom.xml +++ b/message-router/publisher/features/pom.xml @@ -1,58 +1,33 @@ + - 4.0.0 + 4.0.0 + + odlparent-lite + org.onap.ccsdk.parent + 1.2.2-SNAPSHOT + + + org.onap.ccsdk.sli.adaptors.messagerouter + publisher.features + 0.4.2-SNAPSHOT + ccsdk-sli-adaptors :: messagerouter :: ${project.artifactId} - - org.onap.ccsdk.sli.adaptors.messagerouter - publisher.aggregate - 0.4.2-SNAPSHOT - + pom - publisher.features - pom - - - - - src/main/feature - true - ${project.build.directory}/feature - - - - - org.apache.maven.plugins - maven-resources-plugin - - - - resources - - - - - - org.codehaus.mojo - build-helper-maven-plugin - - - attach-artifacts - package - - attach-artifact - - - - - target/feature/feature.xml - xml - features - - - - - - - - + + ccsdk-messagerouter-publisher + features-messagerouter-publisher + diff --git a/message-router/publisher/installer/pom.xml b/message-router/publisher/installer/pom.xml index c2c148196..8a8dc411b 100644 --- a/message-router/publisher/installer/pom.xml +++ b/message-router/publisher/installer/pom.xml @@ -23,7 +23,7 @@ ${project.groupId} - publisher.features + ccsdk-messagerouter-publisher ${project.version} xml features diff --git a/message-router/publisher/provider/src/main/java/org/onap/ccsdk/messagerouter/publisher/provider/impl/PublisherApiImpl.java b/message-router/publisher/provider/src/main/java/org/onap/ccsdk/messagerouter/publisher/provider/impl/PublisherApiImpl.java deleted file mode 100755 index 3e8ab3333..000000000 --- a/message-router/publisher/provider/src/main/java/org/onap/ccsdk/messagerouter/publisher/provider/impl/PublisherApiImpl.java +++ /dev/null @@ -1,149 +0,0 @@ -package org.onap.ccsdk.messagerouter.publisher.provider.impl; - -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.SocketException; -import java.net.URL; -import java.util.Base64; - -import org.onap.ccsdk.messagerouter.publisher.api.PublisherApi; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class PublisherApiImpl implements PublisherApi { - private static final Logger logger = LoggerFactory.getLogger(PublisherApiImpl.class); - protected final Integer DEFAULT_CONNECT_TIMEOUT = 30000; // will be treated as 30 seconds - protected final Integer DEFAULT_READ_TIMEOUT = 180000; // will be treated as 3 minutes - private String authorizationString; - protected Integer connectTimeout; - protected Integer readTimeout; - protected String baseUrl; - protected String username; - protected String[] hosts; - private String password; - - public void setUsername(String username) { - this.username = username; - buildAuthorizationString(); - } - - public void setPassword(String password) { - this.password = password; - buildAuthorizationString(); - } - - public void setHost(String hostString) { - // a comma separated list of hosts can be passed in or a single host may be used - if (!hostString.contains(",")) { - this.hosts = new String[] { hostString }; - } else { - this.hosts = hostString.split(","); - } - } - - public PublisherApiImpl() { - connectTimeout = DEFAULT_CONNECT_TIMEOUT; - readTimeout = DEFAULT_READ_TIMEOUT; - } - - public void init() { - buildAuthorizationString(); - } - - private String buildUrlString(Integer hostIndex, String topic) { - return hosts[hostIndex] + "/events/" + topic; - } - - protected void configureHttpURLConnection(HttpURLConnection httpUrlConnection) { - httpUrlConnection.setRequestProperty("Content-Type", "application/json"); - } - - public Boolean publish(String topic, String body) { - for (int hostIndex = 0; hostIndex < hosts.length; hostIndex++) { - HttpURLConnection httpUrlConnection = null; - URL url = null; - try { - url = new URL(buildUrlString(hostIndex, topic)); - logger.info("Publishing body to topic {} using the url {}", topic, url); - logger.info("Message to publish is\n{}", body); - httpUrlConnection = buildHttpURLConnection(url); - httpUrlConnection.setDoInput(true); - httpUrlConnection.setDoOutput(true); - httpUrlConnection.setUseCaches(false); - httpUrlConnection.setRequestMethod("POST"); - - // Write message - httpUrlConnection.setRequestProperty("Content-Length", Integer.toString(body.length())); - DataOutputStream outStr = new DataOutputStream(httpUrlConnection.getOutputStream()); - outStr.write(body.getBytes()); - outStr.close(); - - int status = httpUrlConnection.getResponseCode(); - logger.info("Publishing body for topic {} using url {} returned status {}.", topic, url, status); - if (status < 300) { - String responseFromDMaaP = readFromStream(httpUrlConnection.getInputStream()); - logger.info("Message router response is\n{}", responseFromDMaaP); - return true; - } else { - if (httpUrlConnection.getErrorStream() != null) { - String responseFromDMaaP = readFromStream(httpUrlConnection.getErrorStream()); - logger.warn("Publishing body for topic {} using url {} failed." + " Error message is\n{}", - topic, url, responseFromDMaaP); - } - return false; - } - - } catch (SocketException socketException) { - logger.error("SocketException was thrown during publishing message to DMaaP on url {}.", url, - socketException); - if (hostIndex < hosts.length) { - logger.info("Message sent to {} failed with a SocketException, but will be tried on {}", - hosts[hostIndex], hosts[hostIndex + 1]); - } - } catch (Exception e) { - logger.warn("Exception was thrown during publishing message to DMaaP on url {}.", url, e); - return false; - } finally { - if (httpUrlConnection != null) { - httpUrlConnection.disconnect(); - } - } - } - return false; - } - - private void buildAuthorizationString() { - String basicAuthString = username + ":" + password; - basicAuthString = Base64.getEncoder().encodeToString(basicAuthString.getBytes()); - this.authorizationString = "Basic " + basicAuthString; - } - - protected HttpURLConnection buildHttpURLConnection(URL url) throws IOException { - HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(); - if (authorizationString != null) { - httpUrlConnection.setRequestProperty("Authorization", authorizationString); - } - httpUrlConnection.setRequestProperty("Accept", "application/json"); - httpUrlConnection.setUseCaches(false); - httpUrlConnection.setConnectTimeout(connectTimeout); - httpUrlConnection.setReadTimeout(readTimeout); - configureHttpURLConnection(httpUrlConnection); - return httpUrlConnection; - } - - protected String readFromStream(InputStream stream) throws IOException { - BufferedReader br = new BufferedReader(new InputStreamReader(stream)); - StringBuilder sb = new StringBuilder(); - String line; - while ((line = br.readLine()) != null) { - sb.append(line); - } - br.close(); - return sb.toString(); - } - -} \ No newline at end of file diff --git a/message-router/publisher/provider/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImpl.java b/message-router/publisher/provider/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImpl.java new file mode 100755 index 000000000..d88dc66b4 --- /dev/null +++ b/message-router/publisher/provider/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImpl.java @@ -0,0 +1,149 @@ +package org.onap.ccsdk.sli.adaptors.messagerouter.publisher.provider.impl; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.SocketException; +import java.net.URL; +import java.util.Base64; + +import org.onap.ccsdk.sli.adaptors.messagerouter.publisher.api.PublisherApi; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PublisherApiImpl implements PublisherApi { + private static final Logger logger = LoggerFactory.getLogger(PublisherApiImpl.class); + protected final Integer DEFAULT_CONNECT_TIMEOUT = 30000; // will be treated as 30 seconds + protected final Integer DEFAULT_READ_TIMEOUT = 180000; // will be treated as 3 minutes + private String authorizationString; + protected Integer connectTimeout; + protected Integer readTimeout; + protected String baseUrl; + protected String username; + protected String[] hosts; + private String password; + + public void setUsername(String username) { + this.username = username; + buildAuthorizationString(); + } + + public void setPassword(String password) { + this.password = password; + buildAuthorizationString(); + } + + public void setHost(String hostString) { + // a comma separated list of hosts can be passed in or a single host may be used + if (!hostString.contains(",")) { + this.hosts = new String[] { hostString }; + } else { + this.hosts = hostString.split(","); + } + } + + public PublisherApiImpl() { + connectTimeout = DEFAULT_CONNECT_TIMEOUT; + readTimeout = DEFAULT_READ_TIMEOUT; + } + + public void init() { + buildAuthorizationString(); + } + + private String buildUrlString(Integer hostIndex, String topic) { + return hosts[hostIndex] + "/events/" + topic; + } + + protected void configureHttpURLConnection(HttpURLConnection httpUrlConnection) { + httpUrlConnection.setRequestProperty("Content-Type", "application/json"); + } + + public Boolean publish(String topic, String body) { + for (int hostIndex = 0; hostIndex < hosts.length; hostIndex++) { + HttpURLConnection httpUrlConnection = null; + URL url = null; + try { + url = new URL(buildUrlString(hostIndex, topic)); + logger.info("Publishing body to topic {} using the url {}", topic, url); + logger.info("Message to publish is\n{}", body); + httpUrlConnection = buildHttpURLConnection(url); + httpUrlConnection.setDoInput(true); + httpUrlConnection.setDoOutput(true); + httpUrlConnection.setUseCaches(false); + httpUrlConnection.setRequestMethod("POST"); + + // Write message + httpUrlConnection.setRequestProperty("Content-Length", Integer.toString(body.length())); + DataOutputStream outStr = new DataOutputStream(httpUrlConnection.getOutputStream()); + outStr.write(body.getBytes()); + outStr.close(); + + int status = httpUrlConnection.getResponseCode(); + logger.info("Publishing body for topic {} using url {} returned status {}.", topic, url, status); + if (status < 300) { + String responseFromDMaaP = readFromStream(httpUrlConnection.getInputStream()); + logger.info("Message router response is\n{}", responseFromDMaaP); + return true; + } else { + if (httpUrlConnection.getErrorStream() != null) { + String responseFromDMaaP = readFromStream(httpUrlConnection.getErrorStream()); + logger.warn("Publishing body for topic {} using url {} failed." + " Error message is\n{}", + topic, url, responseFromDMaaP); + } + return false; + } + + } catch (SocketException socketException) { + logger.error("SocketException was thrown during publishing message to DMaaP on url {}.", url, + socketException); + if (hostIndex < hosts.length) { + logger.info("Message sent to {} failed with a SocketException, but will be tried on {}", + hosts[hostIndex], hosts[hostIndex + 1]); + } + } catch (Exception e) { + logger.warn("Exception was thrown during publishing message to DMaaP on url {}.", url, e); + return false; + } finally { + if (httpUrlConnection != null) { + httpUrlConnection.disconnect(); + } + } + } + return false; + } + + private void buildAuthorizationString() { + String basicAuthString = username + ":" + password; + basicAuthString = Base64.getEncoder().encodeToString(basicAuthString.getBytes()); + this.authorizationString = "Basic " + basicAuthString; + } + + protected HttpURLConnection buildHttpURLConnection(URL url) throws IOException { + HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(); + if (authorizationString != null) { + httpUrlConnection.setRequestProperty("Authorization", authorizationString); + } + httpUrlConnection.setRequestProperty("Accept", "application/json"); + httpUrlConnection.setUseCaches(false); + httpUrlConnection.setConnectTimeout(connectTimeout); + httpUrlConnection.setReadTimeout(readTimeout); + configureHttpURLConnection(httpUrlConnection); + return httpUrlConnection; + } + + protected String readFromStream(InputStream stream) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(stream)); + StringBuilder sb = new StringBuilder(); + String line; + while ((line = br.readLine()) != null) { + sb.append(line); + } + br.close(); + return sb.toString(); + } + +} \ No newline at end of file diff --git a/message-router/publisher/provider/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/message-router/publisher/provider/src/main/resources/OSGI-INF/blueprint/blueprint.xml index da25fd23d..4702a6260 100755 --- a/message-router/publisher/provider/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/message-router/publisher/provider/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -3,12 +3,12 @@ xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0"> + class="org.onap.ccsdk.sli.adaptors.messagerouter.publisher.provider.impl.PublisherApiImpl"> - + \ No newline at end of file diff --git a/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java b/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java deleted file mode 100755 index dbf49fe94..000000000 --- a/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.onap.ccsdk.messagerouter.publisher.client.impl; - -import org.onap.ccsdk.messagerouter.publisher.api.PublisherApi; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ClientImpl { - private static final Logger logger = LoggerFactory.getLogger(ClientImpl.class); - private String topic; - private PublisherApi publisher; - - public void setPublisher(PublisherApi publisherApi) { - this.publisher = publisherApi; - } - - public void setTopic(String topic) { - this.topic = topic; - } - - public ClientImpl() { - - } - - public void init() { - for (int i = 0; i < 5; i++) { - String body = "{\"hello\":\"world " + String.valueOf(Math.random()) + "\"}"; - logger.error("Loop iteration " + i + " sending body " + body + " to the topic " + topic); - Boolean result = publisher.publish(topic, body); - logger.error("Loop iteration " + i + " returned the boolean value " + result); - } - } - -} \ No newline at end of file diff --git a/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java b/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java new file mode 100755 index 000000000..25fc87ead --- /dev/null +++ b/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java @@ -0,0 +1,33 @@ +package org.onap.ccsdk.sli.adaptors.messagerouter.publisher.client.impl; + +import org.onap.ccsdk.sli.adaptors.messagerouter.publisher.api.PublisherApi; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClientImpl { + private static final Logger logger = LoggerFactory.getLogger(ClientImpl.class); + private String topic; + private PublisherApi publisher; + + public void setPublisher(PublisherApi publisherApi) { + this.publisher = publisherApi; + } + + public void setTopic(String topic) { + this.topic = topic; + } + + public ClientImpl() { + + } + + public void init() { + for (int i = 0; i < 5; i++) { + String body = "{\"hello\":\"world " + String.valueOf(Math.random()) + "\"}"; + logger.error("Loop iteration " + i + " sending body " + body + " to the topic " + topic); + Boolean result = publisher.publish(topic, body); + logger.error("Loop iteration " + i + " returned the boolean value " + result); + } + } + +} \ No newline at end of file diff --git a/message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml index c44a68adc..c2e981da2 100755 --- a/message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -3,14 +3,14 @@ xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0"> - + \ No newline at end of file -- cgit 1.2.3-korg From dd48a8a5083e3a65c8b47ce45ce7517afc47c1ff Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Fri, 26 Jul 2019 20:24:59 +0000 Subject: Improve code coverage in sli adaptors increase code coverage in message router in sli adaptors Issue-ID: CCSDK-1548 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: I64f0e025a759b219ce4c7e017b500952d1c7f30b --- .../provider/impl/AbstractBaseConsumerTest.java | 90 +++++++++++ .../provider/impl/ConsumerFactoryTest.java | 164 +++++++++++++++++++++ .../publisher/provider/impl/PublisherApiImpl.java | 23 ++- .../provider/impl/PublisherApiImplTest.java | 51 +++++++ .../publisher/client/impl/ClientImplTest.java | 29 ++++ 5 files changed, 349 insertions(+), 8 deletions(-) create mode 100644 message-router/consumer/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/consumer/provider/impl/AbstractBaseConsumerTest.java create mode 100644 message-router/consumer/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/consumer/provider/impl/ConsumerFactoryTest.java create mode 100644 message-router/publisher/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImplTest.java create mode 100644 message-router/publisher/sample.client/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImplTest.java (limited to 'message-router/publisher/sample.client/src') diff --git a/message-router/consumer/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/consumer/provider/impl/AbstractBaseConsumerTest.java b/message-router/consumer/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/consumer/provider/impl/AbstractBaseConsumerTest.java new file mode 100644 index 000000000..99a4f3ff7 --- /dev/null +++ b/message-router/consumer/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/consumer/provider/impl/AbstractBaseConsumerTest.java @@ -0,0 +1,90 @@ +package org.onap.ccsdk.sli.adaptors.messagerouter.consumer.provider.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.net.HttpURLConnection; +import java.net.URL; + +import org.junit.Test; +import org.onap.ccsdk.sli.adaptors.messagerouter.consumer.api.RequestHandler; + +public class AbstractBaseConsumerTest { + private class DummyConsumer extends AbstractBaseConsumer { + + public DummyConsumer(String username, String password, String host, String authentication, Integer connectTimeout, Integer readTimeout, String group, String id, String filter, Integer limit, Integer timeoutQueryParamValue) { + super(username, password, host, authentication, connectTimeout, readTimeout, group, id, filter, limit, timeoutQueryParamValue); + } + + } + + public DummyConsumer getAuthDummy() { + String username = "deadpool"; + String password = "notSECURE"; + String host = "http://localhost:7001"; + String group = "myCluster"; + String id = "node1"; + Integer connectTimeout = 10000; + Integer readTimeout = 20000; + String authentication = "basic"; + String filter = null; + Integer limit = 3; + Integer timeoutQueryParamValue = 5000; + return new DummyConsumer(username, password, host, authentication, connectTimeout, readTimeout, group, id, filter, limit, timeoutQueryParamValue); + } + + @Test + public void createDummyWithAuth() { + assertNotNull(getAuthDummy()); + } + + @Test + public void createDummyNohAuth() { + String username = null; + String password = null; + String host = "http://localhost:7001"; + String group = "myCluster"; + String id = "node1"; + Integer connectTimeout = 10000; + Integer readTimeout = 20000; + String authentication = "noauth"; + String filter = null; + Integer limit = 3; + Integer timeoutQueryParamValue = 5000; + assertNotNull(new DummyConsumer(username, password, host, authentication, connectTimeout, readTimeout, group, id, filter, limit, timeoutQueryParamValue)); + } + + @Test + public void callClose() throws Exception { + DummyConsumer dummy = getAuthDummy(); + dummy.close(); + } + + @Test + public void registerDummyHandler() throws Exception { + DummyConsumer dummy = getAuthDummy(); + String topic = "politics"; + RequestHandler requestHandler = new RequestHandler() { + + @Override + public void handleRequest(String topic, String requestBody) { + // TODO Auto-generated method stub + + }; + + }; + dummy.registerHandler(topic, requestHandler); + assertEquals(new URL("http://localhost:7001/events/politics/myCluster/node1?timeout=5000&limit=3"), dummy.url); + assertEquals(topic, dummy.topic); + + } + + @Test + public void buildURL() throws Exception { + DummyConsumer dummy = getAuthDummy(); + HttpURLConnection connection = dummy.buildHttpURLConnection(new URL("http://localhost:7001/events/politics/myCluster/node1?timeout=5000&limit=3")); + assertNotNull(connection); + assertEquals("application/json", connection.getRequestProperty("Accept")); + } + +} diff --git a/message-router/consumer/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/consumer/provider/impl/ConsumerFactoryTest.java b/message-router/consumer/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/consumer/provider/impl/ConsumerFactoryTest.java new file mode 100644 index 000000000..d1018a014 --- /dev/null +++ b/message-router/consumer/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/consumer/provider/impl/ConsumerFactoryTest.java @@ -0,0 +1,164 @@ +package org.onap.ccsdk.sli.adaptors.messagerouter.consumer.provider.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.Properties; + +import org.junit.Test; + +public class ConsumerFactoryTest { + + @Test + public void testFactoryClientCreation() throws Exception { + Properties props = new Properties(); + String userName = "deadpool"; + String password = "notSECURE"; + String host = "http://localhost:7001"; + String group = "myCluster"; + String id = "node1"; + Integer connectTimeout = 10000; + Integer readTimeout = 20000; + props.put("username", userName); + props.put("password", password); + props.put("host", host); + props.put("group", group); + + ConsumerFactory factory = new ConsumerFactory(userName, password, host, group, id, connectTimeout, readTimeout); + assertNotNull(factory.createPollingClient()); + assertNotNull(factory.createPullingClient()); + } + + @Test + public void testFactoryDefaults() throws Exception { + Properties props = new Properties(); + String userName = "deadpool"; + String password = "notSECURE"; + String host = "http://localhost:7001"; + String group = "myCluster"; + String id = "node1"; + Integer connectTimeout = 10000; + Integer readTimeout = 20000; + props.put("username", userName); + props.put("password", password); + props.put("host", host); + props.put("group", group); + + ConsumerFactory factory = new ConsumerFactory(userName, password, host, group, id, connectTimeout, readTimeout); + + assertNotNull(factory.getAuth()); + assertNotNull(factory.getConnectTimeout()); + assertNotNull(factory.getReadTimeout()); + assertNotNull(factory.getFetchPause()); + assertNotNull(factory.getLimit()); + assertNotNull(factory.getTimeoutQueryParamValue()); + } + + @Test + public void testFactoryDefaultsWithProps() { + Properties props = new Properties(); + String userName = "deadpool"; + String password = "notSECURE"; + String host = "http://localhost:7001"; + String auth = "basic"; + String group = "myCluster"; + props.put("username", userName); + props.put("password", password); + props.put("host", host); + props.put("group", group); + + ConsumerFactory factory = new ConsumerFactory(props); + + assertNotNull(factory.getAuth()); + assertNotNull(factory.getConnectTimeout()); + assertNotNull(factory.getReadTimeout()); + assertNotNull(factory.getFetchPause()); + assertNotNull(factory.getLimit()); + assertNotNull(factory.getTimeoutQueryParamValue()); + } + + @Test + public void testFactoryOverrides() throws Exception { + Properties props = new Properties(); + String userName = "deadpool"; + String password = "notSECURE"; + String host = "http://localhost:7001"; + String group = "myCluster"; + props.put("username", userName); + props.put("password", password); + props.put("host", host); + props.put("group", group); + + String connectTimeout = "200"; + String readTimeout = "300"; + String fetchPause = "1000"; + String auth = "noauth"; + String timeoutQueryParamValue = "50"; + String limit = "2"; + props.put("connectTimeoutSeconds", connectTimeout); + props.put("readTimeoutMinutes", readTimeout); + props.put("fetchPause", fetchPause); + props.put("auth", auth); + props.put("timeout", timeoutQueryParamValue); + props.put("limit", limit); + + ConsumerFactory factory = new ConsumerFactory(props); + + assertEquals(auth, factory.getAuth()); + assertEquals(Integer.valueOf(connectTimeout), factory.getConnectTimeout()); + assertEquals(Integer.valueOf(readTimeout), factory.getReadTimeout()); + assertEquals(Integer.valueOf(fetchPause), factory.getFetchPause()); + assertEquals(Integer.valueOf(limit), factory.getLimit()); + assertEquals(Integer.valueOf(timeoutQueryParamValue), factory.getTimeoutQueryParamValue()); + } + + @Test + public void testManualOverrides() { + Properties props = new Properties(); + String userName = "deadpool"; + String password = "notSECURE"; + String host = "http://localhost:7001"; + String auth = "basic"; + String group = "myCluster"; + props.put("username", userName); + props.put("password", password); + props.put("host", host); + props.put("group", group); + + ConsumerFactory factory = new ConsumerFactory(props); + + assertNotNull(factory.getAuth()); + assertNotNull(factory.getConnectTimeout()); + assertNotNull(factory.getReadTimeout()); + assertNotNull(factory.getFetchPause()); + assertNotNull(factory.getLimit()); + assertNotNull(factory.getTimeoutQueryParamValue()); + String newAuth = "noauth"; + factory.setAuth(newAuth); + assertEquals(newAuth, factory.getAuth()); + + Integer connectTimeout = 1; + factory.setConnectTimeout(connectTimeout); + assertEquals(connectTimeout, factory.getConnectTimeout()); + + Integer fetchPause = 5; + factory.setFetchPause(fetchPause); + assertEquals(fetchPause, factory.getFetchPause()); + + factory.setFilter("\"filter\":{\n" + "\"class\":\"And\",\n" + "\"filters\":\n" + "[\n" + "{ \"class\":\"Equals\", \"foo\":\"abc\" },\n" + "{ \"class\":\"Assigned\", \"field\":\"bar\" }\n" + "]\n" + "}"); + assertNotNull(factory.getFilter()); + + Integer limit = 3; + factory.setLimit(limit); + assertEquals(limit, factory.getLimit()); + + Integer readTimeout = 2; + factory.setReadTimeout(readTimeout); + assertEquals(readTimeout, factory.getReadTimeout()); + + Integer timeoutQueryParamValue = 47; + factory.setTimeoutQueryParamValue(timeoutQueryParamValue); + assertEquals(timeoutQueryParamValue, factory.getTimeoutQueryParamValue()); + } + +} diff --git a/message-router/publisher/provider/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImpl.java b/message-router/publisher/provider/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImpl.java index d88dc66b4..58d0bc9f1 100755 --- a/message-router/publisher/provider/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImpl.java +++ b/message-router/publisher/provider/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImpl.java @@ -28,12 +28,12 @@ public class PublisherApiImpl implements PublisherApi { public void setUsername(String username) { this.username = username; - buildAuthorizationString(); + setAuthorizationString(); } public void setPassword(String password) { this.password = password; - buildAuthorizationString(); + setAuthorizationString(); } public void setHost(String hostString) { @@ -51,10 +51,10 @@ public class PublisherApiImpl implements PublisherApi { } public void init() { - buildAuthorizationString(); + setAuthorizationString(); } - private String buildUrlString(Integer hostIndex, String topic) { + protected String buildUrlString(Integer hostIndex, String topic) { return hosts[hostIndex] + "/events/" + topic; } @@ -116,15 +116,22 @@ public class PublisherApiImpl implements PublisherApi { return false; } - private void buildAuthorizationString() { - String basicAuthString = username + ":" + password; - basicAuthString = Base64.getEncoder().encodeToString(basicAuthString.getBytes()); - this.authorizationString = "Basic " + basicAuthString; + protected void setAuthorizationString() { + String str = buildAuthorizationString(this.username, this.password); + this.authorizationString = str; + //System.out.println(this.authorizationString); } + + protected String buildAuthorizationString(String username, String password) { + String basicAuthString = username + ":" + password; + basicAuthString = Base64.getEncoder().encodeToString(basicAuthString.getBytes()); + return "Basic " + basicAuthString; + } protected HttpURLConnection buildHttpURLConnection(URL url) throws IOException { HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(); if (authorizationString != null) { + System.out.println(authorizationString); httpUrlConnection.setRequestProperty("Authorization", authorizationString); } httpUrlConnection.setRequestProperty("Accept", "application/json"); diff --git a/message-router/publisher/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImplTest.java b/message-router/publisher/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImplTest.java new file mode 100644 index 000000000..53744f73e --- /dev/null +++ b/message-router/publisher/provider/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/provider/impl/PublisherApiImplTest.java @@ -0,0 +1,51 @@ +package org.onap.ccsdk.sli.adaptors.messagerouter.publisher.provider.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.net.HttpURLConnection; +import java.net.URL; + +import org.junit.Test; + +public class PublisherApiImplTest { + @Test + public void verifyDefaultTimeouts() { + PublisherApiImpl pub = new PublisherApiImpl(); + assertEquals(pub.DEFAULT_CONNECT_TIMEOUT, pub.connectTimeout); + assertEquals(pub.DEFAULT_READ_TIMEOUT, pub.readTimeout); + } + + @Test + public void buildHttpURLConnection() throws Exception { + PublisherApiImpl pub = new PublisherApiImpl(); + pub.init(); + + String myUserName = "Batman"; + pub.setUsername(myUserName); + assertEquals(myUserName, pub.username); + String password = "P@$$"; + pub.setPassword(password); + + HttpURLConnection httpUrlConnection = pub.buildHttpURLConnection(new URL("http://localhost:7001")); + assertNotNull(httpUrlConnection.getReadTimeout()); + assertNotNull(httpUrlConnection.getConnectTimeout()); + assertEquals("application/json", httpUrlConnection.getRequestProperty("Content-Type")); + assertEquals("application/json", httpUrlConnection.getRequestProperty("Accept")); + } + + @Test + public void testMultipleHosts() { + PublisherApiImpl pub = new PublisherApiImpl(); + String myTopic = "worldNews"; + String hostOne = "http://localhost:7001"; + String hostTwo = "http://localhost:7002"; + String hostThree = "http://localhost:7003"; + + pub.setHost(hostOne + "," + hostTwo + "," + hostThree); + + assertEquals("http://localhost:7001/events/worldNews", pub.buildUrlString(0, myTopic)); + assertEquals("http://localhost:7002/events/worldNews", pub.buildUrlString(1, myTopic)); + assertEquals("http://localhost:7003/events/worldNews", pub.buildUrlString(2, myTopic)); + } +} \ No newline at end of file diff --git a/message-router/publisher/sample.client/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImplTest.java b/message-router/publisher/sample.client/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImplTest.java new file mode 100644 index 000000000..82a1566c3 --- /dev/null +++ b/message-router/publisher/sample.client/src/test/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImplTest.java @@ -0,0 +1,29 @@ +package org.onap.ccsdk.sli.adaptors.messagerouter.publisher.client.impl; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.ccsdk.sli.adaptors.messagerouter.publisher.api.PublisherApi; + +public class ClientImplTest { + + @Test + public void testSetTopic() { + ClientImpl impl = new ClientImpl(); + String myTopic = "stock updates"; + impl.setTopic(myTopic); + + PublisherApi publisherImpl = new PublisherApi() { + + @Override + public Boolean publish(String topic, String body) { + assertEquals(myTopic,topic); + return true; + } + + }; + impl.setPublisher(publisherImpl); + impl.init(); + } + +} -- cgit 1.2.3-korg From 25597db4de0c879ee37c7a0be86f110c8e4f5db1 Mon Sep 17 00:00:00 2001 From: Thugutla Sailakshmi Date: Tue, 30 Jul 2019 14:47:50 +0530 Subject: CCSDK-1556 -Move this constructor to comply with Java Code Conventions Move this constructor to comply with Java Code Conventions Issue-ID: CCSDK-1556 Change-Id: If0aa6b99e52c2a6cae7d61c213ca1af6b7890183 Signed-off-by: Thugutla Sailakshmi --- .../publisher/client/impl/ClientImpl.java | 31 +++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'message-router/publisher/sample.client/src') diff --git a/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java b/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java index 25fc87ead..8ce066950 100755 --- a/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java +++ b/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java @@ -1,3 +1,26 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright (C) 2019 IBM. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + package org.onap.ccsdk.sli.adaptors.messagerouter.publisher.client.impl; import org.onap.ccsdk.sli.adaptors.messagerouter.publisher.api.PublisherApi; @@ -8,6 +31,10 @@ public class ClientImpl { private static final Logger logger = LoggerFactory.getLogger(ClientImpl.class); private String topic; private PublisherApi publisher; + + public ClientImpl() { + + } public void setPublisher(PublisherApi publisherApi) { this.publisher = publisherApi; @@ -17,9 +44,7 @@ public class ClientImpl { this.topic = topic; } - public ClientImpl() { - - } + public void init() { for (int i = 0; i < 5; i++) { -- cgit 1.2.3-korg From 41c81f1a6c41d4e480749b71777a934fc70b0c62 Mon Sep 17 00:00:00 2001 From: Thugutla Sailakshmi Date: Tue, 30 Jul 2019 14:59:22 +0530 Subject: Directly append the argument of String.valueOf() Directly append the argument of String.valueOf() Issue-ID: CCSDK-914 Change-Id: I4ba07651a94bb7da9b47d10c5e9582022bde66a5 Signed-off-by: Thugutla Sailakshmi --- .../publisher/client/impl/ClientImpl.java | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'message-router/publisher/sample.client/src') diff --git a/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java b/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java index 25fc87ead..d62648033 100755 --- a/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java +++ b/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/sli/adaptors/messagerouter/publisher/client/impl/ClientImpl.java @@ -1,3 +1,26 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright (C) 2019 IBM. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + package org.onap.ccsdk.sli.adaptors.messagerouter.publisher.client.impl; import org.onap.ccsdk.sli.adaptors.messagerouter.publisher.api.PublisherApi; @@ -23,7 +46,7 @@ public class ClientImpl { public void init() { for (int i = 0; i < 5; i++) { - String body = "{\"hello\":\"world " + String.valueOf(Math.random()) + "\"}"; + String body = "{\"hello\":\"world " + Math.random() + "\"}"; logger.error("Loop iteration " + i + " sending body " + body + " to the topic " + topic); Boolean result = publisher.publish(topic, body); logger.error("Loop iteration " + i + " returned the boolean value " + result); -- cgit 1.2.3-korg