99833da3a392ae91f229cac4f747cb3d323da872
[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.matcher.Matchers.sameBeanAs;
24 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
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.spy;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Optional;
36 import org.hibernate.exception.LockAcquisitionException;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.sdc.api.notification.IResourceInstance;
45 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
46 import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl;
47 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
48 import org.onap.sdc.toscaparser.api.Group;
49 import org.onap.sdc.toscaparser.api.NodeTemplate;
50 import org.onap.sdc.toscaparser.api.elements.Metadata;
51 import org.onap.sdc.toscaparser.api.elements.StatefulEntityType;
52 import org.onap.sdc.utils.DistributionStatusEnum;
53 import org.onap.so.asdc.BaseTest;
54 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
55 import org.onap.so.asdc.client.test.emulators.ArtifactInfoImpl;
56 import org.onap.so.asdc.client.test.emulators.JsonStatusData;
57 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
58 import org.onap.so.asdc.installer.ToscaResourceStructure;
59 import org.onap.so.db.catalog.beans.ConfigurationResource;
60 import org.onap.so.db.catalog.beans.ConfigurationResourceCustomization;
61 import org.onap.so.db.catalog.beans.ServiceProxyResourceCustomization;
62 import org.onap.so.db.catalog.data.repository.AllottedResourceCustomizationRepository;
63 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
64 import org.onap.so.db.catalog.data.repository.ServiceRepository;
65 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
66 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
67 import org.springframework.beans.factory.annotation.Autowired;
68
69 public class ToscaResourceInstallerTest extends BaseTest {
70     @Autowired
71     private ToscaResourceInstaller toscaInstaller;
72     @Autowired
73     private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
74     @Autowired
75     private AllottedResourceRepository allottedRepo;
76     @Autowired
77     private AllottedResourceCustomizationRepository allottedCustomizationRepo;
78     @Autowired
79     private ServiceRepository serviceRepo;
80     @Mock
81     private SdcCsarHelperImpl sdcCsarHelper;
82     @Mock
83     private Metadata metadata;
84     @Mock
85     private ArtifactInfoImpl artifactInfo;
86     @Mock
87     private List<Group> vfGroups;
88     @Mock
89     private IResourceInstance resourceInstance;
90     @Rule
91     public ExpectedException expectedException = ExpectedException.none();
92     @Mock
93     private NodeTemplate nodeTemplate;
94     @Mock
95     private ToscaResourceStructure toscaResourceStructure;
96     @Mock
97     private ServiceProxyResourceCustomization spResourceCustomization;
98     @Mock
99     private ISdcCsarHelper csarHelper;
100     @Mock
101     private StatefulEntityType entityType;
102
103     private NotificationDataImpl notificationData;
104     private JsonStatusData statusData;
105     private static final String MSO = "SO";
106
107     @Before
108     public void before() {
109         MockitoAnnotations.initMocks(this);
110
111         notificationData = new NotificationDataImpl();
112         statusData = new JsonStatusData();
113     }
114
115     @Test
116     public void isResourceAlreadyDeployedTest() throws Exception {
117         notificationData.setServiceName("serviceName");
118         notificationData.setServiceVersion("123456");
119         notificationData.setServiceUUID("serviceUUID");
120         notificationData.setDistributionID("testStatusSuccessTosca");
121
122         WatchdogComponentDistributionStatus expectedComponentDistributionStatus =
123                 new WatchdogComponentDistributionStatus(notificationData.getDistributionID(), MSO);
124         expectedComponentDistributionStatus
125                 .setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
126
127         doReturn(true).when(vfResourceStructure).isDeployedSuccessfully();
128         doReturn(notificationData).when(vfResourceStructure).getNotification();
129         doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
130         doReturn("resourceInstanceName").when(resourceInstance).getResourceInstanceName();
131         doReturn("resourceCustomizationUUID").when(resourceInstance).getResourceCustomizationUUID();
132         doReturn("resourceName").when(resourceInstance).getResourceName();
133
134         toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
135
136         WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(
137                 watchdogCDStatusRepository.findByDistributionId(notificationData.getDistributionID()), MSO);
138
139         verify(vfResourceStructure, times(3)).getResourceInstance();
140         verify(vfResourceStructure, times(5)).getNotification();
141         assertThat(actualWatchdogComponentDistributionStatus,
142                 sameBeanAs(expectedComponentDistributionStatus).ignoring("createTime").ignoring("modifyTime"));
143     }
144
145     @Test
146     public void isResourceAlreadyDeployedFalseTest() throws Exception {
147         notificationData.setServiceName("serviceName");
148         notificationData.setServiceVersion("123456");
149         notificationData.setServiceUUID("serviceUUID");
150         notificationData.setDistributionID("testStatusSuccess");
151
152         doThrow(RuntimeException.class).when(vfResourceStructure).isDeployedSuccessfully();
153         doReturn(notificationData).when(vfResourceStructure).getNotification();
154         doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
155         doReturn("resourceInstanceName").when(resourceInstance).getResourceInstanceName();
156         doReturn("resourceCustomizationUUID").when(resourceInstance).getResourceCustomizationUUID();
157         doReturn("resourceName").when(resourceInstance).getResourceName();
158
159         toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
160
161         verify(vfResourceStructure, times(3)).getResourceInstance();
162         verify(vfResourceStructure, times(4)).getNotification();
163     }
164
165     @Test
166     public void isResourceAlreadyDeployedExceptionTest() throws ArtifactInstallerException {
167         expectedException.expect(ArtifactInstallerException.class);
168
169         toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
170     }
171
172     @Test
173     public void installTheComponentStatusTest() throws Exception {
174         String distributionId = "testStatusSuccessTosca";
175         String componentName = "testComponentName";
176
177         statusData = spy(JsonStatusData.class);
178         doReturn(distributionId).when(statusData).getDistributionID();
179         doReturn(componentName).when(statusData).getComponentName();
180
181         WatchdogComponentDistributionStatus expectedWatchdogComponentDistributionStatus =
182                 new WatchdogComponentDistributionStatus(distributionId, componentName);
183         expectedWatchdogComponentDistributionStatus.setComponentDistributionStatus(statusData.getStatus().toString());
184
185         WatchdogComponentDistributionStatus cdStatus =
186                 new WatchdogComponentDistributionStatus(statusData.getDistributionID(), statusData.getComponentName());
187
188         toscaInstaller.installTheComponentStatus(statusData);
189
190         WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus =
191                 getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId("testStatusSuccessTosca"),
192                         statusData.getComponentName());
193
194         assertEquals(statusData.getDistributionID(), cdStatus.getDistributionId());
195         assertEquals(statusData.getComponentName(), cdStatus.getComponentName());
196         assertThat(actualWatchdogComponentDistributionStatus,
197                 sameBeanAs(expectedWatchdogComponentDistributionStatus).ignoring("createTime").ignoring("modifyTime"));
198     }
199
200     @Test
201     public void installTheComponentStatusExceptionTest() throws ArtifactInstallerException {
202         expectedException.expect(ArtifactInstallerException.class);
203
204         statusData = spy(JsonStatusData.class);
205         doReturn(null).when(statusData).getStatus();
206
207         toscaInstaller.installTheComponentStatus(statusData);
208     }
209
210     @Test
211     public void installTheResourceExceptionTest() throws Exception {
212         expectedException.expect(ArtifactInstallerException.class);
213
214         notificationData.setDistributionID("testStatusFailureTosca");
215         notificationData.setServiceVersion("123456");
216         notificationData.setServiceUUID("serviceUUID");
217         notificationData.setWorkloadContext("workloadContext");
218
219         doReturn(notificationData).when(vfResourceStructure).getNotification();
220         doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
221
222         toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
223     }
224
225     @Test
226     public void installTheResourceDBExceptionTest() throws Exception {
227         notificationData.setDistributionID("testStatusSuccessTosca");
228         notificationData.setServiceVersion("123456");
229         notificationData.setServiceUUID("serviceUUID");
230         notificationData.setWorkloadContext("workloadContext");
231
232         doReturn(notificationData).when(vfResourceStructure).getNotification();
233         doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
234         doThrow(LockAcquisitionException.class).when(toscaResourceStruct).getToscaArtifact();
235
236         toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
237     }
238
239     @Test
240     public void verifyTheFilePrefixInStringTest() {
241         String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
242         String filenameToVerify = "testfile.txt";
243         String expectedFileBody = "abcabcabctestfile.txtabcbabacbcabc";
244
245         String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
246
247         assertEquals(expectedFileBody, newFileBody);
248     }
249
250     @Test
251     public void verifyTheFilePrefixInStringNullBodyTest() {
252         String body = null;
253         String filenameToVerify = "testfile.txt";
254
255         String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
256
257         assertEquals(body, newFileBody);
258     }
259
260     @Test
261     public void verifyTheFilePrefixInStringEmptyBodyTest() {
262         String body = "";
263         String filenameToVerify = "testfile.txt";
264
265         String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
266
267         assertEquals(body, newFileBody);
268     }
269
270     @Test
271     public void verifyTheFilePrefixInStringNullFilenameTest() {
272         String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
273         String filenameToVerify = null;
274
275         String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
276
277         assertEquals(body, newFileBody);
278     }
279
280     @Test
281     public void verifyTheFilePrefixInStringEmptyFilenameTest() {
282         String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
283         String filenameToVerify = "";
284
285         String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
286
287         assertEquals(body, newFileBody);
288     }
289
290     private WatchdogComponentDistributionStatus getWatchdogCDStatusWithName(
291             List<WatchdogComponentDistributionStatus> watchdogComponentDistributionStatuses, String componentName) {
292         WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus =
293                 new WatchdogComponentDistributionStatus();
294         for (WatchdogComponentDistributionStatus watchdogComponentDistributionStatus : watchdogComponentDistributionStatuses) {
295             if (componentName.equals(watchdogComponentDistributionStatus.getComponentName())) {
296                 actualWatchdogComponentDistributionStatus = watchdogComponentDistributionStatus;
297                 break;
298             }
299         }
300         return actualWatchdogComponentDistributionStatus;
301     }
302
303
304
305     private void prepareConfigurationResource() {
306         doReturn(metadata).when(nodeTemplate).getMetaData();
307         doReturn(MockConstants.TEMPLATE_TYPE).when(nodeTemplate).getType();
308
309         doReturn(MockConstants.MODEL_NAME).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
310         doReturn(MockConstants.MODEL_INVARIANT_UUID).when(metadata)
311                 .getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID);
312         doReturn(MockConstants.MODEL_UUID).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_UUID);
313         doReturn(MockConstants.MODEL_VERSION).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_VERSION);
314         doReturn(MockConstants.MODEL_DESCRIPTION).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION);
315         doReturn(MockConstants.MODEL_NAME).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
316         doReturn(MockConstants.MODEL_NAME).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
317     }
318
319     @Test
320     public void getConfigurationResourceTest() {
321         prepareConfigurationResource();
322
323         ConfigurationResource configResource = toscaInstaller.getConfigurationResource(nodeTemplate);
324
325         assertNotNull(configResource);
326         assertEquals(MockConstants.MODEL_NAME, configResource.getModelName());
327         assertEquals(MockConstants.MODEL_INVARIANT_UUID, configResource.getModelInvariantUUID());
328         assertEquals(MockConstants.MODEL_UUID, configResource.getModelUUID());
329         assertEquals(MockConstants.MODEL_VERSION, configResource.getModelVersion());
330         assertEquals(MockConstants.MODEL_DESCRIPTION, configResource.getDescription());
331         assertEquals(MockConstants.TEMPLATE_TYPE, nodeTemplate.getType());
332     }
333
334     private void prepareConfigurationResourceCustomization() {
335         prepareConfigurationResource();
336         doReturn(MockConstants.MODEL_CUSTOMIZATIONUUID).when(metadata)
337                 .getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
338         doReturn(csarHelper).when(toscaResourceStructure).getSdcCsarHelper();
339         doReturn(null).when(csarHelper).getNodeTemplatePropertyLeafValue(nodeTemplate,
340                 SdcPropertyNames.PROPERTY_NAME_NFFUNCTION);
341         doReturn(null).when(csarHelper).getNodeTemplatePropertyLeafValue(nodeTemplate,
342                 SdcPropertyNames.PROPERTY_NAME_NFROLE);
343         doReturn(null).when(csarHelper).getNodeTemplatePropertyLeafValue(nodeTemplate,
344                 SdcPropertyNames.PROPERTY_NAME_NFTYPE);
345         doReturn(MockConstants.MODEL_CUSTOMIZATIONUUID).when(spResourceCustomization).getModelCustomizationUUID();
346     }
347
348
349     @Test
350     public void getConfigurationResourceCustomizationTest() {
351         prepareConfigurationResourceCustomization();
352
353         ConfigurationResourceCustomization configurationResourceCustomization = toscaInstaller
354                 .getConfigurationResourceCustomization(nodeTemplate, toscaResourceStructure, spResourceCustomization);
355         assertNotNull(configurationResourceCustomization);
356         assertNotNull(configurationResourceCustomization.getConfigurationResource());
357         assertEquals(MockConstants.MODEL_CUSTOMIZATIONUUID,
358                 configurationResourceCustomization.getServiceProxyResourceCustomizationUUID());
359     }
360
361     @Test
362     public void getVnrNodeTemplateTest() {
363         prepareConfigurationResourceCustomization();
364         List<NodeTemplate> nodeTemplateList = new ArrayList<>();
365         doReturn(ToscaResourceInstaller.VLAN_NETWORK_RECEPTOR).when(entityType).getType();
366         doReturn(entityType).when(nodeTemplate).getTypeDefinition();
367         nodeTemplateList.add(nodeTemplate);
368         Optional<ConfigurationResourceCustomization> vnrResourceCustomization =
369                 toscaInstaller.getVnrNodeTemplate(nodeTemplateList, toscaResourceStructure, spResourceCustomization);
370         assertTrue(vnrResourceCustomization.isPresent());
371         assertEquals(ToscaResourceInstaller.VLAN_NETWORK_RECEPTOR, entityType.getType());
372     }
373
374     class MockConstants {
375         public final static String MODEL_NAME = "VLAN Network Receptor Configuration";
376         public final static String MODEL_INVARIANT_UUID = "1608eef4-de53-4334-a8d2-ba79cab4bde0";
377         public final static String MODEL_UUID = "212ca27b-554c-474c-96b9-ddc2f1b1ddba";
378         public final static String MODEL_VERSION = "30.0";
379         public final static String MODEL_DESCRIPTION = "VLAN network receptor configuration object";
380         public final static String MODEL_CUSTOMIZATIONUUID = "2db953e8-679d-437b-bff7-cb262638a8cd";
381         public final static String TEMPLATE_TYPE = "org.openecomp.nodes.VLANNetworkReceptor";
382         public final static String TEMPLATE_NAME = "VLAN Network Receptor Configuration 0";
383
384     }
385 }