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.mockito.Matchers.any;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithCatalogFile;
29 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneOfEach;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Properties;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.mockito.internal.util.reflection.Whitebox;
40 import org.onap.aai.babel.service.data.BabelArtifact;
41 import org.onap.aai.modelloader.config.ModelLoaderConfig;
42 import org.onap.aai.modelloader.entity.Artifact;
43 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
44 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifactHandler;
45 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
46 import org.onap.aai.modelloader.entity.model.ModelArtifactHandler;
47 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
48 import org.onap.aai.modelloader.util.ArtifactTestUtils;
49 import org.openecomp.sdc.api.IDistributionClient;
50 import org.openecomp.sdc.api.notification.IArtifactInfo;
51 import org.openecomp.sdc.api.notification.INotificationData;
54 * Tests {@link ArtifactDeploymentManager }
56 public class ArtifactDeploymentManagerTest {
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 = mock(IDistributionClient.class);
76 mockModelArtifactHandler = mock(ModelArtifactHandler.class);
77 mockNotificationPublisher = mock(NotificationPublisher.class);
78 mockVnfCatalogArtifactHandler = mock(VnfCatalogArtifactHandler.class);
80 manager = new ArtifactDeploymentManager(mockDistributionClient, config);
82 Whitebox.setInternalState(manager, "modelArtifactHandler", mockModelArtifactHandler);
83 Whitebox.setInternalState(manager, "notificationPublisher", mockNotificationPublisher);
84 Whitebox.setInternalState(manager, "vnfCatalogArtifactHandler", mockVnfCatalogArtifactHandler);
88 public void tearDown() {
89 configProperties = null;
90 mockDistributionClient = null;
91 mockModelArtifactHandler = null;
92 mockNotificationPublisher = null;
93 mockVnfCatalogArtifactHandler = null;
98 private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException {
99 List<BabelArtifact> toscaArtifacts = new ArrayList<>();
100 IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
102 BabelArtifact xmlArtifact =
103 new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
104 toscaArtifacts.add(xmlArtifact);
106 return toscaArtifacts;
110 public void deploy_catalogDeploymentsFailed()
111 throws IOException, BabelArtifactParsingException, InvalidArchiveException {
112 INotificationData data = getNotificationDataWithCatalogFile();
114 List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
115 catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
117 when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true);
118 when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
120 Mockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
121 data.getServiceArtifacts().get(0));
123 assertFalse(SHOULD_HAVE_RETURNED_FALSE,
124 manager.deploy(data, data.getServiceArtifacts(), new ArrayList<>(), catalogFiles));
126 Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(new ArrayList<Artifact>()),
127 eq(data.getDistributionID()), any(), any());
128 Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
130 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
132 Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
133 eq(data.getDistributionID()), any());
134 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
135 data.getServiceArtifacts().get(0));
138 private void doFailedCombinedTests(boolean modelsOK, boolean catalogsOK)
139 throws IOException, BabelArtifactParsingException, InvalidArchiveException {
140 INotificationData data = getNotificationDataWithOneOfEach();
141 ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
142 byte[] xml = artifactTestUtils.loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
143 List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
144 List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
146 List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
147 catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
149 when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
150 .thenReturn(catalogsOK);
151 when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
152 .thenReturn(modelsOK);
154 Mockito.doNothing().when(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data,
155 data.getServiceArtifacts().get(0));
156 Mockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
157 data.getServiceArtifacts().get(0));
159 assertFalse(SHOULD_HAVE_RETURNED_FALSE,
160 manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles));
162 // Catalog artifacts are only pushed if models are successful.
163 Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
166 Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
170 if (modelsOK && catalogsOK) {
171 Mockito.verify(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data,
172 data.getServiceArtifacts().get(0));
173 Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
174 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
176 Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
177 data.getServiceArtifacts().get(0));
179 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
180 eq(data.getDistributionID()), any());
181 Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
182 eq(data.getDistributionID()), any());
184 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
185 eq(data.getDistributionID()), any());
186 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());