From 28e7f0f13e3d7b75750a449c9bda6c0d4a1986ce Mon Sep 17 00:00:00 2001 From: franciscovila Date: Tue, 15 Mar 2022 08:29:04 +0000 Subject: Copy entry_defintion_type to TOSCA.meta Copy "entry_defintion_type" from the manifest file of ASD package into the generated TOSCA.meta for the VSP package Issue-ID: SDC-3884 Signed-off-by: franciscovila Change-Id: I35c4d0180529e8c60edc714aecadd0150cbd2c03 --- .../impl/ToscaFileOutputServiceCsarImplTest.java | 166 ++++++++++++++++++--- 1 file changed, 142 insertions(+), 24 deletions(-) (limited to 'openecomp-be/lib/openecomp-tosca-lib/src/test') diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImplTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImplTest.java index df7fa2af97..107d166823 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImplTest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImplTest.java @@ -20,9 +20,22 @@ package org.openecomp.sdc.tosca.services.impl; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.tosca.csar.AsdPackageHelper; +import org.openecomp.sdc.tosca.csar.ManifestTokenType; +import org.openecomp.sdc.tosca.csar.ManifestUtils; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.services.ToscaConstants; +import org.openecomp.sdc.tosca.services.ToscaUtil; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Collections; @@ -32,43 +45,43 @@ import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.InjectMocks; -import org.mockito.MockitoAnnotations; -import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; -import org.openecomp.core.utilities.file.FileContentHandler; -import org.openecomp.sdc.common.errors.CoreException; -import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; -import org.openecomp.sdc.tosca.services.ToscaConstants; -import org.openecomp.sdc.tosca.services.ToscaUtil; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.APPLICATION_NAME; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.APPLICATION_PROVIDER; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ENTRY_DEFINITION_TYPE; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryAsd.CREATED_BY_ENTRY; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryAsd.CSAR_VERSION_ENTRY; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryAsd.ENTRY_DEFINITIONS; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryAsd.ETSI_ENTRY_MANIFEST; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryAsd.TOSCA_META_FILE_VERSION_ENTRY; +import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME; public class ToscaFileOutputServiceCsarImplTest { - @InjectMocks - private ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCsarImpl; + private static ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCsarImpl; - @Before - public void init() { - MockitoAnnotations.openMocks(this); + @BeforeAll + public static void setupBeforeClass() { + toscaFileOutputServiceCsarImpl = new ToscaFileOutputServiceCsarImpl(new AsdPackageHelper(new ManifestUtils())); } @Test public void testCreationMetaFile() { - String createdMeta = toscaFileOutputServiceCsarImpl.createMetaFile("entryFile.yaml"); + String createdMeta = toscaFileOutputServiceCsarImpl.createMetaFile("entryFile.yaml", false); String expectedMeta = "TOSCA-Meta-File-Version: 1.0\n" + "CSAR-Version: 1.1\n" + "Created-By: ASDC Onboarding portal\n" + "Entry-Definitions: Definitions" + File.separator + "entryFile.yaml"; - Assert.assertEquals(createdMeta.replaceAll("\\s+", ""), expectedMeta.replaceAll("\\s+", "")); + assertEquals(createdMeta.replaceAll("\\s+", ""), expectedMeta.replaceAll("\\s+", "")); } @Test public void testCSARFileCreationWithExternalArtifacts() throws IOException { ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl = - new ToscaFileOutputServiceCsarImpl(); + new ToscaFileOutputServiceCsarImpl(new AsdPackageHelper(new ManifestUtils())); ServiceTemplate mainServiceTemplate = new ServiceTemplate(); Map metadata1 = new HashMap<>(); metadata1.put("Template_author", "OPENECOMP"); @@ -139,6 +152,112 @@ public class ToscaFileOutputServiceCsarImplTest { Files.delete(Paths.get(file.getAbsolutePath())); } + @Test + public void testAsdCSARFileCreationWithExternalArtifacts() throws IOException { + + ServiceTemplate mainServiceTemplate = new ServiceTemplate(); + Map metadata1 = new HashMap<>(); + metadata1.put("Template_author", "OPENECOMP"); + metadata1.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "ST1"); + metadata1.put("Template_version", "1.0.0"); + metadata1.put("filename", "asd.yaml"); + mainServiceTemplate.setMetadata(metadata1); + mainServiceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0"); + mainServiceTemplate.setDescription("testing desc tosca service template"); + + ServiceTemplate additionalServiceTemplate = new ServiceTemplate(); + Map metadata2 = new HashMap<>(); + metadata2.put("Template_author", "OPENECOMP"); + metadata2.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "ST2"); + metadata2.put("Template_version", "1.0.0"); + additionalServiceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0"); + additionalServiceTemplate.setDescription("testing desc tosca service template"); + additionalServiceTemplate.setMetadata(metadata2); + + FileContentHandler handler = new FileContentHandler(); + String metaFile = new StringBuilder() + .append(TOSCA_META_FILE_VERSION_ENTRY.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" 1.0").append("\n") + .append(CSAR_VERSION_ENTRY.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" 1.1").append("\n") + .append(CREATED_BY_ENTRY.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" Vendor").append("\n") + .append(ENTRY_DEFINITIONS.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append("Definitions/asd.yaml").append("\n") + .append(ETSI_ENTRY_MANIFEST.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + "asd.mf").append("\n").toString(); + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); + + Map manifestMap = new HashMap<>(); + String manifestContent = new StringBuilder().append("metadata") + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append("\n") + .append(ENTRY_DEFINITION_TYPE.getToken()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append("asd").append("\n") + .append(ManifestTokenType.RELEASE_DATE_TIME.getToken()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append("2021-10-21T11:30:00+05:00").append("\n") + .append(APPLICATION_NAME.getToken()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append("SampleApp").append("\n") + .append(APPLICATION_PROVIDER.getToken()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append("MyCompany") + .toString(); + String manifestFile = "asd.mf"; + manifestMap.put(manifestFile, manifestContent.getBytes()); + handler.setFiles(manifestMap); + + Map definitionsInput = new HashMap<>(); + definitionsInput + .put(ToscaUtil.getServiceTemplateFileName(mainServiceTemplate), mainServiceTemplate); + definitionsInput.put(ToscaUtil.getServiceTemplateFileName(additionalServiceTemplate), + additionalServiceTemplate); + + Map dummyHeatArtifacts = new HashMap<>(); + String file1Content = "this is file number 1"; + String file2Content = "this is file number 2"; + String file1 = "file1.xml"; + dummyHeatArtifacts.put(file1, file1Content.getBytes()); + String file2 = "file2.yml"; + dummyHeatArtifacts.put(file2, file2Content.getBytes()); + handler.setFiles(dummyHeatArtifacts); + + + FileContentHandler heatFiles = new FileContentHandler(); + heatFiles.setFiles(dummyHeatArtifacts); + heatFiles.addAll(handler); + Map licenseArtifacts = new HashMap<>(); + + FileContentHandler licenseArtifactsFiles = new FileContentHandler(); + + licenseArtifacts.put( + ToscaFileOutputServiceCsarImpl.EXTERNAL_ARTIFACTS_FOLDER_NAME + File.separator + + "license-file-1.xml", file1Content.getBytes()); + licenseArtifacts.put( + ToscaFileOutputServiceCsarImpl.EXTERNAL_ARTIFACTS_FOLDER_NAME + File.separator + + "license-file-2.xml", file1Content.getBytes()); + + licenseArtifactsFiles.setFiles(licenseArtifacts); + + byte[] csarFile = toscaFileOutputServiceCsarImpl.createOutputFile( + new ToscaServiceModel(heatFiles, definitionsInput, + ToscaUtil.getServiceTemplateFileName(mainServiceTemplate)), licenseArtifactsFiles); + + File file = File.createTempFile("resultFile", "zip"); + try (FileOutputStream fos = new FileOutputStream(file)) { + fos.write(csarFile); + } + + try (ZipFile zipFile = new ZipFile(file)) { + + Enumeration entries = zipFile.entries(); + int count = 0; + while (entries.hasMoreElements()) { + count++; + entries.nextElement(); + } + assertEquals(9, count); + } + + Files.delete(Paths.get(file.getAbsolutePath())); + } + @Test public void testCSARFileCreation_noArtifacts() throws IOException { ServiceTemplate serviceTemplate = new ServiceTemplate(); @@ -171,22 +290,21 @@ public class ToscaFileOutputServiceCsarImplTest { count++; entries.nextElement(); } - Assert.assertEquals(2, count); + assertEquals(2, count); } Files.delete(Paths.get(file.getAbsolutePath())); } - @Test(expected = CoreException.class) + @Test public void testCreateOutputFileEntryDefinitionServiceTemplateIsNull() { ToscaServiceModel toscaServiceModel = new ToscaServiceModel(); toscaServiceModel.setServiceTemplates(Collections.emptyMap()); - - toscaFileOutputServiceCsarImpl.createOutputFile(toscaServiceModel, null); + assertThrows(CoreException.class, () -> toscaFileOutputServiceCsarImpl.createOutputFile(toscaServiceModel, null)); } @Test public void testGetArtifactsFolderName() { - Assert.assertEquals("Artifacts", toscaFileOutputServiceCsarImpl.getArtifactsFolderName()); + assertEquals("Artifacts", toscaFileOutputServiceCsarImpl.getArtifactsFolderName()); } } -- cgit 1.2.3-korg