Convert project from AJSC to Spring Boot
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / ArtifactDeploymentManagerTest.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.junit.Assert.assertFalse;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.mock;
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 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.mockito.internal.util.reflection.Whitebox;
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.util.ArtifactTestUtils;
49 import org.onap.sdc.api.IDistributionClient;
50 import org.onap.sdc.api.notification.INotificationData;
51
52 /**
53  * Tests {@link ArtifactDeploymentManager }
54  */
55 public class ArtifactDeploymentManagerTest {
56
57     private static final String CONFIG_FILE = "model-loader.properties";
58     private static final String SHOULD_HAVE_RETURNED_FALSE = "This should have returned false";
59
60     private Properties configProperties;
61     private ArtifactDeploymentManager manager;
62
63     private IDistributionClient mockDistributionClient;
64     private ModelArtifactHandler mockModelArtifactHandler;
65     private NotificationPublisher mockNotificationPublisher;
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         ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
73
74         mockDistributionClient = mock(IDistributionClient.class);
75         mockModelArtifactHandler = mock(ModelArtifactHandler.class);
76         mockNotificationPublisher = mock(NotificationPublisher.class);
77         mockVnfCatalogArtifactHandler = mock(VnfCatalogArtifactHandler.class);
78
79         manager = new ArtifactDeploymentManager(mockDistributionClient, config);
80
81         Whitebox.setInternalState(manager, "modelArtifactHandler", mockModelArtifactHandler);
82         Whitebox.setInternalState(manager, "notificationPublisher", mockNotificationPublisher);
83         Whitebox.setInternalState(manager, "vnfCatalogArtifactHandler", mockVnfCatalogArtifactHandler);
84     }
85
86     @After
87     public void tearDown() {
88         configProperties = null;
89         mockDistributionClient = null;
90         mockModelArtifactHandler = null;
91         mockNotificationPublisher = null;
92         mockVnfCatalogArtifactHandler = null;
93         manager = null;
94     }
95
96     private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException {
97         List<BabelArtifact> toscaArtifacts = new ArrayList<>();
98         org.onap.sdc.api.notification.IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
99
100         BabelArtifact xmlArtifact =
101                 new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
102         toscaArtifacts.add(xmlArtifact);
103
104         return toscaArtifacts;
105     }
106
107     @Test
108     public void deploy_catalogDeploymentsFailed()
109             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
110         INotificationData data = getNotificationDataWithCatalogFile();
111
112         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
113         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
114
115         when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true);
116         when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
117                 .thenReturn(false);
118         Mockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
119                 data.getServiceArtifacts().get(0));
120
121         assertFalse(SHOULD_HAVE_RETURNED_FALSE,
122                 manager.deploy(data, data.getServiceArtifacts(), new ArrayList<>(), catalogFiles));
123
124         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(new ArrayList<Artifact>()),
125                 eq(data.getDistributionID()), any(), any());
126         Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
127                 any(), any());
128         Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
129                 any());
130         Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
131                 eq(data.getDistributionID()), any());
132         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
133                 data.getServiceArtifacts().get(0));
134     }
135
136     private void doFailedCombinedTests(boolean modelsOK, boolean catalogsOK)
137             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
138         INotificationData data = getNotificationDataWithOneOfEach();
139         ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
140         byte[] xml = artifactTestUtils.loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
141         List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
142         List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
143
144         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
145         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
146
147         when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()))
148                 .thenReturn(catalogsOK);
149         when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
150                 .thenReturn(modelsOK);
151
152         Mockito.doNothing().when(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data,
153                 data.getServiceArtifacts().get(0));
154         Mockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
155                 data.getServiceArtifacts().get(0));
156
157         assertFalse(SHOULD_HAVE_RETURNED_FALSE,
158                 manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles));
159
160         // Catalog artifacts are only pushed if models are successful.
161         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
162                 any());
163         if (modelsOK) {
164             Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
165                     any(), any());
166         }
167
168         if (modelsOK && catalogsOK) {
169             Mockito.verify(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data,
170                     data.getServiceArtifacts().get(0));
171             Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
172             Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
173         } else {
174             Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
175                     data.getServiceArtifacts().get(0));
176             if (modelsOK) {
177                 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
178                         eq(data.getDistributionID()), any());
179                 Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
180                         eq(data.getDistributionID()), any());
181             } else {
182                 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
183                         eq(data.getDistributionID()), any());
184                 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
185             }
186         }
187     }
188
189 }