Remove deprecated AJSC configuration
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / ArtifactDownloadManagerTest.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.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithInvalidType;
29 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithModelQuerySpec;
30 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneOfEach;
31 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneService;
32 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile;
33
34 import java.io.IOException;
35 import java.security.KeyManagementException;
36 import java.security.KeyStoreException;
37 import java.security.NoSuchAlgorithmException;
38 import java.security.UnrecoverableKeyException;
39 import java.security.cert.CertificateException;
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Properties;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.mockito.Matchers;
47 import org.mockito.Mockito;
48 import org.mockito.internal.util.reflection.Whitebox;
49 import org.onap.aai.babel.service.data.BabelArtifact;
50 import org.onap.aai.modelloader.config.ModelLoaderConfig;
51 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
52 import org.onap.aai.modelloader.restclient.BabelServiceClient;
53 import org.onap.aai.modelloader.restclient.BabelServiceClient.BabelServiceException;
54 import org.onap.aai.modelloader.restclient.BabelServiceClientFactory;
55 import org.onap.aai.modelloader.util.ArtifactTestUtils;
56 import org.onap.sdc.api.IDistributionClient;
57 import org.onap.sdc.api.notification.IArtifactInfo;
58 import org.onap.sdc.api.notification.INotificationData;
59 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
60 import org.onap.sdc.impl.DistributionClientDownloadResultImpl;
61 import org.onap.sdc.utils.DistributionActionResultEnum;
62
63 /**
64  * Tests {@link ArtifactDownloadManager}
65  */
66 public class ArtifactDownloadManagerTest {
67
68     private static final String FALSE_SHOULD_HAVE_BEEN_RETURNED = "A value of 'false' should have been returned";
69     private static final String OOPS = "oops";
70     private static final String TRUE_SHOULD_HAVE_BEEN_RETURNED = "A value of 'true' should have been returned";
71
72     private ArtifactDownloadManager downloadManager;
73     private BabelServiceClient mockBabelClient;
74     private IDistributionClient mockDistributionClient;
75     private NotificationPublisher mockNotificationPublisher;
76     private BabelArtifactConverter mockBabelArtifactConverter;
77     private BabelServiceClientFactory mockClientFactory;
78
79     @Before
80     public void setup() throws Exception {
81         mockBabelClient = mock(BabelServiceClient.class);
82         mockDistributionClient = mock(IDistributionClient.class);
83         mockNotificationPublisher = mock(NotificationPublisher.class);
84         mockBabelArtifactConverter = mock(BabelArtifactConverter.class);
85         mockClientFactory = mock(BabelServiceClientFactory.class);
86         when(mockClientFactory.create(Mockito.any())).thenReturn(mockBabelClient);
87
88         Properties configProperties = new Properties();
89         configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties"));
90         downloadManager = new ArtifactDownloadManager(mockDistributionClient,
91                 new ModelLoaderConfig(configProperties, "."), mockClientFactory);
92
93         Whitebox.setInternalState(downloadManager, "notificationPublisher", mockNotificationPublisher);
94         Whitebox.setInternalState(downloadManager, "babelArtifactConverter", mockBabelArtifactConverter);
95     }
96
97     @After
98     public void tearDown() {
99         downloadManager = null;
100         mockDistributionClient = null;
101         mockNotificationPublisher = null;
102     }
103
104     @Test
105     public void downloadArtifacts_emptyListSupplied() {
106         List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>();
107         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
108
109         assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED, downloadManager
110                 .downloadArtifacts(getNotificationDataWithOneService(), new ArrayList<>(), modelFiles, catalogFiles));
111
112         Mockito.verifyZeroInteractions(mockBabelClient, mockDistributionClient, mockNotificationPublisher,
113                 mockBabelArtifactConverter);
114     }
115
116     @Test
117     public void downloadArtifacts_artifactDownloadFails() {
118         INotificationData data = getNotificationDataWithOneService();
119         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
120         when(mockDistributionClient.download(artifact))
121                 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.FAIL, OOPS, null));
122         doNothing().when(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, artifact,
123                 OOPS);
124
125         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
126                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
127
128         Mockito.verify(mockDistributionClient).download(artifact);
129         Mockito.verify(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, artifact, OOPS);
130
131         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
132     }
133
134     private IDistributionClientDownloadResult createDistributionClientDownloadResult(
135             DistributionActionResultEnum status, String message, byte[] payload) {
136         IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message);
137
138         ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload);
139
140         return downloadResult;
141     }
142
143     @Test
144     public void downloadArtifacts_noSuchAlgorithmExceptionFromCreatingBabelClient() throws Exception {
145         doCreateBabelClientFailureTest(NoSuchAlgorithmException.class);
146     }
147
148     @SuppressWarnings("unchecked")
149     private void doCreateBabelClientFailureTest(Class<? extends Throwable> exception) throws Exception {
150         when(mockClientFactory.create(Mockito.any())).thenThrow(exception);
151
152         INotificationData data = getNotificationDataWithToscaCsarFile();
153         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
154         setupValidDownloadCsarMocks(data, artifactInfo, new ArtifactTestUtils());
155         doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
156
157         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
158                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
159
160         Mockito.verify(mockDistributionClient).download(artifactInfo);
161         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifactInfo);
162         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
163
164         Mockito.verifyZeroInteractions(mockBabelArtifactConverter);
165     }
166
167     @Test
168     public void downloadArtifacts_keyStoreExceptionFromCreatingBabelClient() throws Exception {
169         doCreateBabelClientFailureTest(KeyStoreException.class);
170     }
171
172     @Test
173     public void downloadArtifacts_certificateExceptionFromCreatingBabelClient() throws Exception {
174         doCreateBabelClientFailureTest(CertificateException.class);
175     }
176
177     @Test
178     public void downloadArtifacts_iOExceptionFromCreatingBabelClient() throws Exception {
179         doCreateBabelClientFailureTest(IOException.class);
180     }
181
182     @Test
183     public void downloadArtifacts_unrecoverableKeyExceptionFromCreatingBabelClient() throws Exception {
184         doCreateBabelClientFailureTest(UnrecoverableKeyException.class);
185     }
186
187     @Test
188     public void downloadArtifacts_keyManagementExceptionFromCreatingBabelClient() throws Exception {
189         doCreateBabelClientFailureTest(KeyManagementException.class);
190     }
191
192     /**
193      * Test disabled as exception handling needs to be reworked
194      *
195      * @throws IOException
196      */
197     @SuppressWarnings("unchecked")
198     @Test
199     public void downloadArtifacts_invalidToscaCsarFile() throws IOException, BabelServiceException {
200         INotificationData data = getNotificationDataWithToscaCsarFile();
201         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
202         when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult(
203                 DistributionActionResultEnum.SUCCESS, null, "This is not a valid Tosca CSAR File".getBytes()));
204         doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
205         when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
206                 Matchers.anyString())).thenThrow(BabelServiceException.class);
207         doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
208
209         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
210                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
211
212         Mockito.verify(mockDistributionClient).download(artifact);
213         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
214         Mockito.verify(mockBabelClient).postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
215                 Matchers.anyString());
216         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
217
218         Mockito.verifyZeroInteractions(mockBabelArtifactConverter);
219
220     }
221
222     @Test
223     public void downloadArtifacts_invalidModelQuerySpec() {
224         INotificationData data = getNotificationDataWithModelQuerySpec();
225         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
226
227         List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>();
228
229         when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult(
230                 DistributionActionResultEnum.SUCCESS, null, "This is not a valid Model Query Spec".getBytes()));
231         doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
232
233         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
234                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, null));
235
236         Mockito.verify(mockDistributionClient).download(artifact);
237         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
238         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
239
240         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
241     }
242
243     @Test
244     public void downloadArtifacts_validToscaCsarFile()
245             throws IOException, BabelServiceException, BabelArtifactParsingException {
246         INotificationData data = getNotificationDataWithToscaCsarFile();
247         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
248
249         setupValidDownloadCsarMocks(data, artifactInfo, new ArtifactTestUtils());
250
251         List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>();
252         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
253         assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED,
254                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles));
255
256         assertTrue("There should not have been any catalog files", catalogFiles.size() == 0);
257
258         Mockito.verify(mockDistributionClient).download(artifactInfo);
259         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifactInfo);
260         Mockito.verify(mockBabelClient).postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
261                 Matchers.anyString());
262         Mockito.verify(mockBabelArtifactConverter).convertToModel(Matchers.any());
263         Mockito.verify(mockBabelArtifactConverter).convertToCatalog(Matchers.any());
264     }
265
266     private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo,
267             ArtifactTestUtils artifactTestUtils) throws IOException, BabelServiceException {
268         when(mockDistributionClient.download(artifactInfo))
269                 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
270                         artifactTestUtils.loadResource("compressedArtifacts/service-VscpaasTest-csar.csar")));
271         when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
272                 Matchers.anyString())).thenReturn(createBabelArtifacts());
273     }
274
275     private List<BabelArtifact> createBabelArtifacts() {
276         List<BabelArtifact> artifactList = new ArrayList<>();
277         artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
278         artifactList.add(new BabelArtifact("VNFCArtifact", BabelArtifact.ArtifactType.VNFCATALOG, "Some VNFC payload"));
279         return artifactList;
280     }
281
282     @Test
283     public void downloadArtifacts_validModelQuerySpec()
284             throws IOException, BabelServiceException, BabelArtifactParsingException {
285         ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
286         INotificationData data = getNotificationDataWithModelQuerySpec();
287         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
288         setupValidModelQuerySpecMocks(artifactTestUtils, data, artifact);
289
290         List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>();
291         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
292         assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED,
293                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelFiles, catalogFiles));
294
295         assertTrue("There should have been some model artifacts", !modelFiles.isEmpty());
296         assertTrue("There should not have been any catalog artifacts", catalogFiles.isEmpty());
297
298         Mockito.verify(mockDistributionClient).download(artifact);
299         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
300
301         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
302     }
303
304     private void setupValidModelQuerySpecMocks(ArtifactTestUtils artifactTestUtils, INotificationData data,
305             IArtifactInfo artifact) throws IOException {
306         when(mockDistributionClient.download(artifact))
307                 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
308                         artifactTestUtils.loadResource("models/named-query-wan-connector.xml")));
309         doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
310     }
311
312     @Test
313     public void downloadArtifacts_validCsarAndModelFiles()
314             throws IOException, BabelServiceException, BabelArtifactParsingException {
315         ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
316         INotificationData data = getNotificationDataWithOneOfEach();
317         List<IArtifactInfo> artifacts = new ArrayList<>();
318
319         IArtifactInfo serviceArtifact = data.getServiceArtifacts().get(0);
320         IArtifactInfo modelSpecArtifact = data.getResources().get(1).getArtifacts().get(0);
321
322         artifacts.add(serviceArtifact);
323         artifacts.add(modelSpecArtifact);
324
325         setupValidDownloadCsarMocks(data, serviceArtifact, artifactTestUtils);
326         setupValidModelQuerySpecMocks(artifactTestUtils, data, modelSpecArtifact);
327
328         List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>();
329         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
330         assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED,
331                 downloadManager.downloadArtifacts(data, artifacts, modelFiles, catalogFiles));
332
333         Mockito.verify(mockDistributionClient).download(serviceArtifact);
334         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, serviceArtifact);
335         Mockito.verify(mockBabelClient).postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
336                 Matchers.anyString());
337         Mockito.verify(mockBabelArtifactConverter).convertToModel(Matchers.any());
338         Mockito.verify(mockBabelArtifactConverter).convertToCatalog(Matchers.any());
339
340         Mockito.verify(mockDistributionClient).download(modelSpecArtifact);
341         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data,
342                 modelSpecArtifact);
343
344         Mockito.verifyNoMoreInteractions(mockBabelClient, mockBabelArtifactConverter);
345     }
346
347     @Test
348     @SuppressWarnings("unchecked")
349     public void activateCallback_toscaToModelConverterHasProcessToscaArtifactsException() throws Exception {
350         ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
351         INotificationData data = getNotificationDataWithToscaCsarFile();
352         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
353
354         when(mockDistributionClient.download(artifactInfo))
355                 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
356                         artifactTestUtils.loadResource("compressedArtifacts/service-VscpaasTest-csar.csar")));
357         when(mockBabelArtifactConverter.convertToModel(Mockito.anyList()))
358                 .thenThrow(BabelArtifactParsingException.class);
359         doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
360         when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
361                 Matchers.anyString())).thenReturn(createBabelArtifacts());
362
363         List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>();
364         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
365         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
366                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles));
367
368         assertTrue("There should not have been any catalog files", catalogFiles.size() == 0);
369
370         Mockito.verify(mockDistributionClient).download(artifactInfo);
371         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
372         Mockito.verify(mockBabelClient).postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
373                 Matchers.anyString());
374         Mockito.verify(mockBabelArtifactConverter).convertToModel(Matchers.any());
375     }
376
377     @Test
378     public void downloadArtifacts_invalidType()
379             throws IOException, BabelServiceException, BabelArtifactParsingException {
380         INotificationData data = getNotificationDataWithInvalidType();
381         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
382
383         List<org.onap.aai.modelloader.entity.Artifact> catalogArtifacts = new ArrayList<>();
384
385         when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult(
386                 DistributionActionResultEnum.SUCCESS, null, "This content does not matter.".getBytes()));
387         doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
388
389         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
390                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, catalogArtifacts));
391
392         Mockito.verify(mockDistributionClient).download(artifact);
393         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
394         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
395
396         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
397     }
398 }