diff options
author | 2017-10-24 11:20:03 +0300 | |
---|---|---|
committer | 2017-10-24 11:24:03 +0300 | |
commit | 43645dccbaced7a45b37f9e05221186231c39d74 (patch) | |
tree | 4e503cc762adf04d95366e08955c5ba5bd4b8c59 /openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp | |
parent | aa61644704edc4b2ef231d75b537b38d0ae6d7c6 (diff) |
Fixed resources not being closed in tests
Fixed static analysis violations in a few modules of SDC onboarding -
high-severity issues like not releasing resources (e.g. FileInputStream).
Did some minor code cleanup.
Change-Id: I89a229ad6bc150951f1f3cc437b3a175a663e203
Issue-ID: SDC-291
Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp')
3 files changed, 135 insertions, 125 deletions
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())); } } |