X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Fnotification%2FArtifactDownloadManagerVnfcTest.java;fp=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Fnotification%2FArtifactDownloadManagerVnfcTest.java;h=6c7a77f47f8db556eadecdfb778a220d6f889bd4;hb=7e6fe8c29c5a5cfa5caf6ab47b30280e1fc20432;hp=0000000000000000000000000000000000000000;hpb=c5aea4a8bc398fc1c6220875e55b9520fd7f7524;p=aai%2Fmodel-loader.git diff --git a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java new file mode 100644 index 0000000..6c7a77f --- /dev/null +++ b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java @@ -0,0 +1,227 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 European Software Marketing Ltd. + * ================================================================================ + * 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.aai.modelloader.notification; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.mockito.internal.util.reflection.Whitebox; +import org.onap.aai.babel.service.data.BabelArtifact; +import org.onap.aai.modelloader.config.ModelLoaderConfig; +import org.onap.aai.modelloader.entity.Artifact; +import org.onap.aai.modelloader.entity.ArtifactType; +import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact; +import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; +import org.onap.aai.modelloader.entity.model.ModelArtifact; +import org.onap.aai.modelloader.extraction.InvalidArchiveException; +import org.onap.aai.modelloader.extraction.VnfCatalogExtractor; +import org.onap.aai.modelloader.restclient.BabelServiceClient; +import org.onap.aai.modelloader.restclient.BabelServiceClientException; +import org.onap.aai.modelloader.service.BabelServiceClientFactory; +import org.onap.aai.modelloader.util.ArtifactTestUtils; +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.results.IDistributionClientDownloadResult; +import org.onap.sdc.impl.DistributionClientDownloadResultImpl; +import org.onap.sdc.utils.DistributionActionResultEnum; + +/** + * Tests {@link ArtifactDownloadManager} with VNF Catalog Artifacts. + */ +public class ArtifactDownloadManagerVnfcTest { + + private ArtifactDownloadManager downloadManager; + private BabelServiceClient mockBabelClient; + private IDistributionClient mockDistributionClient; + private NotificationPublisher mockNotificationPublisher; + private BabelArtifactConverter mockBabelArtifactConverter; + private BabelServiceClientFactory mockClientFactory; + private VnfCatalogExtractor mockVnfCatalogExtractor; + + @Before + public void setup() throws Exception { + mockBabelClient = mock(BabelServiceClient.class); + mockDistributionClient = mock(IDistributionClient.class); + mockNotificationPublisher = mock(NotificationPublisher.class); + mockBabelArtifactConverter = mock(BabelArtifactConverter.class); + mockClientFactory = mock(BabelServiceClientFactory.class); + when(mockClientFactory.create(Mockito.any())).thenReturn(mockBabelClient); + mockVnfCatalogExtractor = mock(VnfCatalogExtractor.class); + + Properties configProperties = new Properties(); + configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties")); + downloadManager = new ArtifactDownloadManager(mockDistributionClient, + new ModelLoaderConfig(configProperties, "."), mockClientFactory); + + + Whitebox.setInternalState(downloadManager, "notificationPublisher", mockNotificationPublisher); + Whitebox.setInternalState(downloadManager, "babelArtifactConverter", mockBabelArtifactConverter); + Whitebox.setInternalState(downloadManager, "vnfCatalogExtractor", mockVnfCatalogExtractor); + } + + @Test + public void downloadArtifacts_validToscaVnfcCsarFile() + throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException { + INotificationData data = getNotificationDataWithToscaCsarFile(); + IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); + + setupValidDownloadCsarMocks(data, artifactInfo); + when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), + Matchers.anyString())).thenReturn(createBabelArtifacts()); + when(mockVnfCatalogExtractor.extract(Matchers.any(), Matchers.anyString())).thenReturn(new ArrayList<>()); + + List modelArtifacts = new ArrayList<>(); + List catalogFiles = new ArrayList<>(); + assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles), + is(true)); + + assertEquals("There should have been some catalog files", 2, catalogFiles.size()); + } + + @Test + public void downloadArtifacts_validXmlVnfcCsarFile() + throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException { + INotificationData data = getNotificationDataWithToscaCsarFile(); + IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); + + setupValidDownloadCsarMocks(data, artifactInfo); + when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), + Matchers.anyString())).thenReturn(createBabelArtifactsNoVnfc()); + when(mockVnfCatalogExtractor.extract(Matchers.any(), Matchers.anyString())) + .thenReturn(createXmlVnfcArtifacts()); + + List modelArtifacts = new ArrayList<>(); + List catalogFiles = new ArrayList<>(); + assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles), + is(true)); + + assertEquals("There should have been some catalog files", 3, catalogFiles.size()); + } + + @Test + public void downloadArtifacts_validNoVnfcCsarFile() + throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException { + INotificationData data = getNotificationDataWithToscaCsarFile(); + IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); + + setupValidDownloadCsarMocks(data, artifactInfo); + when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), + Matchers.anyString())).thenReturn(createBabelArtifactsNoVnfc()); + when(mockVnfCatalogExtractor.extract(Matchers.any(), Matchers.anyString())).thenReturn(new ArrayList<>()); + + List modelArtifacts = new ArrayList<>(); + List catalogFiles = new ArrayList<>(); + assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles), + is(true)); + + assertEquals("There should not have been any catalog files", 0, catalogFiles.size()); + } + + @Test + public void downloadArtifacts_invalidXmlAndToscaVnfcCsarFile() + throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException { + INotificationData data = getNotificationDataWithToscaCsarFile(); + IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); + + setupValidDownloadCsarMocks(data, artifactInfo); + when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), + Matchers.anyString())).thenReturn(createBabelArtifacts()); + when(mockVnfCatalogExtractor.extract(Matchers.any(), Matchers.anyString())) + .thenReturn(createXmlVnfcArtifacts()); + doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo); + + List modelArtifacts = new ArrayList<>(); + List catalogFiles = new ArrayList<>(); + assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles), + is(false)); + + Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo); + } + + private IDistributionClientDownloadResult createDistributionClientDownloadResult( + DistributionActionResultEnum status, String message, byte[] payload) { + IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message); + + ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload); + + return downloadResult; + } + + private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo) + throws IOException, BabelServiceClientException, BabelArtifactParsingException { + when(mockDistributionClient.download(artifactInfo)) + .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null, + new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar"))); + when(mockBabelArtifactConverter.convertToModel(Mockito.anyListOf(BabelArtifact.class))) + .thenReturn(createModelArtifacts()); + when(mockBabelArtifactConverter.convertToCatalog(Mockito.anyListOf(BabelArtifact.class))) + .thenReturn(createToscaVnfcArtifacts()); + } + + private List createBabelArtifacts() { + List artifactList = new ArrayList<>(); + artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload")); + artifactList.add(new BabelArtifact("VNFCArtifact", BabelArtifact.ArtifactType.VNFCATALOG, "Some VNFC payload")); + return artifactList; + } + + private List createBabelArtifactsNoVnfc() { + List artifactList = new ArrayList<>(); + artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload")); + return artifactList; + } + + private List createModelArtifacts() { + List modelArtifacts = new ArrayList<>(); + modelArtifacts.add(new ModelArtifact()); + modelArtifacts.add(new ModelArtifact()); + return modelArtifacts; + } + + private List createToscaVnfcArtifacts() { + List vnfcArtifacts = new ArrayList<>(); + vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload")); + vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload")); + return vnfcArtifacts; + } + + private List createXmlVnfcArtifacts() { + List vnfcArtifacts = new ArrayList<>(); + vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload")); + vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload")); + vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload")); + return vnfcArtifacts; + } +}