diff options
author | Joss Armstrong <joss.armstrong@ericsson.com> | 2019-02-12 17:33:13 +0000 |
---|---|---|
committer | Takamune Cho <takamune.cho@att.com> | 2019-02-12 19:42:32 +0000 |
commit | 80af84927200d30a9c0deb923a24c757db0b8b83 (patch) | |
tree | 115ca2213720f4cee8fcdafcd8153bdfe0297941 /appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src | |
parent | a264d9e09fbc24add3b24746ddf628f0dcc1b7a5 (diff) |
Test coverage in DmaapProducerImpl
Increased coverage to 80%
Issue-ID: APPC-1415
Change-Id: Ie4817c5bdf514d4fe7f68b73812bbc2383c8cbbe
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
Diffstat (limited to 'appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src')
2 files changed, 79 insertions, 8 deletions
diff --git a/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/main/java/org/onap/appc/adapter/messaging/dmaap/impl/DmaapProducerImpl.java b/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/main/java/org/onap/appc/adapter/messaging/dmaap/impl/DmaapProducerImpl.java index ee4061c94..a2f2c9d2b 100644 --- a/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/main/java/org/onap/appc/adapter/messaging/dmaap/impl/DmaapProducerImpl.java +++ b/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/main/java/org/onap/appc/adapter/messaging/dmaap/impl/DmaapProducerImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2019 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. @@ -51,10 +53,11 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; + public class DmaapProducerImpl implements Producer { private static final EELFLogger LOG = EELFManager.getInstance().getLogger(DmaapProducerImpl.class); - private static final Configuration configuration = ConfigurationFactory.getConfiguration(); + private static Configuration configuration = ConfigurationFactory.getConfiguration(); private Set<String> topics; @@ -199,7 +202,7 @@ public class DmaapProducerImpl implements Producer { 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) { diff --git a/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/test/java/org/onap/appc/adapter/messaging/dmaap/impl/TestDmaapProducerImpl.java b/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/test/java/org/onap/appc/adapter/messaging/dmaap/impl/TestDmaapProducerImpl.java index e16e05995..206b5a669 100644 --- a/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/test/java/org/onap/appc/adapter/messaging/dmaap/impl/TestDmaapProducerImpl.java +++ b/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/test/java/org/onap/appc/adapter/messaging/dmaap/impl/TestDmaapProducerImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. * ============================================================================= * 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. @@ -25,18 +27,38 @@ package org.onap.appc.adapter.messaging.dmaap.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -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 java.util.Set; - import org.junit.Before; -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.MRBatchingPublisher; +import com.att.nsa.mr.client.MRClientFactory; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ConfigurationFactory.class, FrameworkUtil.class, MRClientFactory.class}) public class TestDmaapProducerImpl { String[] hostList = { "192.168.1.1" }; Collection<String> hosts = new HashSet<String>(Arrays.asList(hostList)); @@ -123,6 +145,7 @@ public class TestDmaapProducerImpl { @Test public void testPost() { + producer = new DmaapProducerImpl(hosts, topic, key, secret); boolean successful = producer.post("partition", "data"); assertEquals(true, successful); } @@ -136,9 +159,9 @@ public class TestDmaapProducerImpl { producer.close(); } - @Test public void testCloseWithClient() { + producer = new DmaapProducerImpl(hosts, topic, key, secret); producer.post("partition", "data"); assertNotNull(producer); producer.close(); @@ -158,4 +181,49 @@ public class TestDmaapProducerImpl { } + @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()); + DmaapProducerImpl producer = Mockito.spy(new DmaapProducerImpl(hosts, topic, key, secret)); + Whitebox.setInternalState(DmaapProducerImpl.class, "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.doNothing().when(metric).incrementPublishedMessage(); + 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.metric("DMAAP_KPI")).thenReturn(metric); + 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(producer).getMetricservice(); + Set<MRBatchingPublisher> clients = new HashSet<>(); + MRBatchingPublisher client = Mockito.mock(MRBatchingPublisher.class); + clients.add(client); + Mockito.doReturn(0).when(client).send(Mockito.anyString(), Mockito.anyString()); + Whitebox.setInternalState(producer, "clients", clients); + producer.post(null, null); + Mockito.verify(policy).init(); + } + } |