Merge "Update model-loader dependencies"
[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.junit.Assert.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 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mockito;
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.fixture.NotificationDataFixtureBuilder;
49 import org.onap.aai.modelloader.service.ArtifactDeploymentManager;
50 import org.onap.aai.modelloader.util.ArtifactTestUtils;
51 import org.onap.sdc.api.notification.INotificationData;
52 import org.springframework.test.util.ReflectionTestUtils;
53
54 /**
55  * Tests {@link ArtifactDeploymentManager}.
56  */
57 public class TestArtifactDeploymentManager {
58
59     private static final String CONFIG_FILE = "model-loader.properties";
60     private static final String SHOULD_HAVE_RETURNED_FALSE = "This should have returned false";
61
62     private Properties configProperties;
63     private ArtifactDeploymentManager manager;
64
65     private ModelArtifactHandler mockModelArtifactHandler;
66     private VnfCatalogArtifactHandler mockVnfCatalogArtifactHandler;
67
68     @Before
69     public void setup() throws IOException {
70         configProperties = new Properties();
71         configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
72
73         mockModelArtifactHandler = mock(ModelArtifactHandler.class);
74         mockVnfCatalogArtifactHandler = mock(VnfCatalogArtifactHandler.class);
75
76         manager = new ArtifactDeploymentManager(new ModelLoaderConfig(configProperties, null));
77
78         ReflectionTestUtils.setField(manager, "modelArtifactHandler", mockModelArtifactHandler);
79         ReflectionTestUtils.setField(manager, "vnfCatalogArtifactHandler", mockVnfCatalogArtifactHandler);
80     }
81
82     @After
83     public void tearDown() {
84         configProperties = null;
85         mockModelArtifactHandler = null;
86         mockVnfCatalogArtifactHandler = null;
87         manager = null;
88     }
89
90     @Test
91     public void deploy_csarDeploymentsFailed() throws IOException, BabelArtifactParsingException {
92         INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
93         byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
94         List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
95         List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
96
97         when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
98                 .thenReturn(false);
99
100         assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, new ArrayList<>()), is(false));
101
102         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
103                 any());
104         Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).pushArtifacts(eq(modelArtifacts),
105                 eq(data.getDistributionID()), any(), any());
106         Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
107                 any());
108         Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(eq(new ArrayList<Artifact>()),
109                 eq(data.getDistributionID()), any());
110     }
111
112     private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException {
113         List<BabelArtifact> toscaArtifacts = new ArrayList<>();
114         org.onap.sdc.api.notification.IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
115
116         BabelArtifact xmlArtifact =
117                 new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
118         toscaArtifacts.add(xmlArtifact);
119
120         return toscaArtifacts;
121     }
122
123     @Test
124     public void deploy_catalogDeploymentsFailed()
125             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
126         INotificationData data = getNotificationDataWithCatalogFile();
127
128         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
129         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
130
131         when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true);
132         when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
133                 .thenReturn(false);
134
135         assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, new ArrayList<>(), catalogFiles), is(false));
136
137         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(new ArrayList<Artifact>()),
138                 eq(data.getDistributionID()), any(), any());
139         Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
140                 any(), any());
141         Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
142                 any());
143         Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
144                 eq(data.getDistributionID()), any());
145     }
146
147     @Test
148     public void testNoArtifactsDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
149         doFailedCombinedTests(false, false);
150     }
151
152     @Test
153     public void testModelsNotDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
154         doFailedCombinedTests(false, true);
155     }
156
157     @Test
158     public void testCatalogsNotDeployed() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
159         doFailedCombinedTests(true, false);
160     }
161
162     private void doFailedCombinedTests(boolean modelsDeployed, boolean catalogsDeployed)
163             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
164         INotificationData data = getNotificationDataWithOneOfEach();
165         byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
166         List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
167         List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
168
169         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
170         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
171
172         when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
173                 .thenReturn(catalogsDeployed);
174         when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
175                 .thenReturn(modelsDeployed);
176
177         assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, catalogFiles), is(false));
178
179         // Catalog artifacts are only pushed if models are successful.
180         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
181                 any());
182         if (modelsDeployed) {
183             Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
184                     any(), any());
185         }
186
187         if (modelsDeployed && catalogsDeployed) {
188             Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
189             Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
190         } else {
191             if (modelsDeployed) {
192                 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
193                         eq(data.getDistributionID()), any());
194                 Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
195                         eq(data.getDistributionID()), any());
196             } else {
197                 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
198                         eq(data.getDistributionID()), any());
199                 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
200             }
201         }
202     }
203
204     /**
205      * Deploy both models and VNF images.
206      * 
207      * @throws IOException
208      * @throws BabelArtifactParsingException
209      * @throws InvalidArchiveException
210      */
211     @Test
212     public void testDeploySuccess() throws IOException, BabelArtifactParsingException, InvalidArchiveException {
213         INotificationData data = getNotificationDataWithOneOfEach();
214         byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
215         List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
216         List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
217
218         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
219         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
220
221         when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
222                 .thenReturn(true);
223         when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
224                 .thenReturn(true);
225
226         assertThat(manager.deploy(data, modelArtifacts, catalogFiles), is(true));
227
228         Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
229                 any(), any());
230         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
231                 any());
232         Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
233         Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
234     }
235 }