Add support for loading VNF Catalog XML files
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / ArtifactDownloadManagerVnfcTest.java
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 (file)
index 0000000..6c7a77f
--- /dev/null
@@ -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<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());
+    }
+
+    @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<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", 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<Artifact> modelArtifacts = new ArrayList<>();
+        List<Artifact> 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<Artifact> modelArtifacts = new ArrayList<>();
+        List<Artifact> 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<BabelArtifact> createBabelArtifacts() {
+        List<BabelArtifact> 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<BabelArtifact> createBabelArtifactsNoVnfc() {
+        List<BabelArtifact> artifactList = new ArrayList<>();
+        artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
+        return artifactList;
+    }
+
+    private List<Artifact> createModelArtifacts() {
+        List<Artifact> modelArtifacts = new ArrayList<>();
+        modelArtifacts.add(new ModelArtifact());
+        modelArtifacts.add(new ModelArtifact());
+        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"));
+        vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
+        vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
+        return vnfcArtifacts;
+    }
+}