From 4e2bcb85f310296e7668859278b5f0517ad0db11 Mon Sep 17 00:00:00 2001 From: Jakub Dudycz Date: Thu, 22 Mar 2018 14:54:38 +0100 Subject: EventHandlerImpl unit tests Improved code coverage. Change-Id: Iee626e206b1fc2ef1047fc4d50b030319afe0403 Issue-ID: APPC-753 Signed-off-by: Jakub Dudycz --- .../appc/listener/impl/EventHandlerImplTest.java | 246 +++++++++++++++++++++ .../onap/appc/listener/impl/TestEventHandler.java | 173 --------------- 2 files changed, 246 insertions(+), 173 deletions(-) create mode 100644 appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/impl/EventHandlerImplTest.java delete mode 100644 appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/impl/TestEventHandler.java (limited to 'appc-event-listener/appc-event-listener-bundle') diff --git a/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/impl/EventHandlerImplTest.java b/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/impl/EventHandlerImplTest.java new file mode 100644 index 000000000..c022e0622 --- /dev/null +++ b/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/impl/EventHandlerImplTest.java @@ -0,0 +1,246 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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.appc.listener.impl; + +import static com.google.common.collect.Lists.newArrayList; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; +import java.util.Set; +import java.util.function.Function; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.appc.adapter.message.Consumer; +import org.onap.appc.adapter.message.Producer; +import org.onap.appc.listener.ListenerProperties; + +/** + * Test the ProviderAdapter implementation. + */ + +@RunWith(MockitoJUnitRunner.class) +public class EventHandlerImplTest { + + private TestEventHandlerImpl adapter; + private ListenerProperties properties; + + @Mock + private Producer mockProducer; + @Mock + private Consumer mockConsumer; + + private static final String PROP_FILE = "/org/onap/appc/default.properties"; + + private static final String MESSAGE_FILE = "/DCAEResponse.txt"; + + /** + * Setup the test environment. + */ + @Before + public void setup() { + Properties allProps = new Properties(); + try { + allProps.load(getClass().getResourceAsStream(PROP_FILE)); + allProps.remove("appc.ClosedLoop.topic.read.filter"); + properties = new ListenerProperties("appc.ClosedLoop", allProps); + } catch (IOException e) { + System.out.println("WARNING: Failed to load properties file: " + PROP_FILE); + } + adapter = new TestEventHandlerImpl(properties); + adapter.setConsumer(mockConsumer); + adapter.setProducer(mockProducer); + } + + @Test + public void testInitialProperties() { + assertEquals(properties.getProperty("topic.read"), adapter.getReadTopic()); + assertTrue(adapter.getWriteTopics().contains(properties.getProperty("topic.write"))); + assertEquals(properties.getProperty("client.name"), adapter.getClientName()); + assertEquals(properties.getProperty("client.name.id"), adapter.getClientId()); + + String hostStr = properties.getProperty("poolMembers"); + int hostCount = hostStr.length() > 0 ? hostStr.split(",").length : 0; + assertEquals(hostCount, adapter.getPool().size()); + } + + @Test + public void testGettersAndSetters() { + String readTopic = "read"; + Set writeTopic = new HashSet<>(); + writeTopic.add("write"); + String clientName = "APPC-TEST"; + String clientId = "00"; + String newHost = "google.com"; + + adapter.setReadTopic(readTopic); + assertEquals(readTopic, adapter.getReadTopic()); + + adapter.setWriteTopics(writeTopic); + assertEquals(writeTopic, adapter.getWriteTopics()); + + adapter.setClientName(clientName); + assertEquals(clientName, adapter.getClientName()); + + adapter.setClientId(clientId); + assertEquals(clientId, adapter.getClientId()); + + adapter.setCredentials("fake", "secret"); + adapter.clearCredentials(); + + int oldSize = adapter.getPool().size(); + adapter.addToPool(newHost); + assertEquals(oldSize + 1, adapter.getPool().size()); + assertTrue(adapter.getPool().contains(newHost)); + + adapter.removeFromPool(newHost); + assertEquals(oldSize, adapter.getPool().size()); + assertFalse(adapter.getPool().contains(newHost)); + + } + + @Test + public void getIncomingEvents_should_success_when_no_errors_encountered() { + + List testResult = newArrayList("test-result1", "test-result2", "test-result3"); + when(mockConsumer.fetch(anyInt(), anyInt())).thenReturn(testResult); + + List result = adapter.getIncomingEvents(5); + + for (int i = 0; i < testResult.size(); i++) { + assertEquals(testResult.get(i), result.get(i)); + } + } + + + @Test + public void postStatus_should_success_when_no_errors_encountered() { + + adapter.postStatus("test-partition", "test-event"); + verify(mockProducer).post("test-partition", "test-event"); + + adapter.postStatus("test-event"); + verify(mockProducer).post(null, "test-event"); + } + + + @Test + public void closeClients_should_close_producer_and_consumer() { + adapter.getIncomingEvents(5); + adapter.postStatus("test-partition", "test-event"); + + adapter.closeClients(); + verify(mockConsumer).close(); + verify(mockProducer).close(); + } + + +// @Test + public void testRun() { + EventHandlerImpl adapter = new EventHandlerImpl(properties); + + // Runoff any old data + List result = adapter.getIncomingEvents(); + assertNotNull(result); + + // Post new data + DummyObj data = new DummyObj(); + data.key = "value"; + adapter.postStatus(data.toJson()); + + // Wait to account for network delay + sleep(2000); + + // Get data back + List result2 = adapter.getIncomingEvents(DummyObj.class); + assertNotNull(result2); +// assertEquals(1, result2.size()); + assertEquals(data.toJson(), result2.get(0).toJson()); + } + + private class TestEventHandlerImpl extends EventHandlerImpl { + + private Consumer mockConsumer; + private Producer mockProducer; + + private TestEventHandlerImpl(ListenerProperties props) { + super(props); + } + + @Override + protected Consumer getConsumer() { + return mockConsumer; + } + + @Override + protected Producer getProducer() { + return mockProducer; + } + + private void setConsumer(Consumer consumer) { + mockConsumer = consumer; + } + + private void setProducer(Producer producer) { + mockProducer = producer; + } + } + + @JsonSerialize + public static class DummyObj implements Serializable { + + @JsonProperty("request") // Call request for default filter + public String key; + + public DummyObj() { + } + + public String toJson() { + return String.format("{\"request\": \"%s\"}", key); + } + } + + private void sleep(long ms) { + try { + Thread.sleep(ms); + } catch (Exception e) { + return; + } + } +} diff --git a/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/impl/TestEventHandler.java b/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/impl/TestEventHandler.java deleted file mode 100644 index 992566358..000000000 --- a/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/impl/TestEventHandler.java +++ /dev/null @@ -1,173 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.listener.impl; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.io.Serializable; -import java.util.HashSet; -import java.util.List; -import java.util.Properties; -import java.util.Set; - -import org.apache.commons.lang3.StringUtils; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.onap.appc.listener.EventHandler; -import org.onap.appc.listener.ListenerProperties; -import org.onap.appc.listener.impl.EventHandlerImpl; - -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; - -/** - * Test the ProviderAdapter implementation. - * - */ - -public class TestEventHandler { - - private ListenerProperties prop; - - private EventHandler adapter; - - private static final String PROP_FILE = "/org/onap/appc/default.properties"; - - private static final String MESSAGE_FILE = "/DCAEResponse.txt"; - - /** - * Setup the test environment. - * - * @throws NoSuchMethodException - * @throws SecurityException - * @throws NoSuchFieldException - */ - @Before - public void setup() { - Properties allProps = new Properties(); - try { - allProps.load(getClass().getResourceAsStream(PROP_FILE)); - allProps.remove("appc.ClosedLoop.topic.read.filter"); - prop = new ListenerProperties("appc.ClosedLoop", allProps); - } catch (IOException e) { - System.out.println("WARNING: Failed to load properties file: " + PROP_FILE); - } - adapter = new EventHandlerImpl(prop); - } - - @Test - public void testInitialProperties() { - assertEquals(prop.getProperty("topic.read"), adapter.getReadTopic()); - assertTrue(adapter.getWriteTopics().contains(prop.getProperty("topic.write"))); - assertEquals(prop.getProperty("client.name"), adapter.getClientName()); - assertEquals(prop.getProperty("client.name.id"), adapter.getClientId()); - - String hostStr = prop.getProperty("poolMembers"); - int hostCount = hostStr.length()>0 ? hostStr.split(",").length : 0; - assertEquals(hostCount, adapter.getPool().size()); - } - - @Test - public void testGettersAndSetters() { - String readTopic = "read"; - Set writeTopic = new HashSet(); - writeTopic.add("write"); - String clientName = "APPC-TEST"; - String clientId = "00"; - String newHost = "google.com"; - - adapter.setReadTopic(readTopic); - assertEquals(readTopic, adapter.getReadTopic()); - - adapter.setWriteTopics(writeTopic); - assertEquals(writeTopic, adapter.getWriteTopics()); - - adapter.setClientName(clientName); - assertEquals(clientName, adapter.getClientName()); - - adapter.setClientId(clientId); - assertEquals(clientId, adapter.getClientId()); - - adapter.setCredentials("fake", "secret"); - adapter.clearCredentials(); - - int oldSize = adapter.getPool().size(); - adapter.addToPool(newHost); - assertEquals(oldSize + 1, adapter.getPool().size()); - assertTrue(adapter.getPool().contains(newHost)); - - adapter.removeFromPool(newHost); - assertEquals(oldSize, adapter.getPool().size()); - assertFalse(adapter.getPool().contains(newHost)); - - } - -// @Test - public void testRun() { - // Runoff any old data - List result1 = adapter.getIncomingEvents(); - assertNotNull(result1); - - // Post new data - DummyObj data = new DummyObj(); - data.key = "value"; - adapter.postStatus(data.toJson()); - - // Wait to account for network delay - sleep(2000); - - // Get data back - List result2 = adapter.getIncomingEvents(DummyObj.class); - assertNotNull(result2); -// assertEquals(1, result2.size()); - assertEquals(data.toJson(), result2.get(0).toJson()); - } - - @JsonSerialize - public static class DummyObj implements Serializable { - @JsonProperty("request") // Call request for default filter - public String key; - - public DummyObj() { - } - - public String toJson() { - return String.format("{\"request\": \"%s\"}", key); - } - } - - private void sleep(long ms) { - try { - Thread.sleep(ms); - } catch (Exception e) { - return; - } - } -} -- cgit 1.2.3-korg