From a1194798f406efe32cb33510f1a9d58b0b5f9bd9 Mon Sep 17 00:00:00 2001 From: "beili.zhou" Date: Tue, 29 Aug 2017 18:33:57 -0400 Subject: Remove catch throwable and error Fix the following sonar blocker issue: - Catch Exception instead of Throwable - Catch Exception instead of Error Issue-Id: APPC-175 Change-Id: I16a1018001b067216740ec11e5a97e52d714db69 Signed-off-by: beili.zhou --- .../openecomp/appc/sdc/listener/AsdcCallback.java | 7 +-- .../appc/sdc/listener/AsdcCallbackTest.java | 58 +++++++++++++--------- 2 files changed, 38 insertions(+), 27 deletions(-) (limited to 'appc-asdc-listener/appc-asdc-listener-bundle/src') diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcCallback.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcCallback.java index 345149686..b5307d697 100644 --- a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcCallback.java +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcCallback.java @@ -83,15 +83,16 @@ public class AsdcCallback implements INotificationCallback { BundleContext bctx = FrameworkUtil.getBundle(EventSender.class).getBundleContext(); ServiceReference sref = bctx.getServiceReference(EventSender.class); eventSender = (EventSender) bctx.getService(sref); - } catch (Throwable t) { - logger.error("AsdcCallback failed on initializing EventSender", t); + } catch (Exception e) { + logger.error("AsdcCallback failed on initializing EventSender", e); } } if (isRunning.get()) { for(IArtifactInfo artifact:data.getServiceArtifacts()){ - ArtifactProcessor artifactProcessor = ArtifactProcessorFactory.getArtifactProcessor(client, eventSender, data, null, artifact, storeUri); + ArtifactProcessor artifactProcessor = ArtifactProcessorFactory.getArtifactProcessor( + client, eventSender, data, null, artifact, storeUri); if(artifactProcessor!=null){ executor.submit(artifactProcessor); } diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/AsdcCallbackTest.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/AsdcCallbackTest.java index b4ee459f2..3971ec248 100644 --- a/appc-asdc-listener/appc-asdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/AsdcCallbackTest.java +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/AsdcCallbackTest.java @@ -25,6 +25,7 @@ package org.openecomp.appc.sdc.listener; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Matchers; @@ -69,42 +70,45 @@ import java.util.List; ArtifactProcessorFactory.class}) public class AsdcCallbackTest { - IDistributionClient client; + private IDistributionClient client; private EventSender eventSender; private INotificationCallback asdcCallback; private ArtifactStorageService storageService; private ToscaCsarArtifactProcessor artifactProcessor; - @Before public void setup() throws Exception { client = PowerMockito.mock(IDistributionClient.class); eventSender = PowerMockito.mock(EventSender.class); asdcCallback = new AsdcCallback(null,client); - artifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0) - ,getServiceArtifacts().get(0),null)); + artifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor( + client, eventSender, getNotificationData(), getResources().get(0), + getServiceArtifacts().get(0),null)); storageService = PowerMockito.mock(ArtifactStorageService.class); Whitebox.setInternalState(artifactProcessor,"artifactStorageService", storageService); - PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact((IDistributionClientDownloadResult) Matchers.anyObject()); + PowerMockito.doCallRealMethod().when(artifactProcessor) + .processArtifact((IDistributionClientDownloadResult) Matchers.anyObject()); PowerMockito.doCallRealMethod().when(artifactProcessor).run(); - PowerMockito.mockStatic(ArtifactProcessorFactory.class); - PowerMockito.when(ArtifactProcessorFactory.getArtifactProcessor((IDistributionClient)Matchers.anyObject(), (EventSender)Matchers.anyObject(), + PowerMockito.when(ArtifactProcessorFactory.getArtifactProcessor((IDistributionClient)Matchers.anyObject(), + (EventSender)Matchers.anyObject(), (INotificationData)Matchers.anyObject(), (IResourceInstance)Matchers.anyObject(), (IArtifactInfo)Matchers.anyObject(), (URI)Matchers.anyObject())).thenReturn(artifactProcessor); Whitebox.setInternalState(asdcCallback,"eventSender", eventSender); PowerMockito.doReturn(readDownloadResult()).when(client).download((IArtifactInfo) Matchers.anyObject()); - PowerMockito.doReturn(null).when(client).sendDownloadStatus((IDistributionStatusMessage) Matchers.anyObject()); + PowerMockito.doReturn(null).when(client).sendDownloadStatus( + (IDistributionStatusMessage) Matchers.anyObject()); - PowerMockito.doReturn(null).when(storageService).retrieveSDCArtifact(Matchers.anyString(),Matchers.anyString(),Matchers.anyString()); + PowerMockito.doReturn(null).when(storageService).retrieveSDCArtifact(Matchers.anyString(), + Matchers.anyString(), Matchers.anyString()); PowerMockito.doAnswer(new Answer() { @Override - public Object answer(InvocationOnMock invocationOnMock) throws Throwable { + public Object answer(InvocationOnMock invocationOnMock) throws Exception { System.out.print(invocationOnMock.getArguments()[0].toString()); return null; } @@ -112,7 +116,8 @@ public class AsdcCallbackTest { } private IDistributionClientDownloadResult readDownloadResult() throws IOException, URISyntaxException { - DistributionClientDownloadResultImpl downloadResult = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS,"Download success"); + DistributionClientDownloadResultImpl downloadResult = new DistributionClientDownloadResultImpl( + DistributionActionResultEnum.SUCCESS,"Download success"); File file = new File(this.getClass().getResource("/csar/service-ServiceAppc-csar.csar").toURI()); byte[] bFile = new byte[(int) file.length()]; @@ -124,14 +129,12 @@ public class AsdcCallbackTest { return downloadResult; } - -// @Test - public void testASDCListener() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { - - + @Ignore + @Test + public void testASDCListener() + throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { INotificationData notificationData = getNotificationData(); asdcCallback.activateCallback(notificationData); - // pause(); } @@ -143,9 +146,11 @@ public class AsdcCallbackTest { // } // } - private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException { + private INotificationData getNotificationData() + throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException { - INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl"); + INotificationData notificationData = (INotificationData)getObject( + "org.openecomp.sdc.impl.NotificationDataImpl"); List serviceArtifacts = getServiceArtifacts(); @@ -153,9 +158,11 @@ public class AsdcCallbackTest { return notificationData; } - private List getResources() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException { + private List getResources() + throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException { List resources = new ArrayList<>(); - IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance"); + IResourceInstance resource = (IResourceInstance)getObject( + "org.openecomp.sdc.impl.JsonContainerResourceInstance"); List serviceArtifacts = getServiceArtifacts(); invokeMethod(resource,"setArtifacts",serviceArtifacts); @@ -166,7 +173,8 @@ public class AsdcCallbackTest { return resources; } - private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException { + private void invokeMethod(Object object, String methodName,Object... arguments) + throws IllegalAccessException, InvocationTargetException { Method[] methods = object.getClass().getDeclaredMethods(); for(Method method:methods){ if(methodName.equalsIgnoreCase(method.getName())){ @@ -176,13 +184,15 @@ public class AsdcCallbackTest { } } - private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { + private Object getObject(String fqcn) + throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { Constructor constructor = Class.forName(fqcn).getDeclaredConstructors()[0]; constructor.setAccessible(true); return constructor.newInstance(); } - private List getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException { + private List getServiceArtifacts() + throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException { List serviceArtifacts = new ArrayList<>(); IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl"); invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR"); -- cgit 1.2.3-korg