--- /dev/null
+package org.onap.ccsdk.sli.adaptors.messagerouter.consumer.provider.impl;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
+\r
+import java.net.HttpURLConnection;\r
+import java.net.URL;\r
+\r
+import org.junit.Test;\r
+import org.onap.ccsdk.sli.adaptors.messagerouter.consumer.api.RequestHandler;\r
+\r
+public class AbstractBaseConsumerTest {\r
+ private class DummyConsumer extends AbstractBaseConsumer {\r
+\r
+ public DummyConsumer(String username, String password, String host, String authentication, Integer connectTimeout, Integer readTimeout, String group, String id, String filter, Integer limit, Integer timeoutQueryParamValue) {\r
+ super(username, password, host, authentication, connectTimeout, readTimeout, group, id, filter, limit, timeoutQueryParamValue);\r
+ }\r
+\r
+ }\r
+\r
+ public DummyConsumer getAuthDummy() {\r
+ String username = "deadpool";\r
+ String password = "notSECURE";\r
+ String host = "http://localhost:7001";\r
+ String group = "myCluster";\r
+ String id = "node1";\r
+ Integer connectTimeout = 10000;\r
+ Integer readTimeout = 20000;\r
+ String authentication = "basic";\r
+ String filter = null;\r
+ Integer limit = 3;\r
+ Integer timeoutQueryParamValue = 5000;\r
+ return new DummyConsumer(username, password, host, authentication, connectTimeout, readTimeout, group, id, filter, limit, timeoutQueryParamValue);\r
+ }\r
+\r
+ @Test\r
+ public void createDummyWithAuth() {\r
+ assertNotNull(getAuthDummy());\r
+ }\r
+\r
+ @Test\r
+ public void createDummyNohAuth() {\r
+ String username = null;\r
+ String password = null;\r
+ String host = "http://localhost:7001";\r
+ String group = "myCluster";\r
+ String id = "node1";\r
+ Integer connectTimeout = 10000;\r
+ Integer readTimeout = 20000;\r
+ String authentication = "noauth";\r
+ String filter = null;\r
+ Integer limit = 3;\r
+ Integer timeoutQueryParamValue = 5000;\r
+ assertNotNull(new DummyConsumer(username, password, host, authentication, connectTimeout, readTimeout, group, id, filter, limit, timeoutQueryParamValue));\r
+ }\r
+\r
+ @Test\r
+ public void callClose() throws Exception {\r
+ DummyConsumer dummy = getAuthDummy();\r
+ dummy.close();\r
+ }\r
+\r
+ @Test\r
+ public void registerDummyHandler() throws Exception {\r
+ DummyConsumer dummy = getAuthDummy();\r
+ String topic = "politics";\r
+ RequestHandler requestHandler = new RequestHandler() {\r
+\r
+ @Override\r
+ public void handleRequest(String topic, String requestBody) {\r
+ // TODO Auto-generated method stub\r
+\r
+ };\r
+\r
+ };\r
+ dummy.registerHandler(topic, requestHandler);\r
+ assertEquals(new URL("http://localhost:7001/events/politics/myCluster/node1?timeout=5000&limit=3"), dummy.url);\r
+ assertEquals(topic, dummy.topic);\r
+\r
+ }\r
+\r
+ @Test\r
+ public void buildURL() throws Exception {\r
+ DummyConsumer dummy = getAuthDummy();\r
+ HttpURLConnection connection = dummy.buildHttpURLConnection(new URL("http://localhost:7001/events/politics/myCluster/node1?timeout=5000&limit=3"));\r
+ assertNotNull(connection);\r
+ assertEquals("application/json", connection.getRequestProperty("Accept"));\r
+ }\r
+\r
+}\r
--- /dev/null
+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());
+ }
+
+}
\r
public void setUsername(String username) {\r
this.username = username;\r
- buildAuthorizationString();\r
+ setAuthorizationString();\r
}\r
\r
public void setPassword(String password) {\r
this.password = password;\r
- buildAuthorizationString();\r
+ setAuthorizationString();\r
}\r
\r
public void setHost(String hostString) {\r
}\r
\r
public void init() {\r
- buildAuthorizationString();\r
+ setAuthorizationString();\r
}\r
\r
- private String buildUrlString(Integer hostIndex, String topic) {\r
+ protected String buildUrlString(Integer hostIndex, String topic) {\r
return hosts[hostIndex] + "/events/" + topic;\r
}\r
\r
return false;\r
}\r
\r
- private void buildAuthorizationString() {\r
- String basicAuthString = username + ":" + password;\r
- basicAuthString = Base64.getEncoder().encodeToString(basicAuthString.getBytes());\r
- this.authorizationString = "Basic " + basicAuthString;\r
+ protected void setAuthorizationString() {\r
+ String str = buildAuthorizationString(this.username, this.password);\r
+ this.authorizationString = str;\r
+ //System.out.println(this.authorizationString);\r
}\r
+ \r
+ protected String buildAuthorizationString(String username, String password) {\r
+ String basicAuthString = username + ":" + password;\r
+ basicAuthString = Base64.getEncoder().encodeToString(basicAuthString.getBytes());\r
+ return "Basic " + basicAuthString;\r
+ }\r
\r
protected HttpURLConnection buildHttpURLConnection(URL url) throws IOException {\r
HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();\r
if (authorizationString != null) {\r
+ System.out.println(authorizationString);\r
httpUrlConnection.setRequestProperty("Authorization", authorizationString);\r
}\r
httpUrlConnection.setRequestProperty("Accept", "application/json");\r
--- /dev/null
+package org.onap.ccsdk.sli.adaptors.messagerouter.publisher.provider.impl;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
+\r
+import java.net.HttpURLConnection;\r
+import java.net.URL;\r
+\r
+import org.junit.Test;\r
+\r
+public class PublisherApiImplTest {\r
+ @Test\r
+ public void verifyDefaultTimeouts() {\r
+ PublisherApiImpl pub = new PublisherApiImpl();\r
+ assertEquals(pub.DEFAULT_CONNECT_TIMEOUT, pub.connectTimeout);\r
+ assertEquals(pub.DEFAULT_READ_TIMEOUT, pub.readTimeout);\r
+ }\r
+\r
+ @Test\r
+ public void buildHttpURLConnection() throws Exception {\r
+ PublisherApiImpl pub = new PublisherApiImpl();\r
+ pub.init();\r
+\r
+ String myUserName = "Batman";\r
+ pub.setUsername(myUserName);\r
+ assertEquals(myUserName, pub.username);\r
+ String password = "P@$$";\r
+ pub.setPassword(password);\r
+\r
+ HttpURLConnection httpUrlConnection = pub.buildHttpURLConnection(new URL("http://localhost:7001"));\r
+ assertNotNull(httpUrlConnection.getReadTimeout());\r
+ assertNotNull(httpUrlConnection.getConnectTimeout());\r
+ assertEquals("application/json", httpUrlConnection.getRequestProperty("Content-Type"));\r
+ assertEquals("application/json", httpUrlConnection.getRequestProperty("Accept"));\r
+ }\r
+\r
+ @Test\r
+ public void testMultipleHosts() {\r
+ PublisherApiImpl pub = new PublisherApiImpl();\r
+ String myTopic = "worldNews";\r
+ String hostOne = "http://localhost:7001";\r
+ String hostTwo = "http://localhost:7002";\r
+ String hostThree = "http://localhost:7003";\r
+\r
+ pub.setHost(hostOne + "," + hostTwo + "," + hostThree);\r
+\r
+ assertEquals("http://localhost:7001/events/worldNews", pub.buildUrlString(0, myTopic));\r
+ assertEquals("http://localhost:7002/events/worldNews", pub.buildUrlString(1, myTopic));\r
+ assertEquals("http://localhost:7003/events/worldNews", pub.buildUrlString(2, myTopic));\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+package org.onap.ccsdk.sli.adaptors.messagerouter.publisher.client.impl;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+\r
+import org.junit.Test;\r
+import org.onap.ccsdk.sli.adaptors.messagerouter.publisher.api.PublisherApi;\r
+\r
+public class ClientImplTest {\r
+ \r
+ @Test\r
+ public void testSetTopic() {\r
+ ClientImpl impl = new ClientImpl();\r
+ String myTopic = "stock updates";\r
+ impl.setTopic(myTopic);\r
+ \r
+ PublisherApi publisherImpl = new PublisherApi() {\r
+\r
+ @Override\r
+ public Boolean publish(String topic, String body) {\r
+ assertEquals(myTopic,topic);\r
+ return true;\r
+ }\r
+ \r
+ };\r
+ impl.setPublisher(publisherImpl);\r
+ impl.init();\r
+ }\r
+\r
+}\r