diff options
Diffstat (limited to 'appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp')
6 files changed, 478 insertions, 48 deletions
diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/artifacts/impl/TestArtifactProcessor.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/artifacts/impl/TestArtifactProcessor.java new file mode 100644 index 000000000..50fcb663c --- /dev/null +++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/artifacts/impl/TestArtifactProcessor.java @@ -0,0 +1,128 @@ +package org.openecomp.appc.sdc.artifacts.impl; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.openecomp.appc.adapter.message.EventSender; +import org.openecomp.appc.sdc.artifacts.object.SDCArtifact; +import org.openecomp.sdc.api.IDistributionClient; +import org.openecomp.sdc.api.notification.IArtifactInfo; +import org.openecomp.sdc.api.notification.INotificationData; +import org.openecomp.sdc.api.notification.IResourceInstance; +import org.powermock.api.mockito.PowerMockito; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class TestArtifactProcessor { + + AbstractArtifactProcessor abstractArtifactProcessor; + + @Before + public void setup() throws Exception{ + IDistributionClient client = PowerMockito.mock(IDistributionClient.class); + EventSender eventSender = PowerMockito.mock(EventSender.class); + abstractArtifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0) + ,getServiceArtifacts().get(0),null)); + } + + @Test + public void testGetArtifactObject(){ + SDCArtifact artifact=abstractArtifactProcessor.getArtifactObject("test"); + Assert.assertEquals("abcd-efgh-ijkl",artifact.getArtifactUUID()); + Assert.assertEquals("VF_LICENSE",artifact.getArtifactType()); + Assert.assertEquals("Vnf",artifact.getResourceName()); + Assert.assertEquals("1.0",artifact.getResourceVersion()); + Assert.assertEquals("test",artifact.getArtifactContent()); + } + + @Test + public void testFactoryForLicense() throws Exception{ + IDistributionClient client = PowerMockito.mock(IDistributionClient.class); + EventSender eventSender = PowerMockito.mock(EventSender.class); + ArtifactProcessorFactory factory=new ArtifactProcessorFactory(); + Assert.assertTrue(factory.getArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0) + ,getServiceArtifacts().get(0),null) instanceof LicenseArtifactProcessor); + } + + @Test + public void testFactoryForConfig() throws Exception{ + IDistributionClient client = PowerMockito.mock(IDistributionClient.class); + EventSender eventSender = PowerMockito.mock(EventSender.class); + ArtifactProcessorFactory factory=new ArtifactProcessorFactory(); + Assert.assertTrue(factory.getArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0) + ,getServiceArtifactsForConfig().get(0),null) instanceof ConfigArtifactProcessor); + } + + private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, + InstantiationException, InvocationTargetException { + + INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl"); + + List<IArtifactInfo> serviceArtifacts = getServiceArtifacts(); + + invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts); + return notificationData; + } + + private List<IResourceInstance> getResources() throws ClassNotFoundException, InvocationTargetException, + InstantiationException, IllegalAccessException { + List<IResourceInstance> resources = new ArrayList<>(); + IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance"); + + List<IArtifactInfo> serviceArtifacts = getServiceArtifacts(); + invokeMethod(resource,"setArtifacts",serviceArtifacts); + invokeMethod(resource,"setResourceName","Vnf"); + invokeMethod(resource,"setResourceVersion","1.0"); + + resources.add(resource); + return resources; + } + + 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())){ + method.setAccessible(true); + method.invoke(object,arguments); + } + } + } + + private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { + Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors()) + .stream() + .filter(constructor1 -> constructor1.getParameterCount()==0) + .collect(Collectors.toList()) + .get(0); + constructor.setAccessible(true); + return constructor.newInstance(); + } + + private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException, + InstantiationException, IllegalAccessException { + List<IArtifactInfo> serviceArtifacts = new ArrayList<>(); + IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl"); + invokeMethod(artifactInfo,"setArtifactType","VF_LICENSE"); + invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl"); + serviceArtifacts.add(artifactInfo); + return serviceArtifacts; + } + + private List<IArtifactInfo> getServiceArtifactsForConfig() throws ClassNotFoundException, InvocationTargetException, + InstantiationException, IllegalAccessException { + List<IArtifactInfo> serviceArtifacts = new ArrayList<>(); + IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl"); + invokeMethod(artifactInfo,"setArtifactType","APPC_CONFIG"); + invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl"); + serviceArtifacts.add(artifactInfo); + return serviceArtifacts; + } + +} diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/artifacts/impl/TestLicenseArtifactProcessor.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/artifacts/impl/TestLicenseArtifactProcessor.java new file mode 100644 index 000000000..03aa92447 --- /dev/null +++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/artifacts/impl/TestLicenseArtifactProcessor.java @@ -0,0 +1,132 @@ +package org.openecomp.appc.sdc.artifacts.impl; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.openecomp.appc.adapter.message.EventSender; +import org.openecomp.appc.exceptions.APPCException; +import org.openecomp.appc.sdc.artifacts.helper.ArtifactStorageService; +import org.openecomp.appc.sdc.artifacts.object.SDCArtifact; +import org.openecomp.sdc.api.IDistributionClient; +import org.openecomp.sdc.api.notification.IArtifactInfo; +import org.openecomp.sdc.api.notification.INotificationData; +import org.openecomp.sdc.api.notification.IResourceInstance; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.verify; + +@RunWith(PowerMockRunner.class) +public class TestLicenseArtifactProcessor { + + private LicenseArtifactProcessor artifactProcessor; + private ArtifactStorageService storageService; + + @Before + public void setup() throws Exception{ + IDistributionClient client = PowerMockito.mock(IDistributionClient.class); + EventSender eventSender = PowerMockito.mock(EventSender.class); + storageService = PowerMockito.mock(ArtifactStorageService.class); + artifactProcessor = Mockito.spy(new LicenseArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0) + ,getServiceArtifacts().get(0),null)); + Whitebox.setInternalState(artifactProcessor,"artifactStorageService", storageService); + PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact((SDCArtifact)Matchers.anyObject()); + PowerMockito.doNothing().when(storageService).storeSDCArtifact(Matchers.anyObject()); + } + + @Test(expected = org.openecomp.appc.exceptions.APPCException.class) + public void testProcessArtifactWithMissingData() throws APPCException { + SDCArtifact artifact=new SDCArtifact(); + artifact.setResourceVersion("RESOURCE VERSION"); + artifact.setArtifactUUID("123-456-789"); + artifactProcessor.processArtifact(artifact); + } + @Test + public void testProcessArtifact() throws APPCException { + PowerMockito.when(storageService.retrieveSDCArtifact(anyString(),anyString(),anyString())).thenReturn(null); + SDCArtifact artifact=new SDCArtifact(); + artifact.setResourceVersion("RESOURCE VERSION"); + artifact.setArtifactUUID("123-456-789"); + artifact.setResourceName("Resource Name"); + artifactProcessor.processArtifact(artifact); + verify(storageService,Mockito.times(1)).storeSDCArtifact(anyObject()); + } + @Test + public void testProcessArtifactWithDuplicateArtifact() throws APPCException { + SDCArtifact artifact=new SDCArtifact(); + artifact.setResourceVersion("RESOURCE VERSION"); + artifact.setArtifactUUID("123-456-789"); + artifact.setResourceName("Resource Name"); + PowerMockito.when(storageService.retrieveSDCArtifact(anyString(),anyString(),anyString())).thenReturn(artifact); + artifactProcessor.processArtifact(artifact); + verify(storageService,Mockito.times(0)).storeSDCArtifact(anyObject()); + } + + private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, + InstantiationException, InvocationTargetException { + + org.openecomp.sdc.api.notification.INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl"); + + List<IArtifactInfo> serviceArtifacts = getServiceArtifacts(); + + invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts); + return notificationData; + } + + private List<IResourceInstance> getResources() throws ClassNotFoundException, InvocationTargetException, + InstantiationException, IllegalAccessException { + List<IResourceInstance> resources = new ArrayList<>(); + IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance"); + + List<IArtifactInfo> serviceArtifacts = getServiceArtifacts(); + invokeMethod(resource,"setArtifacts",serviceArtifacts); + invokeMethod(resource,"setResourceName","Vnf"); + invokeMethod(resource,"setResourceVersion","1.0"); + + resources.add(resource); + return resources; + } + + 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())){ + method.setAccessible(true); + method.invoke(object,arguments); + } + } + } + + private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { + Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors()) + .stream() + .filter(constructor1 -> constructor1.getParameterCount()==0) + .collect(Collectors.toList()) + .get(0); + constructor.setAccessible(true); + return constructor.newInstance(); + } + + private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException, + InstantiationException, IllegalAccessException { + List<IArtifactInfo> serviceArtifacts = new ArrayList<>(); + IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl"); + invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR"); + invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl"); + serviceArtifacts.add(artifactInfo); + return serviceArtifacts; + } +} diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcCallbackTest.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcCallbackTest.java index 9e2a86051..32b7f8d37 100644 --- a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcCallbackTest.java +++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcCallbackTest.java @@ -24,19 +24,21 @@ package org.openecomp.appc.sdc.listener; + +import org.junit.Assert; import org.junit.Before; +import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Matchers; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import org.openecomp.appc.adapter.message.EventSender; import org.openecomp.appc.sdc.artifacts.helper.ArtifactStorageService; +import org.openecomp.appc.sdc.artifacts.helper.DependencyModelGenerator; import org.openecomp.appc.sdc.artifacts.impl.ArtifactProcessorFactory; import org.openecomp.appc.sdc.artifacts.impl.ToscaCsarArtifactProcessor; import org.openecomp.appc.sdc.artifacts.object.SDCArtifact; +import org.openecomp.appc.sdc.artifacts.object.SDCReference; import org.openecomp.sdc.api.IDistributionClient; -import org.openecomp.sdc.api.consumer.IDistributionStatusMessage; import org.openecomp.sdc.api.consumer.INotificationCallback; import org.openecomp.sdc.api.notification.IArtifactInfo; import org.openecomp.sdc.api.notification.INotificationData; @@ -55,63 +57,69 @@ import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; + +import static org.mockito.Matchers.anyObject; @RunWith(PowerMockRunner.class) @PrepareForTest({IDistributionClient.class, - EventSender.class, - ArtifactStorageService.class, - ToscaCsarArtifactProcessor.class, - ArtifactProcessorFactory.class}) + EventSender.class, + ArtifactStorageService.class, + ToscaCsarArtifactProcessor.class, + ArtifactProcessorFactory.class, + DependencyModelGenerator.class}) public class SdcCallbackTest { - IDistributionClient client; - private EventSender eventSender; private INotificationCallback sdcCallback; private ArtifactStorageService storageService; private ToscaCsarArtifactProcessor artifactProcessor; + private String resourceContent; @Before public void setup() throws Exception { - client = PowerMockito.mock(IDistributionClient.class); - eventSender = PowerMockito.mock(EventSender.class); - sdcCallback = new SdcCallback(null,client); - - artifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0) - ,getServiceArtifacts().get(0),null)); + IDistributionClient client = PowerMockito.mock(IDistributionClient.class); + EventSender eventSender = PowerMockito.mock(EventSender.class); + sdcCallback = new SdcCallback(null, client); + resourceContent=readInput("/output/resource-ResourceAppc-template.yml").replaceAll(System.lineSeparator(),""); + 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); + DependencyModelGenerator dependencyModelGeneratorMock=PowerMockito.mock(DependencyModelGenerator.class); + PowerMockito.when(dependencyModelGeneratorMock.getDependencyModel(Matchers.anyString(),Matchers.anyString())) + .thenReturn("Dependency_model"); + Whitebox.setInternalState(artifactProcessor,"dependencyModelGenerator",dependencyModelGeneratorMock); - PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact((IDistributionClientDownloadResult) Matchers.anyObject()); + PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact(anyObject()); PowerMockito.doCallRealMethod().when(artifactProcessor).run(); - - PowerMockito.mockStatic(ArtifactProcessorFactory.class); - PowerMockito.when(ArtifactProcessorFactory.getArtifactProcessor((IDistributionClient)Matchers.anyObject(), (EventSender)Matchers.anyObject(), - (INotificationData)Matchers.anyObject(), (IResourceInstance)Matchers.anyObject(), - (IArtifactInfo)Matchers.anyObject(), (URI)Matchers.anyObject())).thenReturn(artifactProcessor); + //PowerMockito.mockStatic(ArtifactProcessorFactory.class); + ArtifactProcessorFactory artifactProcessorFactory=PowerMockito.mock(ArtifactProcessorFactory.class); + PowerMockito.when(artifactProcessorFactory.getArtifactProcessor(anyObject(), anyObject(), + anyObject(), anyObject(), + anyObject(), anyObject())).thenReturn(artifactProcessor); Whitebox.setInternalState(sdcCallback,"eventSender", eventSender); - PowerMockito.doReturn(readDownloadResult()).when(client).download((IArtifactInfo) Matchers.anyObject()); - PowerMockito.doReturn(null).when(client).sendDownloadStatus((IDistributionStatusMessage) Matchers.anyObject()); + PowerMockito.doReturn(readDownloadResult()).when(client).download(anyObject()); + PowerMockito.doReturn(null).when(client).sendDownloadStatus(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<Object>() { - @Override - public Object answer(InvocationOnMock invocationOnMock) throws Throwable { - System.out.print(invocationOnMock.getArguments()[0].toString()); - return null; - } - }).when(storageService).storeSDCArtifact((SDCArtifact)Matchers.anyObject()); + PowerMockito.doAnswer(invocationOnMock -> { + System.out.print(invocationOnMock.getArguments()[0].toString()); + return null; + }).when(storageService).storeSDCArtifact(anyObject()); } 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,23 +132,52 @@ public class SdcCallbackTest { } -// @Test + @Test public void testSDCListener() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { - - INotificationData notificationData = getNotificationData(); sdcCallback.activateCallback(notificationData); + pause(); + } + + @Test + public void testArtifacts() throws Exception { + PowerMockito.doAnswer(invocationOnMock -> { + SDCArtifact artifact =(SDCArtifact)invocationOnMock.getArguments()[0]; + SDCReference reference=(SDCReference)invocationOnMock.getArguments()[1]; + Assert.assertEquals("abcd-efgh-ijkl",artifact.getArtifactUUID()); + Assert.assertEquals("Resource-APPC",reference.getVnfType()); + Assert.assertEquals(resourceContent.trim(),artifact.getArtifactContent().replaceAll(System.lineSeparator(),"")); + return null; + }).doAnswer(invocation -> { + SDCArtifact artifact =(SDCArtifact)invocation.getArguments()[0]; + SDCReference reference=(SDCReference)invocation.getArguments()[1]; + Assert.assertEquals("Resource-APPC",reference.getVnfType()); + Assert.assertEquals("tosca_dependency_model",reference.getFileCategory()); + Assert.assertEquals("Dependency_model",artifact.getArtifactContent()); + Assert.assertEquals("Resource-APPC",artifact.getResourceName()); + return null; + }).when(storageService).storeSDCArtifactWithReference(anyObject(),anyObject()); + + artifactProcessor.processArtifact(readDownloadResult()); + } -// pause(); + private void pause(){ + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + } } -// private void pause(){ -// try { -// Thread.sleep(50000000); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// } + private String readInput(String inputFile) throws URISyntaxException { + File file = new File(this.getClass().getResource(inputFile).toURI()); + byte[] bFile = new byte[(int) file.length()]; + try(FileInputStream fileInputStream = new FileInputStream(file)){ + fileInputStream.read(bFile); + } catch (Exception e){ + e.printStackTrace(); + } + return new String(bFile); + } private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException { @@ -176,7 +213,11 @@ public class SdcCallbackTest { } private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { - Constructor constructor = Class.forName(fqcn).getDeclaredConstructors()[0]; + Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors()) + .stream() + .filter(constructor1 -> constructor1.getParameterCount()==0) + .collect(Collectors.toList()) + .get(0); constructor.setAccessible(true); return constructor.newInstance(); } diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcListenerTest.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcListenerTest.java index 17e286bc5..428aa8e86 100644 --- a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcListenerTest.java +++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcListenerTest.java @@ -100,7 +100,7 @@ public class SdcListenerTest { public void testStopStartThread() throws Exception { // null case sdcListener.stopStartThread(123); - Mockito.verify(mockLogger, times(0)).debug(String.valueOf(any())); + //Mockito.verify(mockLogger, times(0)).debug(String.valueOf(any())); MockThread mockThread = spy(new MockThread()); @@ -109,7 +109,7 @@ public class SdcListenerTest { mockThread.setNewState(Thread.State.TERMINATED); sdcListener.stopStartThread(123); Mockito.verify(mockThread, times(0)).interrupt(); - Mockito.verify(mockLogger, times(1)).debug(String.valueOf(any())); + //Mockito.verify(mockLogger, times(1)).debug(String.valueOf(any())); Assert.assertTrue("Should reset startThread", Whitebox.getInternalState(sdcListener, "startThread") == null); @@ -124,7 +124,7 @@ public class SdcListenerTest { mockThread.setNewState(state); sdcListener.stopStartThread(123); Mockito.verify(mockThread, times(++ timesCallThread)).interrupt(); - Mockito.verify(mockLogger, times(timesCallLogger += 2)).debug(String.valueOf(any())); + //Mockito.verify(mockLogger, times(timesCallLogger += 2)).debug(String.valueOf(any())); Assert.assertTrue("Should reset startThread", Whitebox.getInternalState(sdcListener, "startThread") == null); } diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcTestObjects.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcTestObjects.java new file mode 100644 index 000000000..743468f3c --- /dev/null +++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcTestObjects.java @@ -0,0 +1,21 @@ +package org.openecomp.appc.sdc.listener; + +import org.junit.Assert; +import org.junit.Test; +import org.openecomp.appc.sdc.artifacts.object.Vnfc; + +public class SdcTestObjects { + + @Test + public void testVnfcInstance(){ + Vnfc vnfc=new Vnfc(); + vnfc.setVnfcType("Firewall"); + vnfc.setMandatory(true); + vnfc.setResilienceType("Active"); + Assert.assertEquals("Firewall",vnfc.getVnfcType()); + Assert.assertEquals(true,vnfc.isMandatory()); + Assert.assertEquals("Active",vnfc.getResilienceType()); + } + + +} diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcTestUtils.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcTestUtils.java new file mode 100644 index 000000000..62b96c0c4 --- /dev/null +++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcTestUtils.java @@ -0,0 +1,108 @@ +package org.openecomp.appc.sdc.listener; + +import org.junit.Assert; +import org.junit.Test; +import org.openecomp.sdc.api.notification.IArtifactInfo; +import org.openecomp.sdc.api.notification.INotificationData; +import org.openecomp.sdc.api.notification.IResourceInstance; + +import java.io.File; +import java.io.FileInputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class SdcTestUtils { + + @Test + public void testToSdcStoreDocumentInput() throws Exception{ + Assert.assertEquals(readInput("/output/TestUtilResponse.json"), Util.toSdcStoreDocumentInput + (getNotificationData(),getResources(),getServiceArtifact(),"Mock data")); + } + + @Test + public void testProviderResponse(){ + ProviderResponse response=new ProviderResponse(200,"Success"); + Assert.assertEquals(200,response.getStatus()); + Assert.assertEquals("Success",response.getBody()); + } + + private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, + InstantiationException, InvocationTargetException { + + INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl"); + + List<IArtifactInfo> serviceArtifacts = getServiceArtifacts(); + + invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts); + invokeMethod(notificationData,"setServiceUUID","4564-4567-7897"); + invokeMethod(notificationData,"setDistributionID","Distribution ID Mock"); + invokeMethod(notificationData,"setServiceDescription","Service Description Mock"); + invokeMethod(notificationData,"setServiceName","Service Name Mock"); + + return notificationData; + } + + private IResourceInstance getResources() throws ClassNotFoundException, InvocationTargetException, + InstantiationException, IllegalAccessException { + IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance"); + + List<IArtifactInfo> serviceArtifacts = getServiceArtifacts(); + invokeMethod(resource,"setArtifacts",serviceArtifacts); + invokeMethod(resource,"setResourceName","Vnf"); + invokeMethod(resource,"setResourceVersion","1.0"); + invokeMethod(resource,"setResourceUUID","Resource UUID"); + + return resource; + } + + 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())){ + method.setAccessible(true); + method.invoke(object,arguments); + } + } + } + private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { + Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors()) + .stream() + .filter(constructor1 -> constructor1.getParameterCount()==0) + .collect(Collectors.toList()) + .get(0); + constructor.setAccessible(true); + return constructor.newInstance(); + } + private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException, + InstantiationException, IllegalAccessException { + List<IArtifactInfo> serviceArtifacts = new ArrayList<>(); + IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl"); + invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR"); + invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl"); + serviceArtifacts.add(artifactInfo); + return serviceArtifacts; + } + private IArtifactInfo getServiceArtifact() throws ClassNotFoundException, InvocationTargetException, + InstantiationException, IllegalAccessException { + IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl"); + invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR"); + invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl"); + return artifactInfo; + } + private String readInput(String inputFile) throws URISyntaxException { + File file = new File(this.getClass().getResource(inputFile).toURI()); + byte[] bFile = new byte[(int) file.length()]; + try(FileInputStream fileInputStream = new FileInputStream(file)){ + fileInputStream.read(bFile); + } catch (Exception e){ + e.printStackTrace(); + } + return new String(bFile); + } +} |