Test Coverage in DmaapConsumerImpl 05/78305/3
authorJoss Armstrong <joss.armstrong@ericsson.com>
Tue, 12 Feb 2019 15:21:50 +0000 (15:21 +0000)
committerTakamune Cho <takamune.cho@att.com>
Tue, 12 Feb 2019 21:31:49 +0000 (21:31 +0000)
Increased coverage

Issue-ID: APPC-1415
Change-Id: I29e2d0883914aeec652a47436c86c6ea8e5ab7a5
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/DmaapConsumerImpl.java
appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/test/java/org/onap/appc/adapter/messaging/dmaap/impl/TestConsumerProducerImpl.java
appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/test/java/org/onap/appc/adapter/messaging/dmaap/impl/TestDmaapConsumerImpl.java

index 686bb07..3045520 100644 (file)
@@ -8,6 +8,8 @@
  * ================================================================================
  * Modifications Copyright (C) 2018 IBM
  * ================================================================================
+ * 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.
  * You may obtain a copy of the License at
@@ -53,7 +55,7 @@ import org.osgi.framework.ServiceReference;
 public class DmaapConsumerImpl implements Consumer {
 
     private static final EELFLogger LOG                = EELFManager.getInstance().getLogger(DmaapConsumerImpl.class);
-    private final Configuration     configuration      = ConfigurationFactory.getConfiguration();
+    private Configuration           configuration      = ConfigurationFactory.getConfiguration();
     // Default values
     private static final int        DEFAULT_TIMEOUT_MS = 60000;
     private static final int        DEFAULT_LIMIT      = 1000;
@@ -93,7 +95,7 @@ public class DmaapConsumerImpl implements Consumer {
 
     private void initMetric() {
         LOG.debug("Metric getting initialized");
-        MetricService metricService = getMetricservice();
+        MetricService metricService = getMetricService();
         if (metricService != null) {
             metricRegistry = metricService.createRegistry("APPC");
 
@@ -108,7 +110,10 @@ public class DmaapConsumerImpl implements Consumer {
                 logPublishers[0] = logPublisher;
 
                 PublishingPolicy manuallyScheduledPublishingPolicy = metricRegistry.policyBuilderFactory()
-                        .scheduledPolicyBuilder().withPublishers(logPublishers).withMetrics(metrics).build();
+                        .scheduledPolicyBuilder()
+                        .withPublishers(logPublishers)
+                        .withMetrics(metrics)
+                        .build();
 
                 LOG.debug("Policy getting initialized");
                 manuallyScheduledPublishingPolicy.init();
@@ -215,7 +220,7 @@ public class DmaapConsumerImpl implements Consumer {
         useHttps = yes;
     }
 
-    private MetricService getMetricservice() {
+    protected MetricService getMetricService() {
         BundleContext bctx = FrameworkUtil.getBundle(MetricService.class).getBundleContext();
         ServiceReference sref = bctx.getServiceReference(MetricService.class.getName());
         if (sref != null) {
index 1832c76..9df2f07 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.
@@ -27,19 +29,20 @@ 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.util.*;
-
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.UUID;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.onap.appc.adapter.message.Consumer;
 import org.onap.appc.adapter.message.Producer;
-import org.onap.appc.adapter.messaging.dmaap.impl.DmaapConsumerImpl;
-import org.onap.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
 import org.onap.appc.configuration.Configuration;
 import org.onap.appc.configuration.ConfigurationFactory;
 
+
 public class TestConsumerProducerImpl {
 
     private Collection<String> urls;
@@ -52,17 +55,17 @@ public class TestConsumerProducerImpl {
 
     @Before
     public void setup() {
-       System.out.println("setup entry...");
-//        urls = new HashSet<String>();
-//        urls.add("dmaaphost1");
-//        urls.add("dmaaphost2");
-//        //remove unavailable dmaap instance for build
-//        //urls.add("dmaaphost3");
-//
-//        topicRead = "APPC-UNIT-TEST";
-//        topicWrite = "APPC-UNIT-TEST";
-//        group = "APPC-CLIENT";
-//        groupId = "0";
+        System.out.println("setup entry...");
+        //        urls = new HashSet<String>();
+        //        urls.add("dmaaphost1");
+        //        urls.add("dmaaphost2");
+        //        //remove unavailable dmaap instance for build
+        //        //urls.add("dmaaphost3");
+        //
+        //        topicRead = "APPC-UNIT-TEST";
+        //        topicWrite = "APPC-UNIT-TEST";
+        //        group = "APPC-CLIENT";
+        //        groupId = "0";
         Configuration configuration = ConfigurationFactory.getConfiguration();
         List<String> hosts = Arrays.asList(configuration.getProperty("poolMembers").split(","));
         urls = new HashSet<String>(hosts);
@@ -83,18 +86,18 @@ public class TestConsumerProducerImpl {
     @Ignore
     @Test
     public void testWriteRead() {
-       System.out.println("testWriteRead entry...");
+        System.out.println("testWriteRead entry...");
         Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
 
         String s1 = UUID.randomUUID().toString();
         String s2 = UUID.randomUUID().toString();
         if (p.post("TEST", s1) == false) {
-               // try again - 2nd attempt may succeed if cambria client failed over
-               p.post("TEST", s1);
+            // try again - 2nd attempt may succeed if cambria client failed over
+            p.post("TEST", s1);
         }
         if (p.post("TEST", s2) == false) {
-               // try again - 2nd attempt may succeed if cambria client failed over
-               p.post("TEST", s2);
+            // try again - 2nd attempt may succeed if cambria client failed over
+            p.post("TEST", s2);
         }
 
         Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
@@ -102,7 +105,7 @@ public class TestConsumerProducerImpl {
         // if fetch is empty, try again - a 2nd attempt may succeed if
         // cambria client has failed over
         if ((out == null) || out.isEmpty()) {
-               out = c.fetch();
+            out = c.fetch();
         }
 
         assertNotNull(out);
@@ -118,19 +121,19 @@ public class TestConsumerProducerImpl {
     @Test
     @Ignore // Https Not support on jenkins server
     public void testWriteReadHttps() {
-       System.out.println("testWriteReadHttps entry...");
+        System.out.println("testWriteReadHttps entry...");
         Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
         p.useHttps(true);
 
         String s1 = UUID.randomUUID().toString();
         String s2 = UUID.randomUUID().toString();
         if (p.post("TEST", s1) == false) {
-               // try again - 2nd attempt may succeed if cambria client failed over
-               p.post("TEST", s1);
+            // try again - 2nd attempt may succeed if cambria client failed over
+            p.post("TEST", s1);
         }
         if (p.post("TEST", s2) == false) {
-               // try again - 2nd attempt may succeed if cambria client failed over
-               p.post("TEST", s2);
+            // try again - 2nd attempt may succeed if cambria client failed over
+            p.post("TEST", s2);
         }
 
         Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
@@ -140,7 +143,7 @@ public class TestConsumerProducerImpl {
         // if fetch is empty, try again - a 2nd attempt may succeed if
         // cambria client has failed over
         if ((out == null) || out.isEmpty()) {
-               out = c.fetch();
+            out = c.fetch();
         }
 
         assertNotNull(out);
@@ -153,7 +156,7 @@ public class TestConsumerProducerImpl {
     @Test
     @Ignore // requires connection to a live DMaaP server
     public void testBadUrl() {
-       System.out.println("testBadUrl entry...");
+        System.out.println("testBadUrl entry...");
         urls.clear();
         urls.add("something.local");
 
@@ -168,7 +171,7 @@ public class TestConsumerProducerImpl {
     @Test
     @Ignore // requires connection to a live DMaaP server
     public void testAuth() {
-       System.out.println("testAuth entry...");
+        System.out.println("testAuth entry...");
         Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
         Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
 
@@ -185,34 +188,34 @@ public class TestConsumerProducerImpl {
     @Ignore
     @Test
     public void testFailover() {
-       System.out.println("testFailover entry...");
-       urls.clear();
+        System.out.println("testFailover entry...");
+        urls.clear();
         urls.add("openecomp2.org");  // bad url
         urls.add("dmaaphost2");
         Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
 
         String s1 = UUID.randomUUID().toString();
         if (p.post("TEST", s1) == false) {
-               // try again - cambria client should have failed over
-               p.post("TEST", s1);
+            // try again - cambria client should have failed over
+            p.post("TEST", s1);
         }
 
         urls.clear();
         urls.add("openecomp3.org");  // bad url
         urls.add("dmaaphost3");
-        
+
         Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
         List<String> out = c.fetch(1000, 1000);
         // if fetch is empty, try again - cambria client should have failed over
         if ((out == null) || out.isEmpty()) {
-               out = c.fetch();
+            out = c.fetch();
         }
 
         assertNotNull(out);
         assertEquals(1, out.size());
         assertEquals(s1, out.get(0));
     }
-    
+
     /**
      * Reads through the entire topic so it is clean for testing. WARNING - ONLY USE ON TOPICS WHERE YOU ARE THE ONLY
      * WRITER. Could end in an infinite loop otherwise.
@@ -228,7 +231,7 @@ public class TestConsumerProducerImpl {
     @Test
     @Ignore
     public void testFilter() {
-       System.out.println("testFilter entry...");
+        System.out.println("testFilter entry...");
         List<String> res;
         String filter = "{\"class\":\"Assigned\",\"field\":\"request\"}";
         Consumer c = new DmaapConsumerImpl(urls, "DCAE-CLOSED-LOOP-EVENTS-DEV1510SIM", group, groupId,user,password,filter);
index c7f3837..0d48636 100644 (file)
@@ -6,6 +6,8 @@
  * ================================================================================
  * Modifications Copyright (C) 2018 IBM
  * ================================================================================
+ * 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. 
  * You may obtain a copy of the License at 
 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.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Properties;
-
 import org.junit.Ignore;
 import org.junit.Test;
-
-
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.onap.appc.configuration.Configuration;
+import org.onap.appc.configuration.ConfigurationFactory;
+import org.onap.appc.metricservice.MetricRegistry;
+import org.onap.appc.metricservice.MetricService;
+import org.onap.appc.metricservice.metric.DmaapRequestCounterBuilder;
+import org.onap.appc.metricservice.metric.DmaapRequestCounterMetric;
+import org.onap.appc.metricservice.metric.MetricBuilderFactory;
+import org.onap.appc.metricservice.metric.impl.MetricBuilderFactoryImpl;
+import org.onap.appc.metricservice.policy.PolicyBuilderFactory;
+import org.onap.appc.metricservice.policy.PublishingPolicy;
+import org.onap.appc.metricservice.policy.ScheduledPolicyBuilder;
+import org.osgi.framework.FrameworkUtil;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.Whitebox;
+import com.att.nsa.mr.client.MRClientFactory;
+import com.att.nsa.mr.client.MRConsumer;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ConfigurationFactory.class, FrameworkUtil.class, MRClientFactory.class})
 public class TestDmaapConsumerImpl {
     String[]           hostList = { "192.168.1.1" };
     Collection<String> hosts    = new HashSet<String>(Arrays.asList(hostList));
@@ -111,7 +137,7 @@ public class TestDmaapConsumerImpl {
        DmaapConsumerImpl consumer = new DmaapConsumerImpl(hosts, topic, group, id, key, secret);
 
         assertNotNull(consumer);
-        
+
         consumer.fetch(5000,500);
     }
 
@@ -159,23 +185,69 @@ public class TestDmaapConsumerImpl {
         assertEquals(true, consumer.isHttps());
 
     }
-    
+
     @Test
-    public void testGetClient() 
+    public void testGetClient() throws FileNotFoundException, IOException 
     {
-       DmaapConsumerImpl consumer = new DmaapConsumerImpl(hosts, topic, group, id, key, secret);
-       assertNotNull(consumer);    
-       consumer.getClient(1000,5);
-       Properties props= consumer.getProperties();
-       assertEquals("1000", props.getProperty("timeout"));
-       assertEquals("5", props.getProperty("limit"));
+        DmaapConsumerImpl consumer = new DmaapConsumerImpl(hosts, topic, group, id, key, secret);
+        assertNotNull(consumer);
+        PowerMockito.mockStatic(MRClientFactory.class);
+        PowerMockito.when(MRClientFactory.createConsumer(Mockito.anyString())).thenReturn(Mockito.mock(MRConsumer.class));
+        assertTrue(consumer.getClient(1000,5) instanceof MRConsumer);
+        Properties props= consumer.getProperties();
+        assertEquals("1000", props.getProperty("timeout"));
+        assertEquals("5", props.getProperty("limit"));
     }
-    
+
     @Test
-    public void testInitMetric() 
+    public void testGetClientExceptionFlow() throws FileNotFoundException, IOException 
     {
-       DmaapConsumerImpl consumer = new DmaapConsumerImpl(hosts, topic, group, id, key, secret);
-       assertNotNull(consumer); 
-       
+        DmaapConsumerImpl consumer = new DmaapConsumerImpl(hosts, topic, group, id, key, secret);
+        assertNotNull(consumer);
+        PowerMockito.mockStatic(MRClientFactory.class);
+        PowerMockito.when(MRClientFactory.createConsumer(Mockito.anyString())).thenThrow(new IOException());
+        assertFalse(consumer.getClient(1000,5) instanceof MRConsumer);
+        Properties props= consumer.getProperties();
+        assertEquals("1000", props.getProperty("timeout"));
+        assertEquals("5", props.getProperty("limit"));
     }
+
+    @Test
+    public void testInitMetric() throws FileNotFoundException, IOException 
+    {
+        Configuration configuration = Mockito.mock(Configuration.class);
+        Properties properties = new Properties();
+        properties.put("metric.enabled", "true");
+        Mockito.when(configuration.getProperties()).thenReturn(properties);
+        PowerMockito.mockStatic(MRClientFactory.class);
+        PowerMockito.when(MRClientFactory.createConsumer(Mockito.anyString())).thenThrow(new IOException());        
+        DmaapConsumerImpl consumer = Mockito.spy(new DmaapConsumerImpl(hosts, topic, group, id, key, secret));
+        Whitebox.setInternalState(consumer, "configuration", configuration);
+        MetricService metricService = Mockito.mock(MetricService.class);
+        MetricRegistry metricRegistry = Mockito.mock(MetricRegistry.class);
+        MetricBuilderFactory metricBuilderFactory = Mockito.spy(new MetricBuilderFactoryImpl());
+        DmaapRequestCounterBuilder builder = Mockito.mock(DmaapRequestCounterBuilder.class);
+        DmaapRequestCounterMetric metric = Mockito.mock(DmaapRequestCounterMetric.class);
+        Mockito.when(builder.withName(Mockito.anyString())).thenReturn(builder);
+        Mockito.when(builder.withType(Mockito.any())).thenReturn(builder);
+        Mockito.when(builder.withPublishedMessage(Mockito.anyLong())).thenReturn(builder);
+        Mockito.when(builder.withRecievedMessage(Mockito.anyLong())).thenReturn(builder);
+        Mockito.when(builder.build()).thenReturn(metric);
+        Mockito.when(metricBuilderFactory.dmaapRequestCounterBuilder()).thenReturn(builder);
+        Mockito.when(metricRegistry.register(Mockito.any())).thenReturn(true);
+        PublishingPolicy policy = Mockito.mock(PublishingPolicy.class);
+        PolicyBuilderFactory policyFactory = Mockito.mock(PolicyBuilderFactory.class);
+        Mockito.when(metricRegistry.policyBuilderFactory()).thenReturn(policyFactory);
+        ScheduledPolicyBuilder policyBuilder = Mockito.mock(ScheduledPolicyBuilder.class);
+        Mockito.when(policyBuilder.withPublishers(Mockito.any())).thenReturn(policyBuilder);
+        Mockito.when(policyBuilder.withMetrics(Mockito.any())).thenReturn(policyBuilder);
+        Mockito.when(policyBuilder.build()).thenReturn(policy);
+        Mockito.when(policyFactory.scheduledPolicyBuilder()).thenReturn(policyBuilder);
+        Mockito.when(metricRegistry.metricBuilderFactory()).thenReturn(metricBuilderFactory);
+        Mockito.when(metricService.createRegistry("APPC")).thenReturn(metricRegistry);
+        Mockito.doReturn(metricService).when(consumer).getMetricService();
+        consumer.fetch(1, 1);
+        Mockito.verify(policy).init();
+    }
+
 }