Revisions made to the Model Loader to use Babel
[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 Amdocs
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.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithInvalidType;
26 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithModelQuerySpec;
27 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneService;
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 org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Matchers;
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.model.BabelArtifactParsingException;
43 import org.onap.aai.modelloader.restclient.BabelServiceClient;
44 import org.onap.aai.modelloader.restclient.BabelServiceClient.BabelServiceException;
45 import org.onap.aai.modelloader.util.ArtifactTestUtils;
46 import org.openecomp.sdc.api.IDistributionClient;
47 import org.openecomp.sdc.api.notification.IArtifactInfo;
48 import org.openecomp.sdc.api.notification.INotificationData;
49 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
50 import org.openecomp.sdc.impl.DistributionClientDownloadResultImpl;
51 import org.openecomp.sdc.utils.DistributionActionResultEnum;
52 import org.powermock.api.mockito.PowerMockito;
53 import org.powermock.core.classloader.annotations.PowerMockIgnore;
54 import org.powermock.core.classloader.annotations.PrepareForTest;
55 import org.powermock.modules.junit4.PowerMockRunner;
56 import org.powermock.reflect.Whitebox;
57
58 /**
59  * Tests {@link ArtifactDownloadManager}
60  */
61 @RunWith(PowerMockRunner.class)
62 @PowerMockIgnore({"sun.security.ssl.*", "javax.net.ssl.*"})
63 @PrepareForTest({ArtifactDownloadManager.class})
64 public class ArtifactDownloadManagerTest {
65
66     private static final String FALSE_SHOULD_HAVE_BEEN_RETURNED = "A value of 'false' should have been returned";
67     private static final String OOPS = "oops";
68     private static final String TRUE_SHOULD_HAVE_BEEN_RETURNED = "A value of 'true' should have been returned";
69
70     private ArtifactDownloadManager downloadManager;
71     private BabelServiceClient mockBabelClient;
72     private IDistributionClient mockDistributionClient;
73     private NotificationPublisher mockNotificationPublisher;
74     private BabelArtifactConverter mockBabelArtifactConverter;
75
76     @Before
77     public void setup() throws Exception {
78         mockBabelClient = PowerMockito.mock(BabelServiceClient.class);
79         mockDistributionClient = PowerMockito.mock(IDistributionClient.class);
80         mockNotificationPublisher = PowerMockito.mock(NotificationPublisher.class);
81         mockBabelArtifactConverter = PowerMockito.mock(BabelArtifactConverter.class);
82
83         Properties configProperties = new Properties();
84         configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties"));
85         downloadManager =
86                 new ArtifactDownloadManager(mockDistributionClient, new ModelLoaderConfig(configProperties, "."));
87
88         PowerMockito.whenNew(BabelServiceClient.class).withAnyArguments().thenReturn(mockBabelClient);
89
90         Whitebox.setInternalState(downloadManager, mockNotificationPublisher);
91         Whitebox.setInternalState(downloadManager, mockBabelArtifactConverter);
92     }
93
94     @After
95     public void tearDown() {
96         downloadManager = null;
97         mockDistributionClient = null;
98         mockNotificationPublisher = null;
99     }
100
101     @Test
102     public void downloadArtifacts_emptyListSupplied() {
103         List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>();
104         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
105
106         assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED, downloadManager
107                 .downloadArtifacts(getNotificationDataWithOneService(), new ArrayList<>(), modelFiles, catalogFiles));
108
109         Mockito.verifyZeroInteractions(mockBabelClient, mockDistributionClient, mockNotificationPublisher,
110                 mockBabelArtifactConverter);
111     }
112
113     @Test
114     public void downloadArtifacts_artifactDownloadFails() {
115         INotificationData data = getNotificationDataWithOneService();
116         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
117         PowerMockito.when(mockDistributionClient.download(artifact))
118                 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.FAIL, OOPS, null));
119         PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data,
120                 artifact, OOPS);
121
122         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
123                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
124
125         Mockito.verify(mockDistributionClient).download(artifact);
126         Mockito.verify(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, artifact, OOPS);
127
128         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
129     }
130
131     private IDistributionClientDownloadResult createDistributionClientDownloadResult(
132             DistributionActionResultEnum status, String message, byte[] payload) {
133         IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message);
134
135         ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload);
136
137         return downloadResult;
138     }
139
140     @SuppressWarnings("unchecked")
141     private void doCreateBabelClientFailureTest(Class<? extends Throwable> exception) throws Exception {
142         PowerMockito.whenNew(BabelServiceClient.class).withAnyArguments().thenThrow(exception);
143
144         INotificationData data = getNotificationDataWithToscaCsarFile();
145         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
146         setupValidDownloadCsarMocks(data, artifactInfo, new ArtifactTestUtils());
147         PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
148                 artifactInfo);
149
150         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
151                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
152
153         Mockito.verify(mockDistributionClient).download(artifactInfo);
154         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifactInfo);
155         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
156
157         Mockito.verifyZeroInteractions(mockBabelArtifactConverter);
158     }
159
160     /**
161      * Test disabled as exception handling needs to be reworked
162      * 
163      * @throws IOException
164      */
165     @SuppressWarnings("unchecked")
166     @Test
167     public void downloadArtifacts_invalidToscaCsarFile() throws IOException, BabelServiceException {
168         INotificationData data = getNotificationDataWithToscaCsarFile();
169         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
170         PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult(
171                 DistributionActionResultEnum.SUCCESS, null, "This is not a valid Tosca CSAR File".getBytes()));
172         PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data,
173                 artifact);
174         PowerMockito.when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
175                 Matchers.anyString())).thenThrow(BabelServiceException.class);
176         PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
177                 artifact);
178
179         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
180                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
181
182         Mockito.verify(mockDistributionClient).download(artifact);
183         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
184         Mockito.verify(mockBabelClient).postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
185                 Matchers.anyString());
186         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
187
188         Mockito.verifyZeroInteractions(mockBabelArtifactConverter);
189
190     }
191
192     @Test
193     public void downloadArtifacts_invalidModelQuerySpec() {
194         INotificationData data = getNotificationDataWithModelQuerySpec();
195         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
196
197         List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>();
198
199         PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult(
200                 DistributionActionResultEnum.SUCCESS, null, "This is not a valid Model Query Spec".getBytes()));
201         PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data,
202                 artifact);
203
204         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
205                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, null));
206
207         Mockito.verify(mockDistributionClient).download(artifact);
208         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
209         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
210
211         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
212     }
213
214     private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo,
215             ArtifactTestUtils artifactTestUtils) throws IOException, BabelServiceException {
216         PowerMockito.when(mockDistributionClient.download(artifactInfo))
217                 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
218                         artifactTestUtils.loadResource("compressedArtifacts/service-VscpaasTest-csar.csar")));
219         PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data,
220                 artifactInfo);
221         PowerMockito.when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
222                 Matchers.anyString())).thenReturn(createBabelArtifacts());
223     }
224
225     private List<BabelArtifact> createBabelArtifacts() {
226         List<BabelArtifact> artifactList = new ArrayList<>();
227         artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
228         artifactList.add(new BabelArtifact("VNFCArtifact", BabelArtifact.ArtifactType.VNFCATALOG, "Some VNFC payload"));
229         return artifactList;
230     }
231
232     @Test
233     public void downloadArtifacts_validModelQuerySpec()
234             throws IOException, BabelServiceException, BabelArtifactParsingException {
235         ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
236         INotificationData data = getNotificationDataWithModelQuerySpec();
237         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
238         setupValidModelQuerySpecMocks(artifactTestUtils, data, artifact);
239
240         List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>();
241         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
242         assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED,
243                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelFiles, catalogFiles));
244
245         assertTrue("There should have been some model artifacts", !modelFiles.isEmpty());
246         assertTrue("There should not have been any catalog artifacts", catalogFiles.isEmpty());
247
248         Mockito.verify(mockDistributionClient).download(artifact);
249         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
250
251         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
252     }
253
254     private void setupValidModelQuerySpecMocks(ArtifactTestUtils artifactTestUtils, INotificationData data,
255             IArtifactInfo artifact) throws IOException {
256         PowerMockito.when(mockDistributionClient.download(artifact))
257                 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
258                         artifactTestUtils.loadResource("models/named-query-wan-connector.xml")));
259         PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data,
260                 artifact);
261     }
262
263
264
265     @Test
266     public void downloadArtifacts_invalidType()
267             throws IOException, BabelServiceException, BabelArtifactParsingException {
268         INotificationData data = getNotificationDataWithInvalidType();
269         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
270
271         List<org.onap.aai.modelloader.entity.Artifact> catalogArtifacts = new ArrayList<>();
272
273         PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult(
274                 DistributionActionResultEnum.SUCCESS, null, "This content does not matter.".getBytes()));
275         PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data,
276                 artifact);
277
278         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
279                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, catalogArtifacts));
280
281         Mockito.verify(mockDistributionClient).download(artifact);
282         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
283         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
284
285         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
286     }
287 }