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.Mockito.doNothing;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Properties;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Matchers;
38 import org.mockito.Mockito;
39 import org.mockito.internal.util.reflection.Whitebox;
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;
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 Whitebox.setInternalState(downloadManager, "notificationPublisher", mockNotificationPublisher);
90 Whitebox.setInternalState(downloadManager, "babelArtifactConverter", mockBabelArtifactConverter);
91 Whitebox.setInternalState(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(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
102 Matchers.anyString())).thenReturn(createBabelArtifacts());
103 when(mockVnfCatalogExtractor.extract(Matchers.any(), Matchers.anyString())).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("There should have been some catalog files", 2, catalogFiles.size());
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(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
121 Matchers.anyString())).thenReturn(createBabelArtifactsNoVnfc());
122 when(mockVnfCatalogExtractor.extract(Matchers.any(), Matchers.anyString()))
123 .thenReturn(createXmlVnfcArtifacts());
125 List<Artifact> modelArtifacts = new ArrayList<>();
126 List<Artifact> catalogFiles = new ArrayList<>();
127 assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
130 assertEquals("There should have been some catalog files", 3, catalogFiles.size());
134 public void downloadArtifacts_validNoVnfcCsarFile()
135 throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
136 INotificationData data = getNotificationDataWithToscaCsarFile();
137 IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
139 setupValidDownloadCsarMocks(data, artifactInfo);
140 when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
141 Matchers.anyString())).thenReturn(createBabelArtifactsNoVnfc());
142 when(mockVnfCatalogExtractor.extract(Matchers.any(), Matchers.anyString())).thenReturn(new ArrayList<>());
144 List<Artifact> modelArtifacts = new ArrayList<>();
145 List<Artifact> catalogFiles = new ArrayList<>();
146 assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
149 assertEquals("There should not have been any catalog files", 0, catalogFiles.size());
153 public void downloadArtifacts_invalidXmlAndToscaVnfcCsarFile()
154 throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
155 INotificationData data = getNotificationDataWithToscaCsarFile();
156 IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
158 setupValidDownloadCsarMocks(data, artifactInfo);
159 when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
160 Matchers.anyString())).thenReturn(createBabelArtifacts());
161 when(mockVnfCatalogExtractor.extract(Matchers.any(), Matchers.anyString()))
162 .thenReturn(createXmlVnfcArtifacts());
163 doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
165 List<Artifact> modelArtifacts = new ArrayList<>();
166 List<Artifact> catalogFiles = new ArrayList<>();
167 assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
170 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
173 private IDistributionClientDownloadResult createDistributionClientDownloadResult(
174 DistributionActionResultEnum status, String message, byte[] payload) {
175 IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message);
177 ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload);
179 return downloadResult;
182 private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo)
183 throws IOException, BabelServiceClientException, BabelArtifactParsingException {
184 when(mockDistributionClient.download(artifactInfo))
185 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
186 new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar")));
187 when(mockBabelArtifactConverter.convertToModel(Mockito.anyListOf(BabelArtifact.class)))
188 .thenReturn(createModelArtifacts());
189 when(mockBabelArtifactConverter.convertToCatalog(Mockito.anyListOf(BabelArtifact.class)))
190 .thenReturn(createToscaVnfcArtifacts());
193 private List<BabelArtifact> createBabelArtifacts() {
194 List<BabelArtifact> artifactList = new ArrayList<>();
195 artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
196 artifactList.add(new BabelArtifact("VNFCArtifact", BabelArtifact.ArtifactType.VNFCATALOG, "Some VNFC payload"));
200 private List<BabelArtifact> createBabelArtifactsNoVnfc() {
201 List<BabelArtifact> artifactList = new ArrayList<>();
202 artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
206 private List<Artifact> createModelArtifacts() {
207 List<Artifact> modelArtifacts = new ArrayList<>();
208 modelArtifacts.add(new ModelArtifact());
209 modelArtifacts.add(new ModelArtifact());
210 return modelArtifacts;
213 private List<Artifact> createToscaVnfcArtifacts() {
214 List<Artifact> vnfcArtifacts = new ArrayList<>();
215 vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload"));
216 vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload"));
217 return vnfcArtifacts;
220 private List<Artifact> createXmlVnfcArtifacts() {
221 List<Artifact> vnfcArtifacts = new ArrayList<>();
222 vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
223 vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
224 vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
225 return vnfcArtifacts;