repeat distribution transaction error
[so.git] / asdc-controller / src / test / java / org / onap / so / asdc / installer / heat / ToscaResourceInstallerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.asdc.installer.heat;
22
23 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.spy;
32 import static org.mockito.Mockito.times;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35 import java.util.ArrayList;
36 import java.util.List;
37 import org.hibernate.exception.LockAcquisitionException;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.sdc.api.notification.IArtifactInfo;
45 import org.onap.sdc.api.notification.IResourceInstance;
46 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
47 import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl;
48 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
49 import org.onap.sdc.toscaparser.api.Group;
50 import org.onap.sdc.toscaparser.api.NodeTemplate;
51 import org.onap.sdc.toscaparser.api.elements.Metadata;
52 import org.onap.sdc.toscaparser.api.elements.StatefulEntityType;
53 import org.onap.sdc.utils.DistributionStatusEnum;
54 import org.onap.so.asdc.BaseTest;
55 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
56 import org.onap.so.asdc.client.test.emulators.ArtifactInfoImpl;
57 import org.onap.so.asdc.client.test.emulators.JsonStatusData;
58 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
59 import org.onap.so.asdc.installer.ToscaResourceStructure;
60 import org.onap.so.db.catalog.beans.ConfigurationResource;
61 import org.onap.so.db.catalog.beans.ConfigurationResourceCustomization;
62 import org.onap.so.db.catalog.beans.Service;
63 import org.onap.so.db.catalog.beans.ServiceProxyResourceCustomization;
64 import org.onap.so.db.catalog.beans.ToscaCsar;
65 import org.onap.so.db.catalog.data.repository.AllottedResourceCustomizationRepository;
66 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
67 import org.onap.so.db.catalog.data.repository.ConfigurationResourceCustomizationRepository;
68 import org.onap.so.db.catalog.data.repository.ServiceRepository;
69 import org.onap.so.db.catalog.data.repository.ToscaCsarRepository;
70 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
71 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
72 import org.springframework.beans.factory.annotation.Autowired;
73 import org.springframework.test.util.ReflectionTestUtils;
74 import java.util.Optional;
75
76 public class ToscaResourceInstallerTest extends BaseTest {
77     @Autowired
78     private ToscaResourceInstaller toscaInstaller;
79     @Autowired
80     private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
81     @Autowired
82     private AllottedResourceRepository allottedRepo;
83     @Autowired
84     private AllottedResourceCustomizationRepository allottedCustomizationRepo;
85     @Autowired
86     private ServiceRepository serviceRepo;
87     @Mock
88     private SdcCsarHelperImpl sdcCsarHelper;
89     @Mock
90     private Metadata metadata;
91     @Mock
92     private ArtifactInfoImpl artifactInfo;
93     @Mock
94     private List<Group> vfGroups;
95     @Mock
96     private IResourceInstance resourceInstance;
97     @Rule
98     public ExpectedException expectedException = ExpectedException.none();
99     @Mock
100     private NodeTemplate nodeTemplate;
101     @Mock
102     private ToscaResourceStructure toscaResourceStructure;
103     @Mock
104     private ServiceProxyResourceCustomization spResourceCustomization;
105     @Mock
106     private ISdcCsarHelper csarHelper;
107     @Mock
108     private StatefulEntityType entityType;
109     @Mock
110     private Service service;
111
112     private NotificationDataImpl notificationData;
113     private JsonStatusData statusData;
114     private static final String MSO = "SO";
115
116     @Before
117     public void before() {
118         MockitoAnnotations.initMocks(this);
119
120         notificationData = new NotificationDataImpl();
121         statusData = new JsonStatusData();
122     }
123
124     @Test
125     public void isCsarAlreadyDeployedTest() throws ArtifactInstallerException {
126         IArtifactInfo inputCsar = mock(IArtifactInfo.class);
127         String artifactUuid = "0122c05e-e13a-4c63-b5d2-475ccf13aa74";
128         String checkSum = "MGUzNjJjMzk3OTBkYzExYzQ0MDg2ZDc2M2E2ZjZiZmY=";
129
130         doReturn(checkSum).when(inputCsar).getArtifactChecksum();
131         doReturn(artifactUuid).when(inputCsar).getArtifactUUID();
132
133         doReturn(inputCsar).when(toscaResourceStructure).getToscaArtifact();
134
135         ToscaCsar toscaCsar = mock(ToscaCsar.class);
136
137         Optional<ToscaCsar> returnValue = Optional.of(toscaCsar);
138
139         ToscaCsarRepository toscaCsarRepo = spy(ToscaCsarRepository.class);
140
141
142         doReturn(artifactUuid).when(toscaCsar).getArtifactUUID();
143         doReturn(checkSum).when(toscaCsar).getArtifactChecksum();
144         doReturn(returnValue).when(toscaCsarRepo).findById(artifactUuid);
145
146         ReflectionTestUtils.setField(toscaInstaller, "toscaCsarRepo", toscaCsarRepo);
147
148         boolean isCsarDeployed = toscaInstaller.isCsarAlreadyDeployed(toscaResourceStructure);
149         assertTrue(isCsarDeployed);
150         verify(toscaCsarRepo, times(1)).findById(artifactUuid);
151         verify(toscaResourceStructure, times(1)).getToscaArtifact();
152         verify(toscaCsar, times(2)).getArtifactChecksum();
153     }
154
155     @Test
156     public void isResourceAlreadyDeployedTest() throws Exception {
157         notificationData.setServiceName("serviceName");
158         notificationData.setServiceVersion("123456");
159         notificationData.setServiceUUID("serviceUUID");
160         notificationData.setDistributionID("testStatusSuccessTosca");
161
162         WatchdogComponentDistributionStatus expectedComponentDistributionStatus =
163                 new WatchdogComponentDistributionStatus(notificationData.getDistributionID(), MSO);
164         expectedComponentDistributionStatus
165                 .setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
166
167         doReturn(true).when(vfResourceStructure).isDeployedSuccessfully();
168         doReturn(notificationData).when(vfResourceStructure).getNotification();
169         doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
170         doReturn("resourceInstanceName").when(resourceInstance).getResourceInstanceName();
171         doReturn("resourceCustomizationUUID").when(resourceInstance).getResourceCustomizationUUID();
172         doReturn("resourceName").when(resourceInstance).getResourceName();
173
174         toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure, false);
175
176         WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(
177                 watchdogCDStatusRepository.findByDistributionId(notificationData.getDistributionID()), MSO);
178
179         verify(vfResourceStructure, times(3)).getResourceInstance();
180         verify(vfResourceStructure, times(5)).getNotification();
181         assertThat(actualWatchdogComponentDistributionStatus,
182                 sameBeanAs(expectedComponentDistributionStatus).ignoring("createTime").ignoring("modifyTime"));
183     }
184
185     @Test
186     public void isResourceAlreadyDeployedFalseTest() throws Exception {
187         notificationData.setServiceName("serviceName");
188         notificationData.setServiceVersion("123456");
189         notificationData.setServiceUUID("serviceUUID");
190         notificationData.setDistributionID("testStatusSuccess");
191
192         doThrow(RuntimeException.class).when(vfResourceStructure).isDeployedSuccessfully();
193         doReturn(notificationData).when(vfResourceStructure).getNotification();
194         doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
195         doReturn("resourceInstanceName").when(resourceInstance).getResourceInstanceName();
196         doReturn("resourceCustomizationUUID").when(resourceInstance).getResourceCustomizationUUID();
197         doReturn("resourceName").when(resourceInstance).getResourceName();
198
199         toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure, false);
200
201         verify(vfResourceStructure, times(3)).getResourceInstance();
202         verify(vfResourceStructure, times(4)).getNotification();
203     }
204
205     @Test
206     public void isResourceAlreadyDeployedExceptionTest() throws ArtifactInstallerException {
207         expectedException.expect(ArtifactInstallerException.class);
208
209         toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure, false);
210     }
211
212     @Test
213     public void installTheComponentStatusTest() throws Exception {
214         String distributionId = "testStatusSuccessTosca";
215         String componentName = "testComponentName";
216
217         statusData = spy(JsonStatusData.class);
218         doReturn(distributionId).when(statusData).getDistributionID();
219         doReturn(componentName).when(statusData).getComponentName();
220
221         WatchdogComponentDistributionStatus expectedWatchdogComponentDistributionStatus =
222                 new WatchdogComponentDistributionStatus(distributionId, componentName);
223         expectedWatchdogComponentDistributionStatus.setComponentDistributionStatus(statusData.getStatus().toString());
224
225         WatchdogComponentDistributionStatus cdStatus =
226                 new WatchdogComponentDistributionStatus(statusData.getDistributionID(), statusData.getComponentName());
227
228         toscaInstaller.installTheComponentStatus(statusData);
229
230         WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus =
231                 getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId("testStatusSuccessTosca"),
232                         statusData.getComponentName());
233
234         assertEquals(statusData.getDistributionID(), cdStatus.getDistributionId());
235         assertEquals(statusData.getComponentName(), cdStatus.getComponentName());
236         assertThat(actualWatchdogComponentDistributionStatus,
237                 sameBeanAs(expectedWatchdogComponentDistributionStatus).ignoring("createTime").ignoring("modifyTime"));
238     }
239
240     @Test
241     public void installTheComponentStatusExceptionTest() throws ArtifactInstallerException {
242         expectedException.expect(ArtifactInstallerException.class);
243
244         statusData = spy(JsonStatusData.class);
245         doReturn(null).when(statusData).getStatus();
246
247         toscaInstaller.installTheComponentStatus(statusData);
248     }
249
250     @Test
251     public void installTheResourceExceptionTest() throws Exception {
252         expectedException.expect(ArtifactInstallerException.class);
253
254         notificationData.setDistributionID("testStatusFailureTosca");
255         notificationData.setServiceVersion("123456");
256         notificationData.setServiceUUID("serviceUUID");
257         notificationData.setWorkloadContext("workloadContext");
258
259         doReturn(notificationData).when(vfResourceStructure).getNotification();
260         doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
261
262         toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
263     }
264
265     @Test
266     public void installTheResourceDBExceptionTest() throws Exception {
267         notificationData.setDistributionID("testStatusSuccessTosca");
268         notificationData.setServiceVersion("123456");
269         notificationData.setServiceUUID("serviceUUID");
270         notificationData.setWorkloadContext("workloadContext");
271
272         doReturn(notificationData).when(vfResourceStructure).getNotification();
273         doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
274         doThrow(LockAcquisitionException.class).when(toscaResourceStruct).getToscaArtifact();
275
276         toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
277     }
278
279     @Test
280     public void verifyTheFilePrefixInStringTest() {
281         String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
282         String filenameToVerify = "testfile.txt";
283         String expectedFileBody = "abcabcabctestfile.txtabcbabacbcabc";
284
285         String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
286
287         assertEquals(expectedFileBody, newFileBody);
288     }
289
290     @Test
291     public void verifyTheFilePrefixInStringNullBodyTest() {
292         String body = null;
293         String filenameToVerify = "testfile.txt";
294
295         String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
296
297         assertEquals(body, newFileBody);
298     }
299
300     @Test
301     public void verifyTheFilePrefixInStringEmptyBodyTest() {
302         String body = "";
303         String filenameToVerify = "testfile.txt";
304
305         String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
306
307         assertEquals(body, newFileBody);
308     }
309
310     @Test
311     public void verifyTheFilePrefixInStringNullFilenameTest() {
312         String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
313         String filenameToVerify = null;
314
315         String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
316
317         assertEquals(body, newFileBody);
318     }
319
320     @Test
321     public void verifyTheFilePrefixInStringEmptyFilenameTest() {
322         String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
323         String filenameToVerify = "";
324
325         String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
326
327         assertEquals(body, newFileBody);
328     }
329
330     private WatchdogComponentDistributionStatus getWatchdogCDStatusWithName(
331             List<WatchdogComponentDistributionStatus> watchdogComponentDistributionStatuses, String componentName) {
332         WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus =
333                 new WatchdogComponentDistributionStatus();
334         for (WatchdogComponentDistributionStatus watchdogComponentDistributionStatus : watchdogComponentDistributionStatuses) {
335             if (componentName.equals(watchdogComponentDistributionStatus.getComponentName())) {
336                 actualWatchdogComponentDistributionStatus = watchdogComponentDistributionStatus;
337                 break;
338             }
339         }
340         return actualWatchdogComponentDistributionStatus;
341     }
342
343
344
345     private void prepareConfigurationResource() {
346         doReturn(metadata).when(nodeTemplate).getMetaData();
347         doReturn(MockConstants.TEMPLATE_TYPE).when(nodeTemplate).getType();
348
349         doReturn(MockConstants.MODEL_NAME).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
350         doReturn(MockConstants.MODEL_INVARIANT_UUID).when(metadata)
351                 .getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID);
352         doReturn(MockConstants.MODEL_UUID).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_UUID);
353         doReturn(MockConstants.MODEL_VERSION).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_VERSION);
354         doReturn(MockConstants.MODEL_DESCRIPTION).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION);
355         doReturn(MockConstants.MODEL_NAME).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
356         doReturn(MockConstants.MODEL_NAME).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
357     }
358
359     @Test
360     public void getConfigurationResourceTest() {
361         prepareConfigurationResource();
362
363         ConfigurationResource configResource = toscaInstaller.getConfigurationResource(nodeTemplate);
364
365         assertNotNull(configResource);
366         assertEquals(MockConstants.MODEL_NAME, configResource.getModelName());
367         assertEquals(MockConstants.MODEL_INVARIANT_UUID, configResource.getModelInvariantUUID());
368         assertEquals(MockConstants.MODEL_UUID, configResource.getModelUUID());
369         assertEquals(MockConstants.MODEL_VERSION, configResource.getModelVersion());
370         assertEquals(MockConstants.MODEL_DESCRIPTION, configResource.getDescription());
371         assertEquals(MockConstants.TEMPLATE_TYPE, nodeTemplate.getType());
372     }
373
374     private void prepareConfigurationResourceCustomization() {
375         prepareConfigurationResource();
376         doReturn(MockConstants.MODEL_CUSTOMIZATIONUUID).when(metadata)
377                 .getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
378         doReturn(csarHelper).when(toscaResourceStructure).getSdcCsarHelper();
379         doReturn(null).when(csarHelper).getNodeTemplatePropertyLeafValue(nodeTemplate,
380                 SdcPropertyNames.PROPERTY_NAME_NFFUNCTION);
381         doReturn(null).when(csarHelper).getNodeTemplatePropertyLeafValue(nodeTemplate,
382                 SdcPropertyNames.PROPERTY_NAME_NFROLE);
383         doReturn(null).when(csarHelper).getNodeTemplatePropertyLeafValue(nodeTemplate,
384                 SdcPropertyNames.PROPERTY_NAME_NFTYPE);
385         doReturn(MockConstants.MODEL_CUSTOMIZATIONUUID).when(spResourceCustomization).getModelCustomizationUUID();
386     }
387
388
389     @Test
390     public void getConfigurationResourceCustomizationTest() {
391         prepareConfigurationResourceCustomization();
392
393         ConfigurationResourceCustomization configurationResourceCustomization =
394                 toscaInstaller.getConfigurationResourceCustomization(nodeTemplate, toscaResourceStructure,
395                         spResourceCustomization, service);
396         assertNotNull(configurationResourceCustomization);
397         assertNotNull(configurationResourceCustomization.getConfigurationResource());
398         assertEquals(MockConstants.MODEL_CUSTOMIZATIONUUID,
399                 configurationResourceCustomization.getServiceProxyResourceCustomizationUUID());
400     }
401
402     @Test
403     public void correlateConfigCustomResourcesTest() {
404         ConfigurationResource vrfConfigResource = mock(ConfigurationResource.class);
405         ConfigurationResourceCustomization vrfConfigCustom = mock(ConfigurationResourceCustomization.class);
406         doReturn(ToscaResourceInstaller.NODES_VRF_ENTRY).when(vrfConfigResource).getToscaNodeType();
407         doReturn(vrfConfigResource).when(vrfConfigCustom).getConfigurationResource();
408
409         ConfigurationResource vnrConfigResource = mock(ConfigurationResource.class);
410         ConfigurationResourceCustomization vnrConfigCustom = mock(ConfigurationResourceCustomization.class);
411         doReturn(ToscaResourceInstaller.VLAN_NETWORK_RECEPTOR).when(vnrConfigResource).getToscaNodeType();
412         doReturn(vnrConfigResource).when(vnrConfigCustom).getConfigurationResource();
413
414         ConfigurationResourceCustomizationRepository configCustomizationRepo =
415                 spy(ConfigurationResourceCustomizationRepository.class);
416         ReflectionTestUtils.setField(toscaInstaller, "configCustomizationRepo", configCustomizationRepo);
417         doReturn(vrfConfigCustom).when(configCustomizationRepo).save(vrfConfigCustom);
418         doReturn(vnrConfigCustom).when(configCustomizationRepo).save(vnrConfigCustom);
419
420         List<ConfigurationResourceCustomization> configList = new ArrayList<>();
421         configList.add(vrfConfigCustom);
422         configList.add(vnrConfigCustom);
423         doReturn(configList).when(service).getConfigurationCustomizations();
424
425         toscaInstaller.correlateConfigCustomResources(service);
426         verify(vrfConfigCustom, times(1)).getConfigurationResource();
427         verify(vrfConfigCustom, times(1)).setConfigResourceCustomization(vnrConfigCustom);
428         verify(service, times(1)).getConfigurationCustomizations();
429         verify(vnrConfigCustom, times(1)).getConfigurationResource();
430         verify(vnrConfigCustom, times(1)).setConfigResourceCustomization(vrfConfigCustom);
431     }
432
433     class MockConstants {
434         public final static String MODEL_NAME = "VLAN Network Receptor Configuration";
435         public final static String MODEL_INVARIANT_UUID = "1608eef4-de53-4334-a8d2-ba79cab4bde0";
436         public final static String MODEL_UUID = "212ca27b-554c-474c-96b9-ddc2f1b1ddba";
437         public final static String MODEL_VERSION = "30.0";
438         public final static String MODEL_DESCRIPTION = "VLAN network receptor configuration object";
439         public final static String MODEL_CUSTOMIZATIONUUID = "2db953e8-679d-437b-bff7-cb262638a8cd";
440         public final static String TEMPLATE_TYPE = "org.openecomp.nodes.VLANNetworkReceptor";
441         public final static String TEMPLATE_NAME = "VLAN Network Receptor Configuration 0";
442
443
444     }
445 }