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