2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.modelloader.notification;
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Properties;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.mockito.Mockito;
40 import org.onap.aai.babel.service.data.BabelArtifact;
41 import org.onap.aai.modelloader.config.ModelLoaderConfig;
42 import org.onap.aai.modelloader.entity.Artifact;
43 import org.onap.aai.modelloader.entity.ArtifactType;
44 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
45 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
46 import org.onap.aai.modelloader.entity.model.ModelArtifact;
47 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
48 import org.onap.aai.modelloader.extraction.VnfCatalogExtractor;
49 import org.onap.aai.modelloader.restclient.BabelServiceClient;
50 import org.onap.aai.modelloader.restclient.BabelServiceClientException;
51 import org.onap.aai.modelloader.service.BabelServiceClientFactory;
52 import org.onap.aai.modelloader.util.ArtifactTestUtils;
53 import org.onap.sdc.api.IDistributionClient;
54 import org.onap.sdc.api.notification.IArtifactInfo;
55 import org.onap.sdc.api.notification.INotificationData;
56 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
57 import org.onap.sdc.impl.DistributionClientDownloadResultImpl;
58 import org.onap.sdc.utils.DistributionActionResultEnum;
59 import org.springframework.test.util.ReflectionTestUtils;
62 * Tests {@link ArtifactDownloadManager} with VNF Catalog Artifacts.
64 public class ArtifactDownloadManagerVnfcTest {
66 private ArtifactDownloadManager downloadManager;
67 private BabelServiceClient mockBabelClient;
68 private IDistributionClient mockDistributionClient;
69 private NotificationPublisher mockNotificationPublisher;
70 private BabelArtifactConverter mockBabelArtifactConverter;
71 private BabelServiceClientFactory mockClientFactory;
72 private VnfCatalogExtractor mockVnfCatalogExtractor;
75 public void setup() throws Exception {
76 mockBabelClient = mock(BabelServiceClient.class);
77 mockDistributionClient = mock(IDistributionClient.class);
78 mockNotificationPublisher = mock(NotificationPublisher.class);
79 mockBabelArtifactConverter = mock(BabelArtifactConverter.class);
80 mockClientFactory = mock(BabelServiceClientFactory.class);
81 when(mockClientFactory.create(Mockito.any())).thenReturn(mockBabelClient);
82 mockVnfCatalogExtractor = mock(VnfCatalogExtractor.class);
84 Properties configProperties = new Properties();
85 configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties"));
86 downloadManager = new ArtifactDownloadManager(mockDistributionClient,
87 new ModelLoaderConfig(configProperties, "."), mockClientFactory);
90 ReflectionTestUtils.setField(downloadManager, "notificationPublisher", mockNotificationPublisher);
91 ReflectionTestUtils.setField(downloadManager, "babelArtifactConverter", mockBabelArtifactConverter);
92 ReflectionTestUtils.setField(downloadManager, "vnfCatalogExtractor", mockVnfCatalogExtractor);
96 public void downloadArtifacts_validToscaVnfcCsarFile()
97 throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
98 INotificationData data = getNotificationDataWithToscaCsarFile();
99 IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
101 setupValidDownloadCsarMocks(data, artifactInfo);
102 when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifacts());
103 when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
105 List<Artifact> modelArtifacts = new ArrayList<>();
106 List<Artifact> catalogFiles = new ArrayList<>();
107 assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
110 assertEquals(2, catalogFiles.size(), "There should have been some catalog files");
114 public void downloadArtifacts_validXmlVnfcCsarFile()
115 throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
116 INotificationData data = getNotificationDataWithToscaCsarFile();
117 IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
119 setupValidDownloadCsarMocks(data, artifactInfo);
120 when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifactsNoVnfc());
121 when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts());
123 List<Artifact> modelArtifacts = new ArrayList<>();
124 List<Artifact> catalogFiles = new ArrayList<>();
125 assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
128 assertEquals(3, catalogFiles.size(), "There should have been some catalog files");
132 public void downloadArtifacts_validNoVnfcCsarFile()
133 throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
134 INotificationData data = getNotificationDataWithToscaCsarFile();
135 IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
137 setupValidDownloadCsarMocks(data, artifactInfo);
138 when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifactsNoVnfc());
139 when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
141 List<Artifact> modelArtifacts = new ArrayList<>();
142 List<Artifact> catalogFiles = new ArrayList<>();
143 assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
146 assertEquals(0, catalogFiles.size(), "There should not have been any catalog files");
150 public void downloadArtifacts_invalidXmlAndToscaVnfcCsarFile()
151 throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
152 INotificationData data = getNotificationDataWithToscaCsarFile();
153 IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
155 setupValidDownloadCsarMocks(data, artifactInfo);
156 when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifacts());
157 when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts());
158 doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
160 List<Artifact> modelArtifacts = new ArrayList<>();
161 List<Artifact> catalogFiles = new ArrayList<>();
162 assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
165 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
168 private IDistributionClientDownloadResult createDistributionClientDownloadResult(
169 DistributionActionResultEnum status, String message, byte[] payload) {
170 IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message);
172 ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload);
174 return downloadResult;
177 private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo)
178 throws IOException, BabelServiceClientException, BabelArtifactParsingException {
179 when(mockDistributionClient.download(artifactInfo))
180 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
181 new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar")));
182 when(mockBabelArtifactConverter.convertToModel(Mockito.anyList())).thenReturn(createModelArtifacts());
183 when(mockBabelArtifactConverter.convertToCatalog(Mockito.anyList())).thenReturn(createToscaVnfcArtifacts());
186 private List<BabelArtifact> createBabelArtifacts() {
187 List<BabelArtifact> artifactList = new ArrayList<>();
188 artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
189 artifactList.add(new BabelArtifact("VNFCArtifact", BabelArtifact.ArtifactType.VNFCATALOG, "Some VNFC payload"));
193 private List<BabelArtifact> createBabelArtifactsNoVnfc() {
194 List<BabelArtifact> artifactList = new ArrayList<>();
195 artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
199 private List<Artifact> createModelArtifacts() {
200 List<Artifact> modelArtifacts = new ArrayList<>();
201 modelArtifacts.add(new ModelArtifact());
202 modelArtifacts.add(new ModelArtifact());
203 return modelArtifacts;
206 private List<Artifact> createToscaVnfcArtifacts() {
207 List<Artifact> vnfcArtifacts = new ArrayList<>();
208 vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload"));
209 vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload"));
210 return vnfcArtifacts;
213 private List<Artifact> createXmlVnfcArtifacts() {
214 List<Artifact> vnfcArtifacts = new ArrayList<>();
215 vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
216 vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
217 vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
218 return vnfcArtifacts;