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') 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