diff options
author | Avi Gaffa <avi.gaffa@amdocs.com> | 2017-10-24 10:21:21 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2017-10-24 10:21:21 +0000 |
commit | 5d33372dc5170b8d945a59e92c2cd5b0a5314cc3 (patch) | |
tree | 266b3cfe4cc084ea72bf5bd16cc25c153de20de6 | |
parent | aa587c41afbb85a7d11b051d0518ee5fe2cde6bb (diff) | |
parent | 43645dccbaced7a45b37f9e05221186231c39d74 (diff) |
Merge "Fixed resources not being closed in tests"
8 files changed, 318 insertions, 270 deletions
diff --git a/common/openecomp-sdc-artifact-generator-lib/openecomp-sdc-artifact-generator-core/src/test/java/org/openecomp/sdc/generator/ArtifactGenerationServiceTest.java b/common/openecomp-sdc-artifact-generator-lib/openecomp-sdc-artifact-generator-core/src/test/java/org/openecomp/sdc/generator/ArtifactGenerationServiceTest.java index 01029e8adc..2de7535a8b 100644 --- a/common/openecomp-sdc-artifact-generator-lib/openecomp-sdc-artifact-generator-core/src/test/java/org/openecomp/sdc/generator/ArtifactGenerationServiceTest.java +++ b/common/openecomp-sdc-artifact-generator-lib/openecomp-sdc-artifact-generator-core/src/test/java/org/openecomp/sdc/generator/ArtifactGenerationServiceTest.java @@ -73,9 +73,9 @@ public class ArtifactGenerationServiceTest { private static final String aaiArtifactType = ArtifactType.AAI.name(); private static final String aaiArtifactGroupType = GroupType.DEPLOYMENT.name(); private static final String generatorConfig = "{\"artifactTypes\": [\"OTHER\",\"AAI\"]}"; - Properties properties = new Properties(); - Map<String, String> additionalParams = new HashMap(); - Map<String, String> resourcesVersion = new HashMap<>(); + private final Properties properties = new Properties(); + private final Map<String, String> additionalParams = new HashMap<>(); + private final Map<String, String> resourcesVersion = new HashMap<>(); @BeforeSuite public void loadProperties() throws Exception{ @@ -87,7 +87,7 @@ public class ArtifactGenerationServiceTest { public void testArtifactGeneration() { // Sunny day scenario service with VF anf vfmodule try { - Map<String, Model> outputArtifactMap = new HashMap<>(); + ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "aai/"; @@ -112,7 +112,7 @@ public class ArtifactGenerationServiceTest { Assert.assertEquals(resultData.size(),5); // 1-service,1-VF-resource,1-vfmodule and 2 // others - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); testResourceTosca(toscas.iterator(), outputArtifactMap); } else { @@ -129,7 +129,6 @@ public class ArtifactGenerationServiceTest { // Sunny day scenario service with VF and extra widget like CP anf vf has vfmodule without // member try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testWidgetinServiceTosca/"; @@ -142,7 +141,7 @@ public class ArtifactGenerationServiceTest { List<Artifact> resultData = data.getResultData(); Assert.assertEquals(resultData.size(),5); // 1-service,1-VF-resource,1-vfmodule and 2 // others - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); testResourceTosca(toscas.iterator(), outputArtifactMap); } else { @@ -159,7 +158,6 @@ public class ArtifactGenerationServiceTest { // Sunny day scenario service with VF and extra widget like CP anf vf has vfmodule without // member try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testSameVLdifferentVersion/"; @@ -172,7 +170,7 @@ public class ArtifactGenerationServiceTest { List<Artifact> resultData = data.getResultData(); Assert.assertEquals(resultData.size(),8); // others - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); testResourceTosca(toscas.iterator(), outputArtifactMap); } else { @@ -188,7 +186,6 @@ public class ArtifactGenerationServiceTest { public void testArtifactGenerationAllottedResourceAndL3Network() { // Sunny day scenario service with allotted resource and L3-network try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "aai2/"; @@ -203,7 +200,7 @@ public class ArtifactGenerationServiceTest { Assert.assertEquals(resultData.size(),5); // and 2 // others - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); testResourceTosca(toscas.iterator(), outputArtifactMap); } else { @@ -248,7 +245,6 @@ public class ArtifactGenerationServiceTest { public void testWhenOnlyServToscaNoResTosca() { try { //Testing only service tosca no resource Tosca - Map<String, Model> outputArtifactMap = new HashMap<>(); String aaiResourceBasePaths = "testArtifactGeneration4/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); @@ -261,7 +257,7 @@ public class ArtifactGenerationServiceTest { } } Assert.assertEquals(resultData.size(),3); // 1-service and 2-Others - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); } catch (Exception e) { Assert.fail(e.getMessage()); @@ -286,7 +282,6 @@ public class ArtifactGenerationServiceTest { public void testWhenExtraResToscaNotPartOfServ() { try { // Valid scenario with extra resource tosca which is not part of Service - Map<String, Model> outputArtifactMap = new HashMap<>(); String aaiResourceBasePaths = "testArtifactGeneration6/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); @@ -299,7 +294,7 @@ public class ArtifactGenerationServiceTest { } } Assert.assertEquals(resultData.size(),3); // 1-service and 2 Others - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); } catch (Exception e) { Assert.fail(e.getMessage()); @@ -397,13 +392,8 @@ public class ArtifactGenerationServiceTest { try { //mandatory attribute <vfModuleModelInvariantUUID> missing List<Artifact> inputArtifacts = new ArrayList<>(); - InputStream fis1 = ArtifactGenerationServiceTest.class.getResourceAsStream("/service_vmme_template_ModInvUUID.yml"); - readPayload(inputArtifacts,fis1, "service_vmme_template_ModInvUUID.yml"); - fis1.close(); - - InputStream fis2 = ArtifactGenerationServiceTest.class.getResourceAsStream("/vf_vmme_template_ModInvUUID.yml"); - readPayload(inputArtifacts,fis2, "vf_vmme_template_ModInvUUID.yml"); - fis2.close(); + readPayloadFromResource(inputArtifacts, "service_vmme_template_ModInvUUID.yml"); + readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModInvUUID.yml"); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); Assert.assertEquals( @@ -415,7 +405,7 @@ public class ArtifactGenerationServiceTest { } } - public static void readPayload(List<Artifact> inputArtifacts,InputStream fis, String fileName) throws + public static void readPayload(List<Artifact> inputArtifacts, InputStream fis, String fileName) throws IOException { byte[] payload = new byte[fis.available()]; fis.read(payload); @@ -435,13 +425,9 @@ public class ArtifactGenerationServiceTest { try { //invalid id since not of length 36 for <vfModuleModelInvariantUUID> List<Artifact> inputArtifacts = new ArrayList<>(); - InputStream fis1 = ArtifactGenerationServiceTest.class.getResourceAsStream("/service_vmme_template_InvalidVfModInvUuIdAttr.yml"); - readPayload(inputArtifacts,fis1, "service_vmme_template_InvalidVfModInvUuIdAttr.yml"); - fis1.close(); + readPayloadFromResource(inputArtifacts, "service_vmme_template_InvalidVfModInvUuIdAttr.yml"); - InputStream fis2 = ArtifactGenerationServiceTest.class.getResourceAsStream("/vf_vmme_template_InvalidVfModInvUuIdAttr.yml"); - readPayload(inputArtifacts,fis2, "vf_vmme_template_InvalidVfModInvUuIdAttr.yml"); - fis2.close(); + readPayloadFromResource(inputArtifacts, "vf_vmme_template_InvalidVfModInvUuIdAttr.yml"); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); Assert.assertEquals( @@ -458,13 +444,9 @@ public class ArtifactGenerationServiceTest { try { //mandatory attribute <vfModuleModelName> missing List<Artifact> inputArtifacts = new ArrayList<>(); - InputStream fis1 = ArtifactGenerationServiceTest.class.getResourceAsStream("/service_vmme_template_ModelName.yml"); - readPayload(inputArtifacts,fis1, "service_vmme_template_ModelName.yml"); - fis1.close(); + readPayloadFromResource(inputArtifacts, "service_vmme_template_ModelName.yml"); - InputStream fis2 = ArtifactGenerationServiceTest.class.getResourceAsStream("/vf_vmme_template_ModelName.yml"); - readPayload(inputArtifacts,fis2, "vf_vmme_template_ModelName.yml"); - fis2.close(); + readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModelName.yml"); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); Assert.assertEquals( @@ -497,13 +479,9 @@ public class ArtifactGenerationServiceTest { try { //mandatory attribute <vfModuleModelUUID> missing in Artifact List<Artifact> inputArtifacts = new ArrayList<>(); - InputStream fis1 = ArtifactGenerationServiceTest.class.getResourceAsStream("/service_vmme_template_ModelUUID.yml"); - readPayload(inputArtifacts,fis1, "service_vmme_template_ModelUUID.yml"); - fis1.close(); + readPayloadFromResource(inputArtifacts, "service_vmme_template_ModelUUID.yml"); - InputStream fis2 = ArtifactGenerationServiceTest.class.getResourceAsStream("/vf_vmme_template_ModelUUID.yml"); - readPayload(inputArtifacts,fis2, "vf_vmme_template_ModelUUID.yml"); - fis2.close(); + readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModelUUID.yml"); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); Assert.assertEquals( @@ -521,13 +499,9 @@ public class ArtifactGenerationServiceTest { try { //invalid id since not of length 36 for <vfModuleModelUUID> List<Artifact> inputArtifacts = new ArrayList<>(); - InputStream fis1 = ArtifactGenerationServiceTest.class.getResourceAsStream("/service_vmme_template_InvalidVfModUuIdAttr.yml"); - readPayload(inputArtifacts,fis1, "service_vmme_template_InvalidVfModUuIdAttr.yml"); - fis1.close(); + readPayloadFromResource(inputArtifacts, "service_vmme_template_InvalidVfModUuIdAttr.yml"); - InputStream fis2 = ArtifactGenerationServiceTest.class.getResourceAsStream("/vf_vmme_template_InvalidVfModUuIdAttr.yml"); - readPayload(inputArtifacts,fis2, "vf_vmme_template_InvalidVfModUuIdAttr.yml"); - fis2.close(); + readPayloadFromResource(inputArtifacts, "vf_vmme_template_InvalidVfModUuIdAttr.yml"); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); Assert.assertEquals( @@ -544,13 +518,9 @@ public class ArtifactGenerationServiceTest { try { //mandatory attribute <vfModuleModelVersion> missing List<Artifact> inputArtifacts = new ArrayList<>(); - InputStream fis1 = ArtifactGenerationServiceTest.class.getResourceAsStream("/service_vmme_template_ModelVersion.yml"); - readPayload(inputArtifacts,fis1, "service_vmme_template_ModelVersion.yml"); - fis1.close(); + readPayloadFromResource(inputArtifacts, "service_vmme_template_ModelVersion.yml"); - InputStream fis2 = ArtifactGenerationServiceTest.class.getResourceAsStream("/vf_vmme_template_ModelVersion.yml"); - readPayload(inputArtifacts,fis2, "vf_vmme_template_ModelVersion.yml"); - fis2.close(); + readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModelVersion.yml"); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); Assert.assertEquals( @@ -567,9 +537,7 @@ public class ArtifactGenerationServiceTest { String configLoc = System.getProperty("artifactgenerator.config"); try { System.clearProperty("artifactgenerator.config"); - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); - List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "aai/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); @@ -599,17 +567,14 @@ public class ArtifactGenerationServiceTest { .getName(); String assertMsg = ArtifactType.AAI.name() + ".model-version-id." +Widget.getWidget (Widget.Type.SERVICE).getName(); - OutputStream fos = new FileOutputStream(new File(configLoc)); - String serviceWidgetId = properties.getProperty(serviceWidgetName); //Remove property from Artifact-Generator.properties properties.remove(serviceWidgetName); - properties.store(fos,null); - fos.close(); + try (OutputStream fos = new FileOutputStream(new File(configLoc))) { + properties.store(fos,null); + } - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); - List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "aai/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); @@ -634,9 +599,7 @@ public class ArtifactGenerationServiceTest { String configLoc = System.getProperty("artifactgenerator.config"); try { System.setProperty("artifactgenerator.config",configLoc + File.separator + "testErrorWhenNoFileAtConfigLocation"); - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); - List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "aai/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); @@ -656,9 +619,7 @@ public class ArtifactGenerationServiceTest { // scenario service with VF anf vfmodule but no service version in additional parameter try { additionalParams.clear(); - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); - List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "aai/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); @@ -678,7 +639,6 @@ public class ArtifactGenerationServiceTest { // Sunny day scenario service with VF anf vfmodule and service version as adiitional parameter try { additionalParams.put(AdditionalParams.ServiceVersion.getName(),"9.0"); - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "aai/"; @@ -703,7 +663,7 @@ public class ArtifactGenerationServiceTest { Assert.assertEquals(resultData.size(),5); // 1-service,1-VF-resource,1-vfmodule and 2 // others - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); testResourceTosca(toscas.iterator(), outputArtifactMap); } else { @@ -722,9 +682,7 @@ public class ArtifactGenerationServiceTest { public void testErrorWhenInvalidServiceVersion() { // scenario service with VF anf vfmodule but invalid service version in additional parameter try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); - List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "aai/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); @@ -785,9 +743,7 @@ public class ArtifactGenerationServiceTest { public void testErrorWhenNoResourceVersion() { // scenario service with VF but missing resource version in service tosca try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); - List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testErrorWhenNoResourceVersion/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); @@ -803,9 +759,7 @@ public class ArtifactGenerationServiceTest { public void testErrorWhenInvalidResourceVersion1() { // scenario service with VF but invalid resource version 0.0 in service tosca try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); - List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testErrorWhenInvalidResourceVersion1/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); @@ -821,9 +775,7 @@ public class ArtifactGenerationServiceTest { public void testErrorWhenInvalidResourceVersion2() { // scenario service with VF but invalid resource version 1 in service tosca try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); - List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testErrorWhenInvalidResourceVersion2/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); @@ -838,7 +790,6 @@ public class ArtifactGenerationServiceTest { @Test public void testArtifactGenerationAllottedResourceWithIpMuxAndTunnelXConn() { try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testArtifactGeneration15/"; @@ -851,7 +802,7 @@ public class ArtifactGenerationServiceTest { List<Artifact> resultData = data.getResultData(); Assert.assertEquals(resultData.size(),5); - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); testResourceTosca(toscas.iterator(), outputArtifactMap); } else { @@ -866,9 +817,7 @@ public class ArtifactGenerationServiceTest { // @Test public void testErrorWhenAllottedResourceWithOutProvidingServiceId() { try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); - List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testErrorWhenAllottedResourceWithOutDependingServiceId/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); @@ -883,9 +832,7 @@ public class ArtifactGenerationServiceTest { @Test public void testArtifactGenerationWithoutAllottedResource() { try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); - List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testArtifactGeneration16/"; List<Artifact> inputArtifacts = init(aaiResourceBasePaths); GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams); @@ -900,7 +847,6 @@ public class ArtifactGenerationServiceTest { @Test public void testArtifactGenerationAllottedResourceIpmuxSameInvariantDiffVersion() { try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testArtifactGeneration17/"; @@ -913,7 +859,7 @@ public class ArtifactGenerationServiceTest { List<Artifact> resultData = data.getResultData(); Assert.assertEquals(resultData.size(),5); - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); testResourceTosca(toscas.iterator(), outputArtifactMap); } else { @@ -928,7 +874,6 @@ public class ArtifactGenerationServiceTest { @Test public void testArtifactGenerationAllottedResourceIpmuxSameInvariantSameVersion() { try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testArtifactGeneration18/"; @@ -942,7 +887,7 @@ public class ArtifactGenerationServiceTest { Assert.assertEquals(resultData.size(),4); - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); testResourceTosca(toscas.iterator(), outputArtifactMap); } else { @@ -957,7 +902,6 @@ public class ArtifactGenerationServiceTest { @Test public void testArtifactGenerationAllottedResourceIpmuxWithGroups() { try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testAllotedResourceWithDependingSerWithGroups/"; @@ -970,7 +914,7 @@ public class ArtifactGenerationServiceTest { List<Artifact> resultData = data.getResultData(); Assert.assertEquals(resultData.size(),5); - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); testResourceTosca(toscas.iterator(), outputArtifactMap); } else { @@ -985,7 +929,6 @@ public class ArtifactGenerationServiceTest { @Test public void testArtifactGenerationAllottedResourceWithVF() { try { - Map<String, Model> outputArtifactMap = new HashMap<>(); ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl(); List<ToscaTemplate> toscas = new LinkedList<>(); String aaiResourceBasePaths = "testArtifactGenerationAllottedResourceWithVF/"; @@ -998,7 +941,7 @@ public class ArtifactGenerationServiceTest { List<Artifact> resultData = data.getResultData(); Assert.assertEquals(resultData.size(),7); - outputArtifactMap = populateAAIGeneratedModelStore(resultData); + Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData); testServiceTosca(toscas, outputArtifactMap); testResourceTosca(toscas.iterator(), outputArtifactMap); } else { @@ -1500,7 +1443,7 @@ nodeProperties = node.getProperties(); return matchedModelElements; } - private Map<String, Model> populateAAIGeneratedModelStore(List<Artifact> resultData) { + private Map<String, Model> populateAAIGeneratedModelStore(List<Artifact> resultData) throws IOException { Map<String, Model> outputArtifactMap = new HashMap<>(); for (Artifact outputArtifact : resultData) { if (outputArtifact.getType().equals(ArtifactType.MODEL_INVENTORY_PROFILE.name())) { @@ -1513,16 +1456,21 @@ nodeProperties = node.getProperties(); return outputArtifactMap; } - private Model getUnmarshalledArtifactModel(String aaiModel) { - JAXBContext jaxbContext; + private Model getUnmarshalledArtifactModel(String aaiModel) throws IOException { + try { - jaxbContext = JAXBContext.newInstance(Model.class); + + JAXBContext jaxbContext = JAXBContext.newInstance(Model.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); - InputStream aaiModelStream = new ByteArrayInputStream(aaiModel.getBytes()); - return (Model) unmarshaller.unmarshal(aaiModelStream); + + try (InputStream aaiModelStream = new ByteArrayInputStream(aaiModel.getBytes())) { + return (Model) unmarshaller.unmarshal(aaiModelStream); + } + } catch (JAXBException e) { e.printStackTrace(); } + return null; } @@ -1567,20 +1515,6 @@ nodeProperties = node.getProperties(); return null; } - - private ToscaTemplate getResourceTosca(List<ToscaTemplate> input) { - Iterator<ToscaTemplate> iter = input.iterator(); - while (iter.hasNext()) { - ToscaTemplate tosca = iter.next(); - if (!tosca.isService()) { - iter.remove(); - return tosca; - } - } - return null; - } - - private Map<String, String> getVFModuleMetadataTosca(ToscaTemplate toscaTemplate, String vfModuleModelUUID) { Map<String, GroupDefinition> groupDefinitionMap = @@ -1589,7 +1523,7 @@ nodeProperties = node.getProperties(); for (Map.Entry<String, GroupDefinition> e : groupDefinitionMap.entrySet()) { if (e.getValue().getType().contains("org.openecomp.groups.VfModule")) { String uuid = e.getValue().getMetadata().get("vfModuleModelUUID"); - if (uuid == vfModuleModelUUID) { + if (Objects.equals(uuid, vfModuleModelUUID)) { vfModuleModelMetadata = e.getValue().getMetadata(); vfModuleModelMetadata.put("vf_module_description", (String) e.getValue().getProperties().get("vf_module_description")); @@ -1606,17 +1540,15 @@ nodeProperties = node.getProperties(); Map<String, NodeTemplate> nodeTemplateMaps = toscaTemplate.getTopology_template().getNode_templates(); Map<String, Object> vfModuleMembers = new LinkedHashMap<>(); - List<String> vfModuleModelMetadata = new ArrayList<>(); + List<String> vfModuleModelMetadata; for (Map.Entry<String, GroupDefinition> e : groupDefinitionMap.entrySet()) { if (e.getValue().getType().contains("org.openecomp.groups.VfModule")) { String uuid = e.getValue().getMetadata().get("vfModuleModelUUID"); - if (uuid == vfModuleModelUUID) { + if (Objects.equals(uuid, vfModuleModelUUID)) { vfModuleModelMetadata = e.getValue().getMembers(); if (vfModuleModelMetadata !=null) { - Iterator itr = vfModuleModelMetadata.iterator(); - while (itr.hasNext()) { - Object obj = itr.next(); - NodeTemplate nodeTemplate = nodeTemplateMaps.get(obj); + for (Object key : vfModuleModelMetadata) { + NodeTemplate nodeTemplate = nodeTemplateMaps.get(key); String nodetype = null; if (nodeTemplate != null) { nodetype = nodeTemplate.getType(); @@ -1624,7 +1556,7 @@ nodeProperties = node.getProperties(); if (nodetype != null) { String widgetType = membersType(nodetype); if (widgetType != null) { - vfModuleMembers.put(widgetType, obj); + vfModuleMembers.put(widgetType, key); } } } @@ -1686,24 +1618,24 @@ nodeProperties = node.getProperties(); Assert.fail("Invalid resource directory"); } - for (int i = 0; i < resourceFileList.length; i++) { - byte[] payload = null; + for (String aResourceFileList : resourceFileList) { File resourceFile = new File( - this.getClass().getClassLoader().getResource(aaiResourceBasePaths + resourceFileList[i]) - .getPath()); - FileInputStream fileInputStream; + this.getClass().getClassLoader().getResource(aaiResourceBasePaths + aResourceFileList) + .getPath()); + //convert service tosca file into array of bytes - payload = new byte[(int) resourceFile.length()]; - fileInputStream = new FileInputStream(resourceFile); - fileInputStream.read(payload); - fileInputStream.close(); + byte[] payload = new byte[(int) resourceFile.length()]; + try (FileInputStream fileInputStream = new FileInputStream(resourceFile)) { + fileInputStream.read(payload); + } + String checksum = GeneratorUtil.checkSum(payload); byte[] encodedPayload = GeneratorUtil.encode(payload); Artifact artifact = - new Artifact(aaiArtifactType, aaiArtifactGroupType, checksum, encodedPayload); - artifact.setName(resourceFileList[i]); - artifact.setLabel(resourceFileList[i]); - artifact.setDescription(resourceFileList[i]); + new Artifact(aaiArtifactType, aaiArtifactGroupType, checksum, encodedPayload); + artifact.setName(aResourceFileList); + artifact.setLabel(aResourceFileList); + artifact.setDescription(aResourceFileList); artifact.setVersion("1.0"); inputArtifacts1.add(artifact); @@ -1742,4 +1674,10 @@ nodeProperties = node.getProperties(); } } } + + private void readPayloadFromResource(List<Artifact> inputArtifacts, String file) throws IOException { + try (InputStream stream = ArtifactGenerationServiceTest.class.getResourceAsStream("/" + file)) { + readPayload(inputArtifacts, stream, file); + } + } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java index 8de222e688..d5cd56e058 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java @@ -23,10 +23,13 @@ package org.openecomp.core.utilities.file; import org.apache.commons.collections4.MapUtils; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.Set; +import java.util.function.Function; public class FileContentHandler { @@ -45,12 +48,31 @@ public class FileContentHandler { return null; } - ByteArrayInputStream is = new ByteArrayInputStream(content); - return is; + return new ByteArrayInputStream(content); } - public void addFile(String fileName, byte[] contect) { - files.put(fileName, contect); + /** + * Applies a business logic to a file's content while taking care of all retrieval logic. + * + * @param fileName name of a file inside this content handler. + * @param processor the business logic to work on the file's input stream, which may not be set + * (check the {@link Optional} if no such file can be found + * @param <T> return type, may be {@link java.lang.Void} + * + * @return result produced by the processor + */ + public <T> T processFileContent(String fileName, Function<Optional<InputStream>, T> processor) { + + // do not throw IOException to mimic the existing uses of getFileContent() + try (InputStream contentInputStream = getFileContent(fileName)) { + return processor.apply(Optional.ofNullable(contentInputStream)); + } catch (IOException e) { + throw new ProcessingException("Failed to process file: " + fileName, e); + } + } + + public void addFile(String fileName, byte[] content) { + files.put(fileName, content); } public void addFile(String fileName, InputStream is) { @@ -94,4 +116,26 @@ public class FileContentHandler { public boolean containsFile(String fileName) { return files.containsKey(fileName); } + + /** + * An application-specific runtime exception + */ + private static class ProcessingException extends RuntimeException { + + public ProcessingException() { + super(); + } + + public ProcessingException(String message) { + super(message); + } + + public ProcessingException(Throwable cause) { + super(cause); + } + + public ProcessingException(String msg, Throwable cause) { + super(msg, cause); + } + } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java new file mode 100644 index 0000000000..527ba22c1d --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java @@ -0,0 +1,53 @@ +package org.openecomp.core.utilities.file; + +import org.testng.annotations.Test; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Optional; + +import static org.testng.Assert.*; + +/** + * @author EVITALIY + * @since 24 Oct 17 + */ +public class FileContentHandlerTest { + + private static final String FILE_NAME = "test-file.txt"; + + @Test + public void testProcessFileContent() throws Exception { + + final int size = 13; + FileContentHandler contentHandler = new FileContentHandler(); + final byte[] content = new byte[size]; + Arrays.fill(content, (byte) 44); + contentHandler.addFile(FILE_NAME, content); + assertEquals(contentHandler.processFileContent(FILE_NAME, optional -> { + + try { + byte[] buffer = new byte[size]; + assertTrue(optional.isPresent()); + assertEquals(size, optional.get().read(buffer)); + return buffer; + } catch (IOException e) { + throw new RuntimeException("Unexpected error", e); + } + + }), content); + } + + @Test + public void testProcessEmptyFileContent() throws Exception { + FileContentHandler contentHandler = new FileContentHandler(); + contentHandler.addFile(FILE_NAME, new byte[0]); + assertFalse(contentHandler.processFileContent(FILE_NAME, Optional::isPresent)); + } + + @Test + public void testProcessNoFileContent() throws Exception { + FileContentHandler contentHandler = new FileContentHandler(); + assertFalse(contentHandler.processFileContent("filename", Optional::isPresent)); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/EnvironmentTest.java b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/EnvironmentTest.java index fc01e2e8b0..6312e5a575 100644 --- a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/EnvironmentTest.java +++ b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/EnvironmentTest.java @@ -23,16 +23,18 @@ package org.openecomp.sdc.heat.datatypes.model; import org.junit.Test; import org.openecomp.sdc.tosca.services.YamlUtil; +import java.io.IOException; import java.io.InputStream; public class EnvironmentTest { @Test - public void testYamlToServiceTemplateObj() { + public void testYamlToServiceTemplateObj() throws IOException { YamlUtil yamlUtil = new YamlUtil(); - InputStream yamlFile = yamlUtil.loadYamlFileIs("/mock/model/envSettings.env"); - Environment envVars = yamlUtil.yamlToObject(yamlFile, Environment.class); - envVars.toString(); + try (InputStream yamlFile = yamlUtil.loadYamlFileIs("/mock/model/envSettings.env")) { + Environment envVars = yamlUtil.yamlToObject(yamlFile, Environment.class); + envVars.toString(); + } } @Test @@ -46,9 +48,8 @@ public class EnvironmentTest { if (heatResourceName.length() == lastIndexOfUnderscore) { System.out.println(heatResourceName); } else { - String heatResourceNameSuffix = heatResourceName.substring(lastIndexOfUnderscore + 1); + try { - int heatResourceNameSuffixInt = Integer.parseInt(heatResourceNameSuffix); System.out.println(heatResourceName.substring(0, lastIndexOfUnderscore)); } catch (NumberFormatException ignored) { System.out.println(heatResourceName); diff --git a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/HeatOrchestrationTemplateTest.java b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/HeatOrchestrationTemplateTest.java index 3715b0f999..dd8abdc0b9 100644 --- a/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/HeatOrchestrationTemplateTest.java +++ b/openecomp-be/lib/openecomp-heat-lib/src/test/java/org/openecomp/sdc/heat/datatypes/model/HeatOrchestrationTemplateTest.java @@ -26,6 +26,7 @@ import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.tosca.services.YamlUtil; +import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; @@ -37,12 +38,13 @@ public class HeatOrchestrationTemplateTest { private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName()); @Test - public void testYamlToServiceTemplateObj() { + public void testYamlToServiceTemplateObj() throws IOException { YamlUtil yamlUtil = new YamlUtil(); - InputStream yamlFile = yamlUtil.loadYamlFileIs("/mock/model/testHeat.yml"); - HeatOrchestrationTemplate heatOrchestrationTemplate = - yamlUtil.yamlToObject(yamlFile, HeatOrchestrationTemplate.class); - heatOrchestrationTemplate.toString(); + try (InputStream yamlFile = yamlUtil.loadYamlFileIs("/mock/model/testHeat.yml")) { + HeatOrchestrationTemplate heatOrchestrationTemplate = + yamlUtil.yamlToObject(yamlFile, HeatOrchestrationTemplate.class); + heatOrchestrationTemplate.toString(); + } } @Test diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java index c7f4e3f6d1..e8c9c602f8 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java @@ -46,6 +46,7 @@ import org.openecomp.sdc.tosca.services.ToscaConstants; import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil; import org.openecomp.sdc.tosca.services.YamlUtil; +import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; @@ -271,27 +272,29 @@ public class ToscaModelTest { @Test - public void testYamlToServiceTemplateObj() { - InputStream yamlFile = new YamlUtil().loadYamlFileIs("/mock/model/serviceTemplate.yaml"); - ServiceTemplate serviceTemplateFromYaml = - new YamlUtil().yamlToObject(yamlFile, ServiceTemplate.class); - Assert.assertNotNull(serviceTemplateFromYaml); + public void testYamlToServiceTemplateObj() throws IOException { + try (InputStream yamlFile = new YamlUtil().loadYamlFileIs("/mock/model/serviceTemplate.yaml")) { + ServiceTemplate serviceTemplateFromYaml = + new YamlUtil().yamlToObject(yamlFile, ServiceTemplate.class); + Assert.assertNotNull(serviceTemplateFromYaml); + } } @Test - public void testYamlToServiceTemplateIncludingHeatExtend() { + public void testYamlToServiceTemplateIncludingHeatExtend() throws IOException { ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - InputStream yamlFile = - toscaExtensionYamlUtil.loadYamlFileIs("/mock/model/serviceTemplateHeatExtend.yaml"); - ServiceTemplate serviceTemplateFromYaml = - toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); - ParameterDefinitionExt parameterDefinitionExt = - (ParameterDefinitionExt) serviceTemplateFromYaml.getTopology_template().getInputs() - .get("inParam1"); - Assert.assertNotNull(parameterDefinitionExt.getLabel()); - String backToYamlString = toscaExtensionYamlUtil.objectToYaml(serviceTemplateFromYaml); - Assert.assertNotNull(backToYamlString); + try (InputStream yamlFile = + toscaExtensionYamlUtil.loadYamlFileIs("/mock/model/serviceTemplateHeatExtend.yaml")) { + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + ParameterDefinitionExt parameterDefinitionExt = + (ParameterDefinitionExt) serviceTemplateFromYaml.getTopology_template().getInputs() + .get("inParam1"); + Assert.assertNotNull(parameterDefinitionExt.getLabel()); + String backToYamlString = toscaExtensionYamlUtil.objectToYaml(serviceTemplateFromYaml); + Assert.assertNotNull(backToYamlString); + } } } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java index 7ae91abfa5..aad21634a8 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java @@ -78,9 +78,9 @@ public class ToscaAnalyzerServiceImplTest { public ExpectedException thrown = ExpectedException.none(); @Mock - NodeTemplate nodeTemplateMock; + private NodeTemplate nodeTemplateMock; @Mock - ToscaServiceModel toscaServiceModelMock; + private ToscaServiceModel toscaServiceModelMock; @BeforeClass public static void onlyOnceSetUp() throws IOException { @@ -97,27 +97,29 @@ public class ToscaAnalyzerServiceImplTest { @Test public void testGetRequirements() throws Exception { ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - InputStream yamlFile = toscaExtensionYamlUtil - .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml"); - ServiceTemplate - serviceTemplateFromYaml = - toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); - - NodeTemplate port_0 = - serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui_port_0"); - List<RequirementAssignment> reqList = - toscaAnalyzerService.getRequirements(port_0, ToscaConstants.BINDING_REQUIREMENT_ID); - assertEquals(1, reqList.size()); - - reqList.clear(); - NodeTemplate port_1 = - serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui1_port_1"); - reqList = toscaAnalyzerService.getRequirements(port_1, ToscaConstants.LINK_REQUIREMENT_ID); - assertEquals(2, reqList.size()); - - reqList.clear(); - reqList = toscaAnalyzerService.getRequirements(port_0, ToscaConstants.LINK_REQUIREMENT_ID); - assertEquals(0, reqList.size()); + try (InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) { + + ServiceTemplate + serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + + NodeTemplate port_0 = + serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui_port_0"); + List<RequirementAssignment> reqList = + toscaAnalyzerService.getRequirements(port_0, ToscaConstants.BINDING_REQUIREMENT_ID); + assertEquals(1, reqList.size()); + + reqList.clear(); + NodeTemplate port_1 = + serviceTemplateFromYaml.getTopology_template().getNode_templates().get("cmaui1_port_1"); + reqList = toscaAnalyzerService.getRequirements(port_1, ToscaConstants.LINK_REQUIREMENT_ID); + assertEquals(2, reqList.size()); + + reqList.clear(); + reqList = toscaAnalyzerService.getRequirements(port_0, ToscaConstants.LINK_REQUIREMENT_ID); + assertEquals(0, reqList.size()); + } } @Test @@ -171,44 +173,45 @@ public class ToscaAnalyzerServiceImplTest { .getSubstituteServiceTemplateName("invalid1", invalidSubstitutableNodeTemplate1); assertEquals(false, substituteServiceTemplateName.isPresent()); - if (substitutableNodeTemplate.isPresent()) { - NodeTemplate invalidSubstitutableNodeTemplate2 = substitutableNodeTemplate.get(); - Object serviceTemplateFilter = invalidSubstitutableNodeTemplate2.getProperties() - .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME); + substitutableNodeTemplate.ifPresent(nodeTemplate -> { + Object serviceTemplateFilter = nodeTemplate.getProperties() + .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME); ((Map) serviceTemplateFilter).clear(); toscaAnalyzerService - .getSubstituteServiceTemplateName("invalid2", invalidSubstitutableNodeTemplate2); + .getSubstituteServiceTemplateName("invalid2", nodeTemplate); - } + }); } @Test public void testGetSubstitutableNodeTemplates() throws Exception { ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - InputStream yamlFile = toscaExtensionYamlUtil - .loadYamlFileIs("/mock/analyzerService/ServiceTemplateSubstituteTest.yaml"); - ServiceTemplate serviceTemplateFromYaml = - toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); - - Map<String, NodeTemplate> substitutableNodeTemplates = - toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml); - assertEquals(2, substitutableNodeTemplates.size()); - assertNotNull(substitutableNodeTemplates.get("test_nested1")); - assertNotNull(substitutableNodeTemplates.get("test_nested2")); + try (InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/ServiceTemplateSubstituteTest.yaml")) { + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + + Map<String, NodeTemplate> substitutableNodeTemplates = + toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml); + assertEquals(2, substitutableNodeTemplates.size()); + assertNotNull(substitutableNodeTemplates.get("test_nested1")); + assertNotNull(substitutableNodeTemplates.get("test_nested2")); + + ServiceTemplate emptyServiceTemplate = new ServiceTemplate(); + emptyServiceTemplate.setTopology_template(new TopologyTemplate()); + substitutableNodeTemplates = + toscaAnalyzerService.getSubstitutableNodeTemplates(emptyServiceTemplate); + assertEquals(0, substitutableNodeTemplates.size()); + } - ServiceTemplate emptyServiceTemplate = new ServiceTemplate(); - emptyServiceTemplate.setTopology_template(new TopologyTemplate()); - substitutableNodeTemplates = - toscaAnalyzerService.getSubstitutableNodeTemplates(emptyServiceTemplate); - assertEquals(0, substitutableNodeTemplates.size()); - - yamlFile = toscaExtensionYamlUtil - .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml"); - serviceTemplateFromYaml = toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); - substitutableNodeTemplates = - toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml); - assertEquals(0, substitutableNodeTemplates.size()); + try (InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) { + ServiceTemplate serviceTemplateFromYaml = toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + Map<String, NodeTemplate> substitutableNodeTemplates = + toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplateFromYaml); + assertEquals(0, substitutableNodeTemplates.size()); + } } @Test @@ -217,35 +220,36 @@ public class ToscaAnalyzerServiceImplTest { thrown.expectMessage( "Invalid Tosca model data, missing 'Node Template' entry for 'Node Template' id cmaui_port_9"); ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - InputStream yamlFile = toscaExtensionYamlUtil - .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml"); - ServiceTemplate nestedServiceTemplateFromYaml = - toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); - - Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService - .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", - nestedServiceTemplateFromYaml, "local_storage_server_cmaui"); - assertEquals(true, mappedNodeTemplate.isPresent()); - if (mappedNodeTemplate.isPresent()) { - assertEquals("server_cmaui", mappedNodeTemplate.get().getKey()); - assertNotNull(mappedNodeTemplate.get().getValue()); + try (InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) { + ServiceTemplate nestedServiceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + + Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService + .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", + nestedServiceTemplateFromYaml, "local_storage_server_cmaui"); + assertEquals(true, mappedNodeTemplate.isPresent()); + mappedNodeTemplate.ifPresent(stringNodeTemplateEntry -> { + assertEquals("server_cmaui", stringNodeTemplateEntry.getKey()); + assertNotNull(stringNodeTemplateEntry.getValue()); + }); + + mappedNodeTemplate = toscaAnalyzerService + .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", + nestedServiceTemplateFromYaml, "link_cmaui_port_invalid"); + assertEquals(true, mappedNodeTemplate.isPresent()); + mappedNodeTemplate.ifPresent(stringNodeTemplateEntry -> { + assertEquals("server_cmaui", stringNodeTemplateEntry.getKey()); + assertNotNull(stringNodeTemplateEntry.getValue()); + }); + + ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates() + .get(toscaServiceModel.getEntryDefinitionServiceTemplate()); + mappedNodeTemplate = toscaAnalyzerService.getSubstitutionMappedNodeTemplateByExposedReq( + toscaServiceModel.getEntryDefinitionServiceTemplate(), mainServiceTemplate, + "local_storage_server_cmaui"); + assertEquals(false, mappedNodeTemplate.isPresent()); } - - mappedNodeTemplate = toscaAnalyzerService - .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", - nestedServiceTemplateFromYaml, "link_cmaui_port_invalid"); - assertEquals(true, mappedNodeTemplate.isPresent()); - if (mappedNodeTemplate.isPresent()) { - assertEquals("server_cmaui", mappedNodeTemplate.get().getKey()); - assertNotNull(mappedNodeTemplate.get().getValue()); - } - - ServiceTemplate mainServiceTemplate = toscaServiceModel.getServiceTemplates() - .get(toscaServiceModel.getEntryDefinitionServiceTemplate()); - mappedNodeTemplate = toscaAnalyzerService.getSubstitutionMappedNodeTemplateByExposedReq( - toscaServiceModel.getEntryDefinitionServiceTemplate(), mainServiceTemplate, - "local_storage_server_cmaui"); - assertEquals(false, mappedNodeTemplate.isPresent()); } @Test @@ -283,14 +287,15 @@ public class ToscaAnalyzerServiceImplTest { thrown.expectMessage( "Invalid Tosca model data, missing 'Node Template' entry for 'Node Template' id cmaui_port_9"); ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - InputStream yamlFile = toscaExtensionYamlUtil - .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml"); - ServiceTemplate nestedServiceTemplateFromYaml = - toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + try (InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs("/mock/analyzerService/NestedServiceTemplateReqTest.yaml")) { + ServiceTemplate nestedServiceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); - toscaAnalyzerService - .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", - nestedServiceTemplateFromYaml, "link_cmaui_port_invalid"); + toscaAnalyzerService + .getSubstitutionMappedNodeTemplateByExposedReq("NestedServiceTemplateSubstituteTest.yaml", + nestedServiceTemplateFromYaml, "link_cmaui_port_invalid"); + } } @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 4d025e1540..66dda12b4b 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 @@ -41,7 +41,7 @@ import java.util.zip.ZipFile; public class ToscaFileOutputServiceCsarImplTest { - private ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl = + private final ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl = new ToscaFileOutputServiceCsarImpl(); @Test @@ -112,21 +112,22 @@ public class ToscaFileOutputServiceCsarImplTest { String resultFileName = "resultFile.zip"; File file = new File(resultFileName); - FileOutputStream fos = new FileOutputStream(file); - fos.write(csarFile); - fos.close(); + try (FileOutputStream fos = new FileOutputStream(file)) { + fos.write(csarFile); + } - ZipFile zipFile = new ZipFile(resultFileName); + try (ZipFile zipFile = new ZipFile(resultFileName)) { - Enumeration<? extends ZipEntry> entries = zipFile.entries(); + Enumeration<? extends ZipEntry> entries = zipFile.entries(); - int count = 0; - while (entries.hasMoreElements()) { - count++; - entries.nextElement(); + int count = 0; + while (entries.hasMoreElements()) { + count++; + entries.nextElement(); + } + Assert.assertEquals(7, count); } - Assert.assertEquals(7, count); - zipFile.close(); + Files.delete(Paths.get(file.getPath())); } @@ -150,21 +151,22 @@ public class ToscaFileOutputServiceCsarImplTest { String resultFileName = "resultFile.zip"; File file = new File(resultFileName); - FileOutputStream fos = new FileOutputStream(file); - fos.write(csarFile); - fos.close(); + try (FileOutputStream fos = new FileOutputStream(file)) { + fos.write(csarFile); + } - ZipFile zipFile = new ZipFile(resultFileName); + try (ZipFile zipFile = new ZipFile(resultFileName)) { - Enumeration<? extends ZipEntry> entries = zipFile.entries(); + Enumeration<? extends ZipEntry> entries = zipFile.entries(); - int count = 0; - while (entries.hasMoreElements()) { - count++; - entries.nextElement(); + int count = 0; + while (entries.hasMoreElements()) { + count++; + entries.nextElement(); + } + Assert.assertEquals(2, count); } - Assert.assertEquals(2, count); - zipFile.close(); + Files.delete(Paths.get(file.getPath())); } } |