2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
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.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithCatalogFile;
30 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneOfEach;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Properties;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mockito;
41 import org.onap.aai.babel.service.data.BabelArtifact;
42 import org.onap.aai.modelloader.config.ModelLoaderConfig;
43 import org.onap.aai.modelloader.entity.Artifact;
44 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
45 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifactHandler;
46 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
47 import org.onap.aai.modelloader.entity.model.ModelArtifactHandler;
48 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
49 import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder;
50 import org.onap.aai.modelloader.service.ArtifactDeploymentManager;
51 import org.onap.aai.modelloader.util.ArtifactTestUtils;
52 import org.onap.sdc.api.notification.INotificationData;
53 import org.springframework.test.util.ReflectionTestUtils;
56 * Tests {@link ArtifactDeploymentManager}.
58 public class TestArtifactDeploymentManager {
60 private static final String CONFIG_FILE = "model-loader.properties";
61 private static final String SHOULD_HAVE_RETURNED_FALSE = "This should have returned false";
63 private Properties configProperties;
64 private ArtifactDeploymentManager manager;
66 private ModelArtifactHandler mockModelArtifactHandler;
67 private VnfCatalogArtifactHandler mockVnfCatalogArtifactHandler;
70 public void setup() throws IOException {
71 configProperties = new Properties();
72 configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
74 mockModelArtifactHandler = mock(ModelArtifactHandler.class);
75 mockVnfCatalogArtifactHandler = mock(VnfCatalogArtifactHandler.class);
77 manager = new ArtifactDeploymentManager(new ModelLoaderConfig(configProperties, null));
79 ReflectionTestUtils.setField(manager, "modelArtifactHandler", mockModelArtifactHandler);
80 ReflectionTestUtils.setField(manager, "vnfCatalogArtifactHandler", mockVnfCatalogArtifactHandler);
84 public void tearDown() {
85 configProperties = null;
86 mockModelArtifactHandler = null;
87 mockVnfCatalogArtifactHandler = null;
92 public void deploy_csarDeploymentsFailed() throws IOException, BabelArtifactParsingException {
93 INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
94 byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
95 List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
96 List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
98 when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
101 assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, new ArrayList<>()), is(false));
103 Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
105 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).pushArtifacts(eq(modelArtifacts),
106 eq(data.getDistributionID()), any(), any());
107 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
109 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(eq(new ArrayList<Artifact>()),
110 eq(data.getDistributionID()), any());
113 private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException {
114 List<BabelArtifact> toscaArtifacts = new ArrayList<>();
115 org.onap.sdc.api.notification.IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
117 BabelArtifact xmlArtifact =
118 new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
119 toscaArtifacts.add(xmlArtifact);
121 return toscaArtifacts;
125 public void deploy_catalogDeploymentsFailed()
126 throws IOException, BabelArtifactParsingException, InvalidArchiveException {
127 INotificationData data = getNotificationDataWithCatalogFile();
129 List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
130 catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
132 when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true);
133 when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
136 assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, new ArrayList<>(), catalogFiles), is(false));
138 Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(new ArrayList<Artifact>()),
139 eq(data.getDistributionID()), any(), any());
140 Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
142 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
144 Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
145 eq(data.getDistributionID()), any());
149 public void testNoArtifactsDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
150 doFailedCombinedTests(false, false);
154 public void testModelsNotDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
155 doFailedCombinedTests(false, true);
159 public void testCatalogsNotDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
160 doFailedCombinedTests(true, false);
163 private void doFailedCombinedTests(boolean modelsDeployed, boolean catalogsDeployed)
164 throws IOException, BabelArtifactParsingException, InvalidArchiveException {
165 INotificationData data = getNotificationDataWithOneOfEach();
166 byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
167 List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
168 List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
170 List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
171 catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
173 when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
174 .thenReturn(catalogsDeployed);
175 when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
176 .thenReturn(modelsDeployed);
178 assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, catalogFiles), is(false));
180 // Catalog artifacts are only pushed if models are successful.
181 Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
183 if (modelsDeployed) {
184 Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
188 if (modelsDeployed && catalogsDeployed) {
189 Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
190 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
192 if (modelsDeployed) {
193 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
194 eq(data.getDistributionID()), any());
195 Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
196 eq(data.getDistributionID()), any());
198 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
199 eq(data.getDistributionID()), any());
200 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
206 * Deploy both models and VNF images.
208 * @throws IOException
209 * @throws BabelArtifactParsingException
210 * @throws InvalidArchiveException
213 public void testDeploySuccess() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
214 INotificationData data = getNotificationDataWithOneOfEach();
215 byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
216 List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
217 List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
219 List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
220 catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
222 when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
224 when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
227 assertThat(manager.deploy(data, modelArtifacts, catalogFiles), is(true));
229 Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
231 Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
233 Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
234 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());