diff options
author | k.kazak <k.kazak@samsung.com> | 2019-05-29 14:50:05 +0200 |
---|---|---|
committer | Oren Kleks <orenkle@amdocs.com> | 2019-06-03 05:56:58 +0000 |
commit | 8d52607c8fa8fd4dde5f9c3fec0a8c6ee7a4960d (patch) | |
tree | 307b6ea5910bf02a27205a7bea73b14318c79e7f | |
parent | f4f6b1407a836266ce7d98f33ee899d8a2377d83 (diff) |
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
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 @@ <include>src/main/resources/**/*.json</include> <include>src/test/resources/**/*.json</include> </includes> + <excludes> + <exclude>src/test/resources/graphError.json</exclude> + </excludes> </validationSet> </validationSets> </configuration> @@ -742,4 +745,4 @@ </build> </profile> </profiles> -</project>
\ No newline at end of file +</project> 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<Integer> 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<String> 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<String, Object> load = yaml.loadAs(inputStream,Map.class); - Map<String, Object> nodeType = (Map<String, Object>) load.get(collectionTitle); - nodeTypeList.putAll(nodeType); - - } catch (Exception e) { - System.err.println("Error in opening file " + yamlFile.toAbsolutePath().toString()); - System.exit(1); - } - }); - } + try (Stream<Path> 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<String, Object> load = yaml.loadAs(inputStream, Map.class); + Map<String, Object> nodeType = (Map<String, Object>) 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}}" |