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;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Optional;
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;
71 public class ToscaResourceInstallerTest extends BaseTest {
73 private ToscaResourceInstaller toscaInstaller;
75 private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
77 private AllottedResourceRepository allottedRepo;
79 private AllottedResourceCustomizationRepository allottedCustomizationRepo;
81 private ServiceRepository serviceRepo;
83 private SdcCsarHelperImpl sdcCsarHelper;
85 private Metadata metadata;
87 private ArtifactInfoImpl artifactInfo;
89 private List<Group> vfGroups;
91 private IResourceInstance resourceInstance;
93 public ExpectedException expectedException = ExpectedException.none();
95 private NodeTemplate nodeTemplate;
97 private ToscaResourceStructure toscaResourceStructure;
99 private ServiceProxyResourceCustomization spResourceCustomization;
101 private ISdcCsarHelper csarHelper;
103 private StatefulEntityType entityType;
105 private NotificationDataImpl notificationData;
106 private JsonStatusData statusData;
107 private static final String MSO = "SO";
110 public void before() {
111 MockitoAnnotations.initMocks(this);
113 notificationData = new NotificationDataImpl();
114 statusData = new JsonStatusData();
118 public void isResourceAlreadyDeployedTest() throws Exception {
119 notificationData.setServiceName("serviceName");
120 notificationData.setServiceVersion("123456");
121 notificationData.setServiceUUID("serviceUUID");
122 notificationData.setDistributionID("testStatusSuccessTosca");
124 WatchdogComponentDistributionStatus expectedComponentDistributionStatus =
125 new WatchdogComponentDistributionStatus(notificationData.getDistributionID(), MSO);
126 expectedComponentDistributionStatus.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
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();
135 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
137 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId(notificationData.getDistributionID()), MSO);
139 verify(vfResourceStructure, times(3)).getResourceInstance();
140 verify(vfResourceStructure, times(5)).getNotification();
141 assertThat(actualWatchdogComponentDistributionStatus, sameBeanAs(expectedComponentDistributionStatus)
142 .ignoring("createTime")
143 .ignoring("modifyTime"));
147 public void isResourceAlreadyDeployedFalseTest() throws Exception {
148 notificationData.setServiceName("serviceName");
149 notificationData.setServiceVersion("123456");
150 notificationData.setServiceUUID("serviceUUID");
151 notificationData.setDistributionID("testStatusSuccess");
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();
160 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
162 verify(vfResourceStructure, times(3)).getResourceInstance();
163 verify(vfResourceStructure, times(4)).getNotification();
167 public void isResourceAlreadyDeployedExceptionTest() throws ArtifactInstallerException {
168 expectedException.expect(ArtifactInstallerException.class);
170 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
174 public void installTheComponentStatusTest() throws Exception {
175 String distributionId = "testStatusSuccessTosca";
176 String componentName = "testComponentName";
178 statusData = spy(JsonStatusData.class);
179 doReturn(distributionId).when(statusData).getDistributionID();
180 doReturn(componentName).when(statusData).getComponentName();
182 WatchdogComponentDistributionStatus expectedWatchdogComponentDistributionStatus =
183 new WatchdogComponentDistributionStatus(distributionId, componentName);
184 expectedWatchdogComponentDistributionStatus.setComponentDistributionStatus(statusData.getStatus().toString());
186 WatchdogComponentDistributionStatus cdStatus = new WatchdogComponentDistributionStatus(statusData.getDistributionID(),
187 statusData.getComponentName());
189 toscaInstaller.installTheComponentStatus(statusData);
191 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId("testStatusSuccessTosca"), statusData.getComponentName());
193 assertEquals(statusData.getDistributionID(), cdStatus.getDistributionId());
194 assertEquals(statusData.getComponentName(), cdStatus.getComponentName());
195 assertThat(actualWatchdogComponentDistributionStatus, sameBeanAs(expectedWatchdogComponentDistributionStatus)
196 .ignoring("createTime")
197 .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(List<WatchdogComponentDistributionStatus> watchdogComponentDistributionStatuses, String componentName) {
291 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = new WatchdogComponentDistributionStatus();
292 for(WatchdogComponentDistributionStatus watchdogComponentDistributionStatus : watchdogComponentDistributionStatuses) {
293 if(componentName.equals(watchdogComponentDistributionStatus.getComponentName())) {
294 actualWatchdogComponentDistributionStatus = watchdogComponentDistributionStatus;
298 return actualWatchdogComponentDistributionStatus;
304 private void prepareConfigurationResource() {
305 doReturn(metadata).when(nodeTemplate).getMetaData();
306 doReturn(MockConstants.TEMPLATE_TYPE).when(nodeTemplate).getType();
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);
318 public void getConfigurationResourceTest() {
319 prepareConfigurationResource();
321 ConfigurationResource configResource=toscaInstaller.getConfigurationResource(nodeTemplate);
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());
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();
344 public void getConfigurationResourceCustomizationTest() {
345 prepareConfigurationResourceCustomization();
347 ConfigurationResourceCustomization configurationResourceCustomization = toscaInstaller.getConfigurationResourceCustomization(
348 nodeTemplate, toscaResourceStructure, spResourceCustomization);
349 assertNotNull(configurationResourceCustomization);
350 assertNotNull(configurationResourceCustomization.getConfigurationResource());
351 assertEquals(MockConstants.MODEL_CUSTOMIZATIONUUID, configurationResourceCustomization.getServiceProxyResourceCustomizationUUID());
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());
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";