Revisions made to the Model Loader to use Babel
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / ArtifactDeploymentManagerMockTest.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.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;
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.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;
52
53 /**
54  * Tests {@link ArtifactDeploymentManager }
55  */
56 public class ArtifactDeploymentManagerMockTest {
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 = PowerMockito.mock(IDistributionClient.class);
76         mockModelArtifactHandler = PowerMockito.mock(ModelArtifactHandler.class);
77         mockNotificationPublisher = PowerMockito.mock(NotificationPublisher.class);
78         mockVnfCatalogArtifactHandler = PowerMockito.mock(VnfCatalogArtifactHandler.class);
79
80         manager = new ArtifactDeploymentManager(mockDistributionClient, config);
81
82         Whitebox.setInternalState(manager, mockModelArtifactHandler);
83         Whitebox.setInternalState(manager, mockNotificationPublisher);
84         Whitebox.setInternalState(manager, 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     @Test
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);
104
105         PowerMockito.when(
106                 mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
107                 .thenReturn(false);
108         PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
109                 data.getServiceArtifacts().get(0));
110
111         assertFalse(SHOULD_HAVE_RETURNED_FALSE,
112                 manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, new ArrayList<>()));
113
114         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
115                 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()),
119                 any());
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));
124     }
125 */
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);
129
130         BabelArtifact xmlArtifact =
131                 new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
132         toscaArtifacts.add(xmlArtifact);
133
134         return toscaArtifacts;
135     }
136
137     @Test
138     public void deploy_catalogDeploymentsFailed()
139             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
140         INotificationData data = getNotificationDataWithCatalogFile();
141
142         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
143         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
144
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));
150
151         assertFalse(SHOULD_HAVE_RETURNED_FALSE,
152                 manager.deploy(data, data.getServiceArtifacts(), new ArrayList<>(), catalogFiles));
153
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()),
157                 any(), any());
158         Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
159                 any());
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));
164     }
165
166 /*
167     @Test
168     public void deploy_bothDeploymentsFailed()
169             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
170         doFailedCombinedTests(false, false);
171     }
172 */
173 /*
174     @Test
175     public void deploy_modelsFailedCatalogsOK()
176             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
177         doFailedCombinedTests(false, true);
178     }
179 */
180 /*
181     @Test
182     public void deploy_catalogsFailedModelsOK()
183             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
184         doFailedCombinedTests(true, false);
185     }
186 */
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);
194
195         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
196         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
197
198         PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
199                 any(), any())).thenReturn(catalogsOK);
200         PowerMockito.when(
201                 mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
202                 .thenReturn(modelsOK);
203
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));
208
209         assertFalse(SHOULD_HAVE_RETURNED_FALSE,
210                 manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles));
211
212         // Catalog artifacts are only pushed if models are successful.
213         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
214                 any());
215         if (modelsOK) {
216             Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
217                     any(), any());
218         }
219
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());
225         } else {
226             Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
227                     data.getServiceArtifacts().get(0));
228             if (modelsOK) {
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());
233             } else {
234                 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
235                         eq(data.getDistributionID()), any());
236                 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
237             }
238         }
239     }
240
241 /*
242     @Test
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);
249
250         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
251         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
252
253         PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
254                 any(), any())).thenReturn(true);
255         PowerMockito.when(
256                 mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
257                 .thenReturn(true);
258         PowerMockito.doNothing().when(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data,
259                 data.getServiceArtifacts().get(0));
260
261         assertTrue("This should have returned true",
262                 manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles));
263
264         Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
265                 any(), any());
266         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
267                 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());
272     }
273 */
274 }