Fix for test coverage in dg common impl package 61/75161/1
authorJoss Armstrong <joss.armstrong@ericsson.com>
Mon, 31 Dec 2018 16:18:34 +0000 (16:18 +0000)
committerJoss Armstrong <joss.armstrong@ericsson.com>
Mon, 31 Dec 2018 16:18:54 +0000 (16:18 +0000)
Increased test coverage from 42% to 73%

Issue-ID: APPC-1296
Change-Id: Ib36c86c325bbc49a14a8ae7076a3b3da14c89b6f
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
appc-dg/appc-dg-shared/appc-dg-common/src/main/java/org/onap/appc/dg/common/impl/IntermediateMessageSenderImpl.java
appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/impl/DCAEReporterPluginImplTest.java
appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/impl/EventSenderMock.java
appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/impl/IntermediateMessageSenderImplTest.java [new file with mode: 0644]
appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/impl/JsonDgUtilImplTest.java
appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/impl/TestVNFConfiguratorImpl.java
appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/impl/VNFCResolverTest.java [new file with mode: 0644]
appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/impl/VnfExecutionFlowImplTest.java
appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/objects/TestConnectionDetails.java
appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/utils/JAXBUtilTest.java
appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/utils/JSONUtilTest.java

index d5bf8a9..4cef625 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications (C) 2018 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -36,7 +38,6 @@ import java.util.Set;
 import org.apache.commons.lang3.StringUtils;
 import org.onap.appc.adapter.message.MessageAdapterFactory;
 import org.onap.appc.adapter.message.Producer;
-import org.onap.appc.configuration.Configuration;
 import org.onap.appc.configuration.ConfigurationFactory;
 import org.onap.appc.dg.common.IntermediateMessageSender;
 import org.onap.appc.exceptions.APPCException;
@@ -82,8 +83,8 @@ public class IntermediateMessageSenderImpl implements IntermediateMessageSender
         if (ctx != null) {
             ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
             if (svcRef != null) {
-                producer = ((MessageAdapterFactory) ctx.getService(svcRef))
-                    .createProducer(pool, writeTopic, apiKey, apiSecret);
+                MessageAdapterFactory messageAdapterFactory = ((MessageAdapterFactory) ctx.getService(svcRef));
+                    producer = messageAdapterFactory.createProducer(pool, writeTopic, apiKey, apiSecret);
             }
         }
     }
index 71b8062..95f7ba7 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications (C) 2018 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,6 +25,8 @@
 
 package org.onap.appc.dg.common.impl;
 
+import java.util.HashMap;
+import java.util.Map;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -43,12 +47,10 @@ import org.osgi.framework.ServiceReference;
 import org.powermock.api.mockito.PowerMockito;
 import org.powermock.core.classloader.annotations.PrepareForTest;
 import org.powermock.modules.junit4.PowerMockRunner;
-import java.util.HashMap;
-import java.util.Map;
 
 
 @RunWith(PowerMockRunner.class)
-@PrepareForTest({DCAEReporterPluginImpl.class, FrameworkUtil.class})
+@PrepareForTest(FrameworkUtil.class)
 public class DCAEReporterPluginImplTest {
     private SvcLogicContext ctx;
     private Map<String, String> params;
@@ -99,6 +101,29 @@ public class DCAEReporterPluginImplTest {
         positiveAssert();
     }
 
+    @Test
+    public void testReportErrorDefinition() throws APPCException {
+        ctx = new SvcLogicContext();
+        params = new HashMap<>();
+        params.put(Constants.EVENT_MESSAGE, "ERROR DESCRIPTION");
+        params.put(Constants.DG_ERROR_CODE, "200");
+        ctx.setAttribute("input.common-header.api-ver", apiVer);
+        ctx.setAttribute("input.common-header.request-id", requestId);
+        dcaeReporterPlugin.report(params, ctx);
+        EventMessage msg = eventSender.getMsg();
+        Assert.assertEquals("ERROR DESCRIPTION", msg.getEventStatus().getReason());
+    }
+
+    @Test
+    public void testReportSuccess() throws APPCException {
+        ctx = new SvcLogicContext();
+        params = new HashMap<>();
+        params.put(Constants.DG_ERROR_CODE, "200");
+        dcaeReporterPlugin.reportSuccess(params, ctx);
+        EventMessage msg = eventSender.getMsg();
+        Assert.assertEquals(new Integer(500), msg.getEventStatus().getCode());
+    }
+
     private void errorReasonNullAssert() throws APPCException {
         dcaeReporterPlugin.report(params, ctx);
         MessageDestination destination = eventSender.getDestination();
index 7747391..5495995 100644 (file)
@@ -24,7 +24,6 @@
 package org.onap.appc.dg.common.impl;
 
 import java.util.Map;
-
 import org.onap.appc.adapter.message.EventSender;
 import org.onap.appc.adapter.message.MessageDestination;
 import org.onap.appc.adapter.message.event.EventMessage;
diff --git a/appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/impl/IntermediateMessageSenderImplTest.java b/appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/impl/IntermediateMessageSenderImplTest.java
new file mode 100644 (file)
index 0000000..74c1ec7
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2018 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.dg.common.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Matchers;
+import org.mockito.Mockito;
+import org.mockito.Spy;
+import org.onap.appc.adapter.message.MessageAdapterFactory;
+import org.onap.appc.adapter.message.Producer;
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(FrameworkUtil.class)
+public class IntermediateMessageSenderImplTest {
+
+    private SvcLogicContext ctx;
+    private Map<String, String> params;
+
+    private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
+    private final Bundle bundleService = Mockito.mock(Bundle.class);
+    private final ServiceReference sref = Mockito.mock(ServiceReference.class);
+    private final MessageAdapterFactory mockFactory = Mockito.mock(MessageAdapterFactory.class);
+    private final Producer mockProducer = Mockito.mock(Producer.class);
+
+    @InjectMocks
+    private IntermediateMessageSenderImpl intermediateMessageSenderImpl;
+
+    @Spy
+    private EventSenderMock eventSender = new EventSenderMock();
+
+    @SuppressWarnings("unchecked")
+    @Before
+    public void setUp() throws NoSuchFieldException, IllegalAccessException {
+        PowerMockito.mockStatic(FrameworkUtil.class);
+        PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
+        PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
+        PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
+        PowerMockito.when(bundleContext.<MessageAdapterFactory>getService(sref)).thenReturn(mockFactory);
+        PowerMockito.when(mockFactory.createProducer(Matchers.anyCollection(), Mockito.anyString(), Mockito.anyString(),
+                Mockito.anyString())).thenReturn(mockProducer);
+    }
+
+    @Test
+    public void testSendEmptyMessage() {
+        intermediateMessageSenderImpl.init();
+        ctx = new SvcLogicContext();
+        params = new HashMap<>();
+        intermediateMessageSenderImpl.sendMessage(params, ctx);
+        Assert.assertEquals("FAILURE", ctx.getAttribute("STATUS"));
+    }
+
+    @Test
+    public void testSendMessage() {
+        intermediateMessageSenderImpl.init();
+        ctx = new SvcLogicContext();
+        ctx.setAttribute("input.common-header.request-id", "REQUEST-ID");
+        params = new HashMap<>();
+        params.put("message", "TEST MESSAGE");
+        params.put("code", "TEST CODE");
+        intermediateMessageSenderImpl.sendMessage(params, ctx);
+        Assert.assertEquals("SUCCESS", ctx.getAttribute("STATUS"));
+    }
+}
index 8adaaee..950cb2f 100644 (file)
@@ -27,11 +27,9 @@ package org.onap.appc.dg.common.impl;
 
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-
 import java.text.SimpleDateFormat;
 import java.util.HashMap;
 import java.util.Map;
-
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
index 21213ca..8b08fc2 100644 (file)
 
 package org.onap.appc.dg.common.impl;
 
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Matchers;
 import org.onap.appc.dg.common.VNFConfigurator;
 import org.onap.appc.exceptions.APPCException;
 import org.onap.appc.mdsal.MDSALStore;
 import org.onap.appc.mdsal.impl.MDSALStoreFactory;
 import org.onap.appc.mdsal.impl.MDSALStoreImpl;
 import org.onap.appc.mdsal.objects.BundleInfo;
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Matchers;
 import org.powermock.api.mockito.PowerMockito;
 import org.powermock.core.classloader.annotations.PrepareForTest;
 import org.powermock.modules.junit4.PowerMockRunner;
-
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 
 @RunWith(PowerMockRunner.class)
 @PrepareForTest({MDSALStoreImpl.class,MDSALStoreFactory.class})
diff --git a/appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/impl/VNFCResolverTest.java b/appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/onap/appc/dg/common/impl/VNFCResolverTest.java
new file mode 100644 (file)
index 0000000..fce57f3
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2018 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.dg.common.impl;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+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.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.onap.appc.dao.util.DBUtils;
+import org.onap.appc.rankingframework.AbstractRankedAttributesResolverFactory;
+
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({DBUtils.class, AbstractRankedAttributesResolverFactory.class})
+public class VNFCResolverTest {
+
+    @Rule
+    public ExpectedException expectedEx = ExpectedException.none();
+
+    @Before
+    public void setUp() throws NoSuchFieldException, IllegalAccessException, SQLException {
+        PowerMockito.mockStatic(DBUtils.class);
+        Connection mockConnection = Mockito.mock(Connection.class);
+        PreparedStatement mockStatement = Mockito.mock(PreparedStatement.class);
+        ResultSet mockResultSet = Mockito.mock(ResultSet.class);
+        PowerMockito.when(DBUtils.getConnection(Mockito.anyString())).thenReturn(mockConnection);
+        PowerMockito.when(mockConnection.prepareStatement(Mockito.anyString())).thenReturn(mockStatement);
+        PowerMockito.when(mockStatement.executeQuery()).thenReturn(mockResultSet);
+        PowerMockito.when(mockResultSet.next()).thenReturn(true);
+    }
+
+    @Test
+    public void test() {
+        VNFCResolver resolver = (VNFCResolver) ResolverFactory.createResolver("VNFC");
+        expectedEx.expect(IllegalArgumentException.class);
+        expectedEx.expectMessage("Duplicated configuration entry:");
+        FlowKey flowKey = resolver.resolve("Start", "null", "null", "null");
+    }
+
+}
index c03f8a1..2efda6b 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications (C) 2018 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.appc.dg.common.impl;
 
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -42,14 +46,12 @@ 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 java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
+import org.powermock.reflect.Whitebox;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 
 @RunWith(PowerMockRunner.class)
-@PrepareForTest({VnfExecutionFlowImplTest.class, FrameworkUtil.class,DependencyManager.class,DependencyModelFactory.class})
+@PrepareForTest({FrameworkUtil.class,DependencyManager.class,DependencyModelFactory.class})
 @SuppressWarnings("unchecked")
 public class VnfExecutionFlowImplTest {
 
@@ -74,6 +76,7 @@ public class VnfExecutionFlowImplTest {
                 .thenReturn(dependencyModel);
 
         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
+        Whitebox.setInternalState(vnfExecutionFlow, "logger", logger);
         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
     }
 
@@ -91,6 +94,7 @@ public class VnfExecutionFlowImplTest {
                 .thenReturn(dependencyModel);
 
         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
+        Whitebox.setInternalState(vnfExecutionFlow, "logger", logger);
         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
     }
 
@@ -107,6 +111,7 @@ public class VnfExecutionFlowImplTest {
                 .thenReturn(dependencyModel);
 
         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
+        Whitebox.setInternalState(vnfExecutionFlow, "logger", logger);
         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
     }
 
index 37be0e1..50429f6 100644 (file)
@@ -23,16 +23,14 @@ package org.onap.appc.dg.common.utils;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
-
-import org.junit.Before;
-import org.junit.Test;
-import java.util.HashMap;
-import java.util.Map;
+import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
-import java.io.FileNotFoundException;
-
 import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.Map;
+import org.junit.Before;
+import org.junit.Test;
 
 
 public class JSONUtilTest {