0dcff3276e3c024f0e653b0bbb87530c8983bf2d
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / ArtifactDeploymentManagerTest.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.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;
30
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;
52
53 /**
54  * Tests {@link ArtifactDeploymentManager }
55  */
56 public class ArtifactDeploymentManagerTest {
57
58     private static final String CONFIG_FILE = "model-loader.properties";
59     private static final String SHOULD_HAVE_RETURNED_FALSE = "This should have returned false";
60
61     private Properties configProperties;
62     private ArtifactDeploymentManager manager;
63
64     private IDistributionClient mockDistributionClient;
65     private ModelArtifactHandler mockModelArtifactHandler;
66     private NotificationPublisher mockNotificationPublisher;
67     private VnfCatalogArtifactHandler mockVnfCatalogArtifactHandler;
68
69     @Before
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);
74
75         mockDistributionClient = mock(IDistributionClient.class);
76         mockModelArtifactHandler = mock(ModelArtifactHandler.class);
77         mockNotificationPublisher = mock(NotificationPublisher.class);
78         mockVnfCatalogArtifactHandler = mock(VnfCatalogArtifactHandler.class);
79
80         manager = new ArtifactDeploymentManager(mockDistributionClient, config);
81
82         Whitebox.setInternalState(manager, "modelArtifactHandler", mockModelArtifactHandler);
83         Whitebox.setInternalState(manager, "notificationPublisher", mockNotificationPublisher);
84         Whitebox.setInternalState(manager, "vnfCatalogArtifactHandler", mockVnfCatalogArtifactHandler);
85     }
86
87     @After
88     public void tearDown() {
89         configProperties = null;
90         mockDistributionClient = null;
91         mockModelArtifactHandler = null;
92         mockNotificationPublisher = null;
93         mockVnfCatalogArtifactHandler = null;
94         manager = null;
95     }
96
97
98     private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException {
99         List<BabelArtifact> toscaArtifacts = new ArrayList<>();
100         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
101
102         BabelArtifact xmlArtifact =
103                 new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
104         toscaArtifacts.add(xmlArtifact);
105
106         return toscaArtifacts;
107     }
108
109     @Test
110     public void deploy_catalogDeploymentsFailed()
111             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
112         INotificationData data = getNotificationDataWithCatalogFile();
113
114         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
115         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
116
117         when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true);
118         when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
119                 .thenReturn(false);
120         Mockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
121                 data.getServiceArtifacts().get(0));
122
123         assertFalse(SHOULD_HAVE_RETURNED_FALSE,
124                 manager.deploy(data, data.getServiceArtifacts(), new ArrayList<>(), catalogFiles));
125
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()),
129                 any(), any());
130         Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
131                 any());
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));
136     }
137
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);
145
146         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
147         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
148
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);
153
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));
158
159         assertFalse(SHOULD_HAVE_RETURNED_FALSE,
160                 manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles));
161
162         // Catalog artifacts are only pushed if models are successful.
163         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
164                 any());
165         if (modelsOK) {
166             Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
167                     any(), any());
168         }
169
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());
175         } else {
176             Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
177                     data.getServiceArtifacts().get(0));
178             if (modelsOK) {
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());
183             } else {
184                 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
185                         eq(data.getDistributionID()), any());
186                 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
187             }
188         }
189     }
190
191 }