Revisions made to the Model Loader to use Babel
[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 Amdocs
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.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithCatalogFile;
27 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneOfEach;
28
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Properties;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mockito;
38 import org.onap.aai.babel.service.data.BabelArtifact;
39 import org.onap.aai.modelloader.config.ModelLoaderConfig;
40 import org.onap.aai.modelloader.entity.Artifact;
41 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
42 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifactHandler;
43 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
44 import org.onap.aai.modelloader.entity.model.ModelArtifactHandler;
45 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
46 import org.onap.aai.modelloader.util.ArtifactTestUtils;
47 import org.openecomp.sdc.api.IDistributionClient;
48 import org.openecomp.sdc.api.notification.INotificationData;
49 import org.powermock.api.mockito.PowerMockito;
50 import org.powermock.modules.junit4.PowerMockRunner;
51 import org.powermock.reflect.Whitebox;
52
53 /**
54  * Tests {@link ArtifactDeploymentManager }
55  */
56 @RunWith(PowerMockRunner.class)
57 public class ArtifactDeploymentManagerTest {
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 IDistributionClient mockDistributionClient;
66     private ModelArtifactHandler mockModelArtifactHandler;
67     private NotificationPublisher mockNotificationPublisher;
68     private VnfCatalogArtifactHandler mockVnfCatalogArtifactHandler;
69
70     @Before
71     public void setup() throws IOException {
72         configProperties = new Properties();
73         configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
74         ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
75
76         mockDistributionClient = PowerMockito.mock(IDistributionClient.class);
77         mockModelArtifactHandler = PowerMockito.mock(ModelArtifactHandler.class);
78         mockNotificationPublisher = PowerMockito.mock(NotificationPublisher.class);
79         mockVnfCatalogArtifactHandler = PowerMockito.mock(VnfCatalogArtifactHandler.class);
80
81         manager = new ArtifactDeploymentManager(mockDistributionClient, config);
82
83         Whitebox.setInternalState(manager, mockModelArtifactHandler);
84         Whitebox.setInternalState(manager, mockNotificationPublisher);
85         Whitebox.setInternalState(manager, mockVnfCatalogArtifactHandler);
86     }
87
88     @After
89     public void tearDown() {
90         configProperties = null;
91         mockDistributionClient = null;
92         mockModelArtifactHandler = null;
93         mockNotificationPublisher = null;
94         mockVnfCatalogArtifactHandler = null;
95         manager = null;
96     }
97
98
99     private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException {
100         List<BabelArtifact> toscaArtifacts = new ArrayList<>();
101         org.openecomp.sdc.api.notification.IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
102
103         BabelArtifact xmlArtifact =
104                 new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml));
105         toscaArtifacts.add(xmlArtifact);
106
107         return toscaArtifacts;
108     }
109
110     @Test
111     public void deploy_catalogDeploymentsFailed()
112             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
113         INotificationData data = getNotificationDataWithCatalogFile();
114
115         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
116         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
117
118         PowerMockito.when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true);
119         PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
120                 any(), any())).thenReturn(false);
121         PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
122                 data.getServiceArtifacts().get(0));
123
124         assertFalse(SHOULD_HAVE_RETURNED_FALSE,
125                 manager.deploy(data, data.getServiceArtifacts(), new ArrayList<>(), catalogFiles));
126
127         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(new ArrayList<Artifact>()),
128                 eq(data.getDistributionID()), any(), any());
129         Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
130                 any(), any());
131         Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()),
132                 any());
133         Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
134                 eq(data.getDistributionID()), any());
135         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
136                 data.getServiceArtifacts().get(0));
137     }
138
139
140     private void doFailedCombinedTests(boolean modelsOK, boolean catalogsOK)
141             throws IOException, BabelArtifactParsingException, InvalidArchiveException {
142         INotificationData data = getNotificationDataWithOneOfEach();
143         ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
144         byte[] xml = artifactTestUtils.loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml");
145         List<BabelArtifact> toscaArtifacts = setupTest(xml, data);
146         List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts);
147
148         List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>();
149         catalogFiles.add(new VnfCatalogArtifact("Some catalog content"));
150
151         PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
152                 any(), any())).thenReturn(catalogsOK);
153         PowerMockito.when(
154                 mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()))
155                 .thenReturn(modelsOK);
156
157         PowerMockito.doNothing().when(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data,
158                 data.getServiceArtifacts().get(0));
159         PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
160                 data.getServiceArtifacts().get(0));
161
162         assertFalse(SHOULD_HAVE_RETURNED_FALSE,
163                 manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles));
164
165         // Catalog artifacts are only pushed if models are successful.
166         Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(),
167                 any());
168         if (modelsOK) {
169             Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()),
170                     any(), any());
171         }
172
173         if (modelsOK && catalogsOK) {
174             Mockito.verify(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data,
175                     data.getServiceArtifacts().get(0));
176             Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any());
177             Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
178         } else {
179             Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data,
180                     data.getServiceArtifacts().get(0));
181             if (modelsOK) {
182                 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
183                         eq(data.getDistributionID()), any());
184                 Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
185                         eq(data.getDistributionID()), any());
186             } else {
187                 Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()),
188                         eq(data.getDistributionID()), any());
189                 Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any());
190             }
191         }
192     }
193
194 }