Merge changes Id0369478,I82d8306f
[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
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Optional;
37
38 import org.hibernate.exception.LockAcquisitionException;
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.sdc.api.notification.IResourceInstance;
47 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
48 import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl;
49 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
50 import org.onap.sdc.toscaparser.api.Group;
51 import org.onap.sdc.toscaparser.api.NodeTemplate;
52 import org.onap.sdc.toscaparser.api.elements.Metadata;
53 import org.onap.sdc.toscaparser.api.elements.StatefulEntityType;
54 import org.onap.sdc.utils.DistributionStatusEnum;
55 import org.onap.so.asdc.BaseTest;
56 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
57 import org.onap.so.asdc.client.test.emulators.ArtifactInfoImpl;
58 import org.onap.so.asdc.client.test.emulators.JsonStatusData;
59 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
60 import org.onap.so.asdc.installer.ToscaResourceStructure;
61 import org.onap.so.db.catalog.beans.ConfigurationResource;
62 import org.onap.so.db.catalog.beans.ConfigurationResourceCustomization;
63 import org.onap.so.db.catalog.beans.ServiceProxyResourceCustomization;
64 import org.onap.so.db.catalog.data.repository.AllottedResourceCustomizationRepository;
65 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
66 import org.onap.so.db.catalog.data.repository.ServiceRepository;
67 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
68 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
69 import org.springframework.beans.factory.annotation.Autowired;
70
71 public class ToscaResourceInstallerTest extends BaseTest {
72         @Autowired
73         private ToscaResourceInstaller toscaInstaller;
74         @Autowired
75         private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
76         @Autowired
77         private AllottedResourceRepository allottedRepo;
78         @Autowired
79         private AllottedResourceCustomizationRepository allottedCustomizationRepo;
80         @Autowired
81         private ServiceRepository serviceRepo;
82         @Mock
83         private SdcCsarHelperImpl sdcCsarHelper;
84         @Mock
85         private Metadata metadata;
86         @Mock
87         private ArtifactInfoImpl artifactInfo;
88         @Mock
89         private List<Group> vfGroups;
90         @Mock
91         private IResourceInstance resourceInstance;
92         @Rule
93         public ExpectedException expectedException = ExpectedException.none();
94         @Mock
95         private NodeTemplate nodeTemplate;
96         @Mock
97         private ToscaResourceStructure toscaResourceStructure;
98         @Mock 
99         private ServiceProxyResourceCustomization spResourceCustomization;
100         @Mock
101         private ISdcCsarHelper csarHelper;
102         @Mock
103         private StatefulEntityType entityType;
104
105         private NotificationDataImpl notificationData;
106         private JsonStatusData statusData;
107         private static final String MSO = "SO";
108
109         @Before
110         public void before() {
111                 MockitoAnnotations.initMocks(this);
112                 
113                 notificationData = new NotificationDataImpl();
114                 statusData = new JsonStatusData();
115         }
116         
117         @Test
118         public void isResourceAlreadyDeployedTest() throws Exception {
119                 notificationData.setServiceName("serviceName");
120                 notificationData.setServiceVersion("123456");
121                 notificationData.setServiceUUID("serviceUUID");
122                 notificationData.setDistributionID("testStatusSuccessTosca");
123                 
124                 WatchdogComponentDistributionStatus expectedComponentDistributionStatus = 
125                                 new WatchdogComponentDistributionStatus(notificationData.getDistributionID(), MSO);
126                 expectedComponentDistributionStatus.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
127                 
128                 doReturn(true).when(vfResourceStructure).isDeployedSuccessfully();
129                 doReturn(notificationData).when(vfResourceStructure).getNotification();
130                 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
131                 doReturn("resourceInstanceName").when(resourceInstance).getResourceInstanceName();
132                 doReturn("resourceCustomizationUUID").when(resourceInstance).getResourceCustomizationUUID();
133                 doReturn("resourceName").when(resourceInstance).getResourceName();
134                 
135                 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
136                 
137                 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId(notificationData.getDistributionID()), MSO);
138                 
139                 verify(vfResourceStructure, times(3)).getResourceInstance();
140                 verify(vfResourceStructure, times(5)).getNotification();
141                 assertThat(actualWatchdogComponentDistributionStatus, sameBeanAs(expectedComponentDistributionStatus)
142                                 .ignoring("createTime")
143                                 .ignoring("modifyTime"));
144         }
145         
146         @Test
147         public void isResourceAlreadyDeployedFalseTest() throws Exception {
148                 notificationData.setServiceName("serviceName");
149                 notificationData.setServiceVersion("123456");
150                 notificationData.setServiceUUID("serviceUUID");
151                 notificationData.setDistributionID("testStatusSuccess");
152                 
153                 doThrow(RuntimeException.class).when(vfResourceStructure).isDeployedSuccessfully();
154                 doReturn(notificationData).when(vfResourceStructure).getNotification();
155                 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
156                 doReturn("resourceInstanceName").when(resourceInstance).getResourceInstanceName();
157                 doReturn("resourceCustomizationUUID").when(resourceInstance).getResourceCustomizationUUID();
158                 doReturn("resourceName").when(resourceInstance).getResourceName();
159                 
160                 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
161                 
162                 verify(vfResourceStructure, times(3)).getResourceInstance();
163                 verify(vfResourceStructure, times(4)).getNotification();
164         }
165         
166         @Test
167         public void isResourceAlreadyDeployedExceptionTest() throws ArtifactInstallerException {
168                 expectedException.expect(ArtifactInstallerException.class);
169                 
170                 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
171         }
172         
173         @Test
174         public void installTheComponentStatusTest() throws Exception {
175                 String distributionId = "testStatusSuccessTosca";
176                 String componentName = "testComponentName";
177                 
178                 statusData = spy(JsonStatusData.class);
179                 doReturn(distributionId).when(statusData).getDistributionID();
180                 doReturn(componentName).when(statusData).getComponentName();
181                 
182                 WatchdogComponentDistributionStatus expectedWatchdogComponentDistributionStatus = 
183                                 new WatchdogComponentDistributionStatus(distributionId, componentName);
184                 expectedWatchdogComponentDistributionStatus.setComponentDistributionStatus(statusData.getStatus().toString());
185                 
186                 WatchdogComponentDistributionStatus cdStatus = new WatchdogComponentDistributionStatus(statusData.getDistributionID(),
187                                 statusData.getComponentName());
188                 
189                 toscaInstaller.installTheComponentStatus(statusData);
190                 
191                 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId("testStatusSuccessTosca"), statusData.getComponentName());
192                 
193                 assertEquals(statusData.getDistributionID(), cdStatus.getDistributionId());
194                 assertEquals(statusData.getComponentName(), cdStatus.getComponentName());
195                 assertThat(actualWatchdogComponentDistributionStatus, sameBeanAs(expectedWatchdogComponentDistributionStatus)
196                                 .ignoring("createTime")
197                                 .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(List<WatchdogComponentDistributionStatus> watchdogComponentDistributionStatuses, String componentName) {
291                 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = new WatchdogComponentDistributionStatus();
292                 for(WatchdogComponentDistributionStatus watchdogComponentDistributionStatus : watchdogComponentDistributionStatuses) {
293                         if(componentName.equals(watchdogComponentDistributionStatus.getComponentName())) {
294                                 actualWatchdogComponentDistributionStatus = watchdogComponentDistributionStatus;
295                                 break;
296                         }
297                 }
298                 return actualWatchdogComponentDistributionStatus;
299         }
300         
301         
302         
303         
304         private void prepareConfigurationResource() {
305                 doReturn(metadata).when(nodeTemplate).getMetaData();
306                 doReturn(MockConstants.TEMPLATE_TYPE).when(nodeTemplate).getType();
307                 
308                 doReturn(MockConstants.MODEL_NAME).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_NAME);                
309                 doReturn(MockConstants.MODEL_INVARIANT_UUID).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID);             
310                 doReturn(MockConstants.MODEL_UUID).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_UUID);                
311                 doReturn(MockConstants.MODEL_VERSION).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_VERSION);
312                 doReturn(MockConstants.MODEL_DESCRIPTION).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION);
313                 doReturn(MockConstants.MODEL_NAME).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
314                 doReturn(MockConstants.MODEL_NAME).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
315         }
316         
317         @Test
318         public void getConfigurationResourceTest() {
319                 prepareConfigurationResource();
320                 
321                 ConfigurationResource configResource=toscaInstaller.getConfigurationResource(nodeTemplate);
322                 
323                 assertNotNull(configResource);
324                 assertEquals(MockConstants.MODEL_NAME, configResource.getModelName());
325                 assertEquals(MockConstants.MODEL_INVARIANT_UUID, configResource.getModelInvariantUUID());
326                 assertEquals(MockConstants.MODEL_UUID, configResource.getModelUUID());
327                 assertEquals(MockConstants.MODEL_VERSION, configResource.getModelVersion());
328                 assertEquals(MockConstants.MODEL_DESCRIPTION, configResource.getDescription());
329                 assertEquals(MockConstants.TEMPLATE_TYPE, nodeTemplate.getType());
330         }
331         
332         private void prepareConfigurationResourceCustomization() {
333                 prepareConfigurationResource();
334                 doReturn(MockConstants.MODEL_CUSTOMIZATIONUUID).when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
335                 doReturn(csarHelper).when(toscaResourceStructure).getSdcCsarHelper();
336                 doReturn(null).when(csarHelper).getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION);
337                 doReturn(null).when(csarHelper).getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE);
338                 doReturn(null).when(csarHelper).getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE);
339                 doReturn(MockConstants.MODEL_CUSTOMIZATIONUUID).when(spResourceCustomization).getModelCustomizationUUID();
340         }
341         
342         
343         @Test
344         public void getConfigurationResourceCustomizationTest() {
345                 prepareConfigurationResourceCustomization();            
346                 
347                 ConfigurationResourceCustomization configurationResourceCustomization = toscaInstaller.getConfigurationResourceCustomization(
348                                 nodeTemplate, toscaResourceStructure, spResourceCustomization);
349                 assertNotNull(configurationResourceCustomization);
350                 assertNotNull(configurationResourceCustomization.getConfigurationResource());
351                 assertEquals(MockConstants.MODEL_CUSTOMIZATIONUUID, configurationResourceCustomization.getServiceProxyResourceCustomizationUUID());
352         }
353         
354         @Test
355         public void getVnrNodeTemplateTest() {
356                 prepareConfigurationResourceCustomization();
357                 List<NodeTemplate> nodeTemplateList = new ArrayList<>();
358                 doReturn(ToscaResourceInstaller.VLAN_NETWORK_RECEPTOR).when(entityType).getType();
359                 doReturn(entityType).when(nodeTemplate).getTypeDefinition();
360                 nodeTemplateList.add(nodeTemplate);
361                 Optional<ConfigurationResourceCustomization> vnrResourceCustomization= 
362                                 toscaInstaller.getVnrNodeTemplate(nodeTemplateList, toscaResourceStructure, spResourceCustomization);
363                 assertTrue(vnrResourceCustomization.isPresent());
364                 assertEquals(ToscaResourceInstaller.VLAN_NETWORK_RECEPTOR, entityType.getType());
365         }
366         
367         class MockConstants{
368                 public final static String MODEL_NAME = "VLAN Network Receptor Configuration";
369                 public final static String MODEL_INVARIANT_UUID = "1608eef4-de53-4334-a8d2-ba79cab4bde0";
370                 public final static String MODEL_UUID = "212ca27b-554c-474c-96b9-ddc2f1b1ddba";
371                 public final static String MODEL_VERSION = "30.0";
372                 public final static String MODEL_DESCRIPTION = "VLAN network receptor configuration object";
373                 public final static String MODEL_CUSTOMIZATIONUUID = "2db953e8-679d-437b-bff7-cb262638a8cd";
374                 public final static String TEMPLATE_TYPE = "org.openecomp.nodes.VLANNetworkReceptor";
375                 public final static String TEMPLATE_NAME = "VLAN Network Receptor Configuration 0";
376                 
377         }
378 }