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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
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;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.onap.aai.babel.service.data.BabelArtifact;
40 import org.onap.aai.modelloader.config.ModelLoaderConfig;
41 import org.onap.aai.modelloader.entity.Artifact;
42 import org.onap.aai.modelloader.entity.ArtifactType;
43 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
44 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
45 import org.onap.aai.modelloader.entity.model.ModelArtifact;
46 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
47 import org.onap.aai.modelloader.extraction.VnfCatalogExtractor;
48 import org.onap.aai.modelloader.restclient.BabelServiceClient;
49 import org.onap.aai.modelloader.restclient.BabelServiceClientException;
50 import org.onap.aai.modelloader.service.BabelServiceClientFactory;
51 import org.onap.aai.modelloader.util.ArtifactTestUtils;
52 import org.onap.sdc.api.IDistributionClient;
53 import org.onap.sdc.api.notification.IArtifactInfo;
54 import org.onap.sdc.api.notification.INotificationData;
55 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
56 import org.onap.sdc.impl.DistributionClientDownloadResultImpl;
57 import org.onap.sdc.utils.DistributionActionResultEnum;
58 import org.springframework.test.util.ReflectionTestUtils;
61 * Tests {@link ArtifactDownloadManager} with VNF Catalog Artifacts.
63 public class ArtifactDownloadManagerVnfcTest {
65 private ArtifactDownloadManager downloadManager;
66 private BabelServiceClient mockBabelClient;
67 private IDistributionClient mockDistributionClient;
68 private NotificationPublisher mockNotificationPublisher;
69 private BabelArtifactConverter mockBabelArtifactConverter;
70 private BabelServiceClientFactory mockClientFactory;
71 private VnfCatalogExtractor mockVnfCatalogExtractor;
74 public void setup() throws Exception {
75 mockBabelClient = mock(BabelServiceClient.class);
76 mockDistributionClient = mock(IDistributionClient.class);
77 mockNotificationPublisher = mock(NotificationPublisher.class);
78 mockBabelArtifactConverter = mock(BabelArtifactConverter.class);
79 mockClientFactory = mock(BabelServiceClientFactory.class);
80 when(mockClientFactory.create(Mockito.any())).thenReturn(mockBabelClient);
81 mockVnfCatalogExtractor = mock(VnfCatalogExtractor.class);
83 Properties configProperties = new Properties();
84 configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties"));
85 downloadManager = new ArtifactDownloadManager(mockDistributionClient,
86 new ModelLoaderConfig(configProperties, "."), mockClientFactory);
89 ReflectionTestUtils.setField(downloadManager, "notificationPublisher", mockNotificationPublisher);
90 ReflectionTestUtils.setField(downloadManager, "babelArtifactConverter", mockBabelArtifactConverter);
91 ReflectionTestUtils.setField(downloadManager, "vnfCatalogExtractor", mockVnfCatalogExtractor);
95 public void downloadArtifacts_validToscaVnfcCsarFile()
96 throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
97 INotificationData data = getNotificationDataWithToscaCsarFile();
98 IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
100 setupValidDownloadCsarMocks(data, artifactInfo);
101 when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifacts());
102 when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
104 List<Artifact> modelArtifacts = new ArrayList<>();
105 List<Artifact> catalogFiles = new ArrayList<>();
106 assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
109 assertEquals("There should have been some catalog files", 2, catalogFiles.size());
113 public void downloadArtifacts_validXmlVnfcCsarFile()
114 throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
115 INotificationData data = getNotificationDataWithToscaCsarFile();
116 IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
118 setupValidDownloadCsarMocks(data, artifactInfo);
119 when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifactsNoVnfc());
120 when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts());
122 List<Artifact> modelArtifacts = new ArrayList<>();
123 List<Artifact> catalogFiles = new ArrayList<>();
124 assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
127 assertEquals("There should have been some catalog files", 3, catalogFiles.size());
131 public void downloadArtifacts_validNoVnfcCsarFile()
132 throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
133 INotificationData data = getNotificationDataWithToscaCsarFile();
134 IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
136 setupValidDownloadCsarMocks(data, artifactInfo);
137 when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifactsNoVnfc());
138 when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
140 List<Artifact> modelArtifacts = new ArrayList<>();
141 List<Artifact> catalogFiles = new ArrayList<>();
142 assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
145 assertEquals("There should not have been any catalog files", 0, catalogFiles.size());
149 public void downloadArtifacts_invalidXmlAndToscaVnfcCsarFile()
150 throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
151 INotificationData data = getNotificationDataWithToscaCsarFile();
152 IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
154 setupValidDownloadCsarMocks(data, artifactInfo);
155 when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifacts());
156 when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts());
157 doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
159 List<Artifact> modelArtifacts = new ArrayList<>();
160 List<Artifact> catalogFiles = new ArrayList<>();
161 assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
164 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
167 private IDistributionClientDownloadResult createDistributionClientDownloadResult(
168 DistributionActionResultEnum status, String message, byte[] payload) {
169 IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message);
171 ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload);
173 return downloadResult;
176 private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo)
177 throws IOException, BabelServiceClientException, BabelArtifactParsingException {
178 when(mockDistributionClient.download(artifactInfo))
179 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
180 new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar")));
181 when(mockBabelArtifactConverter.convertToModel(Mockito.anyList())).thenReturn(createModelArtifacts());
182 when(mockBabelArtifactConverter.convertToCatalog(Mockito.anyList())).thenReturn(createToscaVnfcArtifacts());
185 private List<BabelArtifact> createBabelArtifacts() {
186 List<BabelArtifact> artifactList = new ArrayList<>();
187 artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
188 artifactList.add(new BabelArtifact("VNFCArtifact", BabelArtifact.ArtifactType.VNFCATALOG, "Some VNFC payload"));
192 private List<BabelArtifact> createBabelArtifactsNoVnfc() {
193 List<BabelArtifact> artifactList = new ArrayList<>();
194 artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
198 private List<Artifact> createModelArtifacts() {
199 List<Artifact> modelArtifacts = new ArrayList<>();
200 modelArtifacts.add(new ModelArtifact());
201 modelArtifacts.add(new ModelArtifact());
202 return modelArtifacts;
205 private List<Artifact> createToscaVnfcArtifacts() {
206 List<Artifact> vnfcArtifacts = new ArrayList<>();
207 vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload"));
208 vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload"));
209 return vnfcArtifacts;
212 private List<Artifact> createXmlVnfcArtifacts() {
213 List<Artifact> vnfcArtifacts = new ArrayList<>();
214 vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
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 return vnfcArtifacts;