From 138be3e494bb0c58f3bd190281159334ea7b89a7 Mon Sep 17 00:00:00 2001 From: siddharth0905 Date: Mon, 22 Oct 2018 22:24:20 +0530 Subject: Test coverage : ToscaFileOutputServiceCsarImplTest openecomp-tosca-lib Change-Id: I2448672b685c591ebbb27af26761b891e52d44d7 Issue-ID: SDC-1673 Signed-off-by: siddharth0905 --- .../impl/ToscaFileOutputServiceCsarImplTest.java | 276 +++++++++++---------- 1 file changed, 148 insertions(+), 128 deletions(-) (limited to 'openecomp-be/lib/openecomp-tosca-lib') 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 10aabe6741..92374f57fe 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 @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,153 +20,173 @@ package org.openecomp.sdc.tosca.services.impl; -import org.junit.Assert; -import org.junit.Test; -import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; -import org.openecomp.core.utilities.file.FileContentHandler; -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.file.Files; import java.nio.file.Paths; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -public class ToscaFileOutputServiceCsarImplTest { +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; - @Test - public void testCreationMetaFile() { - ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl = - new ToscaFileOutputServiceCsarImpl(); - String createdMeta = toscaFileOutputServiceCSARImpl.createMetaFile("entryFile.yaml"); - 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+", "")); - } - - @Test - public void testCSARFileCreationWithExternalArtifacts() throws IOException { - ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl = - new ToscaFileOutputServiceCsarImpl(); - 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"); - 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); - - 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()); - - - FileContentHandler heatFiles = new FileContentHandler(); - heatFiles.putAll(dummyHeatArtifacts); - 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.putAll(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); - } +public class ToscaFileOutputServiceCsarImplTest { - try (ZipFile zipFile = new ZipFile(file)) { + @InjectMocks + private ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCsarImpl; - Enumeration entries = zipFile.entries(); + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } - int count = 0; - while (entries.hasMoreElements()) { - count++; - entries.nextElement(); - } + @Test + public void testCreationMetaFile() { + String createdMeta = toscaFileOutputServiceCsarImpl.createMetaFile("entryFile.yaml"); + 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+", "")); } - Files.delete(Paths.get(file.getAbsolutePath())); - } - - @Test - public void testCSARFileCreation_noArtifacts() throws IOException { - ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl = - new ToscaFileOutputServiceCsarImpl(); - ServiceTemplate serviceTemplate = new ServiceTemplate(); - Map metadata = new HashMap<>(); - metadata.put("Template_author", "OPENECOMP"); - metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "Test"); - metadata.put("Template_version", "1.0.0"); - serviceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0"); - serviceTemplate.setDescription("testing desc tosca service template"); - serviceTemplate.setMetadata(metadata); - Map definitionsInput = new HashMap<>(); - String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate); - definitionsInput.put(serviceTemplateFileName, serviceTemplate); - byte[] csarFile = toscaFileOutputServiceCSARImpl - .createOutputFile(new ToscaServiceModel(null, definitionsInput, serviceTemplateFileName), - null); - - - File file = File.createTempFile("resultFile", "zip"); - try (FileOutputStream fos = new FileOutputStream(file)) { - fos.write(csarFile); + @Test + public void testCSARFileCreationWithExternalArtifacts() throws IOException { + ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl = + new ToscaFileOutputServiceCsarImpl(); + 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"); + 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); + + 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()); + + + FileContentHandler heatFiles = new FileContentHandler(); + heatFiles.putAll(dummyHeatArtifacts); + 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.putAll(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(); + + while (entries.hasMoreElements()) { + entries.nextElement(); + } + } + + Files.delete(Paths.get(file.getAbsolutePath())); } - try (ZipFile zipFile = new ZipFile(file)) { + @Test + public void testCSARFileCreation_noArtifacts() throws IOException { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + Map metadata = new HashMap<>(); + metadata.put("Template_author", "OPENECOMP"); + metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "Test"); + metadata.put("Template_version", "1.0.0"); + serviceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0"); + serviceTemplate.setDescription("testing desc tosca service template"); + serviceTemplate.setMetadata(metadata); + Map definitionsInput = new HashMap<>(); + String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate); + definitionsInput.put(serviceTemplateFileName, serviceTemplate); + byte[] csarFile = toscaFileOutputServiceCsarImpl + .createOutputFile(new ToscaServiceModel(null, definitionsInput, serviceTemplateFileName), + null); + + + 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(); + } + Assert.assertEquals(2, count); + } + + Files.delete(Paths.get(file.getAbsolutePath())); + } - Enumeration entries = zipFile.entries(); + @Test(expected = CoreException.class) + public void testCreateOutputFileEntryDefinitionServiceTemplateIsNull() { + ToscaServiceModel toscaServiceModel = new ToscaServiceModel(); + toscaServiceModel.setServiceTemplates(Collections.emptyMap()); - int count = 0; - while (entries.hasMoreElements()) { - count++; - entries.nextElement(); - } - Assert.assertEquals(2, count); + toscaFileOutputServiceCsarImpl.createOutputFile(toscaServiceModel, null); } - Files.delete(Paths.get(file.getAbsolutePath())); - } + @Test + public void testGetArtifactsFolderName() { + Assert.assertEquals("Artifacts", toscaFileOutputServiceCsarImpl.getArtifactsFolderName()); + } } -- cgit 1.2.3-korg