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.mockito.Matchers.any;
26 import static org.mockito.Matchers.eq;
27 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithCatalogFile;
28 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneOfEach;
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.Mockito;
38 import org.onap.aai.babel.service.data.BabelArtifact;
39 import org.onap.aai.modelloader.config.ModelLoaderConfig;
40 import org.onap.aai.modelloader.entity.Artifact;
41 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
42 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifactHandler;
43 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
44 import org.onap.aai.modelloader.entity.model.ModelArtifactHandler;
45 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
46 import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder;
47 import org.onap.aai.modelloader.util.ArtifactTestUtils;
48 import org.openecomp.sdc.api.IDistributionClient;
49 import org.openecomp.sdc.api.notification.INotificationData;
50 import org.powermock.api.mockito.PowerMockito;
51 import org.powermock.reflect.Whitebox;
54 * Tests {@link ArtifactDeploymentManager }
56 public class ArtifactDeploymentManagerMockTest {
58 private static final String CONFIG_FILE = "model-loader.properties";
59 private static final String SHOULD_HAVE_RETURNED_FALSE = "This should have returned false";
61 private Properties configProperties;
62 private ArtifactDeploymentManager manager;
64 private IDistributionClient mockDistributionClient;
65 private ModelArtifactHandler mockModelArtifactHandler;
66 private NotificationPublisher mockNotificationPublisher;
67 private VnfCatalogArtifactHandler mockVnfCatalogArtifactHandler;
70 public void setup() throws IOException {
71 configProperties = new Properties();
72 configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
73 ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
75 mockDistributionClient = PowerMockito.mock(IDistributionClient.class);
76 mockModelArtifactHandler = PowerMockito.mock(ModelArtifactHandler.class);
77 mockNotificationPublisher = PowerMockito.mock(NotificationPublisher.class);
78 mockVnfCatalogArtifactHandler = PowerMockito.mock(VnfCatalogArtifactHandler.class);
80 manager = new ArtifactDeploymentManager(mockDistributionClient, config);
82 Whitebox.setInternalState(manager, mockModelArtifactHandler);
83 Whitebox.setInternalState(manager, mockNotificationPublisher);
84 Whitebox.setInternalState(manager, mockVnfCatalogArtifactHandler);
88 public void tearDown() {
89 configProperties = null;
90 mockDistributionClient = null;
91 mockModelArtifactHandler = null;
92 mockNotificationPublisher = null;
93 mockVnfCatalogArtifactHandler = null;
98 public void deploy_csarDeploymentsFailed() throws IOException, BabelArtifactParsingException {
99 ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
100 INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
101 byte[] xml = artifactTestUtils.loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
102 List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
103 List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
106 mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
108 PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
109 data.getServiceArtifacts().get(0));
111 assertFalse(SHOULD_HAVE_RETURNED_FALSE,
112 manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, new ArrayList<>()));
114 Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
116 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).pushArtifacts(eq(modelArtifacts),
117 eq(data.getDistributionID()), any(), any());
118 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
120 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(eq(new ArrayList<Artifact>()),
121 eq(data.getDistributionID()), any());
122 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
123 data.getServiceArtifacts().get(0));
126 private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException {
127 List<BabelArtifact> toscaArtifacts = new ArrayList<>();
128 org.openecomp.sdc.api.notification.IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
130 BabelArtifact xmlArtifact =
131 new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
132 toscaArtifacts.add(xmlArtifact);
134 return toscaArtifacts;
138 public void deploy_catalogDeploymentsFailed()
139 throws IOException, BabelArtifactParsingException, InvalidArchiveException {
140 INotificationData data = getNotificationDataWithCatalogFile();
142 List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
143 catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
145 PowerMockito.when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true);
146 PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
147 any(), any())).thenReturn(false);
148 PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
149 data.getServiceArtifacts().get(0));
151 assertFalse(SHOULD_HAVE_RETURNED_FALSE,
152 manager.deploy(data, data.getServiceArtifacts(), new ArrayList<>(), catalogFiles));
154 Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(new ArrayList<Artifact>()),
155 eq(data.getDistributionID()), any(), any());
156 Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
158 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
160 Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
161 eq(data.getDistributionID()), any());
162 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
163 data.getServiceArtifacts().get(0));
168 public void deploy_bothDeploymentsFailed()
169 throws IOException, BabelArtifactParsingException, InvalidArchiveException {
170 doFailedCombinedTests(false, false);
175 public void deploy_modelsFailedCatalogsOK()
176 throws IOException, BabelArtifactParsingException, InvalidArchiveException {
177 doFailedCombinedTests(false, true);
182 public void deploy_catalogsFailedModelsOK()
183 throws IOException, BabelArtifactParsingException, InvalidArchiveException {
184 doFailedCombinedTests(true, false);
187 private void doFailedCombinedTests(boolean modelsOK, boolean catalogsOK)
188 throws IOException, BabelArtifactParsingException, InvalidArchiveException {
189 INotificationData data = getNotificationDataWithOneOfEach();
190 ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
191 byte[] xml = artifactTestUtils.loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
192 List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
193 List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
195 List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
196 catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
198 PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
199 any(), any())).thenReturn(catalogsOK);
201 mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
202 .thenReturn(modelsOK);
204 PowerMockito.doNothing().when(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data,
205 data.getServiceArtifacts().get(0));
206 PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
207 data.getServiceArtifacts().get(0));
209 assertFalse(SHOULD_HAVE_RETURNED_FALSE,
210 manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles));
212 // Catalog artifacts are only pushed if models are successful.
213 Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
216 Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
220 if (modelsOK && catalogsOK) {
221 Mockito.verify(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data,
222 data.getServiceArtifacts().get(0));
223 Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
224 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
226 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
227 data.getServiceArtifacts().get(0));
229 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
230 eq(data.getDistributionID()), any());
231 Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
232 eq(data.getDistributionID()), any());
234 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
235 eq(data.getDistributionID()), any());
236 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
243 public void deploy_bothOK() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
244 INotificationData data = getNotificationDataWithOneOfEach();
245 ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
246 byte[] xml = artifactTestUtils.loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
247 List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
248 List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
250 List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
251 catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
253 PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
254 any(), any())).thenReturn(true);
256 mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
258 PowerMockito.doNothing().when(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data,
259 data.getServiceArtifacts().get(0));
261 assertTrue("This should have returned true",
262 manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles));
264 Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
266 Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
268 Mockito.verify(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data,
269 data.getServiceArtifacts().get(0));
270 Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
271 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());