2 * ============LICENSE_START==========================================
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
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.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;
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;
59 * Tests {@link ArtifactDownloadManager}
61 @RunWith(PowerMockRunner.class)
62 @PowerMockIgnore({"sun.security.ssl.*", "javax.net.ssl.*"})
63 @PrepareForTest({ArtifactDownloadManager.class})
64 public class ArtifactDownloadManagerTest {
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";
70 private ArtifactDownloadManager downloadManager;
71 private BabelServiceClient mockBabelClient;
72 private IDistributionClient mockDistributionClient;
73 private NotificationPublisher mockNotificationPublisher;
74 private BabelArtifactConverter mockBabelArtifactConverter;
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);
83 Properties configProperties = new Properties();
84 configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties"));
86 new ArtifactDownloadManager(mockDistributionClient, new ModelLoaderConfig(configProperties, "."));
88 PowerMockito.whenNew(BabelServiceClient.class).withAnyArguments().thenReturn(mockBabelClient);
90 Whitebox.setInternalState(downloadManager, mockNotificationPublisher);
91 Whitebox.setInternalState(downloadManager, mockBabelArtifactConverter);
95 public void tearDown() {
96 downloadManager = null;
97 mockDistributionClient = null;
98 mockNotificationPublisher = null;
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<>();
106 assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED, downloadManager
107 .downloadArtifacts(getNotificationDataWithOneService(), new ArrayList<>(), modelFiles, catalogFiles));
109 Mockito.verifyZeroInteractions(mockBabelClient, mockDistributionClient, mockNotificationPublisher,
110 mockBabelArtifactConverter);
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,
122 assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
123 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
125 Mockito.verify(mockDistributionClient).download(artifact);
126 Mockito.verify(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, artifact, OOPS);
128 Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
131 private IDistributionClientDownloadResult createDistributionClientDownloadResult(
132 DistributionActionResultEnum status, String message, byte[] payload) {
133 IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message);
135 ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload);
137 return downloadResult;
140 @SuppressWarnings("unchecked")
141 private void doCreateBabelClientFailureTest(Class<? extends Throwable> exception) throws Exception {
142 PowerMockito.whenNew(BabelServiceClient.class).withAnyArguments().thenThrow(exception);
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,
150 assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
151 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
153 Mockito.verify(mockDistributionClient).download(artifactInfo);
154 Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifactInfo);
155 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
157 Mockito.verifyZeroInteractions(mockBabelArtifactConverter);
161 * Test disabled as exception handling needs to be reworked
163 * @throws IOException
165 @SuppressWarnings("unchecked")
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,
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,
179 assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
180 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
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);
188 Mockito.verifyZeroInteractions(mockBabelArtifactConverter);
193 public void downloadArtifacts_invalidModelQuerySpec() {
194 INotificationData data = getNotificationDataWithModelQuerySpec();
195 IArtifactInfo artifact = data.getServiceArtifacts().get(0);
197 List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>();
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,
204 assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
205 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, null));
207 Mockito.verify(mockDistributionClient).download(artifact);
208 Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
209 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
211 Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
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,
221 PowerMockito.when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
222 Matchers.anyString())).thenReturn(createBabelArtifacts());
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"));
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);
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));
245 assertTrue("There should have been some model artifacts", !modelFiles.isEmpty());
246 assertTrue("There should not have been any catalog artifacts", catalogFiles.isEmpty());
248 Mockito.verify(mockDistributionClient).download(artifact);
249 Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
251 Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
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,
266 public void downloadArtifacts_invalidType()
267 throws IOException, BabelServiceException, BabelArtifactParsingException {
268 INotificationData data = getNotificationDataWithInvalidType();
269 IArtifactInfo artifact = data.getServiceArtifacts().get(0);
271 List<org.onap.aai.modelloader.entity.Artifact> catalogArtifacts = new ArrayList<>();
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,
278 assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
279 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, catalogArtifacts));
281 Mockito.verify(mockDistributionClient).download(artifact);
282 Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
283 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
285 Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);