2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.asdc.installer.heat;
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;
76 public class ToscaResourceInstallerTest extends BaseTest {
78 private ToscaResourceInstaller toscaInstaller;
80 private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
82 private AllottedResourceRepository allottedRepo;
84 private AllottedResourceCustomizationRepository allottedCustomizationRepo;
86 private ServiceRepository serviceRepo;
88 private SdcCsarHelperImpl sdcCsarHelper;
90 private Metadata metadata;
92 private ArtifactInfoImpl artifactInfo;
94 private List<Group> vfGroups;
96 private IResourceInstance resourceInstance;
98 public ExpectedException expectedException = ExpectedException.none();
100 private NodeTemplate nodeTemplate;
102 private ToscaResourceStructure toscaResourceStructure;
104 private ServiceProxyResourceCustomization spResourceCustomization;
106 private ISdcCsarHelper csarHelper;
108 private StatefulEntityType entityType;
110 private Service service;
112 private NotificationDataImpl notificationData;
113 private JsonStatusData statusData;
114 private static final String MSO = "SO";
117 public void before() {
118 MockitoAnnotations.initMocks(this);
120 notificationData = new NotificationDataImpl();
121 statusData = new JsonStatusData();
125 public void isCsarAlreadyDeployedTest() throws ArtifactInstallerException {
126 IArtifactInfo inputCsar = mock(IArtifactInfo.class);
127 String artifactUuid = "0122c05e-e13a-4c63-b5d2-475ccf13aa74";
128 String checkSum = "MGUzNjJjMzk3OTBkYzExYzQ0MDg2ZDc2M2E2ZjZiZmY=";
130 doReturn(checkSum).when(inputCsar).getArtifactChecksum();
131 doReturn(artifactUuid).when(inputCsar).getArtifactUUID();
133 doReturn(inputCsar).when(toscaResourceStructure).getToscaArtifact();
135 ToscaCsar toscaCsar = mock(ToscaCsar.class);
137 Optional<ToscaCsar> returnValue = Optional.of(toscaCsar);
139 ToscaCsarRepository toscaCsarRepo = spy(ToscaCsarRepository.class);
142 doReturn(artifactUuid).when(toscaCsar).getArtifactUUID();
143 doReturn(checkSum).when(toscaCsar).getArtifactChecksum();
144 doReturn(returnValue).when(toscaCsarRepo).findById(artifactUuid);
146 ReflectionTestUtils.setField(toscaInstaller, "toscaCsarRepo", toscaCsarRepo);
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();
156 public void isResourceAlreadyDeployedTest() throws Exception {
157 notificationData.setServiceName("serviceName");
158 notificationData.setServiceVersion("123456");
159 notificationData.setServiceUUID("serviceUUID");
160 notificationData.setDistributionID("testStatusSuccessTosca");
162 WatchdogComponentDistributionStatus expectedComponentDistributionStatus =
163 new WatchdogComponentDistributionStatus(notificationData.getDistributionID(), MSO);
164 expectedComponentDistributionStatus
165 .setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
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();
174 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure, false);
176 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(
177 watchdogCDStatusRepository.findByDistributionId(notificationData.getDistributionID()), MSO);
179 verify(vfResourceStructure, times(3)).getResourceInstance();
180 verify(vfResourceStructure, times(5)).getNotification();
181 assertThat(actualWatchdogComponentDistributionStatus,
182 sameBeanAs(expectedComponentDistributionStatus).ignoring("createTime").ignoring("modifyTime"));
186 public void isResourceAlreadyDeployedFalseTest() throws Exception {
187 notificationData.setServiceName("serviceName");
188 notificationData.setServiceVersion("123456");
189 notificationData.setServiceUUID("serviceUUID");
190 notificationData.setDistributionID("testStatusSuccess");
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();
199 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure, false);
201 verify(vfResourceStructure, times(3)).getResourceInstance();
202 verify(vfResourceStructure, times(4)).getNotification();
206 public void isResourceAlreadyDeployedExceptionTest() throws ArtifactInstallerException {
207 expectedException.expect(ArtifactInstallerException.class);
209 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure, false);
213 public void installTheComponentStatusTest() throws Exception {
214 String distributionId = "testStatusSuccessTosca";
215 String componentName = "testComponentName";
217 statusData = spy(JsonStatusData.class);
218 doReturn(distributionId).when(statusData).getDistributionID();
219 doReturn(componentName).when(statusData).getComponentName();
221 WatchdogComponentDistributionStatus expectedWatchdogComponentDistributionStatus =
222 new WatchdogComponentDistributionStatus(distributionId, componentName);
223 expectedWatchdogComponentDistributionStatus.setComponentDistributionStatus(statusData.getStatus().toString());
225 WatchdogComponentDistributionStatus cdStatus =
226 new WatchdogComponentDistributionStatus(statusData.getDistributionID(), statusData.getComponentName());
228 toscaInstaller.installTheComponentStatus(statusData);
230 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus =
231 getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId("testStatusSuccessTosca"),
232 statusData.getComponentName());
234 assertEquals(statusData.getDistributionID(), cdStatus.getDistributionId());
235 assertEquals(statusData.getComponentName(), cdStatus.getComponentName());
236 assertThat(actualWatchdogComponentDistributionStatus,
237 sameBeanAs(expectedWatchdogComponentDistributionStatus).ignoring("createTime").ignoring("modifyTime"));
241 public void installTheComponentStatusExceptionTest() throws ArtifactInstallerException {
242 expectedException.expect(ArtifactInstallerException.class);
244 statusData = spy(JsonStatusData.class);
245 doReturn(null).when(statusData).getStatus();
247 toscaInstaller.installTheComponentStatus(statusData);
251 public void installTheResourceExceptionTest() throws Exception {
252 expectedException.expect(ArtifactInstallerException.class);
254 notificationData.setDistributionID("testStatusFailureTosca");
255 notificationData.setServiceVersion("123456");
256 notificationData.setServiceUUID("serviceUUID");
257 notificationData.setWorkloadContext("workloadContext");
259 doReturn(notificationData).when(vfResourceStructure).getNotification();
260 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
262 toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
266 public void installTheResourceDBExceptionTest() throws Exception {
267 notificationData.setDistributionID("testStatusSuccessTosca");
268 notificationData.setServiceVersion("123456");
269 notificationData.setServiceUUID("serviceUUID");
270 notificationData.setWorkloadContext("workloadContext");
272 doReturn(notificationData).when(vfResourceStructure).getNotification();
273 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
274 doThrow(LockAcquisitionException.class).when(toscaResourceStruct).getToscaArtifact();
276 toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
280 public void verifyTheFilePrefixInStringTest() {
281 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
282 String filenameToVerify = "testfile.txt";
283 String expectedFileBody = "abcabcabctestfile.txtabcbabacbcabc";
285 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
287 assertEquals(expectedFileBody, newFileBody);
291 public void verifyTheFilePrefixInStringNullBodyTest() {
293 String filenameToVerify = "testfile.txt";
295 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
297 assertEquals(body, newFileBody);
301 public void verifyTheFilePrefixInStringEmptyBodyTest() {
303 String filenameToVerify = "testfile.txt";
305 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
307 assertEquals(body, newFileBody);
311 public void verifyTheFilePrefixInStringNullFilenameTest() {
312 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
313 String filenameToVerify = null;
315 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
317 assertEquals(body, newFileBody);
321 public void verifyTheFilePrefixInStringEmptyFilenameTest() {
322 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
323 String filenameToVerify = "";
325 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
327 assertEquals(body, newFileBody);
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;
340 return actualWatchdogComponentDistributionStatus;
345 private void prepareConfigurationResource() {
346 doReturn(metadata).when(nodeTemplate).getMetaData();
347 doReturn(MockConstants.TEMPLATE_TYPE).when(nodeTemplate).getType();
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);
360 public void getConfigurationResourceTest() {
361 prepareConfigurationResource();
363 ConfigurationResource configResource = toscaInstaller.getConfigurationResource(nodeTemplate);
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());
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();
390 public void getConfigurationResourceCustomizationTest() {
391 prepareConfigurationResourceCustomization();
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());
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();
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();
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);
420 List<ConfigurationResourceCustomization> configList = new ArrayList<>();
421 configList.add(vrfConfigCustom);
422 configList.add(vnrConfigCustom);
423 doReturn(configList).when(service).getConfigurationCustomizations();
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);
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";