2371bac930d914212484bff2ce8291cc2ce5f24a
[sdc.git] /
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.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertThrows;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.anyString;
31 import static org.mockito.ArgumentMatchers.nullable;
32 import static org.mockito.Mockito.doNothing;
33 import static org.mockito.Mockito.doReturn;
34 import static org.mockito.Mockito.doThrow;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
37 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME;
38 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME;
39 import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME;
40
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Optional;
49 import org.apache.commons.io.IOUtils;
50 import org.junit.jupiter.api.AfterEach;
51 import org.junit.jupiter.api.BeforeEach;
52 import org.junit.jupiter.api.Test;
53 import org.mockito.ArgumentCaptor;
54 import org.mockito.Captor;
55 import org.mockito.InjectMocks;
56 import org.mockito.Mock;
57 import org.mockito.MockitoAnnotations;
58 import org.mockito.Spy;
59 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
60 import org.onap.sdc.tosca.services.YamlUtil;
61 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
62 import org.openecomp.core.model.dao.ServiceModelDao;
63 import org.openecomp.core.utilities.file.FileContentHandler;
64 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
65 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
66 import org.openecomp.sdc.common.errors.CoreException;
67 import org.openecomp.sdc.common.errors.ErrorCategory;
68 import org.openecomp.sdc.common.errors.ErrorCode;
69 import org.openecomp.sdc.healing.api.HealingManager;
70 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
71 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
72 import org.openecomp.sdc.vendorlicense.licenseartifacts.VendorLicenseArtifactsService;
73 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
74 import org.openecomp.sdc.vendorsoftwareproduct.ManualVspToscaManager;
75 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManager;
76 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
77 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
78 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
79 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
80 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
81 import org.openecomp.sdc.vendorsoftwareproduct.dao.PackageInfoDao;
82 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
83 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
84 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
85 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo;
86 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
87 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
88 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
89 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
90 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
91 import org.openecomp.sdc.versioning.ActionVersioningManager;
92 import org.openecomp.sdc.versioning.dao.types.Version;
93 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
94 import org.openecomp.sdc.versioning.types.VersionInfo;
95 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
96
97 class VendorSoftwareProductManagerImplTest {
98
99     private static final String VSP_ID = "vspId";
100     private static final Version VERSION01 = new Version("0, 1");
101     private static final Version VERSION10 = new Version("1, 0");
102     private static final String USER1 = "vspTestUser1";
103     private static final String USER2 = "vspTestUser2";
104
105     @Mock
106     private ActionVersioningManager versioningManagerMock;
107     @Mock
108     private OrchestrationTemplateDao orchestrationTemplateDataDaoMock;
109     @Mock
110     private VendorLicenseFacade vendorLicenseFacadeMock;
111     @Mock
112     private ServiceModelDao<ToscaServiceModel> serviceModelDaoMock;
113     @Mock
114     private EnrichedServiceModelDao<ToscaServiceModel> enrichedServiceModelDaoMock;
115     @Mock
116     private HealingManager healingManagerMock;
117     @Mock
118     private VendorLicenseArtifactsService licenseArtifactsServiceMock;
119     @Mock
120     private CompositionEntityDataManager compositionEntityDataManagerMock;
121     @Mock
122     private InformationArtifactGenerator informationArtifactGeneratorMock;
123     @Mock
124     private PackageInfoDao packageInfoDao;
125     @Mock
126     private VendorSoftwareProductInfoDao vspInfoDaoMock;
127     @Mock
128     private ManualVspToscaManager manualVspToscaManager;
129     @Mock
130     private DeploymentFlavorDao deploymentFlavorDaoMock;
131     @Spy
132     @InjectMocks
133     private VendorSoftwareProductManagerImpl vendorSoftwareProductManager;
134     @Mock
135     private OrchestrationTemplateCandidateManager orchestrationTemplateCandidateManagerMock;
136     @Mock
137     private ComponentDependencyModelDao componentDependencyModelDao;
138     private OrchestrationTemplateCandidateManager candidateManager;
139     private MonitoringUploadsManager monitoringUploadsManager;
140
141     @Captor
142     private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
143
144     @BeforeEach
145     void setUp() {
146         MockitoAnnotations.openMocks(this);
147     }
148
149     @AfterEach
150     void tearDown() {
151         vendorSoftwareProductManager = null;
152     }
153
154     @Test
155     void testCreatePackageEtsiVNF() throws IOException {
156         try (
157             final InputStream metadataInput = getClass()
158                 .getResourceAsStream("/vspmanager.csar/metadata/ValidETSItosca.meta");
159             final InputStream manifestInput = getClass()
160                 .getResourceAsStream("/vspmanager.csar/manifest/ValidNonManoTosca.mf")) {
161
162             final FileContentHandler handler = new FileContentHandler();
163             final byte[] metadataInputBytes = IOUtils.toByteArray(metadataInput);
164             handler.addFile(TOSCA_META_PATH_FILE_NAME, metadataInputBytes);
165             handler.addFile(TOSCA_META_ORIG_PATH_FILE_NAME, metadataInputBytes);
166             handler.addFile(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, IOUtils.toByteArray(manifestInput));
167             final ToscaServiceModel toscaMetadata = new ToscaServiceModel(handler, new HashMap<>(), "");
168             when(enrichedServiceModelDaoMock.getServiceModel(any(), any())).thenReturn(toscaMetadata);
169             final VspDetails vsp =
170                 createVspDetails("0", new Version(), "Vsp_PNF", "Test-vsp-pnf", "vendorName", "esy", "icon",
171                     "category", "subCategory", "123", null);
172             //want to avoid triggering populateVersionsForVlm method
173             vsp.setVlmVersion(null);
174
175             when(vspInfoDaoMock.get(any())).thenReturn(vsp);
176             when(licenseArtifactsServiceMock.createLicenseArtifacts(any(), any(), any(), any()))
177                 .thenReturn(new FileContentHandler());
178             final PackageInfo packageInfo = vendorSoftwareProductManager.createPackage("0", new Version());
179             assertEquals(packageInfo.getResourceType(), ResourceTypeEnum.VF.name());
180         }
181     }
182
183     @Test
184     void testCreatePackageEtsiNoManifest() throws IOException {
185         try (InputStream metadataInput = getClass().getResourceAsStream("/vspmanager.csar/metadata/ValidETSItosca.meta")) {
186             FileContentHandler handler = new FileContentHandler();
187             handler.addFile(TOSCA_META_PATH_FILE_NAME, IOUtils.toByteArray(metadataInput));
188             ToscaServiceModel toscaMetadata = new ToscaServiceModel(handler, new HashMap<>(), "");
189             when(enrichedServiceModelDaoMock.getServiceModel(any(), any())).thenReturn(toscaMetadata);
190             VspDetails vsp =
191                 createVspDetails("0", new Version(), "Vsp_PNF", "Test-vsp-pnf", "vendorName", "esy", "icon",
192                     "category", "subCategory", "123", null);
193             //want to avoid triggering populateVersionsForVlm method
194             vsp.setVlmVersion(null);
195
196             when(vspInfoDaoMock.get(any())).thenReturn(vsp);
197             when(licenseArtifactsServiceMock.createLicenseArtifacts(any(), any(), any(), any())).thenReturn(new FileContentHandler());
198             assertThrows(IOException.class, () -> vendorSoftwareProductManager.createPackage("0", new Version()));
199         }
200     }
201
202     @Test
203     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     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     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         assertNotNull(vsp);
294         vspToCreate.setId(vsp.getId());
295         vspToCreate.setVersion(VERSION01);
296         assertVspsEquals(vsp, vspToCreate);
297     }
298
299     @Test
300     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         assertThrows(CoreException.class, () -> vendorSoftwareProductManager.updateVsp(updatedVsp));
321     }
322
323     @Test
324     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     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         assertNull(dfEntity.getDeploymentFlavorCompositionData().getFeatureGroupId());
388
389     }
390
391     @Test
392     void testGetNonExistingVersion_negative() {
393         var notExistVersion = new Version("43, 8");
394         doReturn(null).when(vspInfoDaoMock).get(any(VspDetails.class));
395         assertThrows(CoreException.class, () -> vendorSoftwareProductManager.getVsp(VSP_ID, notExistVersion));
396     }
397
398     @Test
399     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     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     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         assertNotNull(packageInfo.getVspId());
471         assertEquals("1.0", packageInfo.getVendorRelease());
472     }
473
474     @Test
475     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         assertNotNull(validationResponse);
494         assertFalse(validationResponse.isValid());
495         assertNotNull(validationResponse.getVspErrors());
496         assertEquals(1, validationResponse.getVspErrors().size());
497
498     }
499
500     @Test
501     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         assertNotNull(validationResponse);
518         assertFalse(validationResponse.isValid());
519         assertNotNull(validationResponse.getVspErrors());
520         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         assertEquals(actual.getId(), expected.getId());
544         assertEquals(actual.getVersion(), expected.getVersion());
545         assertEquals(actual.getName(), expected.getName());
546         assertEquals(actual.getDescription(), expected.getDescription());
547         assertEquals(actual.getIcon(), expected.getIcon());
548         assertEquals(actual.getCategory(), expected.getCategory());
549         assertEquals(actual.getSubCategory(), expected.getSubCategory());
550         assertEquals(actual.getVendorName(), expected.getVendorName());
551         assertEquals(actual.getVendorId(), expected.getVendorId());
552         assertEquals(actual.getLicenseAgreement(), expected.getLicenseAgreement());
553         assertEquals(actual.getFeatureGroups(), expected.getFeatureGroups());
554     }
555
556 }