e0e00f8d9dc342c812bc36d55bbeb1b2e70e5061
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
22
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Captor;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.mockito.Spy;
29 import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
30 import org.openecomp.core.factory.impl.AbstractFactoryBase;
31 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
32 import org.openecomp.core.model.dao.ServiceModelDao;
33 import org.openecomp.core.model.types.ServiceElement;
34 import org.openecomp.core.utilities.file.FileContentHandler;
35 import org.openecomp.core.utilities.file.FileUtils;
36 import org.openecomp.sdc.activityLog.ActivityLogManager;
37 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
38 import org.openecomp.sdc.common.errors.CoreException;
39 import org.openecomp.sdc.common.errors.ErrorCategory;
40 import org.openecomp.sdc.common.errors.ErrorCode;
41 import org.openecomp.sdc.common.errors.Messages;
42 import org.openecomp.sdc.healing.api.HealingManager;
43 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
44 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
45 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
46 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
47 import org.openecomp.sdc.vendorsoftwareproduct.ManualVspToscaManager;
48 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManager;
49 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
50 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
51 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
52 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
53 import org.openecomp.sdc.vendorsoftwareproduct.dao.PackageInfoDao;
54 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
55 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
56 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
57 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo;
58 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
59 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
60 import org.openecomp.sdc.vendorsoftwareproduct.impl.mock.EnrichmentManagerFactoryImpl;
61 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
62 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
63 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
64 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
65 import org.openecomp.sdc.vendorsoftwareproduct.types.VersionedVendorSoftwareProductInfo;
66 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
67 import org.openecomp.sdc.versioning.VersioningManager;
68 import org.openecomp.sdc.versioning.dao.types.Version;
69 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
70 import org.openecomp.sdc.versioning.errors.EditOnEntityLockedByOtherErrorBuilder;
71 import org.openecomp.sdc.versioning.errors.EntityNotExistErrorBuilder;
72 import org.openecomp.sdc.versioning.types.VersionInfo;
73 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
74 import org.testng.Assert;
75 import org.testng.annotations.BeforeMethod;
76 import org.testng.annotations.Test;
77
78 import java.io.File;
79 import java.io.FileInputStream;
80 import java.io.IOException;
81 import java.io.InputStream;
82 import java.net.URL;
83 import java.nio.ByteBuffer;
84 import java.util.ArrayList;
85 import java.util.Arrays;
86 import java.util.Collections;
87 import java.util.HashMap;
88 import java.util.List;
89 import java.util.Map;
90 import java.util.zip.ZipEntry;
91 import java.util.zip.ZipInputStream;
92
93 import static org.mockito.Matchers.any;
94 import static org.mockito.Matchers.anyObject;
95 import static org.mockito.Matchers.eq;
96 import static org.mockito.Mockito.doNothing;
97 import static org.mockito.Mockito.doReturn;
98 import static org.mockito.Mockito.doThrow;
99 import static org.mockito.Mockito.never;
100 import static org.mockito.Mockito.verify;
101
102
103 public class VendorSoftwareProductManagerImplTest {
104   private static final String INVALID_VERSION_MSG = "Invalid requested version.";
105
106   private static String VSP_ID = "vspId";
107   private static String VERSION_ID = "versionId";
108   public static final Version VERSION01 = new Version(0, 1);
109   private static final Version VERSION10 = new Version(1, 0);
110   private static final String USER1 = "vspTestUser1";
111   private static final String USER2 = "vspTestUser2";
112   private static final String USER3 = "vspTestUser3";
113   private static String id006 = null;
114   private static String id007 = null;
115
116   @Mock
117   private VersioningManager versioningManagerMock;
118   @Mock
119   private VendorSoftwareProductDao vendorSoftwareProductDaoMock; // todo get rid of
120   @Mock
121   private OrchestrationTemplateDao orchestrationTemplateDataDaoMock;
122   @Mock
123   private VendorLicenseFacade vendorLicenseFacadeMock;
124   @Mock
125   private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDaoMock;
126   @Mock
127   private EnrichedServiceModelDao<ToscaServiceModel, ServiceElement> enrichedServiceModelDaoMock;
128   @Mock
129   private HealingManager healingManagerMock;
130   @Mock
131   private VendorLicenseArtifactsService licenseArtifactsServiceMock;
132   @Mock
133   private CompositionEntityDataManager compositionEntityDataManagerMock;
134   @Mock
135   private InformationArtifactGenerator informationArtifactGeneratorMock;
136   @Mock
137   private ActivityLogManager activityLogManagerMock;
138   @Mock
139   private PackageInfoDao packageInfoDao;
140   @Mock
141   private VendorSoftwareProductInfoDao vspInfoDaoMock;
142   @Mock
143   private ManualVspToscaManager manualVspToscaManager;
144   @Mock
145   private DeploymentFlavorDao deploymentFlavorDaoMock;
146
147
148   @Spy
149   @InjectMocks
150   private VendorSoftwareProductManagerImpl vendorSoftwareProductManager;
151
152   private OrchestrationTemplateCandidateManager candidateManager;
153   private MonitoringUploadsManager monitoringUploadsManager;
154
155   @Captor
156   private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
157
158   @BeforeMethod
159   public void setUp() throws Exception {
160     MockitoAnnotations.initMocks(this);
161   }
162
163   @Test
164   public void testListWhenNone() {
165     doReturn(new HashMap<>()).when(versioningManagerMock).listEntitiesVersionInfo
166             (VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, USER1,
167                     VersionableEntityAction.Read);
168     List<VersionedVendorSoftwareProductInfo> vsps =
169             vendorSoftwareProductManager.listVsps(null, USER1);
170     Assert.assertEquals(vsps.size(), 0);
171   }
172
173   @Test
174   public void testList() {
175     String vsp1id = "vsp1_id";
176     String vsp2id = "vsp2_id";
177     Map<String, VersionInfo> vspsTobeReturned = new HashMap<>();
178
179     VersionInfo versionInfo1 = new VersionInfo();
180     versionInfo1.setActiveVersion(VERSION01);
181     vspsTobeReturned.put(vsp1id, versionInfo1);
182
183     VersionInfo versionInfo2 = new VersionInfo();
184     versionInfo2.setActiveVersion(VERSION10);
185     vspsTobeReturned.put(vsp2id, versionInfo2);
186
187     doReturn(vspsTobeReturned).when(versioningManagerMock).listEntitiesVersionInfo
188             (VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, USER1,
189                     VersionableEntityAction.Read);
190
191     VspDetails vsp1 = new VspDetails(vsp1id, VERSION01);
192     vsp1.setWritetimeMicroSeconds(8L);
193     doReturn(vsp1).when(vspInfoDaoMock)
194             .get(any(VspDetails.class));
195
196     List<VersionedVendorSoftwareProductInfo> vsps =
197             vendorSoftwareProductManager.listVsps(null, USER1);
198     Assert.assertEquals(vsps.size(), 2);
199   }
200
201   @Test
202   public void testListFinalsWhenNone() {
203     String vsp1id = "vsp1_id";
204     String vsp2id = "vsp2_id";
205     Map<String, VersionInfo> vspsTobeReturned = new HashMap<>();
206
207     VersionInfo versionInfo1 = new VersionInfo();
208     versionInfo1.setActiveVersion(VERSION01);
209     vspsTobeReturned.put(vsp1id, versionInfo1);
210
211     VersionInfo versionInfo2 = new VersionInfo();
212     versionInfo2.setActiveVersion(VERSION10);
213     vspsTobeReturned.put(vsp2id, versionInfo2);
214
215     doReturn(vspsTobeReturned).when(versioningManagerMock).listEntitiesVersionInfo
216             (VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, USER1,
217                     VersionableEntityAction.Read);
218
219     List<VersionedVendorSoftwareProductInfo> vsps =
220             vendorSoftwareProductManager.listVsps(VersionStatus.Final.name(), USER1);
221     Assert.assertEquals(vsps.size(), 0);
222   }
223
224   @Test
225   public void testListFinals() {
226     String vsp1id = "vsp1_id";
227     String vsp2id = "vsp2_id";
228     Map<String, VersionInfo> vspsTobeReturned = new HashMap<>();
229
230     VersionInfo versionInfo1 = new VersionInfo();
231     versionInfo1.setActiveVersion(VERSION01);
232     vspsTobeReturned.put(vsp1id, versionInfo1);
233
234     VersionInfo versionInfo2 = new VersionInfo();
235     versionInfo2.setActiveVersion(new Version(1, 3));
236     versionInfo2.setLatestFinalVersion(VERSION10);
237     vspsTobeReturned.put(vsp2id, versionInfo2);
238
239     doReturn(vspsTobeReturned).when(versioningManagerMock).listEntitiesVersionInfo
240             (VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, USER1,
241                     VersionableEntityAction.Read);
242
243     VspDetails vsp2 = new VspDetails(vsp2id, VERSION10);
244     vsp2.setWritetimeMicroSeconds(8L);
245     doReturn(vsp2).when(vspInfoDaoMock)
246             .get(any(VspDetails.class));
247
248     List<VersionedVendorSoftwareProductInfo> vsps =
249             vendorSoftwareProductManager.listVsps(VersionStatus.Final.name(), USER1);
250     Assert.assertEquals(vsps.size(), 1);
251   }
252
253   @Test(expectedExceptions = CoreException.class)
254   public void testCreateWithExistingName_negative() {
255     doThrow(new CoreException(
256             new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).build()))
257             .when(vendorSoftwareProductManager).validateUniqueName("Vsp1");
258     VspDetails expectedVsp =
259             createVspDetails(null, null, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
260                     "category", "subCategory", "123", null);
261
262     vendorSoftwareProductManager.createVsp(expectedVsp, USER1);
263   }
264
265   @Test
266   public void testCreate() {
267     doNothing().when(vendorSoftwareProductManager).validateUniqueName("Vsp1");
268     doNothing().when(vendorSoftwareProductManager).createUniqueName("Vsp1");
269     doReturn(VERSION01).when(versioningManagerMock).create(anyObject(), anyObject(), anyObject());
270     doReturn("{}")
271             .when(vendorSoftwareProductManager).getVspQuestionnaireSchema(anyObject());
272
273     VspDetails vspToCreate =
274             createVspDetails(null, null, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
275                     "category", "subCategory", "123", null);
276
277     VspDetails vsp = vendorSoftwareProductManager.createVsp(vspToCreate, USER1);
278
279     Assert.assertNotNull(vsp);
280     vspToCreate.setId(vsp.getId());
281     vspToCreate.setVersion(VERSION01);
282     assertVspsEquals(vsp, vspToCreate);
283     verify(activityLogManagerMock).addActionLog(activityLogEntityArg.capture(), eq(USER1));
284     ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
285     Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION01.getMajor() + 1));
286     Assert.assertTrue(activityLogEntity.isSuccess());
287   }
288
289   @Test(expectedExceptions = CoreException.class)
290   public void testUpdateWithExistingName_negative() {
291     VersionInfo versionInfo = new VersionInfo();
292     versionInfo.setActiveVersion(VERSION01);
293     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
294             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
295             VersionableEntityAction.Write);
296
297     VspDetails existingVsp =
298             createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-existingVsp", "vendorName", "vlm1Id",
299                     "icon", "category", "subCategory", "123", null);
300     VspDetails updatedVsp =
301             createVspDetails(VSP_ID, VERSION01, "Vsp1_updated", "Test-existingVsp", "vendorName",
302                     "vlm1Id", "icon", "category", "subCategory", "123", null);
303     doReturn(existingVsp).when(vspInfoDaoMock)
304             .get(any(VspDetails.class));
305     doThrow(new CoreException(
306             new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).build()))
307             .when(vendorSoftwareProductManager)
308             .updateUniqueName(existingVsp.getName(), updatedVsp.getName());
309
310     vendorSoftwareProductManager.updateVsp(updatedVsp, USER1);
311   }
312
313   @Test
314   public void testUpdate() {
315     VersionInfo versionInfo = new VersionInfo();
316     versionInfo.setActiveVersion(VERSION01);
317     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
318             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
319             VersionableEntityAction.Write);
320     VspDetails existingVsp =
321             createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
322                     "category",
323                     "subCategory", "456", null);
324     VspDetails updatedVsp =
325             createVspDetails(VSP_ID, VERSION01, "VSP1_updated", null, "vendorName", "vlm1Id", "icon",
326                     "category_updated",
327                     "subCategory", "456", null);
328     existingVsp.setWritetimeMicroSeconds(8L);
329     doReturn(existingVsp).when(vspInfoDaoMock)
330             .get(any(VspDetails.class));
331     doNothing().when(vendorSoftwareProductManager)
332             .updateUniqueName(existingVsp.getName(), updatedVsp.getName());
333
334     vendorSoftwareProductManager.updateVsp(updatedVsp, USER1);
335
336     verify(vspInfoDaoMock).update(updatedVsp);
337   }
338
339   @Test
340   public void testUpdateRemoveFG() {
341     VersionInfo versionInfo = new VersionInfo();
342     versionInfo.setActiveVersion(VERSION01);
343     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
344             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
345             VersionableEntityAction.Write);
346     List<String> fgs = new ArrayList<String>();
347     fgs.add("fg1"); fgs.add("fg2");
348     VspDetails existingVsp =
349             createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
350                     "category",
351                     "subCategory", "456", fgs);
352
353     List<String> updFgs = new ArrayList<String>();
354     updFgs.add("fg2");
355     VspDetails updatedVsp =
356             createVspDetails(VSP_ID, VERSION01, "VSP1_updated", null, "vendorName", "vlm1Id", "icon",
357                     "category_updated",
358                     "subCategory", "456", updFgs);
359     existingVsp.setWritetimeMicroSeconds(8L);
360     doReturn(existingVsp).when(vspInfoDaoMock)
361             .get(any(VspDetails.class));
362     doNothing().when(vendorSoftwareProductManager)
363             .updateUniqueName(existingVsp.getName(), updatedVsp.getName());
364
365     DeploymentFlavorEntity dfEntity = new DeploymentFlavorEntity(VSP_ID,VERSION01,"DF_ID");
366     DeploymentFlavor flavor = new DeploymentFlavor();
367     flavor.setFeatureGroupId("fg1");
368     dfEntity.setDeploymentFlavorCompositionData(flavor);
369
370     List<DeploymentFlavorEntity> dfList = new ArrayList<DeploymentFlavorEntity>();
371     dfList.add(dfEntity);
372
373     doReturn(dfList).when(deploymentFlavorDaoMock).list(anyObject());
374
375     vendorSoftwareProductManager.updateVsp(updatedVsp, USER1);
376
377     verify(vendorSoftwareProductDaoMock).updateDeploymentFlavor(dfEntity);
378
379     Assert.assertNull(dfEntity.getDeploymentFlavorCompositionData().getFeatureGroupId());
380
381   }
382
383   @Test(expectedExceptions = CoreException.class)
384   public void testGetNonExistingVersion_negative() {
385     Version notExistversion = new Version(43, 8);
386     doReturn(null).when(vspInfoDaoMock).get(any(VspDetails.class));
387     vendorSoftwareProductManager.getVsp(VSP_ID, notExistversion, USER1);
388   }
389
390   @Test
391   public void testGetCheckedOutVersion() {
392     VersionInfo versionInfo = new VersionInfo();
393     versionInfo.setActiveVersion(VERSION01);
394     versionInfo.setStatus(VersionStatus.Locked);
395     versionInfo.setLockingUser(USER1);
396     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
397             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
398             VersionableEntityAction.Read);
399
400     VspDetails existingVsp =
401             createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
402                     "category",
403                     "subCategory", "456", null);
404     existingVsp.setWritetimeMicroSeconds(8L);
405     doReturn(existingVsp).when(vspInfoDaoMock).get(any(VspDetails.class));
406
407     VspDetails actualVsp =
408             vendorSoftwareProductManager.getVsp(VSP_ID, VERSION01, USER1);
409
410     assertVspsEquals(actualVsp, existingVsp);
411   }
412
413   @Test
414   public void testGetOldVersion() {
415     VersionInfo versionInfo = new VersionInfo();
416     versionInfo.setActiveVersion(new Version(0, 2));
417     versionInfo.setViewableVersions(Arrays.asList(VERSION01, new Version(0, 2)));
418     versionInfo.setStatus(VersionStatus.Locked);
419     versionInfo.setLockingUser(USER2);
420     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
421             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
422             VersionableEntityAction.Read);
423
424     VspDetails existingVsp =
425             createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
426                     "category",
427                     "subCategory", "456", null);
428     existingVsp.setWritetimeMicroSeconds(8L);
429     doReturn(existingVsp)
430             .when(vspInfoDaoMock).get(any(VspDetails.class));
431
432     VspDetails actualVsp =
433             vendorSoftwareProductManager.getVsp(VSP_ID, VERSION01, USER1);
434
435     VspDetails expectedVsp =
436             vspInfoDaoMock
437                     .get(new VspDetails(VSP_ID, VERSION01));
438     assertVspsEquals(actualVsp, expectedVsp);
439   }
440
441   @Test
442   public void testCheckin() {
443     doReturn(VERSION01).when(versioningManagerMock)
444             .checkin(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
445                     USER1, null);
446     Version version = vendorSoftwareProductManager.checkin(VSP_ID, USER1);
447
448     Assert.assertEquals(version, VERSION01);
449     verify(versioningManagerMock)
450             .checkin(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
451                     USER1, null);
452     verify(activityLogManagerMock).addActionLog(activityLogEntityArg.capture(), eq(USER1));
453     ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
454     Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION01.getMajor() + 1));
455     Assert.assertTrue(activityLogEntity.isSuccess());
456   }
457
458   @Test
459   public void testCheckout() {
460     doReturn(VERSION01).when(versioningManagerMock)
461             .checkout(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
462                     USER1);
463     Version version = vendorSoftwareProductManager.checkout(VSP_ID, USER1);
464
465     Assert.assertEquals(version, VERSION01);
466     verify(versioningManagerMock)
467             .checkout(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
468                     USER1);
469
470     verify(activityLogManagerMock).addActionLog(activityLogEntityArg.capture(), eq(USER1));
471     ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
472     Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION01.getMajor() + 1));
473     Assert.assertTrue(activityLogEntity.isSuccess());
474   }
475
476
477   @Test
478   public void testUndoCheckout() {
479     Version existingVersion = new Version(0, 2);
480     VersionInfo versionInfo = new VersionInfo();
481     versionInfo.setActiveVersion(existingVersion);
482     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
483             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
484             VSP_ID, USER1, VersionableEntityAction.Read);
485
486     doReturn(VERSION01).when(versioningManagerMock).undoCheckout(VendorSoftwareProductConstants
487             .VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1);
488
489     VspDetails vsp = new VspDetails(VSP_ID, existingVersion);
490     vsp.setName("ExistingName");
491     doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
492     doNothing().when(vendorSoftwareProductManager).updateUniqueName(vsp.getName(), vsp.getName());
493
494     Version undoCheckoutVersion = vendorSoftwareProductManager.undoCheckout(VSP_ID, USER1);
495
496     Assert.assertEquals(undoCheckoutVersion, VERSION01);
497   }
498
499 /*
500   @Test
501   public void testSubmitWithMissingData() throws IOException {
502     VersionInfo versionInfo = new VersionInfo();
503     versionInfo.setActiveVersion(VERSION01);
504
505     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
506         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
507         VSP_ID, USER1, VersionableEntityAction.Read);
508
509     VspDetails vsp = new VspDetails(VSP_ID, VERSION01);
510     vsp.setOnboardingMethod("Manual");
511     doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
512
513     VspQuestionnaireEntity vspQuestionnaire = new VspQuestionnaireEntity(VSP_ID, VERSION01);
514     vspQuestionnaire.setQuestionnaireData("{}");
515     doReturn(vspQuestionnaire).when(vspInfoDaoMock).getQuestionnaire(VSP_ID, VERSION01);
516
517     ComponentEntity comp1 = new ComponentEntity(VSP_ID, VERSION01, "comp1");
518     comp1.setQuestionnaireData("{}");
519     doReturn(Collections.singleton(comp1)).when(vendorSoftwareProductDaoMock)
520         .listComponentsCompositionAndQuestionnaire(VSP_ID, VERSION01);
521
522     NicEntity nic1 = new NicEntity(VSP_ID, VERSION01, "comp1", "nic1");
523     nic1.setQuestionnaireData("{}");
524     doReturn(Collections.singleton(nic1))
525         .when(vendorSoftwareProductDaoMock).listNicsByVsp(VSP_ID, VERSION01);
526
527     ValidationResponse validationResponse = vendorSoftwareProductManager.submit(VSP_ID, USER1);
528     Assert.assertNotNull(validationResponse);
529     Assert.assertFalse(validationResponse.isValid());
530     List<String> errorIds = validationResponse.getVspErrors().stream().map(ErrorCode::id).distinct()
531         .collect(Collectors.toList());
532     Assert.assertTrue(errorIds.contains(ValidationErrorBuilder.FIELD_VALIDATION_ERROR_ERR_ID));
533     Assert.assertTrue(errorIds.contains(VendorSoftwareProductErrorCodes.VSP_INVALID));
534
535     verify(versioningManagerMock, never())
536         .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
537             USER1, null);
538     verify(activityLogManagerMock, never()).addActionLog(any(ActivityLogEntity.class), eq(USER1));
539   }
540
541   */
542
543   // TODO: 3/15/2017 fix and enable
544   //@Test
545   public void testSubmitWithInvalidLicensingData() throws IOException {
546     VersionInfo versionInfo = new VersionInfo();
547     versionInfo.setActiveVersion(VERSION01);
548     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
549             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE,
550             VSP_ID, USER1, VersionableEntityAction.Read);
551
552     VspDetails vsp =
553             createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
554                     "category", "subCategory", "licenseAgreementId",
555                     Collections.singletonList("featureGroupId"));
556     doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
557     UploadDataEntity uploadData = new UploadDataEntity(VSP_ID, VERSION01);
558     uploadData.setContentData(
559             ByteBuffer.wrap(FileUtils.toByteArray(getFileInputStream("/emptyComposition"))));
560     doReturn(uploadData).when(orchestrationTemplateDataDaoMock)
561             .getOrchestrationTemplate(anyObject(), anyObject());
562     doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(),
563             "MainServiceTemplate.yaml"))
564             .when(serviceModelDaoMock).getServiceModel(VSP_ID, VERSION01);
565
566     ValidationResponse validationResponse = vendorSoftwareProductManager.submit(VSP_ID, USER1);
567     Assert.assertNotNull(validationResponse);
568     Assert.assertFalse(validationResponse.isValid());
569     Assert.assertNull(validationResponse.getVspErrors());
570     Assert.assertEquals(validationResponse.getLicensingDataErrors(), 1);
571
572     verify(versioningManagerMock, never())
573             .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
574                     USER1, null);
575
576     //TODO - check..
577     verify(activityLogManagerMock, never()).addActionLog(any(ActivityLogEntity.class), eq(USER1));
578   }
579
580   // TODO: 3/15/2017 fix and enable
581   //@Test
582   public void testSubmit() throws IOException {
583     mockVersioning(VersionableEntityAction.Read);
584
585     EnrichmentManagerFactory.getInstance();
586     AbstractFactoryBase
587             .registerFactory(EnrichmentManagerFactory.class, EnrichmentManagerFactoryImpl.class);
588
589     VspDetails vsp =
590             createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
591                     "category", "subCategory", "123", Collections.singletonList("fg1"));
592     doReturn(vsp).when(vspInfoDaoMock).get(anyObject());
593     UploadDataEntity uploadData = new UploadDataEntity(VSP_ID, VERSION01);
594     uploadData.setContentData(
595             ByteBuffer.wrap(FileUtils.toByteArray(getFileInputStream("/emptyComposition"))));
596     doReturn(uploadData).when(orchestrationTemplateDataDaoMock)
597             .getOrchestrationTemplate(anyObject(), anyObject());
598     doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(),
599             "MainServiceTemplate.yaml"))
600             .when(serviceModelDaoMock).getServiceModel(VSP_ID, VERSION01);
601
602     ValidationResponse validationResponse = vendorSoftwareProductManager.submit(VSP_ID, USER1);
603     Assert.assertTrue(validationResponse.isValid());
604
605 /*    Assert.assertEquals(vsp2.getVersionInfo().getActiveVersion(), VERSION10);
606     Assert.assertEquals(vsp2.getVersionInfo().getStatus(), VersionStatus.Final);
607     Assert.assertNull(vsp2.getVersionInfo().getLockingUser());*/
608
609     verify(versioningManagerMock)
610             .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID,
611                     USER1, null);
612     verify(activityLogManagerMock).addActionLog(activityLogEntityArg.capture(), eq(USER1));
613     ActivityLogEntity activityLogEntity = activityLogEntityArg.getValue();
614     Assert.assertEquals(activityLogEntity.getVersionId(), String.valueOf(VERSION10.getMajor()));
615     Assert.assertTrue(activityLogEntity.isSuccess());
616   }
617
618   @Test(expectedExceptions = CoreException.class)
619   public void testCreatePackageOnNonFinalVersion_negative() throws IOException {
620     vendorSoftwareProductManager.createPackage(VSP_ID, VERSION01, USER1);
621   }
622
623   @Test
624   public void testCreatePackage() throws IOException {
625     /*VspDetails vspDetailsMock = new VspDetails("vspId", new Version(1, 0));
626     doReturn(vspDetailsMock).when(vspInfoDaoMock).get(anyObject());*/
627     VersionInfo versionInfo = new VersionInfo();
628     versionInfo.setActiveVersion(VERSION10);
629     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
630             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
631             VersionableEntityAction.Read);
632
633     doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(), "")).when
634             (enrichedServiceModelDaoMock).getServiceModel(VSP_ID, VERSION10);
635
636     VspDetails vsp = new VspDetails(VSP_ID, VERSION10);
637     vsp.setVendorId("vendorId");
638     vsp.setVlmVersion(VERSION10);
639     vsp.setFeatureGroups(Arrays.asList("fg1", "fg2"));
640     doReturn(vsp).when(vspInfoDaoMock).get(any(VspDetails.class));
641
642     doReturn(new FileContentHandler()).when(licenseArtifactsServiceMock)
643             .createLicenseArtifacts(VSP_ID, vsp.getVendorId(), VERSION10, vsp.getFeatureGroups(),
644                     USER1);
645
646     PackageInfo packageInfo = vendorSoftwareProductManager.createPackage(VSP_ID, VERSION10, USER1);
647     Assert.assertNotNull(packageInfo.getVspId());
648   }
649
650   // TODO: 3/15/2017 fix and enable
651   //@Test(dependsOnMethods = {"testListFinals"})
652   public void testUploadFileMissingFile() {
653     InputStream zis = getFileInputStream("/vspmanager/zips/missingYml.zip");
654
655     UploadFileResponse uploadFileResponse =
656             candidateManager.upload(VSP_ID, VERSION01, zis, USER1, "zip", "missingYml");
657
658     Assert.assertEquals(uploadFileResponse.getErrors().size(), 0);
659   }
660
661   // TODO: 3/15/2017 fix and enable
662   //@Test(dependsOnMethods = {"testUploadFileMissingFile"})
663   public void testUploadNotZipFile() throws IOException {
664     URL url = this.getClass().getResource("/notZipFile");
665
666     try {
667       candidateManager
668               .upload(VSP_ID, VERSION01,
669                       url.openStream(), USER1, "zip", "notZipFile");
670       candidateManager.process(VSP_ID, VERSION01, USER1);
671     } catch (Exception ce) {
672       Assert.assertEquals(ce.getMessage(), Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());
673     }
674
675     verify(activityLogManagerMock, never()).addActionLog(any(ActivityLogEntity.class), eq(USER1));
676   }
677
678
679   private List<String> getWantedFileNamesFromCsar(String pathInCsar)
680           throws IOException {
681     File translatedFile = vendorSoftwareProductManager.getTranslatedFile(VSP_ID, VERSION10, USER1);
682
683     return getFileNamesFromFolderInCsar(translatedFile,
684             pathInCsar);
685   }
686
687   private List<String> getFileNamesFromFolderInCsar(File csar, String folderName)
688           throws IOException {
689     List<String> fileNames = new ArrayList<>();
690
691     ZipInputStream zip = new ZipInputStream(new FileInputStream(csar));
692     ZipEntry ze;
693
694     while ((ze = zip.getNextEntry()) != null) {
695       String name = ze.getName();
696       if (name.contains(folderName)) {
697         fileNames.add(name);
698       }
699     }
700
701     return fileNames;
702   }
703
704   private void createPackageFromUpload(String vspId, String user, String filePath)
705           throws IOException {
706     uploadFileAndProcess(vspId, user, filePath);
707     checkinSubmitCreatePackage(vspId, user);
708   }
709
710   private void uploadFileAndProcess(String vspId, String user, String filePath) {
711     vendorSoftwareProductManager.checkout(vspId, user);
712     candidateManager.upload(vspId, VERSION01, getFileInputStream(filePath), user, "zip", "file");
713     candidateManager.process(vspId, VERSION01, user);
714   }
715
716   private void checkinSubmitCreatePackage(String vspId, String user) throws IOException {
717     vendorSoftwareProductManager.checkin(vspId, user);
718     ValidationResponse submitResponse = vendorSoftwareProductManager.submit(vspId, user);
719     Assert.assertTrue(submitResponse.isValid());
720     vendorSoftwareProductManager.createPackage(vspId, VERSION10, user);
721   }
722
723   // TODO: 3/15/2017 fix and enable
724 /*
725
726   public void testUpdatedVSPShouldBeInBeginningOfList() {
727     vendorSoftwareProductManager.updateVsp(new VspDetails(), USER3);
728     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
729
730     InputStream zis = getFileInputStream("/vspmanager/zips/fullComposition.zip");
731     candidateManager.upload(id007, VERSION01, zis, USER3);
732     candidateManager.process(id007, VERSION01, USER3);
733     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
734   }
735
736   @Test(dependsOnMethods = {"testUpdatedVSPShouldBeInBeginningOfList"})
737   public void testVSPInBeginningOfListAfterCheckin() {
738     vendorSoftwareProductManager.checkin(id006, USER3);
739     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
740
741     vendorSoftwareProductManager.checkin(id007, USER3);
742     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
743   }
744
745   @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterCheckin"})
746   public void testVSPInBeginningOfListAfterCheckout() {
747     vendorSoftwareProductManager.checkout(id006, USER3);
748     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
749   }
750
751   @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterCheckout"})
752   public void testVSPInBeginningOfListAfterUndoCheckout() {
753     vendorSoftwareProductManager.checkout(id007, USER3);
754     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
755
756     vendorSoftwareProductManager.undoCheckout(id006, USER3);
757     assertVSPInWantedLocationInVSPList(id006, 0, USER3);
758   }
759
760   @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterUndoCheckout"})
761   public void testVSPInBeginningOfListAfterSubmit() throws IOException {
762     vendorSoftwareProductManager.checkin(id007, USER3);
763     vendorSoftwareProductManager.submit(id007, USER3);
764
765     assertVSPInWantedLocationInVSPList(id007, 0, USER3);
766   }
767 */
768
769   private void testLegalUpload(String vspId, Version version, InputStream upload, String user) {
770     candidateManager.upload(vspId, VERSION01, upload, USER1, "zip", "file");
771     candidateManager.process(vspId, VERSION01, user);
772
773     UploadDataEntity uploadData =
774             orchestrationTemplateDataDaoMock.getOrchestrationTemplate(vspId, version);
775     Assert.assertNotNull(uploadData);
776   }
777
778   private void addCapability(String entryValueKey, Map<String, CapabilityDefinition> capabilities,
779                              String key, CapabilityDefinition value) {
780
781     capabilities.put(entryValueKey + "_" + key, value);
782   }
783
784   public InputStream getFileInputStream(String fileName) {
785     URL url = this.getClass().getResource(fileName);
786     try {
787       return url.openStream();
788     } catch (IOException exception) {
789       exception.printStackTrace();
790       return null;
791     }
792   }
793
794   private void assertVSPInWantedLocationInVSPList(String vspId, int location, String user) {
795     List<VersionedVendorSoftwareProductInfo> vspList =
796             vendorSoftwareProductManager.listVsps(null, user);
797     Assert.assertEquals(vspList.get(location).getVspDetails().getId(), vspId);
798   }
799
800
801   static VspDetails createVspDetails(String id, Version version, String name, String desc,
802                                      String vendorName, String vlm, String icon,
803                                      String category, String subCategory,
804                                      String licenseAgreement, List<String> featureGroups) {
805     VspDetails vspDetails = new VspDetails(id, version);
806     vspDetails.setName(name);
807     vspDetails.setDescription(desc);
808     vspDetails.setIcon(icon);
809     vspDetails.setCategory(category);
810     vspDetails.setSubCategory(subCategory);
811     vspDetails.setVendorName(vendorName);
812     vspDetails.setVendorId(vlm);
813     vspDetails.setVlmVersion(new Version(1, 0));
814     vspDetails.setLicenseAgreement(licenseAgreement);
815     vspDetails.setFeatureGroups(featureGroups);
816     vspDetails.setOnboardingMethod("HEAT");
817     return vspDetails;
818   }
819
820   static void assertVspsEquals(VspDetails actual, VspDetails expected) {
821     Assert.assertEquals(actual.getId(), expected.getId());
822     Assert.assertEquals(actual.getVersion(), expected.getVersion());
823     Assert.assertEquals(actual.getName(), expected.getName());
824     Assert.assertEquals(actual.getDescription(), expected.getDescription());
825     Assert.assertEquals(actual.getIcon(), expected.getIcon());
826     Assert.assertEquals(actual.getCategory(), expected.getCategory());
827     Assert.assertEquals(actual.getSubCategory(), expected.getSubCategory());
828     Assert.assertEquals(actual.getVendorName(), expected.getVendorName());
829     Assert.assertEquals(actual.getVendorId(), expected.getVendorId());
830     Assert.assertEquals(actual.getLicenseAgreement(), expected.getLicenseAgreement());
831     Assert.assertEquals(actual.getFeatureGroups(), expected.getFeatureGroups());
832   }
833
834  // todo ********************** move to common **************************************
835
836   private void mockVersioning(VersionableEntityAction action) {
837     VersionInfo versionInfo = new VersionInfo();
838     versionInfo.setActiveVersion(VERSION01);
839     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
840             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
841             action);
842   }
843
844   private void mockVersioningEntityNotExist(VersionableEntityAction action, String vspId) {
845     doThrow(new CoreException(new EntityNotExistErrorBuilder(
846             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, vspId).build()))
847             .when(versioningManagerMock).getEntityVersionInfo(
848             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, vspId, USER1,
849             action);
850   }
851
852   private void MockVersioningEntityLocked(VersionableEntityAction action) {
853     doThrow(new CoreException(new EditOnEntityLockedByOtherErrorBuilder(
854             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1)
855             .build()))
856             .when(versioningManagerMock).getEntityVersionInfo(
857             VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER2,
858             action);
859   }
860 }