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;
27 import static org.junit.Assert.assertNull;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.doNothing;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.doThrow;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.spy;
34 import static org.mockito.Mockito.times;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
39 import java.io.FileInputStream;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.List;
46 import javax.transaction.Transactional;
48 import org.apache.commons.io.IOUtils;
49 import org.hibernate.exception.LockAcquisitionException;
50 import org.junit.Before;
51 import org.junit.Ignore;
52 import org.junit.Rule;
53 import org.junit.Test;
54 import org.junit.rules.ExpectedException;
55 import org.mockito.Mock;
56 import org.mockito.MockitoAnnotations;
57 import org.onap.sdc.api.notification.IResourceInstance;
58 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
59 import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl;
60 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
61 import org.onap.sdc.toscaparser.api.CapabilityAssignment;
62 import org.onap.sdc.toscaparser.api.CapabilityAssignments;
63 import org.onap.sdc.toscaparser.api.Group;
64 import org.onap.sdc.toscaparser.api.NodeTemplate;
65 import org.onap.sdc.toscaparser.api.elements.Metadata;
66 import org.onap.sdc.utils.DistributionStatusEnum;
67 import org.onap.so.asdc.BaseTest;
68 import org.onap.so.asdc.client.ASDCConfiguration;
69 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
70 import org.onap.so.asdc.client.test.emulators.ArtifactInfoImpl;
71 import org.onap.so.asdc.client.test.emulators.JsonStatusData;
72 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
73 import org.onap.so.asdc.installer.VfModuleArtifact;
74 import org.onap.so.asdc.installer.VfResourceStructure;
75 import org.onap.so.db.catalog.beans.AllottedResource;
76 import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
77 import org.onap.so.db.catalog.beans.ExternalServiceToInternalService;
78 import org.onap.so.db.catalog.beans.HeatTemplate;
79 import org.onap.so.db.catalog.beans.NetworkResource;
80 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
81 import org.onap.so.db.catalog.beans.Service;
82 import org.onap.so.db.catalog.beans.VnfResource;
83 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
84 import org.onap.so.db.catalog.data.repository.AllottedResourceCustomizationRepository;
85 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
86 import org.onap.so.db.catalog.data.repository.ExternalServiceToInternalServiceRepository;
87 import org.onap.so.db.catalog.data.repository.ServiceRepository;
88 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
89 import org.onap.so.db.request.beans.WatchdogDistributionStatus;
90 import org.onap.so.db.request.beans.WatchdogServiceModVerIdLookup;
91 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
92 import org.springframework.beans.factory.annotation.Autowired;
94 public class ToscaResourceInstallerTest extends BaseTest {
96 private ToscaResourceInstaller toscaInstaller;
98 private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
100 private AllottedResourceRepository allottedRepo;
102 private AllottedResourceCustomizationRepository allottedCustomizationRepo;
104 private ServiceRepository serviceRepo;
106 private SdcCsarHelperImpl sdcCsarHelper;
108 private Metadata metadata;
110 private ArtifactInfoImpl artifactInfo;
112 private List<Group> vfGroups;
114 private IResourceInstance resourceInstance;
116 public ExpectedException expectedException = ExpectedException.none();
118 private NotificationDataImpl notificationData;
119 private JsonStatusData statusData;
120 private static final String MSO = "SO";
123 public void before() {
124 MockitoAnnotations.initMocks(this);
126 notificationData = new NotificationDataImpl();
127 statusData = new JsonStatusData();
131 public void isResourceAlreadyDeployedTest() throws Exception {
132 notificationData.setServiceName("serviceName");
133 notificationData.setServiceVersion("123456");
134 notificationData.setServiceUUID("serviceUUID");
135 notificationData.setDistributionID("testStatusSuccessTosca");
137 WatchdogComponentDistributionStatus expectedComponentDistributionStatus =
138 new WatchdogComponentDistributionStatus(notificationData.getDistributionID(), MSO);
139 expectedComponentDistributionStatus.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
141 doReturn(true).when(vfResourceStructure).isDeployedSuccessfully();
142 doReturn(notificationData).when(vfResourceStructure).getNotification();
143 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
144 doReturn("resourceInstanceName").when(resourceInstance).getResourceInstanceName();
145 doReturn("resourceCustomizationUUID").when(resourceInstance).getResourceCustomizationUUID();
146 doReturn("resourceName").when(resourceInstance).getResourceName();
148 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
150 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId(notificationData.getDistributionID()), MSO);
152 verify(vfResourceStructure, times(3)).getResourceInstance();
153 verify(vfResourceStructure, times(5)).getNotification();
154 assertThat(actualWatchdogComponentDistributionStatus, sameBeanAs(expectedComponentDistributionStatus)
155 .ignoring("createTime")
156 .ignoring("modifyTime"));
160 public void isResourceAlreadyDeployedFalseTest() throws Exception {
161 notificationData.setServiceName("serviceName");
162 notificationData.setServiceVersion("123456");
163 notificationData.setServiceUUID("serviceUUID");
164 notificationData.setDistributionID("testStatusSuccess");
166 doThrow(RuntimeException.class).when(vfResourceStructure).isDeployedSuccessfully();
167 doReturn(notificationData).when(vfResourceStructure).getNotification();
168 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
169 doReturn("resourceInstanceName").when(resourceInstance).getResourceInstanceName();
170 doReturn("resourceCustomizationUUID").when(resourceInstance).getResourceCustomizationUUID();
171 doReturn("resourceName").when(resourceInstance).getResourceName();
173 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
175 verify(vfResourceStructure, times(3)).getResourceInstance();
176 verify(vfResourceStructure, times(4)).getNotification();
180 public void isResourceAlreadyDeployedExceptionTest() throws ArtifactInstallerException {
181 expectedException.expect(ArtifactInstallerException.class);
183 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
187 public void installTheComponentStatusTest() throws Exception {
188 String distributionId = "testStatusSuccessTosca";
189 String componentName = "testComponentName";
191 statusData = spy(JsonStatusData.class);
192 doReturn(distributionId).when(statusData).getDistributionID();
193 doReturn(componentName).when(statusData).getComponentName();
195 WatchdogComponentDistributionStatus expectedWatchdogComponentDistributionStatus =
196 new WatchdogComponentDistributionStatus(distributionId, componentName);
197 expectedWatchdogComponentDistributionStatus.setComponentDistributionStatus(statusData.getStatus().toString());
199 WatchdogComponentDistributionStatus cdStatus = new WatchdogComponentDistributionStatus(statusData.getDistributionID(),
200 statusData.getComponentName());
202 toscaInstaller.installTheComponentStatus(statusData);
204 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId("testStatusSuccessTosca"), statusData.getComponentName());
206 assertEquals(statusData.getDistributionID(), cdStatus.getDistributionId());
207 assertEquals(statusData.getComponentName(), cdStatus.getComponentName());
208 assertThat(actualWatchdogComponentDistributionStatus, sameBeanAs(expectedWatchdogComponentDistributionStatus)
209 .ignoring("createTime")
210 .ignoring("modifyTime"));
214 public void installTheComponentStatusExceptionTest() throws ArtifactInstallerException {
215 expectedException.expect(ArtifactInstallerException.class);
217 statusData = spy(JsonStatusData.class);
218 doReturn(null).when(statusData).getStatus();
220 toscaInstaller.installTheComponentStatus(statusData);
224 public void installTheResourceExceptionTest() throws Exception {
225 expectedException.expect(ArtifactInstallerException.class);
227 notificationData.setDistributionID("testStatusFailureTosca");
228 notificationData.setServiceVersion("123456");
229 notificationData.setServiceUUID("serviceUUID");
230 notificationData.setWorkloadContext("workloadContext");
232 doReturn(notificationData).when(vfResourceStructure).getNotification();
233 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
235 toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
239 public void installTheResourceDBExceptionTest() throws Exception {
240 notificationData.setDistributionID("testStatusSuccessTosca");
241 notificationData.setServiceVersion("123456");
242 notificationData.setServiceUUID("serviceUUID");
243 notificationData.setWorkloadContext("workloadContext");
245 doReturn(notificationData).when(vfResourceStructure).getNotification();
246 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
247 doThrow(LockAcquisitionException.class).when(toscaResourceStruct).getToscaArtifact();
249 toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
253 public void verifyTheFilePrefixInStringTest() {
254 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
255 String filenameToVerify = "testfile.txt";
256 String expectedFileBody = "abcabcabctestfile.txtabcbabacbcabc";
258 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
260 assertEquals(expectedFileBody, newFileBody);
264 public void verifyTheFilePrefixInStringNullBodyTest() {
266 String filenameToVerify = "testfile.txt";
268 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
270 assertEquals(body, newFileBody);
274 public void verifyTheFilePrefixInStringEmptyBodyTest() {
276 String filenameToVerify = "testfile.txt";
278 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
280 assertEquals(body, newFileBody);
284 public void verifyTheFilePrefixInStringNullFilenameTest() {
285 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
286 String filenameToVerify = null;
288 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
290 assertEquals(body, newFileBody);
294 public void verifyTheFilePrefixInStringEmptyFilenameTest() {
295 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
296 String filenameToVerify = "";
298 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
300 assertEquals(body, newFileBody);
303 private WatchdogComponentDistributionStatus getWatchdogCDStatusWithName(List<WatchdogComponentDistributionStatus> watchdogComponentDistributionStatuses, String componentName) {
304 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = new WatchdogComponentDistributionStatus();
305 for(WatchdogComponentDistributionStatus watchdogComponentDistributionStatus : watchdogComponentDistributionStatuses) {
306 if(componentName.equals(watchdogComponentDistributionStatus.getComponentName())) {
307 actualWatchdogComponentDistributionStatus = watchdogComponentDistributionStatus;
311 return actualWatchdogComponentDistributionStatus;