fix sonar: close resources 64/88764/2
authork.kazak <k.kazak@samsung.com>
Wed, 29 May 2019 12:50:05 +0000 (14:50 +0200)
committerOren Kleks <orenkle@amdocs.com>
Mon, 3 Jun 2019 05:56:58 +0000 (05:56 +0000)
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
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidator.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java
asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidatorTest.java
asdctool/src/test/resources/graphError.json

index 074d551..2bb4059 100644 (file)
                                                                                <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>
                        </build>
                </profile>
        </profiles>
-</project>
\ No newline at end of file
+</project>
index 3d95de7..b404404 100644 (file)
@@ -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);
     }
 
index 5c3c036..eb1d487 100644 (file)
 
 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);
        }
 
index ab6c49c..06beb8d 100644 (file)
@@ -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
+}
index a5a51d6..9dcc931 100644 (file)
@@ -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}}"