From 956e9c683052a92f6303d243df77cbaab0c775cf Mon Sep 17 00:00:00 2001 From: "beili.zhou" Date: Fri, 11 Aug 2017 15:43:00 -0400 Subject: [APPC-45] appc-dg-common start Fix appc-dg-common bundle start issue (does not occur at the APPC start up) by using blueprint for its instantiation. Issue-Id: APPC-45 Change-Id: I40887701269a44ba711a00aa12a551e672b34532 Signed-off-by: beili.zhou --- .../dg/common/impl/DCAEReporterPluginImpl.java | 28 +++++++---- .../resources/OSGI-INF/blueprint/blueprint.xml | 57 +++++++++++++--------- .../dg/common/impl/DCAEReporterPluginImplTest.java | 53 +++++++++----------- 3 files changed, 76 insertions(+), 62 deletions(-) (limited to 'appc-dg/appc-dg-shared/appc-dg-common/src') diff --git a/appc-dg/appc-dg-shared/appc-dg-common/src/main/java/org/openecomp/appc/dg/common/impl/DCAEReporterPluginImpl.java b/appc-dg/appc-dg-shared/appc-dg-common/src/main/java/org/openecomp/appc/dg/common/impl/DCAEReporterPluginImpl.java index 0f99f8314..dd667c270 100644 --- a/appc-dg/appc-dg-shared/appc-dg-common/src/main/java/org/openecomp/appc/dg/common/impl/DCAEReporterPluginImpl.java +++ b/appc-dg/appc-dg-shared/appc-dg-common/src/main/java/org/openecomp/appc/dg/common/impl/DCAEReporterPluginImpl.java @@ -33,9 +33,6 @@ import org.openecomp.appc.adapter.message.event.EventStatus; import org.openecomp.appc.dg.common.DCAEReporterPlugin; import org.openecomp.appc.exceptions.APPCException; import org.openecomp.sdnc.sli.SvcLogicContext; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; import java.util.Map; @@ -44,9 +41,16 @@ public class DCAEReporterPluginImpl implements DCAEReporterPlugin { private EventSender eventSender; public DCAEReporterPluginImpl() { - BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); - ServiceReference sref = bctx.getServiceReference(EventSender.class); - eventSender = (EventSender) bctx.getService(sref); + // do nothing + } + + /** + * Injected by blueprint + * + * @param eventSender to be set + */ + public void setEventSender(EventSender eventSender) { + this.eventSender = eventSender; } @Override @@ -62,7 +66,9 @@ public class DCAEReporterPluginImpl implements DCAEReporterPlugin { apiVersion = ctx.getAttribute("input.common-header.api-ver"); eventId = ctx.getAttribute("input.common-header.request-id"); - EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription)); + EventMessage eventMessage = new EventMessage(new EventHeader( + (new java.util.Date()).toString(), apiVersion, eventId), + new EventStatus(errorCode, errorDescription)); String eventWriteTopic = params.get("event-topic-name"); if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){ eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic); @@ -102,7 +108,9 @@ public class DCAEReporterPluginImpl implements DCAEReporterPlugin { if (null == successDescription) { successDescription = "Success"; } - EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(successReportCode, successDescription)); + EventMessage eventMessage = new EventMessage(new EventHeader( + (new java.util.Date()).toString(), apiVersion, eventId), + new EventStatus(successReportCode, successDescription)); String eventWriteTopic = params.get("event-topic-name"); if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){ eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic); @@ -120,7 +128,9 @@ public class DCAEReporterPluginImpl implements DCAEReporterPlugin { apiVersion = ctx.getAttribute("input.common-header.api-ver"); eventId = ctx.getAttribute("input.common-header.request-id"); - EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription)); + EventMessage eventMessage = new EventMessage(new EventHeader( + (new java.util.Date()).toString(), apiVersion, eventId), + new EventStatus(errorCode, errorDescription)); String eventWriteTopic = params.get("event-topic-name"); if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){ eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic); diff --git a/appc-dg/appc-dg-shared/appc-dg-common/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/appc-dg/appc-dg-shared/appc-dg-common/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 9c282f0ee..9014d9f9d 100644 --- a/appc-dg/appc-dg-shared/appc-dg-common/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/appc-dg/appc-dg-shared/appc-dg-common/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -27,41 +27,54 @@ Starter Blueprint Camel Definition appc-aai-adapter-blueprint --> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> - + - + - - + + + - - + - - + + - - + + - + - - + - - + + - + - + - + - - + + + + diff --git a/appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/openecomp/appc/dg/common/impl/DCAEReporterPluginImplTest.java b/appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/openecomp/appc/dg/common/impl/DCAEReporterPluginImplTest.java index 4534f8521..78a11fb48 100644 --- a/appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/openecomp/appc/dg/common/impl/DCAEReporterPluginImplTest.java +++ b/appc-dg/appc-dg-shared/appc-dg-common/src/test/java/org/openecomp/appc/dg/common/impl/DCAEReporterPluginImplTest.java @@ -24,13 +24,14 @@ package org.openecomp.appc.dg.common.impl; -import org.junit.*; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.*; import org.openecomp.appc.adapter.message.EventSender; import org.openecomp.appc.adapter.message.MessageDestination; import org.openecomp.appc.adapter.message.event.EventMessage; -import org.openecomp.appc.dg.common.impl.DCAEReporterPluginImpl; import org.openecomp.appc.exceptions.APPCException; import org.openecomp.sdnc.sli.SvcLogicContext; import org.osgi.framework.Bundle; @@ -50,35 +51,31 @@ public class DCAEReporterPluginImplTest { private SvcLogicContext ctx; private Map 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 BundleContext bundleContext = Mockito.mock(BundleContext.class); + private final Bundle bundleService = Mockito.mock(Bundle.class); + private final ServiceReference sref = Mockito.mock(ServiceReference.class); + @InjectMocks private DCAEReporterPluginImpl dcaeReporterPlugin; - private EventSenderMock eventSender; + @Spy + private EventSenderMock eventSender = new EventSenderMock(); private String apiVer = "2.0.0"; private String requestId = "123"; private String error = "test-error"; + @SuppressWarnings("unchecked") @Before public void setUp() throws NoSuchFieldException, IllegalAccessException { - eventSender = new EventSenderMock(); 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.getService(sref)).thenReturn(eventSender); - dcaeReporterPlugin = new DCAEReporterPluginImpl(); - } - - @Test public void testReportErrorDescriptionNullBwcModeFalse() throws Exception { - ctx = new SvcLogicContext(); params = new HashMap<>(); params.put("output.status.message", null); @@ -88,6 +85,17 @@ public class DCAEReporterPluginImplTest { errorReasonNullAssert(); } + @Test + public void testReportBwcFalse() throws Exception { + ctx = new SvcLogicContext(); + params = new HashMap<>(); + ctx.setAttribute("isBwcMode", "false"); + params.put("output.status.message", error); + ctx.setAttribute("input.common-header.api-ver", apiVer); + ctx.setAttribute("input.common-header.request-id", requestId); + + positiveAssert(); + } private void errorReasonNullAssert() throws APPCException { dcaeReporterPlugin.report(params, ctx); @@ -97,10 +105,8 @@ public class DCAEReporterPluginImplTest { Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId()); Assert.assertEquals("wrong error message", "Unknown", msg.getEventStatus().getReason()); Assert.assertEquals("wrong destination", destination.name(), "DCAE"); - } - private void positiveAssert() throws APPCException { dcaeReporterPlugin.report(params, ctx); MessageDestination destination = eventSender.getDestination(); @@ -109,20 +115,5 @@ public class DCAEReporterPluginImplTest { Assert.assertEquals("wrong requestId", requestId, msg.getEventHeader().getEventId()); Assert.assertEquals("wrong error message", error, msg.getEventStatus().getReason()); Assert.assertEquals("wrong destination", destination.name(), "DCAE"); - } - - - @Test - public void testReportBwcFalse() throws Exception { - ctx = new SvcLogicContext(); - params = new HashMap<>(); - ctx.setAttribute("isBwcMode", "false"); - params.put("output.status.message", error); - ctx.setAttribute("input.common-header.api-ver", apiVer); - ctx.setAttribute("input.common-header.request-id", requestId); - - positiveAssert(); - - } - } +} -- cgit 1.2.3-korg