X-Git-Url: https://gerrit.onap.org/r/gitweb?p=aai%2Fmodel-loader.git;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Fnotification%2FArtifactDownloadManagerVnfcTest.java;h=3710d62bdf552b77a9cdd8cda76b0c60852088df;hp=c0de0ee0619e37d576ca10a58ae17a59e4e0d23d;hb=HEAD;hpb=50889fa7107127868a9a4391218772ff58d0ac2e diff --git a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java index c0de0ee..acd250b 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java +++ b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java @@ -20,12 +20,10 @@ */ 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.junit.jupiter.api.Assertions.assertEquals; +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.mock; import static org.mockito.Mockito.when; import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile; @@ -33,11 +31,16 @@ 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 java.util.stream.Collectors; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.onap.aai.babel.service.data.BabelArtifact; -import org.onap.aai.modelloader.config.ModelLoaderConfig; +import org.onap.aai.modelloader.babel.BabelArtifactService; import org.onap.aai.modelloader.entity.Artifact; import org.onap.aai.modelloader.entity.ArtifactType; import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact; @@ -47,7 +50,6 @@ 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; @@ -55,111 +57,104 @@ 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; -import org.springframework.test.util.ReflectionTestUtils; /** * 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; + @Mock private ArtifactDownloadManager downloadManager; + @Mock private BabelServiceClient mockBabelClient; + @Mock private IDistributionClient mockDistributionClient; + @Mock private NotificationPublisher mockNotificationPublisher; + @Mock private BabelArtifactConverter mockBabelArtifactConverter; + @Mock private VnfCatalogExtractor mockVnfCatalogExtractor; + @InjectMocks private BabelArtifactService babelArtifactService; - @Before + @BeforeEach 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); + MockitoAnnotations.openMocks(this); Properties configProperties = new Properties(); configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties")); downloadManager = new ArtifactDownloadManager(mockDistributionClient, - new ModelLoaderConfig(configProperties, "."), mockClientFactory); - - - ReflectionTestUtils.setField(downloadManager, "notificationPublisher", mockNotificationPublisher); - ReflectionTestUtils.setField(downloadManager, "babelArtifactConverter", mockBabelArtifactConverter); - ReflectionTestUtils.setField(downloadManager, "vnfCatalogExtractor", mockVnfCatalogExtractor); + mockNotificationPublisher, mockVnfCatalogExtractor, babelArtifactService); } @Test public void downloadArtifacts_validToscaVnfcCsarFile() - throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException { + throws Exception { INotificationData data = getNotificationDataWithToscaCsarFile(); IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); setupValidDownloadCsarMocks(data, artifactInfo); - when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifacts()); + when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifacts()); when(mockVnfCatalogExtractor.extract(any(), any())).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()); + List artifacts = downloadManager.downloadArtifacts(data, data.getServiceArtifacts()); + List catalogArtifacts = artifacts.stream() + .filter(VnfCatalogArtifact.class::isInstance) + .collect(Collectors.toList()); + List modelArtifacts = artifacts.stream() + .filter(ModelArtifact.class::isInstance) + .collect(Collectors.toList()); + assertEquals(1, catalogArtifacts.size(), "There should be a catalog artifact"); + assertEquals(1, modelArtifacts.size(), "There should be a model artifact"); } @Test public void downloadArtifacts_validXmlVnfcCsarFile() - throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException { + throws Exception { INotificationData data = getNotificationDataWithToscaCsarFile(); IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); setupValidDownloadCsarMocks(data, artifactInfo); - when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifactsNoVnfc()); + when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifactsNoVnfc()); when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts()); - List modelArtifacts = new ArrayList<>(); - List catalogFiles = new ArrayList<>(); - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles), - is(true)); + List artifacts = downloadManager.downloadArtifacts(data, data.getServiceArtifacts()); - assertEquals("There should have been some catalog files", 3, catalogFiles.size()); + List catalogArtifacts = artifacts.stream() + .filter(VnfCatalogArtifact.class::isInstance) + .collect(Collectors.toList()); + List modelArtifacts = artifacts.stream() + .filter(ModelArtifact.class::isInstance) + .collect(Collectors.toList()); + assertEquals(3, catalogArtifacts.size(), "There should be three catalog artifacts"); + assertEquals(1, modelArtifacts.size(), "There should be a model artifact"); } @Test public void downloadArtifacts_validNoVnfcCsarFile() - throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException { + throws Exception { INotificationData data = getNotificationDataWithToscaCsarFile(); IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); setupValidDownloadCsarMocks(data, artifactInfo); - when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifactsNoVnfc()); + when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifactsNoVnfc()); when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>()); - List modelArtifacts = new ArrayList<>(); - List catalogFiles = new ArrayList<>(); - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles), - is(true)); + List artifacts = downloadManager.downloadArtifacts(data, data.getServiceArtifacts()); + List catalogArtifacts = artifacts.stream() + .filter(VnfCatalogArtifact.class::isInstance) + .collect(Collectors.toList()); - assertEquals("There should not have been any catalog files", 0, catalogFiles.size()); + assertEquals(0, catalogArtifacts.size(), "There should not have been any catalog files"); } @Test public void downloadArtifacts_invalidXmlAndToscaVnfcCsarFile() - throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException { + throws Exception { INotificationData data = getNotificationDataWithToscaCsarFile(); IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); setupValidDownloadCsarMocks(data, artifactInfo); - when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifacts()); + when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifacts()); when(mockVnfCatalogExtractor.extract(any(), any())).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)); + InvalidArchiveException invalidArchiveException = assertThrows(InvalidArchiveException.class, + () -> downloadManager.downloadArtifacts(data, data.getServiceArtifacts())); Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo); } @@ -178,8 +173,8 @@ public class ArtifactDownloadManagerVnfcTest { when(mockDistributionClient.download(artifactInfo)) .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null, new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar"))); - when(mockBabelArtifactConverter.convertToModel(Mockito.anyList())).thenReturn(createModelArtifacts()); - when(mockBabelArtifactConverter.convertToCatalog(Mockito.anyList())).thenReturn(createToscaVnfcArtifacts()); + when(mockBabelArtifactConverter.convertToModel(Mockito.any(BabelArtifact.class))).thenReturn(createModelArtifacts()); + when(mockBabelArtifactConverter.convertToCatalog(Mockito.any(BabelArtifact.class))).thenReturn(new VnfCatalogArtifact("Some VNFC payload")); } private List createBabelArtifacts() { @@ -198,17 +193,9 @@ public class ArtifactDownloadManagerVnfcTest { 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"));