EventHandlerImpl unit tests 85/37785/3
authorJakub Dudycz <jakub.dudycz@nokia.com>
Thu, 22 Mar 2018 13:54:38 +0000 (14:54 +0100)
committerTakamune Cho <tc012c@att.com>
Thu, 22 Mar 2018 16:23:16 +0000 (16:23 +0000)
Improved code coverage.

Change-Id: Iee626e206b1fc2ef1047fc4d50b030319afe0403
Issue-ID: APPC-753
Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/impl/EventHandlerImplTest.java [moved from appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/impl/TestEventHandler.java with 57% similarity]

  * 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 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 org.apache.commons.lang3.StringUtils;
+import java.util.function.Function;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import org.onap.appc.listener.EventHandler;
+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;
-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 {
+@RunWith(MockitoJUnitRunner.class)
+public class EventHandlerImplTest {
 
-    private ListenerProperties prop;
+    private TestEventHandlerImpl adapter;
+    private ListenerProperties properties;
 
-    private EventHandler adapter;
+    @Mock
+    private Producer mockProducer;
+    @Mock
+    private Consumer mockConsumer;
 
     private static final String PROP_FILE = "/org/onap/appc/default.properties";
 
@@ -64,10 +72,6 @@ public class TestEventHandler {
 
     /**
      * Setup the test environment.
-     * 
-     * @throws NoSuchMethodException
-     * @throws SecurityException
-     * @throws NoSuchFieldException
      */
     @Before
     public void setup() {
@@ -75,29 +79,31 @@ public class TestEventHandler {
         try {
             allProps.load(getClass().getResourceAsStream(PROP_FILE));
             allProps.remove("appc.ClosedLoop.topic.read.filter");
-            prop = new ListenerProperties("appc.ClosedLoop", allProps);
+            properties = new ListenerProperties("appc.ClosedLoop", allProps);
         } catch (IOException e) {
             System.out.println("WARNING: Failed to load properties file: " + PROP_FILE);
         }
-        adapter = new EventHandlerImpl(prop);
+        adapter = new TestEventHandlerImpl(properties);
+        adapter.setConsumer(mockConsumer);
+        adapter.setProducer(mockProducer);
     }
-    
+
     @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());
+        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 = prop.getProperty("poolMembers");
-        int hostCount = hostStr.length()>0 ? hostStr.split(",").length : 0;
+        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<String> writeTopic = new HashSet<String>();
+        Set<String> writeTopic = new HashSet<>();
         writeTopic.add("write");
         String clientName = "APPC-TEST";
         String clientId = "00";
@@ -129,11 +135,49 @@ public class TestEventHandler {
 
     }
 
+    @Test
+    public void getIncomingEvents_should_success_when_no_errors_encountered() {
+
+        List<String> testResult = newArrayList("test-result1", "test-result2", "test-result3");
+        when(mockConsumer.fetch(anyInt(), anyInt())).thenReturn(testResult);
+
+        List<String> 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<String> result1 = adapter.getIncomingEvents();
-        assertNotNull(result1);
+        List<String> result = adapter.getIncomingEvents();
+        assertNotNull(result);
 
         // Post new data
         DummyObj data = new DummyObj();
@@ -150,8 +194,37 @@ public class TestEventHandler {
         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;