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