Improve error logging
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / TestArtifactDeploymentManager.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 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
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.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;
31
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Properties;
36
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;
54
55 /**
56  * Tests {@link ArtifactDeploymentManager}.
57  */
58 public class TestArtifactDeploymentManager {
59
60     private static final String CONFIG_FILE = "model-loader.properties";
61     private static final String SHOULD_HAVE_RETURNED_FALSE = "This should have returned false";
62
63     private Properties configProperties;
64     private ArtifactDeploymentManager manager;
65
66     private ModelArtifactHandler mockModelArtifactHandler;
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
74         mockModelArtifactHandler = mock(ModelArtifactHandler.class);
75         mockVnfCatalogArtifactHandler = mock(VnfCatalogArtifactHandler.class);
76
77         manager = new ArtifactDeploymentManager(new ModelLoaderConfig(configProperties, null));
78
79         ReflectionTestUtils.setField(manager, "modelArtifactHandler", mockModelArtifactHandler);
80         ReflectionTestUtils.setField(manager, "vnfCatalogArtifactHandler", mockVnfCatalogArtifactHandler);
81     }
82
83     @After
84     public void tearDown() {
85         configProperties = null;
86         mockModelArtifactHandler = null;
87         mockVnfCatalogArtifactHandler = null;
88         manager = null;
89     }
90
91     @Test
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);
97
98         when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
99                 .thenReturn(false);
100
101         assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, new ArrayList<>()), is(false));
102
103         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
104                 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()),
108                 any());
109         Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(eq(new ArrayList<Artifact>()),
110                 eq(data.getDistributionID()), any());
111     }
112
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);
116
117         BabelArtifact xmlArtifact =
118                 new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
119         toscaArtifacts.add(xmlArtifact);
120
121         return toscaArtifacts;
122     }
123
124     @Test
125     public void deploy_catalogDeploymentsFailed()
126             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
127         INotificationData data = getNotificationDataWithCatalogFile();
128
129         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
130         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
131
132         when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true);
133         when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
134                 .thenReturn(false);
135
136         assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, new ArrayList<>(), catalogFiles), is(false));
137
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()),
141                 any(), any());
142         Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
143                 any());
144         Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
145                 eq(data.getDistributionID()), any());
146     }
147
148     @Test
149     public void testNoArtifactsDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
150         doFailedCombinedTests(false, false);
151     }
152
153     @Test
154     public void testModelsNotDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
155         doFailedCombinedTests(false, true);
156     }
157
158     @Test
159     public void testCatalogsNotDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
160         doFailedCombinedTests(true, false);
161     }
162
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);
169
170         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
171         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
172
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);
177
178         assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, catalogFiles), is(false));
179
180         // Catalog artifacts are only pushed if models are successful.
181         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
182                 any());
183         if (modelsDeployed) {
184             Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
185                     any(), any());
186         }
187
188         if (modelsDeployed && catalogsDeployed) {
189             Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
190             Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
191         } else {
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());
197             } else {
198                 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
199                         eq(data.getDistributionID()), any());
200                 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
201             }
202         }
203     }
204
205     /**
206      * Deploy both models and VNF images.
207      * 
208      * @throws IOException
209      * @throws BabelArtifactParsingException
210      * @throws InvalidArchiveException
211      */
212     @Test
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);
218
219         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
220         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
221
222         when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
223                 .thenReturn(true);
224         when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
225                 .thenReturn(true);
226
227         assertThat(manager.deploy(data, modelArtifacts, catalogFiles), is(true));
228
229         Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
230                 any(), any());
231         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
232                 any());
233         Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
234         Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
235     }
236 }