diff options
5 files changed, 634 insertions, 109 deletions
diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/helper/TestArtifactStorageService.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/helper/TestArtifactStorageService.java new file mode 100644 index 000000000..df5babb66 --- /dev/null +++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/helper/TestArtifactStorageService.java @@ -0,0 +1,164 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Ericsson. All rights reserved. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.sdc.artifacts.helper; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.sql.SQLException; +import javax.sql.rowset.CachedRowSet; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.internal.util.reflection.Whitebox; +import org.onap.appc.exceptions.APPCException; +import org.onap.appc.sdc.artifacts.object.SDCArtifact; +import org.onap.appc.sdc.artifacts.object.SDCReference; +import org.onap.ccsdk.sli.core.dblib.DbLibService; + +/** + * This class contains test methods for Artifact Storage Service. + * + */ + +public class TestArtifactStorageService { + + private ArtifactStorageService artifactStorageService; + + private ArtifactStorageService artifactStorageServiceSpy; + + private SDCArtifact sdcArtifact; + + private SDCReference sdcReference; + + private DbLibService dbLibService; + + private CachedRowSet cachedRowSet; + + @Before + public void setup() throws Exception { + artifactStorageService = new ArtifactStorageService(); + sdcArtifact = Mockito.mock(SDCArtifact.class); + dbLibService = Mockito.mock(DbLibService.class); + sdcReference = Mockito.mock(SDCReference.class); + cachedRowSet = Mockito.mock(CachedRowSet.class); + Whitebox.setInternalState(artifactStorageService, "dbLibService", dbLibService); + artifactStorageServiceSpy = Mockito.spy(artifactStorageService); + } + + /** + * This method tests the store Artifacts method success scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + * @throws SQLException if there are any sql exception while storing the sdc artifacts + */ + @Test + public void testStoreSDCArtifact() throws APPCException, SQLException { + when(dbLibService.writeData(anyObject(), anyObject(), anyObject())).thenReturn(true); + artifactStorageServiceSpy.storeSDCArtifact(sdcArtifact); + verify(artifactStorageServiceSpy, times(1)).storeSDCArtifact(sdcArtifact); + } + + /** + * This method tests the store Artifacts method failure scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + * @throws SQLException if there are any sql exception while storing the sdc artifacts + */ + @Test(expected = APPCException.class) + public void testStoreSDCArtifactWithException() throws APPCException, SQLException { + when(dbLibService.writeData(anyObject(), anyObject(), anyObject())) + .thenThrow(new SQLException()); + artifactStorageService.storeSDCArtifact(sdcArtifact); + } + + /** + * This method tests the store Artifacts with reference method success scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + * @throws SQLException if there are any sql exception while storing the sdc artifacts + */ + @Test + public void testStoreSDCArtifactWithReference() throws APPCException, SQLException { + when(dbLibService.writeData(anyObject(), anyObject(), anyObject())).thenReturn(true); + when(dbLibService.getData(anyObject(), anyObject(), eq("sdnctl"))).thenReturn(cachedRowSet); + when(cachedRowSet.first()).thenReturn(true); + artifactStorageServiceSpy.storeSDCArtifactWithReference(sdcArtifact, sdcReference); + verify(artifactStorageServiceSpy, times(1)).storeSDCArtifactWithReference(sdcArtifact, + sdcReference); + } + + /** + * This method tests the store Artifacts with reference method success scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + * @throws SQLException if there are any sql exception while storing the sdc artifacts + */ + @Test + public void testArtifactWithExistingRef() throws APPCException, SQLException { + when(dbLibService.writeData(anyObject(), anyObject(), anyObject())).thenReturn(true); + when(dbLibService.getData(anyObject(), anyObject(), eq("sdnctl"))).thenReturn(cachedRowSet); + ArtifactStorageService artifactStorageServiceSpy = Mockito.spy(artifactStorageService); + artifactStorageServiceSpy.storeSDCArtifactWithReference(sdcArtifact, sdcReference); + verify(artifactStorageServiceSpy, times(1)).storeSDCArtifactWithReference(sdcArtifact, + sdcReference); + } + + /** + * This method tests the store Artifacts with reference method failure scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + * @throws SQLException if there are any sql exception while storing the sdc artifacts + */ + @Test(expected = APPCException.class) + public void testArtifactWithExceptionScenario1() throws APPCException, SQLException { + when(dbLibService.getData(anyObject(), anyObject(), eq("sdnctl"))) + .thenThrow(new SQLException()); + artifactStorageService.storeSDCArtifactWithReference(sdcArtifact, sdcReference); + } + + /** + * This method tests the store Artifacts with reference method failure scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + * @throws SQLException if there are any sql exception while storing the sdc artifacts + */ + @Test(expected = APPCException.class) + public void testArtifactWithExceptionScenario2() throws APPCException, SQLException { + when(dbLibService.writeData(anyObject(), anyObject(), anyObject())) + .thenThrow(new SQLException()); + when(dbLibService.getData(anyObject(), anyObject(), eq("sdnctl"))).thenReturn(cachedRowSet); + artifactStorageService.storeSDCArtifactWithReference(sdcArtifact, sdcReference); + } + + /** + * This method tests the retrieve Artifacts with reference method failure scenario scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + * @throws SQLException if there are any sql exception while storing the sdc artifacts + */ + @Test(expected = APPCException.class) + public void testRetrieveSDCReference() throws APPCException, SQLException { + when(dbLibService.getData(anyObject(), anyObject(), eq("sdnctl"))) + .thenThrow(new SQLException()); + artifactStorageService.retrieveSDCReference("vnfType", "category"); + } +} diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestAbstractArtifactProcessor.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestAbstractArtifactProcessor.java new file mode 100644 index 000000000..1f5e4e141 --- /dev/null +++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestAbstractArtifactProcessor.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Ericsson. All rights reserved. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.sdc.artifacts.impl; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.internal.util.reflection.Whitebox; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.appc.adapter.message.EventSender; +import org.onap.sdc.api.IDistributionClient; +import org.onap.sdc.api.notification.IArtifactInfo; +import org.onap.sdc.api.notification.INotificationData; +import org.onap.sdc.api.notification.IResourceInstance; +import org.onap.sdc.api.results.IDistributionClientDownloadResult; +import org.onap.sdc.utils.DistributionActionResultEnum; +import com.att.eelf.configuration.EELFLogger; + +/** + * This class tests the Abstract Artifact Processor + */ +@RunWith(MockitoJUnitRunner.class) +public class TestAbstractArtifactProcessor { + + private AbstractArtifactProcessor abstractArtifactProcessor; + + private EELFLogger logger; + + private IDistributionClient client; + + private IDistributionClientDownloadResult distributionClientDownloadResult; + + private IArtifactInfo artifactInfo; + + private INotificationData notificationData; + + private EventSender eventSender; + + private IResourceInstance resource; + + /** + * Setup the test environment by loading a new Abstract artifact Processor + */ + @Before + public void setup() { + abstractArtifactProcessor = Mockito.mock(AbstractArtifactProcessor.class, CALLS_REAL_METHODS); + logger = Mockito.mock(EELFLogger.class); + Mockito.doCallRealMethod().when(abstractArtifactProcessor).run(); + Whitebox.setInternalState(abstractArtifactProcessor, "logger", logger); + client = Mockito.mock(IDistributionClient.class); + distributionClientDownloadResult = Mockito.mock(IDistributionClientDownloadResult.class); + artifactInfo = Mockito.mock(IArtifactInfo.class); + notificationData = Mockito.mock(INotificationData.class); + eventSender = Mockito.mock(EventSender.class); + resource = Mockito.mock(IResourceInstance.class); + Whitebox.setInternalState(abstractArtifactProcessor, "client", client); + Whitebox.setInternalState(abstractArtifactProcessor, "artifact", artifactInfo); + Whitebox.setInternalState(abstractArtifactProcessor, "notification", notificationData); + Whitebox.setInternalState(abstractArtifactProcessor, "eventSender", eventSender); + Whitebox.setInternalState(abstractArtifactProcessor, "resource", resource); + } + + /** + * This method tests the abstract Artifacts method success scenario + */ + @Test + public void testRun() { + when(client.download(anyObject())).thenReturn(distributionClientDownloadResult); + when(distributionClientDownloadResult.getDistributionActionResult()) + .thenReturn(DistributionActionResultEnum.SUCCESS); + when(client.sendDownloadStatus(anyObject())).thenReturn(distributionClientDownloadResult); + when(distributionClientDownloadResult.getArtifactPayload()).thenReturn(new byte[1]); + abstractArtifactProcessor.run(); + verify(abstractArtifactProcessor, times(1)).run(); + } + + /** + * This method tests the abstract Artifacts method failure scenario + */ + @Test + public void testRunWithDownloadFailed() { + when(client.download(anyObject())).thenReturn(distributionClientDownloadResult); + when(distributionClientDownloadResult.getDistributionActionResult()) + .thenReturn(DistributionActionResultEnum.FAIL); + when(client.sendDownloadStatus(anyObject())).thenReturn(distributionClientDownloadResult); + when(distributionClientDownloadResult.getArtifactPayload()).thenReturn(new byte[1]); + abstractArtifactProcessor.run(); + verify(abstractArtifactProcessor, times(1)).run(); + } + + /** + * This method tests the abstract Artifacts method failure scenario + */ + @Test + public void testRunWithException() { + when(client.download(anyObject())).thenReturn(distributionClientDownloadResult); + when(distributionClientDownloadResult.getDistributionActionResult()) + .thenReturn(DistributionActionResultEnum.SUCCESS); + when(client.sendDownloadStatus(anyObject())).thenReturn(distributionClientDownloadResult); + when(distributionClientDownloadResult.getArtifactPayload()).thenThrow(new RuntimeException()); + abstractArtifactProcessor.run(); + verify(abstractArtifactProcessor, times(1)).run(); + } +} diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestConfigArtifcatProcessor.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestConfigArtifcatProcessor.java new file mode 100644 index 000000000..c11ca746c --- /dev/null +++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestConfigArtifcatProcessor.java @@ -0,0 +1,127 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Ericsson. All rights reserved. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.sdc.artifacts.impl; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.net.URI; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.appc.adapter.message.EventSender; +import org.onap.appc.exceptions.APPCException; +import org.onap.appc.sdc.artifacts.object.SDCArtifact; +import org.onap.appc.sdc.listener.ProviderOperations; +import org.onap.appc.sdc.listener.ProviderResponse; +import org.onap.appc.sdc.listener.Util; +import org.onap.sdc.api.IDistributionClient; +import org.onap.sdc.api.notification.IArtifactInfo; +import org.onap.sdc.api.notification.INotificationData; +import org.onap.sdc.api.notification.IResourceInstance; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +/** + * This class contains test methods for ConfigArtifact Processor. + * + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ProviderOperations.class, Util.class}) +public class TestConfigArtifcatProcessor { + + private ConfigArtifactProcessor configArtifactProcessor; + + private SDCArtifact sdcArtifact; + + private URI uri = URI.create("http://localhost:8080"); + + private IDistributionClient client; + + private EventSender eventSender; + + private ProviderResponse response; + + private INotificationData notificationData; + + private IResourceInstance resourceInstance; + + private IArtifactInfo artifactInfo; + + @Before + public void setup() throws APPCException { + client = Mockito.mock(IDistributionClient.class); + notificationData = Mockito.mock(INotificationData.class); + resourceInstance = Mockito.mock(IResourceInstance.class); + artifactInfo = Mockito.mock(IArtifactInfo.class); + sdcArtifact = Mockito.mock(SDCArtifact.class); + eventSender = Mockito.mock(EventSender.class); + configArtifactProcessor = new ConfigArtifactProcessor(client, eventSender, notificationData, + resourceInstance, artifactInfo, uri); + response = new ProviderResponse(200, "{\"key\":\"value\"}"); + } + + /** + * This method tests the process Artifacts method success scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + */ + @Test + public void testProcessArtifact() throws APPCException { + PowerMockito.mockStatic(ProviderOperations.class); + PowerMockito.mockStatic(Util.class); + when(ProviderOperations.post(anyObject(), anyString(), anyObject())).thenReturn(response); + when(Util.parseResponse(anyObject())).thenReturn(true); + ConfigArtifactProcessor configArtifactProcessorSpy = Mockito.spy(configArtifactProcessor); + configArtifactProcessorSpy.processArtifact(sdcArtifact); + verify(configArtifactProcessorSpy, times(1)).processArtifact(sdcArtifact); + } + + /** + * This method tests the process Artifacts method failure scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + */ + @Test(expected = APPCException.class) + public void testProcessArtifactFail() throws APPCException { + PowerMockito.mockStatic(ProviderOperations.class); + when(ProviderOperations.post(anyObject(), anyString(), anyObject())).thenReturn(response); + configArtifactProcessor.processArtifact(sdcArtifact); + } + + /** + * This method tests the process Artifacts method failure scenario when the uri is null. + * + * @throws APPCException if the there are any exception in sdc artifacts or in creating + * notification data, resource data & service artifacts + */ + @Test + public void testProcessArtifactWithNoURI() throws APPCException { + configArtifactProcessor = new ConfigArtifactProcessor(client, eventSender, notificationData, + resourceInstance, artifactInfo, null); + ConfigArtifactProcessor configArtifactProcessorSpy = Mockito.spy(configArtifactProcessor); + configArtifactProcessorSpy.processArtifact(sdcArtifact); + verify(configArtifactProcessorSpy, times(1)).processArtifact(sdcArtifact); + } +} diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestLicenseArtifactProcessor.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestLicenseArtifactProcessor.java index b43f9410b..6f70681f5 100644 --- a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestLicenseArtifactProcessor.java +++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestLicenseArtifactProcessor.java @@ -6,6 +6,8 @@ * ================================================================================ * Copyright (C) 2017 Amdocs * ============================================================================= + * Modifications (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. * You may obtain a copy of the License at @@ -21,6 +23,10 @@ */ package org.onap.appc.sdc.artifacts.impl; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -38,116 +44,77 @@ 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.onap.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.onap.sdc.api.notification.INotificationData notificationData = (INotificationData)getObject("org.onap.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.onap.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.onap.sdc.impl.ArtifactInfoImpl"); - invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR"); - invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl"); - serviceArtifacts.add(artifactInfo); - return serviceArtifacts; - } + private LicenseArtifactProcessor artifactProcessor; + + private ArtifactStorageService storageService; + + private INotificationData notificationData; + + private IResourceInstance resourceInstance; + + private IArtifactInfo artifactInfo; + + @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, notificationData, + resourceInstance, artifactInfo, null)); + Whitebox.setInternalState(artifactProcessor, "artifactStorageService", storageService); + PowerMockito.doCallRealMethod().when(artifactProcessor) + .processArtifact((SDCArtifact) Matchers.anyObject()); + PowerMockito.doNothing().when(storageService).storeSDCArtifact(Matchers.anyObject()); + } + + @Test(expected = org.onap.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()); + } + + /** + * This method tests the process Artifacts method failure scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + */ + @Test(expected = APPCException.class) + public void testProcessArtifactNullSDCArtifact() throws APPCException { + SDCArtifact artifact = new SDCArtifact(); + artifact.setResourceVersion("RESOURCE VERSION"); + artifact.setArtifactUUID("123-456-789"); + artifact.setResourceName("Resource Name"); + when(storageService.retrieveSDCArtifact(anyString(), anyString(), anyString())) + .thenThrow(new APPCException()); + artifactProcessor.processArtifact(artifact); + } } diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestToscaCsarArtifactProcessor.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestToscaCsarArtifactProcessor.java new file mode 100644 index 000000000..738febef6 --- /dev/null +++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/onap/appc/sdc/artifacts/impl/TestToscaCsarArtifactProcessor.java @@ -0,0 +1,141 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Ericsson. All rights reserved. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.appc.sdc.artifacts.impl; + +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.appc.adapter.message.EventSender; +import org.onap.appc.exceptions.APPCException; +import org.onap.appc.sdc.artifacts.helper.ArtifactStorageService; +import org.onap.appc.sdc.artifacts.helper.DependencyModelGenerator; +import org.onap.appc.sdc.artifacts.object.SDCArtifact; +import org.onap.sdc.api.IDistributionClient; +import org.onap.sdc.api.notification.IArtifactInfo; +import org.onap.sdc.api.notification.INotificationData; +import org.onap.sdc.api.notification.IResourceInstance; +import org.onap.sdc.api.results.IDistributionClientDownloadResult; +import org.powermock.reflect.Whitebox; + +/** + * This class contains test methods for ToscaCsarArtifact Processor + * + */ + +public class TestToscaCsarArtifactProcessor { + + private ToscaCsarArtifactProcessor toscaCsarArtifactProcessor; + + private DependencyModelGenerator dependencyModelGenerator; + + private ArtifactStorageService storageService; + + private SDCArtifact sdcArtifact; + + private IDistributionClientDownloadResult distributionClientDownloadResult; + + private IDistributionClient client; + + private EventSender eventSender; + + private INotificationData notificationData; + + private IResourceInstance resourceInstance; + + private IArtifactInfo artifactInfo; + + @Before + public void setup() throws Exception { + client = Mockito.mock(IDistributionClient.class); + eventSender = Mockito.mock(EventSender.class); + storageService = Mockito.mock(ArtifactStorageService.class); + distributionClientDownloadResult = Mockito.mock(IDistributionClientDownloadResult.class); + dependencyModelGenerator = Mockito.mock(DependencyModelGenerator.class); + toscaCsarArtifactProcessor = new ToscaCsarArtifactProcessor(client, eventSender, + notificationData, resourceInstance, artifactInfo, null); + Whitebox.setInternalState(toscaCsarArtifactProcessor, "artifactStorageService", storageService); + Whitebox.setInternalState(toscaCsarArtifactProcessor, "dependencyModelGenerator", + dependencyModelGenerator); + } + + /** + * This method tests the process Artifacts method success scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + */ + @Test + public void testProcessArtifact() throws APPCException { + ToscaCsarArtifactProcessor toscaCsarArtifactProcessorSpy = + Mockito.spy(toscaCsarArtifactProcessor); + sdcArtifact = new SDCArtifact(); + sdcArtifact.setResourceVersion("RESOURCE VERSION"); + sdcArtifact.setArtifactUUID("123-456-789"); + sdcArtifact.setResourceName("Resource Name"); + when(dependencyModelGenerator.getDependencyModel(anyString(), anyString())) + .thenReturn("dependencyModel"); + toscaCsarArtifactProcessorSpy.processArtifact(sdcArtifact); + verify(toscaCsarArtifactProcessorSpy, times(1)).processArtifact(sdcArtifact); + } + + /** + * This method tests the process Artifacts method failure scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + */ + @Test(expected = APPCException.class) + public void testProcessArtifactFailWithNullValues() throws APPCException { + sdcArtifact = new SDCArtifact(); + toscaCsarArtifactProcessor.processArtifact(sdcArtifact); + } + + /** + * This method tests the process Artifacts method failure scenario + * + * @throws Exception if the there are any exception in sdc artifacts or in creating notification + * data, resource data & service artifacts + */ + @Test(expected = APPCException.class) + public void testProcessArtifactFail() throws Exception { + sdcArtifact = new SDCArtifact(); + sdcArtifact.setResourceVersion("RESOURCE VERSION"); + sdcArtifact.setArtifactUUID("123-456-789"); + sdcArtifact.setResourceName("Resource Name"); + when(dependencyModelGenerator.getDependencyModel(anyString(), anyString())) + .thenThrow(new APPCException()); + toscaCsarArtifactProcessor.processArtifact(sdcArtifact); + } + + /** + * This method tests the process Artifacts method success scenario. + * + * @throws APPCException if the there are any exception in sdc artifacts + */ + // @Test + public void testProcessArtifactDistributionClient() throws APPCException { + when(distributionClientDownloadResult.getArtifactPayload()).thenReturn(new byte[1]); + ToscaCsarArtifactProcessor toscaCsarArtifactProcessorSpy = + Mockito.spy(toscaCsarArtifactProcessor); + toscaCsarArtifactProcessorSpy.processArtifact(distributionClientDownloadResult); + verify(toscaCsarArtifactProcessorSpy, times(1)) + .processArtifact(distributionClientDownloadResult); + } +} |