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.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;
69 public class ToscaResourceInstallerTest extends BaseTest {
71 private ToscaResourceInstaller toscaInstaller;
73 private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
75 private AllottedResourceRepository allottedRepo;
77 private AllottedResourceCustomizationRepository allottedCustomizationRepo;
79 private ServiceRepository serviceRepo;
81 private SdcCsarHelperImpl sdcCsarHelper;
83 private Metadata metadata;
85 private ArtifactInfoImpl artifactInfo;
87 private List<Group> vfGroups;
89 private IResourceInstance resourceInstance;
91 public ExpectedException expectedException = ExpectedException.none();
93 private NodeTemplate nodeTemplate;
95 private ToscaResourceStructure toscaResourceStructure;
97 private ServiceProxyResourceCustomization spResourceCustomization;
99 private ISdcCsarHelper csarHelper;
101 private StatefulEntityType entityType;
103 private NotificationDataImpl notificationData;
104 private JsonStatusData statusData;
105 private static final String MSO = "SO";
108 public void before() {
109 MockitoAnnotations.initMocks(this);
111 notificationData = new NotificationDataImpl();
112 statusData = new JsonStatusData();
116 public void isResourceAlreadyDeployedTest() throws Exception {
117 notificationData.setServiceName("serviceName");
118 notificationData.setServiceVersion("123456");
119 notificationData.setServiceUUID("serviceUUID");
120 notificationData.setDistributionID("testStatusSuccessTosca");
122 WatchdogComponentDistributionStatus expectedComponentDistributionStatus =
123 new WatchdogComponentDistributionStatus(notificationData.getDistributionID(), MSO);
124 expectedComponentDistributionStatus
125 .setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
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();
134 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
136 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(
137 watchdogCDStatusRepository.findByDistributionId(notificationData.getDistributionID()), MSO);
139 verify(vfResourceStructure, times(3)).getResourceInstance();
140 verify(vfResourceStructure, times(5)).getNotification();
141 assertThat(actualWatchdogComponentDistributionStatus,
142 sameBeanAs(expectedComponentDistributionStatus).ignoring("createTime").ignoring("modifyTime"));
146 public void isResourceAlreadyDeployedFalseTest() throws Exception {
147 notificationData.setServiceName("serviceName");
148 notificationData.setServiceVersion("123456");
149 notificationData.setServiceUUID("serviceUUID");
150 notificationData.setDistributionID("testStatusSuccess");
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();
159 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
161 verify(vfResourceStructure, times(3)).getResourceInstance();
162 verify(vfResourceStructure, times(4)).getNotification();
166 public void isResourceAlreadyDeployedExceptionTest() throws ArtifactInstallerException {
167 expectedException.expect(ArtifactInstallerException.class);
169 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
173 public void installTheComponentStatusTest() throws Exception {
174 String distributionId = "testStatusSuccessTosca";
175 String componentName = "testComponentName";
177 statusData = spy(JsonStatusData.class);
178 doReturn(distributionId).when(statusData).getDistributionID();
179 doReturn(componentName).when(statusData).getComponentName();
181 WatchdogComponentDistributionStatus expectedWatchdogComponentDistributionStatus =
182 new WatchdogComponentDistributionStatus(distributionId, componentName);
183 expectedWatchdogComponentDistributionStatus.setComponentDistributionStatus(statusData.getStatus().toString());
185 WatchdogComponentDistributionStatus cdStatus =
186 new WatchdogComponentDistributionStatus(statusData.getDistributionID(), statusData.getComponentName());
188 toscaInstaller.installTheComponentStatus(statusData);
190 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus =
191 getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId("testStatusSuccessTosca"),
192 statusData.getComponentName());
194 assertEquals(statusData.getDistributionID(), cdStatus.getDistributionId());
195 assertEquals(statusData.getComponentName(), cdStatus.getComponentName());
196 assertThat(actualWatchdogComponentDistributionStatus,
197 sameBeanAs(expectedWatchdogComponentDistributionStatus).ignoring("createTime").ignoring("modifyTime"));
201 public void installTheComponentStatusExceptionTest() throws ArtifactInstallerException {
202 expectedException.expect(ArtifactInstallerException.class);
204 statusData = spy(JsonStatusData.class);
205 doReturn(null).when(statusData).getStatus();
207 toscaInstaller.installTheComponentStatus(statusData);
211 public void installTheResourceExceptionTest() throws Exception {
212 expectedException.expect(ArtifactInstallerException.class);
214 notificationData.setDistributionID("testStatusFailureTosca");
215 notificationData.setServiceVersion("123456");
216 notificationData.setServiceUUID("serviceUUID");
217 notificationData.setWorkloadContext("workloadContext");
219 doReturn(notificationData).when(vfResourceStructure).getNotification();
220 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
222 toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
226 public void installTheResourceDBExceptionTest() throws Exception {
227 notificationData.setDistributionID("testStatusSuccessTosca");
228 notificationData.setServiceVersion("123456");
229 notificationData.setServiceUUID("serviceUUID");
230 notificationData.setWorkloadContext("workloadContext");
232 doReturn(notificationData).when(vfResourceStructure).getNotification();
233 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
234 doThrow(LockAcquisitionException.class).when(toscaResourceStruct).getToscaArtifact();
236 toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
240 public void verifyTheFilePrefixInStringTest() {
241 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
242 String filenameToVerify = "testfile.txt";
243 String expectedFileBody = "abcabcabctestfile.txtabcbabacbcabc";
245 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
247 assertEquals(expectedFileBody, newFileBody);
251 public void verifyTheFilePrefixInStringNullBodyTest() {
253 String filenameToVerify = "testfile.txt";
255 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
257 assertEquals(body, newFileBody);
261 public void verifyTheFilePrefixInStringEmptyBodyTest() {
263 String filenameToVerify = "testfile.txt";
265 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
267 assertEquals(body, newFileBody);
271 public void verifyTheFilePrefixInStringNullFilenameTest() {
272 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
273 String filenameToVerify = null;
275 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
277 assertEquals(body, newFileBody);
281 public void verifyTheFilePrefixInStringEmptyFilenameTest() {
282 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
283 String filenameToVerify = "";
285 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
287 assertEquals(body, newFileBody);
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;
300 return actualWatchdogComponentDistributionStatus;
305 private void prepareConfigurationResource() {
306 doReturn(metadata).when(nodeTemplate).getMetaData();
307 doReturn(MockConstants.TEMPLATE_TYPE).when(nodeTemplate).getType();
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);
320 public void getConfigurationResourceTest() {
321 prepareConfigurationResource();
323 ConfigurationResource configResource = toscaInstaller.getConfigurationResource(nodeTemplate);
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());
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();
350 public void getConfigurationResourceCustomizationTest() {
351 prepareConfigurationResourceCustomization();
353 ConfigurationResourceCustomization configurationResourceCustomization = toscaInstaller
354 .getConfigurationResourceCustomization(nodeTemplate, toscaResourceStructure, spResourceCustomization);
355 assertNotNull(configurationResourceCustomization);
356 assertNotNull(configurationResourceCustomization.getConfigurationResource());
357 assertEquals(MockConstants.MODEL_CUSTOMIZATIONUUID,
358 configurationResourceCustomization.getServiceProxyResourceCustomizationUUID());
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());
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";