Return List<Artifact> in ArtifactDownloadManager
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / ArtifactDownloadManagerVnfcTest.java
index c0de0ee..acd250b 100644 (file)
  */
 package org.onap.aai.modelloader.notification;
 
  */
 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.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;
 
 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 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.Mockito;
+import org.mockito.MockitoAnnotations;
 import org.onap.aai.babel.service.data.BabelArtifact;
 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;
 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.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.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.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 {
 
 
 /**
  * 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 {
     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,
 
         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()
     }
 
     @Test
     public void downloadArtifacts_validToscaVnfcCsarFile()
-            throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
+            throws Exception {
         INotificationData data = getNotificationDataWithToscaCsarFile();
         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
 
         setupValidDownloadCsarMocks(data, artifactInfo);
         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<>());
 
         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
 
-        List<Artifact> modelArtifacts = new ArrayList<>();
-        List<Artifact> 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<Artifact> artifacts = downloadManager.downloadArtifacts(data, data.getServiceArtifacts());
+        List<Artifact> catalogArtifacts = artifacts.stream()
+                .filter(VnfCatalogArtifact.class::isInstance)
+                .collect(Collectors.toList());
+        List<Artifact> 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()
     }
 
     @Test
     public void downloadArtifacts_validXmlVnfcCsarFile()
-            throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
+            throws Exception {
         INotificationData data = getNotificationDataWithToscaCsarFile();
         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
 
         setupValidDownloadCsarMocks(data, artifactInfo);
         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());
 
         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts());
 
-        List<Artifact> modelArtifacts = new ArrayList<>();
-        List<Artifact> catalogFiles = new ArrayList<>();
-        assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
-                is(true));
+        List<Artifact> artifacts = downloadManager.downloadArtifacts(data, data.getServiceArtifacts());
 
 
-        assertEquals("There should have been some catalog files", 3, catalogFiles.size());
+        List<Artifact> catalogArtifacts = artifacts.stream()
+                .filter(VnfCatalogArtifact.class::isInstance)
+                .collect(Collectors.toList());
+        List<Artifact> 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()
     }
 
     @Test
     public void downloadArtifacts_validNoVnfcCsarFile()
-            throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
+            throws Exception {
         INotificationData data = getNotificationDataWithToscaCsarFile();
         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
 
         setupValidDownloadCsarMocks(data, artifactInfo);
         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<>());
 
         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
 
-        List<Artifact> modelArtifacts = new ArrayList<>();
-        List<Artifact> catalogFiles = new ArrayList<>();
-        assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
-                is(true));
+        List<Artifact> artifacts = downloadManager.downloadArtifacts(data, data.getServiceArtifacts());
+        List<Artifact> 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()
     }
 
     @Test
     public void downloadArtifacts_invalidXmlAndToscaVnfcCsarFile()
-            throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
+            throws Exception {
         INotificationData data = getNotificationDataWithToscaCsarFile();
         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
 
         setupValidDownloadCsarMocks(data, artifactInfo);
         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);
 
         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts());
         doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
 
-        List<Artifact> modelArtifacts = new ArrayList<>();
-        List<Artifact> 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);
     }
 
         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(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<BabelArtifact> createBabelArtifacts() {
     }
 
     private List<BabelArtifact> createBabelArtifacts() {
@@ -198,17 +193,9 @@ public class ArtifactDownloadManagerVnfcTest {
     private List<Artifact> createModelArtifacts() {
         List<Artifact> modelArtifacts = new ArrayList<>();
         modelArtifacts.add(new ModelArtifact());
     private List<Artifact> createModelArtifacts() {
         List<Artifact> modelArtifacts = new ArrayList<>();
         modelArtifacts.add(new ModelArtifact());
-        modelArtifacts.add(new ModelArtifact());
         return modelArtifacts;
     }
 
         return modelArtifacts;
     }
 
-    private List<Artifact> createToscaVnfcArtifacts() {
-        List<Artifact> vnfcArtifacts = new ArrayList<>();
-        vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload"));
-        vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload"));
-        return vnfcArtifacts;
-    }
-
     private List<Artifact> createXmlVnfcArtifacts() {
         List<Artifact> vnfcArtifacts = new ArrayList<>();
         vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
     private List<Artifact> createXmlVnfcArtifacts() {
         List<Artifact> vnfcArtifacts = new ArrayList<>();
         vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));