From 8d52607c8fa8fd4dde5f9c3fec0a8c6ee7a4960d Mon Sep 17 00:00:00 2001 From: "k.kazak" Date: Wed, 29 May 2019 14:50:05 +0200 Subject: fix sonar: close resources Use try-with-resources to close file data stream Fix tests for GraphJsonValidator Change-Id: I20931b4ef815abfb8da8ebd6242b765d0b02ef23 Issue-ID: SDC-2340 Signed-off-by: k.kazak@samsung.com --- asdctool/pom.xml | 5 +- .../sdc/asdctool/impl/GraphJsonValidator.java | 19 ++++--- .../sdc/asdctool/main/SdcSchemaFileImport.java | 63 ++++++++++++---------- .../sdc/asdctool/impl/GraphJsonValidatorTest.java | 10 ++-- asdctool/src/test/resources/graphError.json | 2 +- 5 files changed, 58 insertions(+), 41 deletions(-) diff --git a/asdctool/pom.xml b/asdctool/pom.xml index 074d551dc4..2bb4059975 100644 --- a/asdctool/pom.xml +++ b/asdctool/pom.xml @@ -637,6 +637,9 @@ src/main/resources/**/*.json src/test/resources/**/*.json + + src/test/resources/graphError.json + @@ -742,4 +745,4 @@ - \ No newline at end of file + diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidator.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidator.java index 3d95de7d5c..b404404bc6 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidator.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidator.java @@ -22,6 +22,7 @@ package org.openecomp.sdc.asdctool.impl; import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.stream.Stream; import org.openecomp.sdc.common.log.wrappers.Logger; import java.io.IOException; @@ -42,14 +43,16 @@ public class GraphJsonValidator { ObjectMapper objectMapper = new ObjectMapper(); List invalidRows = new ArrayList<>(); AtomicInteger atomicInteger = new AtomicInteger(1); - Files.lines(Paths.get(filePath)).forEach(line -> { - try { - verifyJsonLine(objectMapper, atomicInteger, line); - } catch (RuntimeException | IOException e) { - logInvalidJsonRow(atomicInteger, line, e); - invalidRows.add(atomicInteger.get()); - } - }); + try (Stream stream = Files.lines(Paths.get(filePath))) { + stream.forEach(line -> { + try { + verifyJsonLine(objectMapper, atomicInteger, line); + } catch (RuntimeException | IOException e) { + logInvalidJsonRow(atomicInteger, line, e); + invalidRows.add(atomicInteger.get()); + } + }); + } return verificationResult(invalidRows); } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java index 5c3c0361a3..eb1d487cdd 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java @@ -20,6 +20,22 @@ package org.openecomp.sdc.asdctool.main; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.ArrayUtils; import org.openecomp.sdc.asdctool.enums.SchemaZipFileEnum; @@ -35,16 +51,6 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; -import java.io.*; -import java.nio.file.FileSystems; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - public class SdcSchemaFileImport { @@ -235,24 +241,25 @@ public class SdcSchemaFileImport { } for (String nodeTypesMainFolder : nodeTypesMainFolders) { - Files.walk(Paths.get(importToscaPath + SEPARATOR + nodeTypesMainFolder)) - .filter(path -> path.getFileName().toString().toLowerCase().endsWith(YAML_EXTENSION)) - .forEach(yamlFile -> { - try { - String path = yamlFile.toAbsolutePath().toString(); - System.out.println("Processing node type file "+path+"..."); - FileInputStream inputStream = new FileInputStream(path); - Yaml yaml = new Yaml(); - Map load = yaml.loadAs(inputStream,Map.class); - Map nodeType = (Map) load.get(collectionTitle); - nodeTypeList.putAll(nodeType); - - } catch (Exception e) { - System.err.println("Error in opening file " + yamlFile.toAbsolutePath().toString()); - System.exit(1); - } - }); - } + try (Stream paths = Files.walk(Paths.get(importToscaPath + SEPARATOR + nodeTypesMainFolder))) { + paths.filter(path -> path.getFileName().toString().toLowerCase().endsWith(YAML_EXTENSION)) + .forEach(yamlFile -> { + try { + String path = yamlFile.toAbsolutePath().toString(); + System.out.println("Processing node type file " + path + "..."); + FileInputStream inputStream = new FileInputStream(path); + Yaml yaml = new Yaml(); + Map load = yaml.loadAs(inputStream, Map.class); + Map nodeType = (Map) load.get(collectionTitle); + nodeTypeList.putAll(nodeType); + + } catch (Exception e) { + System.err.println("Error in opening file " + yamlFile.toAbsolutePath().toString()); + System.exit(1); + } + }); + } + } createAndSaveSchemaFileYaml("nodes", importFileList, collectionTitle, nodeTypeList); } diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidatorTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidatorTest.java index ab6c49cdc9..06beb8d409 100644 --- a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidatorTest.java +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidatorTest.java @@ -1,5 +1,8 @@ package org.openecomp.sdc.asdctool.impl; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import org.junit.Test; import java.nio.file.NoSuchFileException; @@ -18,6 +21,7 @@ public class GraphJsonValidatorTest { // default test testSubject = createTestSubject(); result = testSubject.verifyTitanJson("src/test/resources/graph.json"); + assertTrue(result); } @Test @@ -28,15 +32,15 @@ public class GraphJsonValidatorTest { // default test testSubject = createTestSubject(); result = testSubject.verifyTitanJson("src/test/resources/graphError.json"); + assertFalse(result); } @Test(expected=NoSuchFileException.class) public void testVerifyTitanJsonNoFile() throws Exception { GraphJsonValidator testSubject; - boolean result; // default test testSubject = createTestSubject(); - result = testSubject.verifyTitanJson("stam"); + testSubject.verifyTitanJson("stam"); } -} \ No newline at end of file +} diff --git a/asdctool/src/test/resources/graphError.json b/asdctool/src/test/resources/graphError.json index a5a51d6693..9dcc9312f4 100644 --- a/asdctool/src/test/resources/graphError.json +++ b/asdctool/src/test/resources/graphError.json @@ -1 +1 @@ -"ERRRRORROROR{{{\"container\":zxcvfxcvxcvxc{\"accessContcxvxcvrolPolicyIDs\":[\"/in-cse/acp-7cxvxcvxcvx1663881\"],\"creationTime\":\"20170630T111742\",\"currentByteSize\":0,\"currentNrOfInstances\":0,\"expirationTime\":\"20180630T111742\",\"lastModifiedTime\":\"20170630T111742\",\"latest\":\"/in-cse/in-name/cnt_900407520/la\",\"maxByteSize\":10000,\"maxInstanceAge\":0,\"maxNrOfInstances\":10,\"oldest\":\"/in-cse/in-name/cnt_900407520/ol\",\"parentID\":\"/in-cse\",\"resourceID\":\"/in-cse/cnt-900407520\",\"resourceName\":((\"cnt_900407520\",\"resourceTypeR\"RXCFV:\"int3\",\"stateTag\":0}}" \ No newline at end of file +(("ERRRRORROROR{{{\"container\":zxcvfxcvxcvxc{\"accessContcxvxcvrolPolicyIDs\":[\"/in-cse/acp-7cxvxcvxcvx1663881\"],\"creationTime\":\"20170630T111742\",\"currentByteSize\":0,\"currentNrOfInstances\":0,\"expirationTime\":\"20180630T111742\",\"lastModifiedTime\":\"20170630T111742\",\"latest\":\"/in-cse/in-name/cnt_900407520/la\",\"maxByteSize\":10000,\"maxInstanceAge\":0,\"maxNrOfInstances\":10,\"oldest\":\"/in-cse/in-name/cnt_900407520/ol\",\"parentID\":\"/in-cse\",\"resourceID\":\"/in-cse/cnt-900407520\",\"resourceName\":((\"cnt_900407520\",\"resourceTypeR\"RXCFV:\"int3\",\"stateTag\":0}}" -- cgit 1.2.3-korg