Revisions made to the Model Loader to use Babel
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / ArtifactDownloadManagerNoMockTest.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.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;
55
56 /**
57  * No-Mock tests
58  * 
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.
61  * 
62  * @author andrewdo
63  *
64  */
65
66 /**
67  * Tests {@link ArtifactDownloadManager}
68  */
69 @PowerMockIgnore({"sun.security.ssl.*", "javax.net.ssl.*"})
70 @PrepareForTest({ArtifactDownloadManager.class})
71 public class ArtifactDownloadManagerNoMockTest {
72
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";
76
77     private ArtifactDownloadManager downloadManager;
78     private BabelServiceClient mockBabelClient;
79     private IDistributionClient mockDistributionClient;
80     private NotificationPublisher mockNotificationPublisher;
81     private BabelArtifactConverter mockBabelArtifactConverter;
82
83     @Before
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);
89
90         Properties configProperties = new Properties();
91         configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties"));
92         downloadManager =
93                 new ArtifactDownloadManager(mockDistributionClient, new ModelLoaderConfig(configProperties, "."));
94
95         PowerMockito.whenNew(BabelServiceClient.class).withAnyArguments().thenReturn(mockBabelClient);
96
97         Whitebox.setInternalState(downloadManager, mockNotificationPublisher);
98         Whitebox.setInternalState(downloadManager, mockBabelArtifactConverter);
99     }
100
101     @After
102     public void tearDown() {
103         downloadManager = null;
104         mockDistributionClient = null;
105         mockNotificationPublisher = null;
106     }
107
108     @Test
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<>();
112
113         assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED, downloadManager
114                 .downloadArtifacts(getNotificationDataWithOneService(), new ArrayList<>(), modelFiles, catalogFiles));
115
116         Mockito.verifyZeroInteractions(mockBabelClient, mockDistributionClient, mockNotificationPublisher,
117                 mockBabelArtifactConverter);
118     }
119
120     @Test
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,
127                 artifact, OOPS);
128
129         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
130                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
131
132         Mockito.verify(mockDistributionClient).download(artifact);
133         Mockito.verify(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, artifact, OOPS);
134
135         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
136     }
137
138     private IDistributionClientDownloadResult createDistributionClientDownloadResult(
139             DistributionActionResultEnum status, String message, byte[] payload) {
140         IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message);
141
142         ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload);
143
144         return downloadResult;
145     }
146
147
148     /**
149      * Test disabled as exception handling needs to be reworked
150      * 
151      * @throws IOException
152      */
153     @SuppressWarnings("unchecked")
154     @Test
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,
161                 artifact);
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,
165                 artifact);
166
167         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
168                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null));
169
170         Mockito.verify(mockDistributionClient).download(artifact);
171         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
172
173         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
174
175         Mockito.verifyZeroInteractions(mockBabelArtifactConverter);
176
177     }
178
179     @Test
180     public void downloadArtifacts_invalidModelQuerySpec() {
181         INotificationData data = getNotificationDataWithModelQuerySpec();
182         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
183
184         List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>();
185
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,
189                 artifact);
190
191         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
192                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, null));
193
194         Mockito.verify(mockDistributionClient).download(artifact);
195         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
196         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
197
198         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
199     }
200
201
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,
208                 artifactInfo);
209         PowerMockito.when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(),
210                 Matchers.anyString())).thenReturn(createBabelArtifacts());
211     }
212
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"));
217         return artifactList;
218     }
219
220     @Test
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);
227
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));
232
233         assertTrue("There should have been some model artifacts", !modelFiles.isEmpty());
234         assertTrue("There should not have been any catalog artifacts", catalogFiles.isEmpty());
235
236         Mockito.verify(mockDistributionClient).download(artifact);
237         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
238
239         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
240     }
241
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,
248                 artifact);
249     }
250
251
252
253     @Test
254     public void downloadArtifacts_invalidType()
255             throws IOException, BabelServiceException, BabelArtifactParsingException {
256         INotificationData data = getNotificationDataWithInvalidType();
257         IArtifactInfo artifact = data.getServiceArtifacts().get(0);
258
259         List<org.onap.aai.modelloader.entity.Artifact> catalogArtifacts = new ArrayList<>();
260
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,
264                 artifact);
265
266         assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED,
267                 downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, catalogArtifacts));
268
269         Mockito.verify(mockDistributionClient).download(artifact);
270         Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact);
271         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact);
272
273         Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter);
274     }
275 }