X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Fnotification%2FTestArtifactDownloadManager.java;fp=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Fnotification%2FTestArtifactDownloadManager.java;h=79eb94d81c3de7991a49f31ce096eca8f6cd74bf;hb=a61b2948f9cbe25d0ec6cd957671d51d83c62a1a;hp=066158360a9170206ecb253adf1faba93379eab3;hpb=98fe4a42e555975fa36ad40020424902cd15be38;p=aai%2Fmodel-loader.git diff --git a/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java b/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java index 0661583..79eb94d 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java +++ b/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java @@ -21,10 +21,9 @@ package org.onap.aai.modelloader.notification; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.collection.IsEmptyCollection.empty; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; @@ -38,8 +37,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Properties; +import java.util.stream.Collectors; -import org.hamcrest.collection.IsEmptyCollection; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -53,10 +52,11 @@ import org.onap.aai.modelloader.entity.Artifact; 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.entity.model.NamedQueryArtifact; +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; @@ -75,7 +75,6 @@ public class TestArtifactDownloadManager { @Mock private IDistributionClient mockDistributionClient; @Mock private NotificationPublisher mockNotificationPublisher; @Mock private BabelArtifactConverter mockBabelArtifactConverter; - @Mock private BabelServiceClientFactory mockClientFactory; @InjectMocks BabelArtifactService babelArtifactService; private VnfCatalogExtractor vnfCatalogExtractor; @@ -83,7 +82,6 @@ public class TestArtifactDownloadManager { public void setup() throws Exception { MockitoAnnotations.openMocks(this); vnfCatalogExtractor = new VnfCatalogExtractor(); - when(mockClientFactory.create(any())).thenReturn(mockBabelClient); Properties configProperties = new Properties(); configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties")); @@ -100,15 +98,12 @@ public class TestArtifactDownloadManager { /** * Test downloading zero artifacts from SDC. + * @throws Exception */ @Test - public void testDownloadWithZeroArtifacts() { - List modelFiles = new ArrayList<>(); - List catalogFiles = new ArrayList<>(); - assertThat(downloadManager.downloadArtifacts(getNotificationDataWithOneService(), new ArrayList<>(), modelFiles, - catalogFiles), is(true)); - assertThat(modelFiles, is(empty())); - assertThat(catalogFiles, is(empty())); + public void testDownloadWithZeroArtifacts() throws Exception { + List artifacts = downloadManager.downloadArtifacts(getNotificationDataWithOneService(), new ArrayList<>()); + assertThat(artifacts, is(empty())); Mockito.verifyNoInteractions(mockBabelClient, mockDistributionClient, mockNotificationPublisher, mockBabelArtifactConverter); } @@ -123,7 +118,8 @@ public class TestArtifactDownloadManager { doNothing().when(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, artifact, errorMessage); - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null), is(false)); + assertThrows(DownloadFailureException.class, + () -> downloadManager.downloadArtifacts(data, data.getServiceArtifacts())); Mockito.verify(mockDistributionClient).download(artifact); Mockito.verify(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, artifact, @@ -132,22 +128,6 @@ public class TestArtifactDownloadManager { Mockito.verifyNoInteractions(mockBabelClient, mockBabelArtifactConverter); } - @Test - public void testErrorCreatingBabelClient() throws Exception { - when(mockClientFactory.create(any())).thenThrow(new BabelServiceClientException(new Exception())); - - INotificationData data = getNotificationDataWithToscaCsarFile(); - IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); - setupValidDownloadCsarMocks(data, artifactInfo, new ArtifactTestUtils()); - doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo); - - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null), is(false)); - - Mockito.verify(mockDistributionClient).download(artifactInfo); - Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifactInfo); - Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo); - } - @Test public void downloadArtifacts_invalidToscaCsarFile() throws IOException, BabelServiceClientException { INotificationData data = getNotificationDataWithToscaCsarFile(); @@ -158,7 +138,8 @@ public class TestArtifactDownloadManager { when(mockBabelClient.postArtifact(any(), any())).thenThrow(new BabelServiceClientException("")); doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact); - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null), is(false)); + assertThrows(ProcessToscaArtifactsException.class, + () -> downloadManager.downloadArtifacts(data, data.getServiceArtifacts())); Mockito.verify(mockDistributionClient).download(artifact); Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); @@ -180,8 +161,8 @@ public class TestArtifactDownloadManager { DistributionActionResultEnum.SUCCESS, null, "This is not a valid Model Query Spec".getBytes())); doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, null), - is(false)); + assertThrows(BabelArtifactParsingException.class, + () -> downloadManager.downloadArtifacts(data, data.getServiceArtifacts())); Mockito.verify(mockDistributionClient).download(artifact); Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); @@ -192,18 +173,17 @@ public class TestArtifactDownloadManager { @Test public void downloadArtifacts_validToscaCsarFile() - throws IOException, BabelServiceClientException, BabelArtifactParsingException { + throws Exception { INotificationData data = getNotificationDataWithToscaCsarFile(); IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); setupValidDownloadCsarMocks(data, artifactInfo, new ArtifactTestUtils()); - when(mockBabelArtifactConverter.convertToModel(any(BabelArtifact.class))).thenReturn(List.of(new ModelArtifact())); - when(mockBabelArtifactConverter.convertToCatalog(any(BabelArtifact.class))).thenReturn(new VnfCatalogArtifact("")); - - List modelArtifacts = new ArrayList<>(); - List catalogArtifacts = new ArrayList<>(); - assertTrue(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogArtifacts)); + List artifacts = downloadManager.downloadArtifacts(data, data.getServiceArtifacts()); + List modelArtifacts = artifacts.stream().filter(ModelArtifact.class::isInstance) + .collect(Collectors.toList()); + List catalogArtifacts = artifacts.stream().filter(VnfCatalogArtifact.class::isInstance) + .collect(Collectors.toList()); assertThat(modelArtifacts.size(), is(1)); assertThat(catalogArtifacts.size(), is(1)); @@ -215,11 +195,14 @@ public class TestArtifactDownloadManager { } private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo, - ArtifactTestUtils artifactTestUtils) throws IOException, BabelServiceClientException { + ArtifactTestUtils artifactTestUtils) throws IOException, BabelServiceClientException, BabelArtifactParsingException { when(mockDistributionClient.download(artifactInfo)) .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null, artifactTestUtils.loadResource("compressedArtifacts/service-VscpaasTest-csar.csar"))); when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifacts()); + + when(mockBabelArtifactConverter.convertToModel(any(BabelArtifact.class))).thenReturn(List.of(new ModelArtifact())); + when(mockBabelArtifactConverter.convertToCatalog(any(BabelArtifact.class))).thenReturn(new VnfCatalogArtifact("")); } private List createBabelArtifacts() { @@ -231,17 +214,19 @@ public class TestArtifactDownloadManager { @Test public void downloadArtifactsWithValidModelQuerySpec() - throws IOException, BabelServiceClientException, BabelArtifactParsingException { + throws Exception { INotificationData data = getNotificationDataWithModelQuerySpec(); IArtifactInfo artifact = data.getServiceArtifacts().get(0); setupValidModelQuerySpecMocks(new ArtifactTestUtils(), data, artifact); - List modelFiles = new ArrayList<>(); - List catalogFiles = new ArrayList<>(); - assertTrue(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelFiles, catalogFiles)); + List artifacts = downloadManager.downloadArtifacts(data, data.getServiceArtifacts()); + List namedQueryArtifacts = artifacts.stream().filter(NamedQueryArtifact.class::isInstance) + .collect(Collectors.toList()); + List catalogArtifacts = artifacts.stream().filter(VnfCatalogArtifact.class::isInstance) + .collect(Collectors.toList()); - assertThat(modelFiles, is(not(IsEmptyCollection.empty()))); - assertThat(catalogFiles, is(empty())); + assertThat(namedQueryArtifacts.size(), is(1)); + assertThat(catalogArtifacts, is(empty())); Mockito.verify(mockDistributionClient).download(artifact); Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); @@ -259,26 +244,25 @@ public class TestArtifactDownloadManager { @Test public void downloadArtifacts_validCsarAndModelFiles() - throws IOException, BabelServiceClientException, BabelArtifactParsingException { + throws Exception { ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils(); INotificationData data = getNotificationDataWithOneOfEach(); - List artifacts = new ArrayList<>(); + List artifactInfos = new ArrayList<>(); IArtifactInfo serviceArtifact = data.getServiceArtifacts().get(0); IArtifactInfo modelSpecArtifact = data.getResources().get(1).getArtifacts().get(0); - artifacts.add(serviceArtifact); - artifacts.add(modelSpecArtifact); + artifactInfos.add(serviceArtifact); + artifactInfos.add(modelSpecArtifact); setupValidDownloadCsarMocks(data, serviceArtifact, artifactTestUtils); setupValidModelQuerySpecMocks(artifactTestUtils, data, modelSpecArtifact); - when(mockBabelArtifactConverter.convertToModel(any(BabelArtifact.class))).thenReturn(List.of(new ModelArtifact())); - when(mockBabelArtifactConverter.convertToCatalog(any(BabelArtifact.class))).thenReturn(new VnfCatalogArtifact("")); - - List modelArtifacts = new ArrayList<>(); - List catalogArtifacts = new ArrayList<>(); - assertTrue(downloadManager.downloadArtifacts(data, artifacts, modelArtifacts, catalogArtifacts)); + List artifacts = downloadManager.downloadArtifacts(data, artifactInfos); + List modelArtifacts = artifacts.stream().filter(artifact -> !(artifact instanceof VnfCatalogArtifact)) + .collect(Collectors.toList()); + List catalogArtifacts = artifacts.stream().filter(VnfCatalogArtifact.class::isInstance) + .collect(Collectors.toList()); assertThat(modelArtifacts.size(), is(2)); assertThat(catalogArtifacts.size(), is(1)); @@ -311,8 +295,9 @@ public class TestArtifactDownloadManager { List modelArtifacts = new ArrayList<>(); List catalogFiles = new ArrayList<>(); - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles), - is(false)); + assertThrows(ProcessToscaArtifactsException.class, + () -> downloadManager.downloadArtifacts(data, data.getServiceArtifacts())); + assertThat(modelArtifacts, is(empty())); assertThat(catalogFiles, is(empty())); @@ -332,8 +317,9 @@ public class TestArtifactDownloadManager { DistributionActionResultEnum.SUCCESS, null, "This content does not matter.".getBytes())); doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, new ArrayList<>()), - is(false)); + assertThrows(InvalidArchiveException.class, + () -> downloadManager.downloadArtifacts(data, data.getServiceArtifacts())); + Mockito.verify(mockDistributionClient).download(artifact); Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);