1b8f33b19262ef4563be9661a674a7ea643b5378
[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.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;
31
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Properties;
36
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;
60
61 /**
62  * Tests {@link ArtifactDownloadManager} with VNF Catalog Artifacts.
63  */
64 public class ArtifactDownloadManagerVnfcTest {
65
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;
73
74     @BeforeEach
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);
83
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);
88
89
90         ReflectionTestUtils.setField(downloadManager, "notificationPublisher", mockNotificationPublisher);
91         ReflectionTestUtils.setField(downloadManager, "babelArtifactConverter", mockBabelArtifactConverter);
92         ReflectionTestUtils.setField(downloadManager, "vnfCatalogExtractor", mockVnfCatalogExtractor);
93     }
94
95     @Test
96     public void downloadArtifacts_validToscaVnfcCsarFile()
97             throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
98         INotificationData data = getNotificationDataWithToscaCsarFile();
99         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
100
101         setupValidDownloadCsarMocks(data, artifactInfo);
102         when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifacts());
103         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
104
105         List<Artifact> modelArtifacts = new ArrayList<>();
106         List<Artifact> catalogFiles = new ArrayList<>();
107         assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
108                 is(true));
109
110         assertEquals(2, catalogFiles.size(), "There should have been some catalog files");
111     }
112
113     @Test
114     public void downloadArtifacts_validXmlVnfcCsarFile()
115             throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
116         INotificationData data = getNotificationDataWithToscaCsarFile();
117         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
118
119         setupValidDownloadCsarMocks(data, artifactInfo);
120         when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifactsNoVnfc());
121         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts());
122
123         List<Artifact> modelArtifacts = new ArrayList<>();
124         List<Artifact> catalogFiles = new ArrayList<>();
125         assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
126                 is(true));
127
128         assertEquals(3, catalogFiles.size(), "There should have been some catalog files");
129     }
130
131     @Test
132     public void downloadArtifacts_validNoVnfcCsarFile()
133             throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
134         INotificationData data = getNotificationDataWithToscaCsarFile();
135         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
136
137         setupValidDownloadCsarMocks(data, artifactInfo);
138         when(mockBabelClient.postArtifact(any(), any(), any(), any())).thenReturn(createBabelArtifactsNoVnfc());
139         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
140
141         List<Artifact> modelArtifacts = new ArrayList<>();
142         List<Artifact> catalogFiles = new ArrayList<>();
143         assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
144                 is(true));
145
146         assertEquals(0, catalogFiles.size(), "There should not have been any catalog files");
147     }
148
149     @Test
150     public void downloadArtifacts_invalidXmlAndToscaVnfcCsarFile()
151             throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
152         INotificationData data = getNotificationDataWithToscaCsarFile();
153         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
154
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);
159
160         List<Artifact> modelArtifacts = new ArrayList<>();
161         List<Artifact> catalogFiles = new ArrayList<>();
162         assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
163                 is(false));
164
165         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
166     }
167
168     private IDistributionClientDownloadResult createDistributionClientDownloadResult(
169             DistributionActionResultEnum status, String message, byte[] payload) {
170         IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message);
171
172         ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload);
173
174         return downloadResult;
175     }
176
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());
184     }
185
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"));
190         return artifactList;
191     }
192
193     private List<BabelArtifact> createBabelArtifactsNoVnfc() {
194         List<BabelArtifact> artifactList = new ArrayList<>();
195         artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
196         return artifactList;
197     }
198
199     private List<Artifact> createModelArtifacts() {
200         List<Artifact> modelArtifacts = new ArrayList<>();
201         modelArtifacts.add(new ModelArtifact());
202         modelArtifacts.add(new ModelArtifact());
203         return modelArtifacts;
204     }
205
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;
211     }
212
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;
219     }
220 }