2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.sdc.vendorsoftwareproduct.impl;
23 import org.apache.commons.lang3.tuple.ImmutablePair;
24 import org.apache.commons.lang3.tuple.Pair;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Captor;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.mockito.Spy;
31 import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
32 import org.openecomp.core.enrichment.types.ArtifactType;
33 import org.openecomp.core.factory.impl.AbstractFactoryBase;
34 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
35 import org.openecomp.core.model.dao.ServiceModelDao;
36 import org.openecomp.core.model.types.ServiceElement;
37 import org.openecomp.core.utilities.file.FileContentHandler;
38 import org.openecomp.core.utilities.file.FileUtils;
39 import org.openecomp.sdc.activityLog.ActivityLogManager;
40 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
41 import org.openecomp.sdc.common.errors.CoreException;
42 import org.openecomp.sdc.common.errors.ErrorCategory;
43 import org.openecomp.sdc.common.errors.ErrorCode;
44 import org.openecomp.sdc.common.errors.Messages;
45 import org.openecomp.sdc.common.errors.ValidationErrorBuilder;
46 import org.openecomp.sdc.healing.api.HealingManager;
47 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
48 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
49 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
50 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
51 import org.openecomp.sdc.vendorsoftwareproduct.MibManager;
52 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
53 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
54 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
55 import org.openecomp.sdc.vendorsoftwareproduct.dao.PackageInfoDao;
56 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
57 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
58 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
59 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo;
60 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
61 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
62 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
63 import org.openecomp.sdc.vendorsoftwareproduct.impl.mock.EnrichmentManagerFactoryImpl;
64 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
65 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
66 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
67 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
68 import org.openecomp.sdc.vendorsoftwareproduct.types.VersionedVendorSoftwareProductInfo;
69 import org.openecomp.sdc.versioning.VersioningManager;
70 import org.openecomp.sdc.versioning.dao.types.Version;
71 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
72 import org.openecomp.sdc.versioning.errors.EditOnEntityLockedByOtherErrorBuilder;
73 import org.openecomp.sdc.versioning.errors.EntityNotExistErrorBuilder;
74 import org.openecomp.sdc.versioning.types.VersionInfo;
75 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
76 import org.testng.Assert;
77 import org.testng.annotations.BeforeMethod;
78 import org.testng.annotations.Test;
81 import java.io.FileInputStream;
82 import java.io.IOException;
83 import java.io.InputStream;
85 import java.nio.ByteBuffer;
86 import java.util.ArrayList;
87 import java.util.Arrays;
88 import java.util.Collections;
89 import java.util.HashMap;
90 import java.util.List;
92 import java.util.stream.Collectors;
93 import java.util.zip.ZipEntry;
94 import java.util.zip.ZipInputStream;
96 import static org.mockito.Matchers.any;
97 import static org.mockito.Matchers.anyObject;
98 import static org.mockito.Matchers.eq;
99 import static org.mockito.Mockito.doNothing;
100 import static org.mockito.Mockito.doReturn;
101 import static org.mockito.Mockito.doThrow;
102 import static org.mockito.Mockito.never;
103 import static org.mockito.Mockito.verify;
107 * Created by TALIO on 4/23/2016
109 public class VendorSoftwareProductManagerImplTest {
110 private static final String INVALID_VERSION_MSG = "Invalid requested version.";
112 private static String VSP_ID = "vspId";
113 private static String VERSION_ID = "versionId";
114 public static final Version VERSION01 = new Version(0, 1);
115 private static final Version VERSION10 = new Version(1, 0);
116 private static final String USER1 = "vspTestUser1";
117 private static final String USER2 = "vspTestUser2";
118 private static final String USER3 = "vspTestUser3";
119 private static String id006 = null;
120 private static String id007 = null;
123 private VersioningManager versioningManagerMock;
125 private VendorSoftwareProductDao vendorSoftwareProductDaoMock; // todo get rid of
127 private OrchestrationTemplateDao orchestrationTemplateDataDaoMock;
129 private VendorLicenseFacade vendorLicenseFacadeMock;
131 private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDaoMock;
133 private EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDaoMock;
135 private HealingManager healingManagerMock;
137 private VendorLicenseArtifactsService licenseArtifactsServiceMock;
139 private CompositionEntityDataManager compositionEntityDataManagerMock;
141 private InformationArtifactGenerator informationArtifactGeneratorMock;
143 private ActivityLogManager activityLogManagerMock;
145 private PackageInfoDao packageInfoDao;
147 private VendorSoftwareProductInfoDao vspInfoDaoMock;
152 private VendorSoftwareProductManagerImpl vendorSoftwareProductManager;
154 private OrchestrationTemplateCandidateManager candidateManager;
155 private MibManager mibManager;
158 private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
161 public void setUp() throws Exception {
162 MockitoAnnotations.initMocks(this);
166 public void testListWhenNone() {
167 doReturn(new HashMap<>()).when(versioningManagerMock).listEntitiesVersionInfo
168 (VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, USER1,
169 VersionableEntityAction.Read);
170 List<VersionedVendorSoftwareProductInfo> vsps =
171 vendorSoftwareProductManager.listVsps(null, USER1);
172 Assert.assertEquals(vsps.size(), 0);
176 public void testList() {
177 String vsp1id = "vsp1_id";
178 String vsp2id = "vsp2_id";
179 Map<String, VersionInfo> vspsTobeReturned = new HashMap<>();
181 VersionInfo versionInfo1 = new VersionInfo();
182 versionInfo1.setActiveVersion(VERSION01);
183 vspsTobeReturned.put(vsp1id, versionInfo1);
185 VersionInfo versionInfo2 = new VersionInfo();
186 versionInfo2.setActiveVersion(VERSION10);
187 vspsTobeReturned.put(vsp2id, versionInfo2);
189 doReturn(vspsTobeReturned).when(versioningManagerMock).listEntitiesVersionInfo
190 (VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, USER1,
191 VersionableEntityAction.Read);
193 VspDetails vsp1 = new VspDetails(vsp1id, VERSION01);
194 vsp1.setWritetimeMicroSeconds(8L);
195 doReturn(vsp1).when(vspInfoDaoMock)
196 .get(any(VspDetails.class));
198 List<VersionedVendorSoftwareProductInfo> vsps =
199 vendorSoftwareProductManager.listVsps(null, USER1);
200 Assert.assertEquals(vsps.size(), 2);
204 public void testListFinalsWhenNone() {
205 String vsp1id = "vsp1_id";
206 String vsp2id = "vsp2_id";
207 Map<String, VersionInfo> vspsTobeReturned = new HashMap<>();
209 VersionInfo versionInfo1 = new VersionInfo();
210 versionInfo1.setActiveVersion(VERSION01);
211 vspsTobeReturned.put(vsp1id, versionInfo1);
213 VersionInfo versionInfo2 = new VersionInfo();
214 versionInfo2.setActiveVersion(VERSION10);
215 vspsTobeReturned.put(vsp2id, versionInfo2);
217 doReturn(vspsTobeReturned).when(versioningManagerMock).listEntitiesVersionInfo
218 (VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, USER1,
219 VersionableEntityAction.Read);
221 List<VersionedVendorSoftwareProductInfo> vsps =
222 vendorSoftwareProductManager.listVsps(VersionStatus.Final.name(), USER1);
223 Assert.assertEquals(vsps.size(), 0);
227 public void testListFinals() {
228 String vsp1id = "vsp1_id";
229 String vsp2id = "vsp2_id";
230 Map<String, VersionInfo> vspsTobeReturned = new HashMap<>();
232 VersionInfo versionInfo1 = new VersionInfo();
233 versionInfo1.setActiveVersion(VERSION01);
234 vspsTobeReturned.put(vsp1id, versionInfo1);
236 VersionInfo versionInfo2 = new VersionInfo();
237 versionInfo2.setActiveVersion(new Version(1, 3));
238 versionInfo2.setLatestFinalVersion(VERSION10);
239 vspsTobeReturned.put(vsp2id, versionInfo2);
241 doReturn(vspsTobeReturned).when(versioningManagerMock).listEntitiesVersionInfo
242 (VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, USER1,
243 VersionableEntityAction.Read);
245 VspDetails vsp2 = new VspDetails(vsp2id, VERSION10);
246 vsp2.setWritetimeMicroSeconds(8L);
247 doReturn(vsp2).when(vspInfoDaoMock)
248 .get(any(VspDetails.class));
250 List<VersionedVendorSoftwareProductInfo> vsps =
251 vendorSoftwareProductManager.listVsps(VersionStatus.Final.name(), USER1);
252 Assert.assertEquals(vsps.size(), 1);
255 @Test(expectedExceptions = CoreException.class)
256 public void testCreateWithExistingName_negative() {
257 doThrow(new CoreException(
258 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).build()))
259 .when(vendorSoftwareProductManager).validateUniqueName("Vsp1");
260 VspDetails expectedVsp =
261 createVspDetails(null, null, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
262 "category", "subCategory", "123", null);
264 vendorSoftwareProductManager.createVsp(expectedVsp, USER1);
268 public void testCreate() {
269 doNothing().when(vendorSoftwareProductManager).validateUniqueName("Vsp1");
270 doNothing().when(vendorSoftwareProductManager).createUniqueName("Vsp1");
271 doReturn(VERSION01).when(versioningManagerMock).create(anyObject(), anyObject(), anyObject());
273 .when(vendorSoftwareProductManager).getVspQuestionnaireSchema(anyObject());
275 VspDetails vspToCreate =
276 createVspDetails(null, null, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
277 "category", "subCategory", "123", null);
279 VspDetails vsp = vendorSoftwareProductManager.createVsp(vspToCreate, USER1);
281 Assert.assertNotNull(vsp);
282 vspToCreate.setId(vsp.getId());
283 vspToCreate.setVersion(VERSION01);
284 assertVspsEquals(vsp, vspToCreate);
285 verify(activityLogManagerMock).addActionLog(activityLogEntityArg.capture(), eq(USER1));
286 ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
287 Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION01.getMajor()+1));
288 Assert.assertTrue(activityLogEntity.isSuccess());
291 @Test(expectedExceptions = CoreException.class)
292 public void testUpdateWithExistingName_negative() {
293 VersionInfo versionInfo = new VersionInfo();
294 versionInfo.setActiveVersion(VERSION01);
295 doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
296 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
297 VersionableEntityAction.Write);
299 VspDetails existingVsp =
300 createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-existingVsp", "vendorName", "vlm1Id",
301 "icon", "category", "subCategory", "123", null);
302 VspDetails updatedVsp =
303 createVspDetails(VSP_ID, VERSION01, "Vsp1_updated", "Test-existingVsp", "vendorName",
304 "vlm1Id", "icon", "category", "subCategory", "123", null);
305 doReturn(existingVsp).when(vspInfoDaoMock)
306 .get(any(VspDetails.class));
307 doThrow(new CoreException(
308 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).build()))
309 .when(vendorSoftwareProductManager)
310 .updateUniqueName(existingVsp.getName(), updatedVsp.getName());
312 vendorSoftwareProductManager.updateVsp(updatedVsp, USER1);
316 public void testUpdate() {
317 VersionInfo versionInfo = new VersionInfo();
318 versionInfo.setActiveVersion(VERSION01);
319 doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
320 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
321 VersionableEntityAction.Write);
322 VspDetails existingVsp =
323 createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
325 "subCategory", "456", null);
326 VspDetails updatedVsp =
327 createVspDetails(VSP_ID, VERSION01, "VSP1_updated", null, "vendorName", "vlm1Id", "icon",
329 "subCategory", "456", null);
330 existingVsp.setWritetimeMicroSeconds(8L);
331 doReturn(existingVsp).when(vspInfoDaoMock)
332 .get(any(VspDetails.class));
333 doNothing().when(vendorSoftwareProductManager)
334 .updateUniqueName(existingVsp.getName(), updatedVsp.getName());
336 vendorSoftwareProductManager.updateVsp(updatedVsp, USER1);
338 verify(vspInfoDaoMock).update(updatedVsp);
341 @Test(expectedExceptions = CoreException.class)
342 public void testGetNonExistingVersion_negative() {
343 Version notExistversion = new Version(43, 8);
344 doReturn(null).when(vspInfoDaoMock).get(any(VspDetails.class));
345 vendorSoftwareProductManager.getVsp(VSP_ID, notExistversion, USER1);
349 public void testGetCheckedOutVersion() {
350 VersionInfo versionInfo = new VersionInfo();
351 versionInfo.setActiveVersion(VERSION01);
352 versionInfo.setStatus(VersionStatus.Locked);
353 versionInfo.setLockingUser(USER1);
354 doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
355 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
356 VersionableEntityAction.Read);
358 VspDetails existingVsp =
359 createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
361 "subCategory", "456", null);
362 existingVsp.setWritetimeMicroSeconds(8L);
363 doReturn(existingVsp).when(vspInfoDaoMock).get(any(VspDetails.class));
365 VspDetails actualVsp =
366 vendorSoftwareProductManager.getVsp(VSP_ID, VERSION01, USER1);
368 assertVspsEquals(actualVsp, existingVsp);
372 public void testGetOldVersion() {
373 VersionInfo versionInfo = new VersionInfo();
374 versionInfo.setActiveVersion(new Version(0, 2));
375 versionInfo.setViewableVersions(Arrays.asList(VERSION01, new Version(0, 2)));
376 versionInfo.setStatus(VersionStatus.Locked);
377 versionInfo.setLockingUser(USER2);
378 doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
379 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
380 VersionableEntityAction.Read);
382 VspDetails existingVsp =
383 createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
385 "subCategory", "456", null);
386 existingVsp.setWritetimeMicroSeconds(8L);
387 doReturn(existingVsp)
388 .when(vspInfoDaoMock).get(any(VspDetails.class));
390 VspDetails actualVsp =
391 vendorSoftwareProductManager.getVsp(VSP_ID, VERSION01, USER1);
393 VspDetails expectedVsp =
395 .get(new VspDetails(VSP_ID, VERSION01));
396 assertVspsEquals(actualVsp, expectedVsp);
400 public void testCheckin() {
401 doReturn(VERSION01).when(versioningManagerMock)
402 .checkin(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
404 Version version = vendorSoftwareProductManager.checkin(VSP_ID, USER1);
406 Assert.assertEquals(version, VERSION01);
407 verify(versioningManagerMock)
408 .checkin(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
410 verify(activityLogManagerMock).addActionLog(activityLogEntityArg.capture(), eq(USER1));
411 ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
412 Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION01.getMajor()+1));
413 Assert.assertTrue(activityLogEntity.isSuccess());
417 public void testCheckout() {
418 doReturn(VERSION01).when(versioningManagerMock)
419 .checkout(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
421 Version version = vendorSoftwareProductManager.checkout(VSP_ID, USER1);
423 Assert.assertEquals(version, VERSION01);
424 verify(versioningManagerMock)
425 .checkout(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
428 verify(activityLogManagerMock).addActionLog(activityLogEntityArg.capture(), eq(USER1));
429 ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
430 Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION01.getMajor()+1));
431 Assert.assertTrue(activityLogEntity.isSuccess());
436 public void testUndoCheckout() {
437 Version existingVersion = new Version(0, 2);
438 VersionInfo versionInfo = new VersionInfo();
439 versionInfo.setActiveVersion(existingVersion);
440 doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
441 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
442 VSP_ID, USER1, VersionableEntityAction.Read);
444 doReturn(VERSION01).when(versioningManagerMock).undoCheckout(VendorSoftwareProductConstants
445 .VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1);
447 VspDetails vsp = new VspDetails(VSP_ID, existingVersion);
448 vsp.setName("ExistingName");
449 doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
450 doNothing().when(vendorSoftwareProductManager).updateUniqueName(vsp.getName(), vsp.getName());
452 Version undoCheckoutVersion = vendorSoftwareProductManager.undoCheckout(VSP_ID, USER1);
454 Assert.assertEquals(undoCheckoutVersion, VERSION01);
459 public void testSubmitWithMissingData() throws IOException {
460 VersionInfo versionInfo = new VersionInfo();
461 versionInfo.setActiveVersion(VERSION01);
462 doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
463 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
464 VSP_ID, USER1, VersionableEntityAction.Read);
466 VspDetails vsp = new VspDetails(VSP_ID, VERSION01);
467 doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
469 ValidationResponse validationResponse = vendorSoftwareProductManager.submit(VSP_ID, USER1);
470 Assert.assertNotNull(validationResponse);
471 Assert.assertFalse(validationResponse.isValid());
472 List<String> errorIds = validationResponse.getVspErrors().stream().map(ErrorCode::id).distinct()
473 .collect(Collectors.toList());
474 Assert.assertTrue(errorIds.contains(ValidationErrorBuilder.FIELD_VALIDATION_ERROR_ERR_ID));
475 Assert.assertTrue(errorIds.contains(VendorSoftwareProductErrorCodes.VSP_INVALID));
477 verify(versioningManagerMock, never())
478 .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
480 verify(activityLogManagerMock, never()).addActionLog(any(ActivityLogEntity.class),eq(USER1));
483 // TODO: 3/15/2017 fix and enable
485 public void testSubmitWithInvalidLicensingData() throws IOException {
486 VersionInfo versionInfo = new VersionInfo();
487 versionInfo.setActiveVersion(VERSION01);
488 doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
489 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
490 VSP_ID, USER1, VersionableEntityAction.Read);
493 createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
494 "category", "subCategory", "licenseAgreementId",
495 Collections.singletonList("featureGroupId"));
496 doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
497 UploadDataEntity uploadData = new UploadDataEntity(VSP_ID, VERSION01);
498 uploadData.setContentData(
499 ByteBuffer.wrap(FileUtils.toByteArray(getFileInputStream("/emptyComposition"))));
500 doReturn(uploadData).when(orchestrationTemplateDataDaoMock)
501 .getOrchestrationTemplate(anyObject(), anyObject());
502 doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(),
503 "MainServiceTemplate.yaml"))
504 .when(serviceModelDaoMock).getServiceModel(VSP_ID, VERSION01);
506 ValidationResponse validationResponse = vendorSoftwareProductManager.submit(VSP_ID, USER1);
507 Assert.assertNotNull(validationResponse);
508 Assert.assertFalse(validationResponse.isValid());
509 Assert.assertNull(validationResponse.getVspErrors());
510 Assert.assertEquals(validationResponse.getLicensingDataErrors(), 1);
512 verify(versioningManagerMock, never())
513 .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
517 verify(activityLogManagerMock, never()).addActionLog(any(ActivityLogEntity.class), eq(USER1));
520 // TODO: 3/15/2017 fix and enable
522 public void testSubmit() throws IOException {
523 mockVersioning(VersionableEntityAction.Read);
525 EnrichmentManagerFactory.getInstance();
527 .registerFactory(EnrichmentManagerFactory.class, EnrichmentManagerFactoryImpl.class);
530 createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
531 "category", "subCategory", "123", Collections.singletonList("fg1"));
532 doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
533 UploadDataEntity uploadData = new UploadDataEntity(VSP_ID, VERSION01);
534 uploadData.setContentData(
535 ByteBuffer.wrap(FileUtils.toByteArray(getFileInputStream("/emptyComposition"))));
536 doReturn(uploadData).when(orchestrationTemplateDataDaoMock)
537 .getOrchestrationTemplate(anyObject(), anyObject());
538 doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(),
539 "MainServiceTemplate.yaml"))
540 .when(serviceModelDaoMock).getServiceModel(VSP_ID, VERSION01);
542 ValidationResponse validationResponse = vendorSoftwareProductManager.submit(VSP_ID, USER1);
543 Assert.assertTrue(validationResponse.isValid());
545 /* Assert.assertEquals(vsp2.getVersionInfo().getActiveVersion(), VERSION10);
546 Assert.assertEquals(vsp2.getVersionInfo().getStatus(), VersionStatus.Final);
547 Assert.assertNull(vsp2.getVersionInfo().getLockingUser());*/
549 verify(versioningManagerMock)
550 .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
552 verify(activityLogManagerMock).addActionLog(activityLogEntityArg.capture(),eq(USER1));
553 ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
554 Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION10.getMajor()));
555 Assert.assertTrue(activityLogEntity.isSuccess());
558 @Test(expectedExceptions = CoreException.class)
559 public void testCreatePackageOnNonFinalVersion_negative() throws IOException {
560 vendorSoftwareProductManager.createPackage(VSP_ID, VERSION01, USER1);
564 public void testCreatePackage() throws IOException {
565 /*VspDetails vspDetailsMock = new VspDetails("vspId", new Version(1, 0));
566 doReturn(vspDetailsMock).when(vspInfoDaoMock).get(anyObject());*/
567 VersionInfo versionInfo = new VersionInfo();
568 versionInfo.setActiveVersion(VERSION10);
569 doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
570 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
571 VersionableEntityAction.Read);
573 doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(), "")).when
574 (enrichedServiceModelDaoMock).getServiceModel(VSP_ID, VERSION10);
576 VspDetails vsp = new VspDetails(VSP_ID, VERSION10);
577 vsp.setVendorId("vendorId");
578 vsp.setVlmVersion(VERSION10);
579 vsp.setFeatureGroups(Arrays.asList("fg1", "fg2"));
580 doReturn(vsp).when(vspInfoDaoMock).get(any(VspDetails.class));
582 doReturn(new FileContentHandler()).when(licenseArtifactsServiceMock)
583 .createLicenseArtifacts(VSP_ID, vsp.getVendorId(), VERSION10, vsp.getFeatureGroups(),
586 PackageInfo packageInfo = vendorSoftwareProductManager.createPackage(VSP_ID, VERSION10, USER1);
587 Assert.assertNotNull(packageInfo.getVspId());
590 // TODO: 3/15/2017 fix and enable
591 //@Test(dependsOnMethods = {"testListFinals"})
592 public void testUploadFileMissingFile() {
593 InputStream zis = getFileInputStream("/vspmanager/zips/missingYml.zip");
595 UploadFileResponse uploadFileResponse =
596 candidateManager.upload(VSP_ID, VERSION01, zis, USER1);
598 Assert.assertEquals(uploadFileResponse.getErrors().size(), 0);
601 // TODO: 3/15/2017 fix and enable
602 //@Test(dependsOnMethods = {"testUploadFileMissingFile"})
603 public void testUploadNotZipFile() throws IOException {
604 URL url = this.getClass().getResource("/notZipFile");
608 .upload(VSP_ID, VERSION01,
609 url.openStream(), USER1);
610 candidateManager.process(VSP_ID, VERSION01, USER1);
611 } catch (Exception ce) {
612 Assert.assertEquals(ce.getMessage(), Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());
615 verify(activityLogManagerMock, never()).addActionLog(any(ActivityLogEntity.class),eq(USER1));
619 public void testEnrichModelInSubmit() {
621 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
623 VspDetails vspDetails = vendorSoftwareProductManager.createVsp(
624 createVspDetails(null, null, "VSP_syb", "Test-vsp_syb", "vendorName", "vlm1Id", "icon",
625 "category", "subCategory", "456", null), USER1);
626 String id = vspDetails.getId();
629 InputStream zis = getFileInputStream("/vspmanager/zips/fullComposition.zip");
630 candidateManager.upload(id, VERSION01, zis, USER1);
631 OrchestrationTemplateActionResponse uploadFileResponse =
632 candidateManager.process(id, VERSION01, USER1);
635 vendorSoftwareProductManager.checkin(id, USER1);
638 ValidationResponse result = vendorSoftwareProductManager.submit(id, USER1);
639 } catch (IOException exception) {
642 VersionedVendorSoftwareProductInfo details =
643 vendorSoftwareProductManager.getVsp(id, null, USER1);
644 Collection<ComponentEntity> components =vendorSoftwareProductManager
645 .listComponents(id, details.getVersionInfo().getActiveVersion(), USER1);
647 ToscaServiceModel model =
648 (ToscaServiceModel) EnrichedServiceModelDaoFactory.getInstance().createInterface()
649 .getServiceModel(id, details.getVersionInfo().getActiveVersion());
651 Map<String, CapabilityDefinition> capabilities = new HashMap<>();
652 for (ComponentEntity component : components) {
653 model.getServiceTemplates().
656 filter(entryValue -> entryValue.getValue() != null &&
657 entryValue.getValue().getNode_types() != null &&
658 entryValue.getValue().
660 containsKey(component.getComponentCompositionData().getName())).
661 forEach(entryValue -> entryValue.getValue().getNode_types().
664 filter(type -> MapUtils.isNotEmpty(type.getCapabilities())).
665 forEach(type -> type.getCapabilities().
667 forEach(entry -> addCapability(entryValue.getKey(), capabilities, entry.getKey(),
668 entry.getValue()))));
672 Assert.assertNotNull(capabilities);
675 @Test(dependsOnMethods = {"testCreatePackage"})
676 public void testEnrichedFilesDeletedOnNewUpload() throws IOException {
677 Version activeVersion;
679 createPackageFromUpload(VSP_ID, USER1, "/fullComposition");
680 activeVersion = vendorSoftwareProductManager.getVsp(VSP_ID, null, USER1).getVersionInfo()
683 List<ServiceArtifact> firstExternalArtifacts = enrichedServiceModelDaoMock
684 .getExternalArtifacts(VSP_ID, activeVersion);
685 ToscaServiceModel firstServiceModel = enrichedServiceModelDaoMock.getServiceModel(VSP_ID,
688 createPackageFromUpload(VSP_ID, USER1, "/emptyComposition");
689 activeVersion = vendorSoftwareProductManager.getVsp(VSP_ID, null, USER1).getVersionInfo()
692 List<ServiceArtifact> secondExternalArtifacts = enrichedServiceModelDaoMock
693 .getExternalArtifacts(VSP_ID, activeVersion);
694 ToscaServiceModel secondServiceModel = enrichedServiceModelDaoMock.getServiceModel(VSP_ID,
697 Assert.assertNotEquals(firstExternalArtifacts, secondExternalArtifacts);
698 Assert.assertNotEquals(firstServiceModel, secondServiceModel);
702 @Test(dependsOnMethods = {"testMibsDeletedInCsar"})
703 public void testServiceTemplatesAreDeletedInCsarOnNewUpload() throws IOException {
704 String nestedPath = "Definitions" + File.separator + "nested";
706 uploadFileAndProcess(VSP_ID, USER1, "/vspmanager/zips/fullCompositionNested.zip");
707 checkinSubmitCreatePackage(VSP_ID, USER1);
708 List<String> nestedFileNamesServiceTemplates =
709 getWantedFileNamesFromCsar(nestedPath);
711 uploadFileAndProcess(VSP_ID, USER1, "/vspmanager/zips/fullComposition.zip");
712 checkinSubmitCreatePackage(VSP_ID, USER1);
713 List<String> emptyNestedNamesList = getWantedFileNamesFromCsar(nestedPath);
715 Assert.assertEquals(emptyNestedNamesList.size(), 0);
716 Assert.assertNotEquals(emptyNestedNamesList.size(), nestedFileNamesServiceTemplates.size());
719 private List<String> getWantedFileNamesFromCsar(String pathInCsar)
721 File translatedFile = vendorSoftwareProductManager.getTranslatedFile(VSP_ID, VERSION10, USER1);
723 return getFileNamesFromFolderInCsar(translatedFile,
727 private List<String> getFileNamesFromFolderInCsar(File csar, String folderName)
729 List<String> fileNames = new ArrayList<>();
731 ZipInputStream zip = new ZipInputStream(new FileInputStream(csar));
734 while ((ze = zip.getNextEntry()) != null) {
735 String name = ze.getName();
736 if (name.contains(folderName)) {
744 private Pair<String, String> uploadMib(String vspId, String user, String filePath,
746 List<ComponentEntity> componentEntities = null;
747 //(List<ComponentEntity>) vendorSoftwareProductManager.listComponents(vspId, null, user);
748 mibManager.upload(getFileInputStream(filePath),
750 VERSION01, componentEntities.get(0).getId(), ArtifactType.SNMP_POLL, user);
751 //TODO: add validate of addActionLog() func call
753 return new ImmutablePair<>(componentEntities.get(0).getId(),
754 componentEntities.get(0).getComponentCompositionData()
758 private void createPackageFromUpload(String vspId, String user, String filePath)
760 uploadFileAndProcess(vspId, user, filePath);
761 checkinSubmitCreatePackage(vspId, user);
764 private void uploadFileAndProcess(String vspId, String user, String filePath) {
765 vendorSoftwareProductManager.checkout(vspId, user);
766 candidateManager.upload(vspId, VERSION01, getFileInputStream(filePath), user);
767 candidateManager.process(vspId, VERSION01, user);
770 private void checkinSubmitCreatePackage(String vspId, String user) throws IOException {
771 vendorSoftwareProductManager.checkin(vspId, user);
772 ValidationResponse submitResponse = vendorSoftwareProductManager.submit(vspId, user);
773 Assert.assertTrue(submitResponse.isValid());
774 vendorSoftwareProductManager.createPackage(vspId, VERSION10, user);
777 // TODO: 3/15/2017 fix and enable
780 public void testUpdatedVSPShouldBeInBeginningOfList() {
781 vendorSoftwareProductManager.updateVsp(new VspDetails(), USER3);
782 assertVSPInWantedLocationInVSPList(id006, 0, USER3);
784 InputStream zis = getFileInputStream("/vspmanager/zips/fullComposition.zip");
785 candidateManager.upload(id007, VERSION01, zis, USER3);
786 candidateManager.process(id007, VERSION01, USER3);
787 assertVSPInWantedLocationInVSPList(id007, 0, USER3);
790 @Test(dependsOnMethods = {"testUpdatedVSPShouldBeInBeginningOfList"})
791 public void testVSPInBeginningOfListAfterCheckin() {
792 vendorSoftwareProductManager.checkin(id006, USER3);
793 assertVSPInWantedLocationInVSPList(id006, 0, USER3);
795 vendorSoftwareProductManager.checkin(id007, USER3);
796 assertVSPInWantedLocationInVSPList(id007, 0, USER3);
799 @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterCheckin"})
800 public void testVSPInBeginningOfListAfterCheckout() {
801 vendorSoftwareProductManager.checkout(id006, USER3);
802 assertVSPInWantedLocationInVSPList(id006, 0, USER3);
805 @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterCheckout"})
806 public void testVSPInBeginningOfListAfterUndoCheckout() {
807 vendorSoftwareProductManager.checkout(id007, USER3);
808 assertVSPInWantedLocationInVSPList(id007, 0, USER3);
810 vendorSoftwareProductManager.undoCheckout(id006, USER3);
811 assertVSPInWantedLocationInVSPList(id006, 0, USER3);
814 @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterUndoCheckout"})
815 public void testVSPInBeginningOfListAfterSubmit() throws IOException {
816 vendorSoftwareProductManager.checkin(id007, USER3);
817 vendorSoftwareProductManager.submit(id007, USER3);
819 assertVSPInWantedLocationInVSPList(id007, 0, USER3);
823 private void testLegalUpload(String vspId, Version version, InputStream upload, String user) {
824 candidateManager.upload(vspId, VERSION01, upload, USER1);
825 candidateManager.process(vspId, VERSION01, user);
827 UploadDataEntity uploadData =
828 orchestrationTemplateDataDaoMock.getOrchestrationTemplate(vspId, version);
829 Assert.assertNotNull(uploadData);
832 private void addCapability(String entryValueKey, Map<String, CapabilityDefinition> capabilities,
833 String key, CapabilityDefinition value) {
835 capabilities.put(entryValueKey + "_" + key, value);
838 public InputStream getFileInputStream(String fileName) {
839 URL url = this.getClass().getResource(fileName);
841 return url.openStream();
842 } catch (IOException exception) {
843 exception.printStackTrace();
848 private void assertVSPInWantedLocationInVSPList(String vspId, int location, String user) {
849 List<VersionedVendorSoftwareProductInfo> vspList =
850 vendorSoftwareProductManager.listVsps(null, user);
851 Assert.assertEquals(vspList.get(location).getVspDetails().getId(), vspId);
855 // private void assertInfoArtifactIsInRightPathInCsar(String vspId, String zipFileName)
856 // throws IOException {
857 // ZipInputStream inputZipStream = new ZipInputStream(new FileInputStream(new File(zipFileName)));
858 // boolean isInfoArtifactInZip = false;
860 // ZipEntry zipEntry;
861 // while ((zipEntry = inputZipStream.getNextEntry()) != null) {
862 // String currentEntryName = zipEntry.getName();
863 // if(currentEntryName.equals("Artifacts\\Informative\\Guide\\VSP_" +
864 // vspId + "_Information.txt")){
865 // isInfoArtifactInZip = true;
870 // Assert.assertTrue(isInfoArtifactInZip);
872 static VspDetails createVspDetails(String id, Version version, String name, String desc,
873 String vendorName, String vlm, String icon,
874 String category, String subCategory,
875 String licenseAgreement, List<String> featureGroups) {
876 VspDetails vspDetails = new VspDetails(id, version);
877 vspDetails.setName(name);
878 vspDetails.setDescription(desc);
879 vspDetails.setIcon(icon);
880 vspDetails.setCategory(category);
881 vspDetails.setSubCategory(subCategory);
882 vspDetails.setVendorName(vendorName);
883 vspDetails.setVendorId(vlm);
884 vspDetails.setVlmVersion(new Version(1, 0));
885 vspDetails.setLicenseAgreement(licenseAgreement);
886 vspDetails.setFeatureGroups(featureGroups);
890 static void assertVspsEquals(VspDetails actual, VspDetails expected) {
891 Assert.assertEquals(actual.getId(), expected.getId());
892 Assert.assertEquals(actual.getVersion(), expected.getVersion());
893 Assert.assertEquals(actual.getName(), expected.getName());
894 Assert.assertEquals(actual.getDescription(), expected.getDescription());
895 Assert.assertEquals(actual.getIcon(), expected.getIcon());
896 Assert.assertEquals(actual.getCategory(), expected.getCategory());
897 Assert.assertEquals(actual.getSubCategory(), expected.getSubCategory());
898 Assert.assertEquals(actual.getVendorName(), expected.getVendorName());
899 Assert.assertEquals(actual.getVendorId(), expected.getVendorId());
900 Assert.assertEquals(actual.getLicenseAgreement(), expected.getLicenseAgreement());
901 Assert.assertEquals(actual.getFeatureGroups(), expected.getFeatureGroups());
906 // public void testDownloadFile() throws IOException {
907 // VspDetails expectedVsp = VSPCommon.createVspDetails(null, null, String.format("VSP-test-%s", vlm1Id), "Test-vsp", "vendorName", "vlm1Id", "icon", "category", "subCategory", "123", null);
908 // VspDetails createdVsp = vendorSoftwareProductManager.createVsp(expectedVsp, USER1);
910 // id005 = createdVsp.getId();
911 // Assert.assertNotNull(id005);
912 // Assert.assertNotNull(createdVsp.getVersion());
914 // try (InputStream zipInputStream = new ZipFileUtils().getZipInputStream("/legalUploadWithWarning")) {
916 // UploadFileResponse uploadFileResponse = vendorSoftwareProductManager.upload(id005, zipInputStream, USER1);
917 // vendorSoftwareProductManager.process(id005, USER1);
918 // Optional<File> fileCandidate = vendorSoftwareProductManager.get(id005, USER1);
920 // File latestHeatPackage = fileCandidate.get();
922 // zipInputStream.reset();
923 // byte[] uploaded = IOUtils.toByteArray(zipInputStream);
925 // Optional<FileContentHandler> zipContentMap = vendorSoftwareProductManager.getZipContentMap(uploadFileResponse, uploaded);
926 // FileContentHandler fileContentHandler = new FileContentHandler();
927 // if(zipContentMap.isPresent()){
928 // fileContentHandler = zipContentMap.get();
931 // uploaded = IOUtils.toByteArray(fileContentHandler.getFiles().values());
933 // byte[] downloaded;
934 // try (BufferedInputStream fileStream = new BufferedInputStream(new FileInputStream(latestHeatPackage))) {
935 // downloaded = IOUtils.toByteArray(fileStream);
938 // Assert.assertTrue(Arrays.equals(uploaded, downloaded));
942 // todo ********************** move to common **************************************
944 private void mockVersioning(VersionableEntityAction action) {
945 VersionInfo versionInfo = new VersionInfo();
946 versionInfo.setActiveVersion(VERSION01);
947 doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
948 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
952 private void mockVersioningEntityNotExist(VersionableEntityAction action, String vspId) {
953 doThrow(new CoreException(new EntityNotExistErrorBuilder(
954 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, vspId).build()))
955 .when(versioningManagerMock).getEntityVersionInfo(
956 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, vspId, USER1,
960 private void MockVersioningEntityLocked(VersionableEntityAction action) {
961 doThrow(new CoreException(new EditOnEntityLockedByOtherErrorBuilder(
962 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1)
964 .when(versioningManagerMock).getEntityVersionInfo(
965 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER2,