Return List<Artifact> in ArtifactDownloadManager
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / ArtifactDownloadManagerVnfcTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.modelloader.notification;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doNothing;
27 import static org.mockito.Mockito.when;
28 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Properties;
34 import java.util.stream.Collectors;
35
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.aai.babel.service.data.BabelArtifact;
43 import org.onap.aai.modelloader.babel.BabelArtifactService;
44 import org.onap.aai.modelloader.entity.Artifact;
45 import org.onap.aai.modelloader.entity.ArtifactType;
46 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
47 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
48 import org.onap.aai.modelloader.entity.model.ModelArtifact;
49 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
50 import org.onap.aai.modelloader.extraction.VnfCatalogExtractor;
51 import org.onap.aai.modelloader.restclient.BabelServiceClient;
52 import org.onap.aai.modelloader.restclient.BabelServiceClientException;
53 import org.onap.aai.modelloader.util.ArtifactTestUtils;
54 import org.onap.sdc.api.IDistributionClient;
55 import org.onap.sdc.api.notification.IArtifactInfo;
56 import org.onap.sdc.api.notification.INotificationData;
57 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
58 import org.onap.sdc.impl.DistributionClientDownloadResultImpl;
59 import org.onap.sdc.utils.DistributionActionResultEnum;
60
61 /**
62  * Tests {@link ArtifactDownloadManager} with VNF Catalog Artifacts.
63  */
64 public class ArtifactDownloadManagerVnfcTest {
65
66     @Mock private ArtifactDownloadManager downloadManager;
67     @Mock private BabelServiceClient mockBabelClient;
68     @Mock private IDistributionClient mockDistributionClient;
69     @Mock private NotificationPublisher mockNotificationPublisher;
70     @Mock private BabelArtifactConverter mockBabelArtifactConverter;
71     @Mock private VnfCatalogExtractor mockVnfCatalogExtractor;
72     @InjectMocks private BabelArtifactService babelArtifactService;
73
74     @BeforeEach
75     public void setup() throws Exception {
76         MockitoAnnotations.openMocks(this);
77
78         Properties configProperties = new Properties();
79         configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties"));
80         downloadManager = new ArtifactDownloadManager(mockDistributionClient,
81                 mockNotificationPublisher, mockVnfCatalogExtractor, babelArtifactService);
82     }
83
84     @Test
85     public void downloadArtifacts_validToscaVnfcCsarFile()
86             throws Exception {
87         INotificationData data = getNotificationDataWithToscaCsarFile();
88         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
89
90         setupValidDownloadCsarMocks(data, artifactInfo);
91         when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifacts());
92         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
93
94         List<Artifact> artifacts = downloadManager.downloadArtifacts(data, data.getServiceArtifacts());
95         List<Artifact> catalogArtifacts = artifacts.stream()
96                 .filter(VnfCatalogArtifact.class::isInstance)
97                 .collect(Collectors.toList());
98         List<Artifact> modelArtifacts = artifacts.stream()
99                 .filter(ModelArtifact.class::isInstance)
100                 .collect(Collectors.toList());
101         assertEquals(1, catalogArtifacts.size(), "There should be a catalog artifact");
102         assertEquals(1, modelArtifacts.size(), "There should be a model artifact");
103     }
104
105     @Test
106     public void downloadArtifacts_validXmlVnfcCsarFile()
107             throws Exception {
108         INotificationData data = getNotificationDataWithToscaCsarFile();
109         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
110
111         setupValidDownloadCsarMocks(data, artifactInfo);
112         when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifactsNoVnfc());
113         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts());
114
115         List<Artifact> artifacts = downloadManager.downloadArtifacts(data, data.getServiceArtifacts());
116
117         List<Artifact> catalogArtifacts = artifacts.stream()
118                 .filter(VnfCatalogArtifact.class::isInstance)
119                 .collect(Collectors.toList());
120         List<Artifact> modelArtifacts = artifacts.stream()
121                 .filter(ModelArtifact.class::isInstance)
122                 .collect(Collectors.toList());
123         assertEquals(3, catalogArtifacts.size(), "There should be three catalog artifacts");
124         assertEquals(1, modelArtifacts.size(), "There should be a model artifact");
125     }
126
127     @Test
128     public void downloadArtifacts_validNoVnfcCsarFile()
129             throws Exception {
130         INotificationData data = getNotificationDataWithToscaCsarFile();
131         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
132
133         setupValidDownloadCsarMocks(data, artifactInfo);
134         when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifactsNoVnfc());
135         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
136
137         List<Artifact> artifacts = downloadManager.downloadArtifacts(data, data.getServiceArtifacts());
138         List<Artifact> catalogArtifacts = artifacts.stream()
139                 .filter(VnfCatalogArtifact.class::isInstance)
140                 .collect(Collectors.toList());
141
142         assertEquals(0, catalogArtifacts.size(), "There should not have been any catalog files");
143     }
144
145     @Test
146     public void downloadArtifacts_invalidXmlAndToscaVnfcCsarFile()
147             throws Exception {
148         INotificationData data = getNotificationDataWithToscaCsarFile();
149         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
150
151         setupValidDownloadCsarMocks(data, artifactInfo);
152         when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifacts());
153         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts());
154         doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
155
156         InvalidArchiveException invalidArchiveException = assertThrows(InvalidArchiveException.class,
157                 () -> downloadManager.downloadArtifacts(data, data.getServiceArtifacts()));
158
159         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
160     }
161
162     private IDistributionClientDownloadResult createDistributionClientDownloadResult(
163             DistributionActionResultEnum status, String message, byte[] payload) {
164         IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message);
165
166         ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload);
167
168         return downloadResult;
169     }
170
171     private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo)
172             throws IOException, BabelServiceClientException, BabelArtifactParsingException {
173         when(mockDistributionClient.download(artifactInfo))
174                 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
175                         new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar")));
176         when(mockBabelArtifactConverter.convertToModel(Mockito.any(BabelArtifact.class))).thenReturn(createModelArtifacts());
177         when(mockBabelArtifactConverter.convertToCatalog(Mockito.any(BabelArtifact.class))).thenReturn(new VnfCatalogArtifact("Some VNFC payload"));
178     }
179
180     private List<BabelArtifact> createBabelArtifacts() {
181         List<BabelArtifact> artifactList = new ArrayList<>();
182         artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
183         artifactList.add(new BabelArtifact("VNFCArtifact", BabelArtifact.ArtifactType.VNFCATALOG, "Some VNFC payload"));
184         return artifactList;
185     }
186
187     private List<BabelArtifact> createBabelArtifactsNoVnfc() {
188         List<BabelArtifact> artifactList = new ArrayList<>();
189         artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
190         return artifactList;
191     }
192
193     private List<Artifact> createModelArtifacts() {
194         List<Artifact> modelArtifacts = new ArrayList<>();
195         modelArtifacts.add(new ModelArtifact());
196         return modelArtifacts;
197     }
198
199     private List<Artifact> createXmlVnfcArtifacts() {
200         List<Artifact> vnfcArtifacts = new ArrayList<>();
201         vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
202         vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
203         vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
204         return vnfcArtifacts;
205     }
206 }