Test coverage in EventSenderDmaapImpl 22/78222/3
authorJoss Armstrong <joss.armstrong@ericsson.com>
Mon, 11 Feb 2019 14:55:10 +0000 (14:55 +0000)
committerTakamune Cho <takamune.cho@att.com>
Mon, 11 Feb 2019 20:04:11 +0000 (20:04 +0000)
Coverage increased from 10% to 92%

Issue-ID: APPC-1415
Change-Id: I846a24364bda29e754fa53ed518de92bf00693e6
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/main/java/org/onap/appc/adapter/messaging/dmaap/impl/EventSenderDmaapImpl.java
appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/test/java/org/onap/appc/adapter/messaging/dmaap/impl/EventSenderDmaapImplTest.java [new file with mode: 0644]

index 6b7ee87..7d8bc76 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2019 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -54,7 +56,7 @@ public class EventSenderDmaapImpl implements EventSender
 
     private static Configuration configuration = ConfigurationFactory.getConfiguration();
 
-    private Map<String,Producer> producerMap = new ConcurrentHashMap<>();
+    private Map<String, Producer> producerMap = new ConcurrentHashMap<>();
 
     public Map<String, Producer> getProducerMap() {
         return producerMap;
@@ -98,7 +100,7 @@ public class EventSenderDmaapImpl implements EventSender
                     break;
                 }
             }
-            producerMap.put(destination.toString(),producer);
+            producerMap.put(destination.toString(), producer);
         }
 
     }
@@ -120,7 +122,7 @@ public class EventSenderDmaapImpl implements EventSender
         Producer producer = createProducer(destination, eventTopicName);
         return producer.post(id, jsonStr);
     }
-    
+
     private Producer createProducer(MessageDestination destination, String eventTopicName) {
         Properties properties = configuration.getProperties();
         final List<String> pool = new ArrayList<>();
@@ -136,7 +138,7 @@ public class EventSenderDmaapImpl implements EventSender
         LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
         LOG.debug(String.format("writeTopic = %s, taken from property: %s", eventTopicName, destination + "." + EVENT_TOPIC_WRITE));
         LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
-        Producer producer = new DmaapProducerImpl(pool, eventTopicName,username, password);
+        Producer producer = new DmaapProducerImpl(pool, eventTopicName, username, password);
 
         for (String url : pool) {
             if (url.contains("3905") || url.contains("https")) {
@@ -160,9 +162,9 @@ public class EventSenderDmaapImpl implements EventSender
         String apiVer = params.get("apiVer");
         String eventId = params.get("eventId");
         String reason = params.get("reason");
-        String entityId=params.get("entityId");
-        if(entityId!=null){
-            reason=reason+"("+entityId+")";
+        String entityId = params.get("entityId");
+        if(entityId != null){
+            reason += "(" + entityId + ")";
         }
         Integer code = Integer.getInteger(params.get("code"), 500);
 
@@ -175,6 +177,6 @@ public class EventSenderDmaapImpl implements EventSender
                         new EventHeader(eventTime, apiVer, eventId),
                         new EventStatus(code, reason));
 
-        return sendEvent(destination,eventMessage);
+        return sendEvent(destination, eventMessage);
     }
 }
diff --git a/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/test/java/org/onap/appc/adapter/messaging/dmaap/impl/EventSenderDmaapImplTest.java b/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/test/java/org/onap/appc/adapter/messaging/dmaap/impl/EventSenderDmaapImplTest.java
new file mode 100644 (file)
index 0000000..76e56d8
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2019 Ericsson
+ * ================================================================================
+ * 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.adapter.messaging.dmaap.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.onap.appc.adapter.message.MessageDestination;
+import org.onap.appc.adapter.message.Producer;
+import org.onap.appc.adapter.message.event.EventHeader;
+import org.onap.appc.adapter.message.event.EventMessage;
+import org.onap.appc.configuration.Configuration;
+import org.onap.appc.configuration.ConfigurationFactory;
+import org.onap.appc.exceptions.APPCException;
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConfigurationFactory.class)
+public class EventSenderDmaapImplTest {
+
+    @Rule
+    public ExpectedException expectedEx = ExpectedException.none();
+
+    @Before
+    public void setup() {
+        Configuration configuration = Mockito.mock(Configuration.class);
+        Properties properties = new Properties();
+        properties.put(MessageDestination.DCAE + "." + EventSenderDmaapImpl.EVENT_POOL_MEMBERS, "host1,host2");
+        Mockito.when(configuration.getProperties()).thenReturn(properties);
+        PowerMockito.mockStatic(ConfigurationFactory.class);
+        PowerMockito.when(ConfigurationFactory.getConfiguration()).thenReturn(configuration);
+    }
+
+    @Test
+    public void testInit() {
+        EventSenderDmaapImpl sender = new EventSenderDmaapImpl();
+        sender.initialize();
+        assertEquals(1, sender.getProducerMap().size());
+    }
+
+    @Test
+    public void testSendEvent() {
+        EventSenderDmaapImpl sender = new EventSenderDmaapImpl();
+        EventMessage eventMessage = Mockito.mock(EventMessage.class);
+        EventHeader eventHeader = Mockito.mock(EventHeader.class);
+        Mockito.when(eventHeader.getEventId()).thenReturn("EVENT_ID");
+        Mockito.when(eventMessage.getEventHeader()).thenReturn(eventHeader);
+        assertTrue(sender.sendEvent(MessageDestination.DCAE, eventMessage, "TOPIC NAME"));
+    }
+
+    @Test
+    public void testSendEventSvcLogicContext() throws APPCException {
+        EventSenderDmaapImpl sender = new EventSenderDmaapImpl();
+        expectedEx.expect(APPCException.class);
+        expectedEx.expectMessage("Missing input parameters: ");
+        sender.sendEvent(MessageDestination.DCAE, new HashMap<String, String>(), new SvcLogicContext());
+    }
+
+    @Test
+    public void testSendEventSvcLogicContextNullParams() throws APPCException {
+        EventSenderDmaapImpl sender = new EventSenderDmaapImpl();
+        expectedEx.expect(APPCException.class);
+        expectedEx.expectMessage("Parameters map is empty (null)");
+        sender.sendEvent(MessageDestination.DCAE, null, new SvcLogicContext());
+    }
+
+    @Test
+    public void testSendEventSvcLogicContextWithParams() throws APPCException {
+        EventSenderDmaapImpl sender = new EventSenderDmaapImpl();
+        Map<String, String> params = new HashMap<>();
+        params.put("apiVer", "apiVer");
+        params.put("eventId", "eventId");
+        params.put("reason", "reason");
+        params.put("entityId", "entityId");
+        Producer producer = Mockito.mock(Producer.class);
+        Mockito.when(producer.post(Mockito.anyString(), Mockito.anyString())).thenReturn(false);
+        Map<String, Producer> producerMap = new HashMap<>();
+        producerMap.put(MessageDestination.DCAE.toString(), producer);
+        sender.setProducerMap(producerMap);
+        assertFalse(sender.sendEvent(MessageDestination.DCAE, params, new SvcLogicContext()));
+    }
+}