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.mockito.Matchers;
38 import org.mockito.Mockito;
39 import org.onap.aai.babel.service.data.BabelArtifact;
40 import org.onap.aai.modelloader.config.ModelLoaderConfig;
41 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
42 import org.onap.aai.modelloader.restclient.BabelServiceClient;
43 import org.onap.aai.modelloader.restclient.BabelServiceClient.BabelServiceException;
44 import org.onap.aai.modelloader.util.ArtifactTestUtils;
45 import org.openecomp.sdc.api.IDistributionClient;
46 import org.openecomp.sdc.api.notification.IArtifactInfo;
47 import org.openecomp.sdc.api.notification.INotificationData;
48 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
49 import org.openecomp.sdc.impl.DistributionClientDownloadResultImpl;
50 import org.openecomp.sdc.utils.DistributionActionResultEnum;
51 import org.powermock.api.mockito.PowerMockito;
52 import org.powermock.core.classloader.annotations.PowerMockIgnore;
53 import org.powermock.core.classloader.annotations.PrepareForTest;
54 import org.powermock.reflect.Whitebox;
59 * Because Jacoco (and other coverage tools) can't cope with mocked classes under some circumstances, coverage is/was
60 * falsely reported as < 50%. Hence these duplicated but non-mock tests to address this, for ONAP reasons.
67 * Tests {@link ArtifactDownloadManager}
69 @PowerMockIgnore({"sun.security.ssl.*", "javax.net.ssl.*"})
70 @PrepareForTest({ArtifactDownloadManager.class})
71 public class ArtifactDownloadManagerNoMockTest {
73 private static final String FALSE_SHOULD_HAVE_BEEN_RETURNED = "A value of 'false' should have been returned";
74 private static final String OOPS = "oops";
75 private static final String TRUE_SHOULD_HAVE_BEEN_RETURNED = "A value of 'true' should have been returned";
77 private ArtifactDownloadManager downloadManager;
78 private BabelServiceClient mockBabelClient;
79 private IDistributionClient mockDistributionClient;
80 private NotificationPublisher mockNotificationPublisher;
81 private BabelArtifactConverter mockBabelArtifactConverter;
84 public void setup() throws Exception {
85 mockBabelClient = PowerMockito.mock(BabelServiceClient.class);
86 mockDistributionClient = PowerMockito.mock(IDistributionClient.class);
87 mockNotificationPublisher = PowerMockito.mock(NotificationPublisher.class);
88 mockBabelArtifactConverter = PowerMockito.mock(BabelArtifactConverter.class);
90 Properties configProperties = new Properties();
91 configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties"));
93 new ArtifactDownloadManager(mockDistributionClient, new ModelLoaderConfig(configProperties, "."));
95 PowerMockito.whenNew(BabelServiceClient.class).withAnyArguments().thenReturn(mockBabelClient);
97 Whitebox.setInternalState(downloadManager, mockNotificationPublisher);
98 Whitebox.setInternalState(downloadManager, mockBabelArtifactConverter);
102 public void tearDown() {
103 downloadManager = null;
104 mockDistributionClient = null;
105 mockNotificationPublisher = null;
109 public void downloadArtifacts_emptyListSupplied() {
110 List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>();
111 List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
113 assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED, downloadManager
114 .downloadArtifacts(getNotificationDataWithOneService(), new ArrayList<>(), modelFiles, catalogFiles));
116 Mockito.verifyZeroInteractions(mockBabelClient, mockDistributionClient, mockNotificationPublisher,
117 mockBabelArtifactConverter);
121 public void downloadArtifacts_artifactDownloadFails() {
122 INotificationData data = getNotificationDataWithOneService();
123 IArtifactInfo artifact = data.getServiceArtifacts().get(0);
124 PowerMockito.when(mockDistributionClient.download(artifact))
125 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.FAIL, OOPS, null));
126 PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data,
129 assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
130 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
132 Mockito.verify(mockDistributionClient).download(artifact);
133 Mockito.verify(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, artifact, OOPS);
135 Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
138 private IDistributionClientDownloadResult createDistributionClientDownloadResult(
139 DistributionActionResultEnum status, String message, byte[] payload) {
140 IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message);
142 ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload);
144 return downloadResult;
149 * Test disabled as exception handling needs to be reworked
151 * @throws IOException
153 @SuppressWarnings("unchecked")
155 public void downloadArtifacts_invalidToscaCsarFile() throws IOException, BabelServiceException {
156 INotificationData data = getNotificationDataWithToscaCsarFile();
157 IArtifactInfo artifact = data.getServiceArtifacts().get(0);
158 PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult(
159 DistributionActionResultEnum.SUCCESS, null, "This is not a valid Tosca CSAR File".getBytes()));
160 PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data,
162 PowerMockito.when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
163 Matchers.anyString())).thenThrow(BabelServiceException.class);
164 PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
167 assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
168 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
170 Mockito.verify(mockDistributionClient).download(artifact);
171 Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
173 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
175 Mockito.verifyZeroInteractions(mockBabelArtifactConverter);
180 public void downloadArtifacts_invalidModelQuerySpec() {
181 INotificationData data = getNotificationDataWithModelQuerySpec();
182 IArtifactInfo artifact = data.getServiceArtifacts().get(0);
184 List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>();
186 PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult(
187 DistributionActionResultEnum.SUCCESS, null, "This is not a valid Model Query Spec".getBytes()));
188 PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data,
191 assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
192 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, null));
194 Mockito.verify(mockDistributionClient).download(artifact);
195 Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
196 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
198 Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
202 private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo,
203 ArtifactTestUtils artifactTestUtils) throws IOException, BabelServiceException {
204 PowerMockito.when(mockDistributionClient.download(artifactInfo))
205 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
206 artifactTestUtils.loadResource("compressedArtifacts/service-VscpaasTest-csar.csar")));
207 PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data,
209 PowerMockito.when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
210 Matchers.anyString())).thenReturn(createBabelArtifacts());
213 private List<BabelArtifact> createBabelArtifacts() {
214 List<BabelArtifact> artifactList = new ArrayList<>();
215 artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
216 artifactList.add(new BabelArtifact("VNFCArtifact", BabelArtifact.ArtifactType.VNFCATALOG, "Some VNFC payload"));
221 public void downloadArtifacts_validModelQuerySpec()
222 throws IOException, BabelServiceException, BabelArtifactParsingException {
223 ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
224 INotificationData data = getNotificationDataWithModelQuerySpec();
225 IArtifactInfo artifact = data.getServiceArtifacts().get(0);
226 setupValidModelQuerySpecMocks(artifactTestUtils, data, artifact);
228 List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>();
229 List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
230 assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED,
231 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelFiles, catalogFiles));
233 assertTrue("There should have been some model artifacts", !modelFiles.isEmpty());
234 assertTrue("There should not have been any catalog artifacts", catalogFiles.isEmpty());
236 Mockito.verify(mockDistributionClient).download(artifact);
237 Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
239 Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
242 private void setupValidModelQuerySpecMocks(ArtifactTestUtils artifactTestUtils, INotificationData data,
243 IArtifactInfo artifact) throws IOException {
244 PowerMockito.when(mockDistributionClient.download(artifact))
245 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
246 artifactTestUtils.loadResource("models/named-query-wan-connector.xml")));
247 PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data,
254 public void downloadArtifacts_invalidType()
255 throws IOException, BabelServiceException, BabelArtifactParsingException {
256 INotificationData data = getNotificationDataWithInvalidType();
257 IArtifactInfo artifact = data.getServiceArtifacts().get(0);
259 List<org.onap.aai.modelloader.entity.Artifact> catalogArtifacts = new ArrayList<>();
261 PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult(
262 DistributionActionResultEnum.SUCCESS, null, "This content does not matter.".getBytes()));
263 PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data,
266 assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
267 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, catalogArtifacts));
269 Mockito.verify(mockDistributionClient).download(artifact);
270 Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
271 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
273 Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);