Fix setting vendor release for VSP with model
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / VendorSoftwareProductManagerImplTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
18
19 import static org.hamcrest.CoreMatchers.containsString;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.equalTo;
22 import static org.hamcrest.Matchers.hasItem;
23 import static org.hamcrest.Matchers.not;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.ArgumentMatchers.nullable;
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.verify;
33 import static org.mockito.Mockito.when;
34 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME;
35 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME;
36 import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME;
37
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Optional;
46 import org.apache.commons.io.IOUtils;
47 import org.junit.After;
48 import org.junit.Assert;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.ArgumentCaptor;
52 import org.mockito.Captor;
53 import org.mockito.InjectMocks;
54 import org.mockito.Mock;
55 import org.mockito.MockitoAnnotations;
56 import org.mockito.Spy;
57 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
58 import org.onap.sdc.tosca.services.YamlUtil;
59 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
60 import org.openecomp.core.model.dao.ServiceModelDao;
61 import org.openecomp.core.utilities.file.FileContentHandler;
62 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
63 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
64 import org.openecomp.sdc.common.errors.CoreException;
65 import org.openecomp.sdc.common.errors.ErrorCategory;
66 import org.openecomp.sdc.common.errors.ErrorCode;
67 import org.openecomp.sdc.healing.api.HealingManager;
68 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
69 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
70 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
71 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
72 import org.openecomp.sdc.vendorsoftwareproduct.ManualVspToscaManager;
73 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManager;
74 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
75 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
76 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
77 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
78 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
79 import org.openecomp.sdc.vendorsoftwareproduct.dao.PackageInfoDao;
80 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
81 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
82 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
83 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo;
84 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
85 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
86 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
87 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
88 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
89 import org.openecomp.sdc.versioning.ActionVersioningManager;
90 import org.openecomp.sdc.versioning.dao.types.Version;
91 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
92 import org.openecomp.sdc.versioning.types.VersionInfo;
93 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
94
95 public class VendorSoftwareProductManagerImplTest {
96
97   private static final String VSP_ID = "vspId";
98   private static final Version VERSION01 = new Version("0, 1");
99   private static final Version VERSION10 = new Version("1, 0");
100   private static final String USER1 = "vspTestUser1";
101   private static final String USER2 = "vspTestUser2";
102
103   @Mock
104   private ActionVersioningManager versioningManagerMock;
105   @Mock
106   private OrchestrationTemplateDao orchestrationTemplateDataDaoMock;
107   @Mock
108   private VendorLicenseFacade vendorLicenseFacadeMock;
109   @Mock
110   private ServiceModelDao<ToscaServiceModel> serviceModelDaoMock;
111   @Mock
112   private EnrichedServiceModelDao<ToscaServiceModel> enrichedServiceModelDaoMock;
113   @Mock
114   private HealingManager healingManagerMock;
115   @Mock
116   private VendorLicenseArtifactsService licenseArtifactsServiceMock;
117   @Mock
118   private CompositionEntityDataManager compositionEntityDataManagerMock;
119   @Mock
120   private InformationArtifactGenerator informationArtifactGeneratorMock;
121   @Mock
122   private PackageInfoDao packageInfoDao;
123   @Mock
124   private VendorSoftwareProductInfoDao vspInfoDaoMock;
125   @Mock
126   private ManualVspToscaManager manualVspToscaManager;
127   @Mock
128   private DeploymentFlavorDao deploymentFlavorDaoMock;
129   @Spy
130   @InjectMocks
131   private VendorSoftwareProductManagerImpl vendorSoftwareProductManager;
132   @Mock
133   private OrchestrationTemplateCandidateManager orchestrationTemplateCandidateManagerMock;
134   @Mock
135   private ComponentDependencyModelDao componentDependencyModelDao;
136   private OrchestrationTemplateCandidateManager candidateManager;
137   private MonitoringUploadsManager monitoringUploadsManager;
138
139   @Captor
140   private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
141
142   @Before
143   public void setUp() {
144     MockitoAnnotations.initMocks(this);
145   }
146
147   @After
148   public void tearDown() {
149     vendorSoftwareProductManager = null;
150   }
151
152     @Test
153     public void testCreatePackageEtsiVNF() throws IOException {
154         try (
155             final InputStream metadataInput = getClass()
156                 .getResourceAsStream("/vspmanager.csar/metadata/ValidETSItosca.meta");
157             final InputStream manifestInput = getClass()
158                 .getResourceAsStream("/vspmanager.csar/manifest/ValidNonManoTosca.mf")) {
159
160             final FileContentHandler handler = new FileContentHandler();
161             final byte[] metadataInputBytes = IOUtils.toByteArray(metadataInput);
162             handler.addFile(TOSCA_META_PATH_FILE_NAME, metadataInputBytes);
163             handler.addFile(TOSCA_META_ORIG_PATH_FILE_NAME, metadataInputBytes);
164             handler.addFile(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, IOUtils.toByteArray(manifestInput));
165             final ToscaServiceModel toscaMetadata = new ToscaServiceModel(handler, new HashMap<>(), "");
166             when(enrichedServiceModelDaoMock.getServiceModel(any(), any())).thenReturn(toscaMetadata);
167             final VspDetails vsp =
168                 createVspDetails("0", new Version(), "Vsp_PNF", "Test-vsp-pnf", "vendorName", "esy", "icon",
169                     "category", "subCategory", "123", null);
170             //want to avoid triggering populateVersionsForVlm method
171             vsp.setVlmVersion(null);
172
173             when(vspInfoDaoMock.get(any())).thenReturn(vsp);
174             when(licenseArtifactsServiceMock.createLicenseArtifacts(any(), any(), any(), any()))
175                 .thenReturn(new FileContentHandler());
176             final PackageInfo packageInfo = vendorSoftwareProductManager.createPackage("0", new Version());
177             assertEquals(packageInfo.getResourceType(), ResourceTypeEnum.VF.name());
178         }
179     }
180
181   @Test(expected = IOException.class)
182   public void testCreatePackageEtsiNoManifest() throws IOException {
183     try(InputStream metadataInput = getClass().getResourceAsStream("/vspmanager.csar/metadata/ValidETSItosca.meta"))
184     {
185       FileContentHandler handler = new FileContentHandler();
186       handler.addFile(TOSCA_META_PATH_FILE_NAME, IOUtils.toByteArray(metadataInput));
187       ToscaServiceModel toscaMetadata = new ToscaServiceModel(handler, new HashMap<>(), "");
188       when(enrichedServiceModelDaoMock.getServiceModel(any(), any())).thenReturn(toscaMetadata );
189       VspDetails vsp =
190               createVspDetails("0", new Version(), "Vsp_PNF", "Test-vsp-pnf", "vendorName", "esy", "icon",
191                       "category", "subCategory", "123", null);
192       //want to avoid triggering populateVersionsForVlm method
193       vsp.setVlmVersion(null);
194
195       when(vspInfoDaoMock.get(any())).thenReturn(vsp);
196       when(licenseArtifactsServiceMock.createLicenseArtifacts(any(),any(), any(), any())).thenReturn(new FileContentHandler());
197       vendorSoftwareProductManager.createPackage("0", new Version());
198       fail();
199     }
200   }
201
202     @Test
203     public void testCreatePackageEtsiPnfWithoutNonMano() throws IOException {
204         try (
205             final InputStream metadataInput = getClass()
206                 .getResourceAsStream("/vspmanager.csar/metadata/ValidETSItosca.meta");
207             final InputStream manifestInput = getClass()
208                 .getResourceAsStream("/vspmanager.csar/manifest/ValidNonManoToscaPnfWithoutNonMano.mf")) {
209
210             final FileContentHandler handler = new FileContentHandler();
211             final byte[] metadataInputBytes = IOUtils.toByteArray(metadataInput);
212             handler.addFile(TOSCA_META_ORIG_PATH_FILE_NAME, metadataInputBytes);
213             handler.addFile(TOSCA_META_PATH_FILE_NAME, metadataInputBytes);
214             handler.addFile(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, IOUtils.toByteArray(manifestInput));
215             final ToscaServiceModel toscaMetadata = new ToscaServiceModel(handler, new HashMap<>(), "");
216             when(enrichedServiceModelDaoMock.getServiceModel(any(), any())).thenReturn(toscaMetadata);
217             final VspDetails vsp =
218                 createVspDetails("0", new Version(), "Vsp_PNF", "Test-vsp-pnf", "vendorName", "esy", "icon",
219                     "category", "subCategory", "123", null);
220             //want to avoid triggering populateVersionsForVlm method
221             vsp.setVlmVersion(null);
222
223             when(vspInfoDaoMock.get(any())).thenReturn(vsp);
224             when(licenseArtifactsServiceMock.createLicenseArtifacts(any(), any(), any(), any()))
225                 .thenReturn(new FileContentHandler());
226             final PackageInfo packageInfo = vendorSoftwareProductManager.createPackage("0", new Version());
227             assertEquals(packageInfo.getResourceType(), ResourceTypeEnum.PNF.name());
228         }
229     }
230
231     @Test
232     public void testCreatePackageEtsiPnfWithNonManoArtifacts() throws IOException {
233         try (
234             final InputStream metadataInput = getClass()
235                 .getResourceAsStream("/vspmanager.csar/metadata/ValidETSItosca.meta");
236             final InputStream manifestInput = getClass()
237                 .getResourceAsStream("/vspmanager.csar/manifest/ValidNonManoToscaPNFWithNonMano.mf");
238             final InputStream mainServiceTemplateYamlFile = getClass()
239                 .getResourceAsStream("/vspmanager.csar/descriptor/MainServiceTemplate.yaml")) {
240
241             final FileContentHandler handler = new FileContentHandler();
242             handler.addFile(TOSCA_META_ORIG_PATH_FILE_NAME, IOUtils.toByteArray(metadataInput));
243             handler.addFile(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, IOUtils.toByteArray(manifestInput));
244             handler.addFile("Deployment/ANOTHER/authorized_keys", "".getBytes());
245
246             final ServiceTemplate mainServiceTemplate = new YamlUtil()
247                 .yamlToObject(mainServiceTemplateYamlFile, ServiceTemplate.class);
248             final String mainServiceTemplateName = "MainServiceTemplate.yaml";
249             final HashMap<String, ServiceTemplate> serviceTemplateMap = new HashMap<>();
250             serviceTemplateMap.put(mainServiceTemplateName, mainServiceTemplate);
251
252             final ToscaServiceModel toscaMetadata = new ToscaServiceModel(handler, serviceTemplateMap,
253                 mainServiceTemplateName);
254             when(enrichedServiceModelDaoMock.getServiceModel(any(), any())).thenReturn(toscaMetadata);
255             final VspDetails vsp =
256                 createVspDetails("0", new Version(), "Vsp_PNF", "Test-vsp-pnf", "vendorName", "esy", "icon",
257                     "category", "subCategory", "123", null);
258             //want to avoid triggering populateVersionsForVlm method
259             vsp.setVlmVersion(null);
260
261             when(vspInfoDaoMock.get(any())).thenReturn(vsp);
262             when(licenseArtifactsServiceMock.createLicenseArtifacts(any(), any(), any(), any()))
263                 .thenReturn(new FileContentHandler());
264             final PackageInfo packageInfo = vendorSoftwareProductManager.createPackage("0", new Version());
265             assertThat("PackageInfo vendor release should be based on the manifest compatible_specification_versions highest version",
266                 packageInfo.getVendorRelease(), equalTo("3.3.1"));
267             System.out.println(packageInfo.getVendorRelease());
268             assertThat("Package Info should contain resource type", packageInfo.getResourceType(),
269                 equalTo(ResourceTypeEnum.PNF.name()));
270             assertThat("Should not contain moved artifact", toscaMetadata.getArtifactFiles().getFileList(),
271                 not(hasItem("Deployment/ANOTHER/authorized_keys")));
272             assertThat("Should contain moved artifact", toscaMetadata.getArtifactFiles().getFileList(),
273                 hasItem("Informational/OTHER/authorized_keys"));
274             final String serviceTemplateAsYaml = new YamlUtil().objectToYaml(toscaMetadata.getServiceTemplates());
275             assertThat("Descriptor should not contain reference to file", serviceTemplateAsYaml,
276                 not(containsString("Artifacts/Deployment/ANOTHER/authorized_keys")));
277             assertThat("Descriptor should contain reference to file", serviceTemplateAsYaml,
278                 containsString("Artifacts/Informational/OTHER/authorized_keys"));
279         }
280     }
281
282   @Test
283   public void testCreate() {
284     doReturn("{}")
285         .when(vendorSoftwareProductManager).getVspQuestionnaireSchema(nullable(SchemaTemplateInput.class));
286
287     VspDetails vspToCreate =
288         createVspDetails(null, null, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
289             "category", "subCategory", "123", null);
290
291     VspDetails vsp = vendorSoftwareProductManager.createVsp(vspToCreate);
292
293     Assert.assertNotNull(vsp);
294     vspToCreate.setId(vsp.getId());
295     vspToCreate.setVersion(VERSION01);
296     assertVspsEquals(vsp, vspToCreate);
297   }
298
299   @Test(expected = CoreException.class)
300   public void testUpdateWithExistingName_negative() {
301     VersionInfo versionInfo = new VersionInfo();
302     versionInfo.setActiveVersion(VERSION01);
303     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
304         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
305         VersionableEntityAction.Write);
306
307     VspDetails existingVsp =
308         createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-existingVsp", "vendorName", "vlm1Id",
309             "icon", "category", "subCategory", "123", null);
310     VspDetails updatedVsp =
311         createVspDetails(VSP_ID, VERSION01, "Vsp1_updated", "Test-existingVsp", "vendorName",
312             "vlm1Id", "icon", "category", "subCategory", "123", null);
313     doReturn(existingVsp).when(vspInfoDaoMock)
314         .get(any(VspDetails.class));
315     doThrow(new CoreException(
316         new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION).build()))
317         .when(vendorSoftwareProductManager)
318         .updateUniqueName(existingVsp.getName(), updatedVsp.getName());
319
320     vendorSoftwareProductManager.updateVsp(updatedVsp);
321   }
322
323   @Test
324   public void testUpdate() {
325     VersionInfo versionInfo = new VersionInfo();
326     versionInfo.setActiveVersion(VERSION01);
327     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
328         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
329         VersionableEntityAction.Write);
330     VspDetails existingVsp =
331         createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
332             "category",
333             "subCategory", "456", null);
334     VspDetails updatedVsp =
335         createVspDetails(VSP_ID, VERSION01, "VSP1_updated", null, "vendorName", "vlm1Id", "icon",
336             "category_updated",
337             "subCategory", "456", null);
338     doReturn(existingVsp).when(vspInfoDaoMock)
339         .get(any(VspDetails.class));
340     doNothing().when(vendorSoftwareProductManager)
341         .updateUniqueName(existingVsp.getName(), updatedVsp.getName());
342
343     vendorSoftwareProductManager.updateVsp(updatedVsp);
344
345     verify(vspInfoDaoMock).update(updatedVsp);
346   }
347
348   @Test
349   public void testUpdateRemoveFG() {
350     VersionInfo versionInfo = new VersionInfo();
351     versionInfo.setActiveVersion(VERSION01);
352     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
353         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
354         VersionableEntityAction.Write);
355     List<String> fgs = new ArrayList<>();
356     fgs.add("fg1");
357     fgs.add("fg2");
358     VspDetails existingVsp =
359         createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
360             "category",
361             "subCategory", "456", fgs);
362
363     List<String> updFgs = new ArrayList<>();
364     VspDetails updatedVsp =
365         createVspDetails(VSP_ID, VERSION01, "VSP1_updated", null, "vendorName", "vlm1Id", "icon",
366             "category_updated",
367             "subCategory", "456", updFgs);
368     doReturn(existingVsp).when(vspInfoDaoMock)
369         .get(any(VspDetails.class));
370     doNothing().when(vendorSoftwareProductManager)
371         .updateUniqueName(existingVsp.getName(), updatedVsp.getName());
372
373     DeploymentFlavorEntity dfEntity = new DeploymentFlavorEntity(VSP_ID, VERSION01, "DF_ID");
374     DeploymentFlavor flavor = new DeploymentFlavor();
375     flavor.setFeatureGroupId("fg1");
376     dfEntity.setDeploymentFlavorCompositionData(flavor);
377
378     List<DeploymentFlavorEntity> dfList = new ArrayList<>();
379     dfList.add(dfEntity);
380
381     doReturn(dfList).when(deploymentFlavorDaoMock).list(any(DeploymentFlavorEntity.class));
382
383     vendorSoftwareProductManager.updateVsp(updatedVsp);
384
385     verify(deploymentFlavorDaoMock).update(dfEntity);
386
387     Assert.assertNull(dfEntity.getDeploymentFlavorCompositionData().getFeatureGroupId());
388
389   }
390
391   @Test(expected = CoreException.class)
392   public void testGetNonExistingVersion_negative() {
393     Version notExistversion = new Version("43, 8");
394     doReturn(null).when(vspInfoDaoMock).get(any(VspDetails.class));
395     vendorSoftwareProductManager.getVsp(VSP_ID, notExistversion);
396   }
397
398   @Test
399   public void testGetCheckedOutVersion() {
400     VersionInfo versionInfo = new VersionInfo();
401     versionInfo.setActiveVersion(VERSION01);
402     versionInfo.setStatus(VersionStatus.Locked);
403     versionInfo.setLockingUser(USER1);
404     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
405         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
406         VersionableEntityAction.Read);
407
408     VspDetails existingVsp =
409         createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
410             "category",
411             "subCategory", "456", null);
412     doReturn(existingVsp).when(vspInfoDaoMock).get(any(VspDetails.class));
413
414     VspDetails actualVsp =
415         vendorSoftwareProductManager.getVsp(VSP_ID, VERSION01);
416
417     assertVspsEquals(actualVsp, existingVsp);
418   }
419
420   @Test
421   public void testGetOldVersion() {
422     VersionInfo versionInfo = new VersionInfo();
423     versionInfo.setActiveVersion(new Version("0, 2"));
424     versionInfo.setViewableVersions(Arrays.asList(VERSION01, new Version("0, 2")));
425     versionInfo.setStatus(VersionStatus.Locked);
426     versionInfo.setLockingUser(USER2);
427     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
428         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
429         VersionableEntityAction.Read);
430
431     VspDetails existingVsp =
432         createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon",
433             "category",
434             "subCategory", "456", null);
435     doReturn(existingVsp)
436         .when(vspInfoDaoMock).get(any(VspDetails.class));
437
438     VspDetails actualVsp =
439         vendorSoftwareProductManager.getVsp(VSP_ID, VERSION01);
440
441     VspDetails expectedVsp =
442         vspInfoDaoMock
443             .get(new VspDetails(VSP_ID, VERSION01));
444     assertVspsEquals(actualVsp, expectedVsp);
445   }
446
447   @Test
448   public void testCreatePackage() throws IOException {
449     VersionInfo versionInfo = new VersionInfo();
450     versionInfo.setActiveVersion(VERSION10);
451     doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo(
452         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1,
453         VersionableEntityAction.Read);
454
455     doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(), "")).when
456         (enrichedServiceModelDaoMock).getServiceModel(VSP_ID, VERSION10);
457     doNothing().when(vendorSoftwareProductManager).populateVersionsForVlm(anyString(), any(Version.class));
458     VspDetails vsp = new VspDetails(VSP_ID, VERSION10);
459     vsp.setVendorId("vendorId");
460     vsp.setVlmVersion(VERSION10);
461     vsp.setFeatureGroups(Arrays.asList("fg1", "fg2"));
462     vsp.setModelIdList(List.of("aModel"));
463     doReturn(vsp).when(vspInfoDaoMock).get(any(VspDetails.class));
464
465     doReturn(new FileContentHandler()).when(licenseArtifactsServiceMock)
466         .createLicenseArtifacts(VSP_ID, vsp.getVendorId(), VERSION10, vsp.getFeatureGroups()
467         );
468
469     PackageInfo packageInfo = vendorSoftwareProductManager.createPackage(VSP_ID, VERSION10);
470     Assert.assertNotNull(packageInfo.getVspId());
471     assertEquals("1.0", packageInfo.getVendorRelease());
472   }
473
474   @Test
475   public void testValidateWithCandidateDataNotProcessed() throws IOException {
476     VspDetails vsp =
477         createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon",
478             "category", "subCategory", "licenseAgreementId",
479             Collections.singletonList("featureGroupId"));
480     vsp.setOnboardingMethod("NetworkPackage");
481     doReturn(vsp).when(vspInfoDaoMock).get(any(VspDetails.class));
482
483     OrchestrationTemplateCandidateData orchestrationTemplateCandidateData = new
484         OrchestrationTemplateCandidateData();
485     orchestrationTemplateCandidateData.setFileSuffix("zip");
486     orchestrationTemplateCandidateData.setFilesDataStructure("testdata");
487     orchestrationTemplateCandidateData.setValidationData("");
488     doReturn(Optional.of(orchestrationTemplateCandidateData))
489         .when(orchestrationTemplateCandidateManagerMock)
490         .getInfo(VSP_ID, VERSION01);
491     ValidationResponse validationResponse =
492         vendorSoftwareProductManager.validate(vsp);
493     Assert.assertNotNull(validationResponse);
494     Assert.assertFalse(validationResponse.isValid());
495     Assert.assertNotNull(validationResponse.getVspErrors());
496     Assert.assertEquals(1, validationResponse.getVspErrors().size());
497
498   }
499
500   @Test
501   public void testValidateWithCandidateProcessedIsInvalid() throws IOException {
502     VspDetails vsp = createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-VSP", "vendorName",
503         "vl1Id", "icond", "category", "subcategory", "licenseAgreementId", Collections
504             .singletonList("featureGroupId"));
505     vsp.setOnboardingMethod("NetworkPackage");
506     doReturn(vsp).when(vspInfoDaoMock).get(any(VspDetails.class));
507
508     OrchestrationTemplateCandidateData orchestrationTemplateCandidateData = new
509         OrchestrationTemplateCandidateData();
510     orchestrationTemplateCandidateData.setFileSuffix("zip");
511     orchestrationTemplateCandidateData.setValidationData("Invalid processed data");
512     doReturn(Optional.of(orchestrationTemplateCandidateData))
513         .when(orchestrationTemplateCandidateManagerMock)
514         .getInfo(VSP_ID, VERSION01);
515     ValidationResponse validationResponse =
516         vendorSoftwareProductManager.validate(vsp);
517     Assert.assertNotNull(validationResponse);
518     Assert.assertFalse(validationResponse.isValid());
519     Assert.assertNotNull(validationResponse.getVspErrors());
520     Assert.assertEquals(1, validationResponse.getVspErrors().size());
521   }
522
523   private static VspDetails createVspDetails(String id, Version version, String name, String desc,
524                                      String vendorName, String vlm, String icon,
525                                      String category, String subCategory,
526                                      String licenseAgreement, List<String> featureGroups) {
527     VspDetails vspDetails = new VspDetails(id, version);
528     vspDetails.setName(name);
529     vspDetails.setDescription(desc);
530     vspDetails.setIcon(icon);
531     vspDetails.setCategory(category);
532     vspDetails.setSubCategory(subCategory);
533     vspDetails.setVendorName(vendorName);
534     vspDetails.setVendorId(vlm);
535     vspDetails.setVlmVersion(new Version("1, 0"));
536     vspDetails.setLicenseAgreement(licenseAgreement);
537     vspDetails.setFeatureGroups(featureGroups);
538     vspDetails.setOnboardingMethod("HEAT");
539     return vspDetails;
540   }
541
542   private static void assertVspsEquals(VspDetails actual, VspDetails expected) {
543     Assert.assertEquals(actual.getId(), expected.getId());
544     Assert.assertEquals(actual.getVersion(), expected.getVersion());
545     Assert.assertEquals(actual.getName(), expected.getName());
546     Assert.assertEquals(actual.getDescription(), expected.getDescription());
547     Assert.assertEquals(actual.getIcon(), expected.getIcon());
548     Assert.assertEquals(actual.getCategory(), expected.getCategory());
549     Assert.assertEquals(actual.getSubCategory(), expected.getSubCategory());
550     Assert.assertEquals(actual.getVendorName(), expected.getVendorName());
551     Assert.assertEquals(actual.getVendorId(), expected.getVendorId());
552     Assert.assertEquals(actual.getLicenseAgreement(), expected.getLicenseAgreement());
553     Assert.assertEquals(actual.getFeatureGroups(), expected.getFeatureGroups());
554   }
555
556 }