Fix sonar issues 33/57433/6
authorParshad Patel <pars.patel@samsung.com>
Wed, 25 Jul 2018 07:45:20 +0000 (16:45 +0900)
committerTal Gitelman <tg851x@intl.att.com>
Sun, 5 Aug 2018 10:50:55 +0000 (10:50 +0000)
Fix try-with-resources and NPE issue

Issue-ID: SDC-1529
Change-Id: Ia1071608528493e0a1a2985c458998d1197a8c0b
Signed-off-by: Parshad Patel <pars.patel@samsung.com>
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLDataAnalyzer.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/main/RemoveUtils.java

index 863f920..e533c31 100644 (file)
@@ -53,30 +53,31 @@ public class GraphMLDataAnalyzer {
                try {
                        String mlFileLocation = args[0];
                        result = _analyzeGraphMLData(mlFileLocation);
-                       System.out.println("Analyzed ML file=" + mlFileLocation + ", XLS result=" + result);
+                       log.info("Analyzed ML file=" + mlFileLocation + ", XLS result=" + result);
                } catch (Exception e) {
-                       log.info("analyze GraphML Data failed - {}" , e);
+                       log.error("analyze GraphML Data failed - {}" , e);
                        return null;
                }
                return result;
        }
 
        private String _analyzeGraphMLData(String mlFileLocation) throws Exception {
-
                // Parse ML file
                SAXBuilder builder = new SAXBuilder();
                File xmlFile = new File(mlFileLocation);
                Document document = (Document) builder.build(xmlFile);
-
+               
                // XLS data file name
                String outputFile = mlFileLocation.replace(".graphml", ".xls");
                Workbook wb = new HSSFWorkbook();
-               FileOutputStream fileOut = new FileOutputStream(outputFile);
-               writeComponents(wb, document);
-               writeComponentInstances(wb, document);
-               wb.write(fileOut);
-               fileOut.close();
-               return outputFile;
+               try(FileOutputStream fileOut = new FileOutputStream(outputFile)){
+                       writeComponents(wb, document);
+                       writeComponentInstances(wb, document);
+                       wb.write(fileOut);
+               }catch(Exception e){
+                       log.error("analyze GraphML Data failed - {}" , e);
+               }
+               return outputFile;      
        }
 
        private void writeComponents(Workbook wb, Document document) {
@@ -132,13 +133,11 @@ public class GraphMLDataAnalyzer {
                        IteratorIterable<Element> dataNodes = edge.getDescendants(filter);
                        for (Element data : dataNodes) {
                                String attributeValue = data.getAttributeValue("key");
-                               switch (attributeValue) {
-                               case "labelE":
+                               if( attributeValue.equals("labelE")) {
                                        String edgeLabel = data.getText();
                                        if (edgeLabel.equals("REQUIREMENT") || edgeLabel.equals("CAPABILITY")) {
                                                componentsHavingReqOrCap.add(edge.getAttributeValue("source"));
                                        }
-                                       break;
                                }
                        }
                }
index 5546300..4bc21b3 100644 (file)
@@ -31,31 +31,24 @@ public class RemoveUtils {
 
                if (args == null || args.length < 1) {
                        removeUsage();
-               }
-
-               String operation = args[0];
-
-               switch (operation.toLowerCase()) {
-
-               case "remove-products":
-
-                       boolean isValid = verifyParamsLength(args, 5);
-                       if (false == isValid) {
+               }else {
+                       String operation = args[0];
+                       if(operation.equalsIgnoreCase("remove-products")) {
+                               boolean isValid = verifyParamsLength(args, 5);
+                               if (!isValid) {
+                                       removeUsage();
+                                       System.exit(1);
+                               }
+                               ProductLogic productLogic = new ProductLogic();
+                               boolean result = productLogic.deleteAllProducts(args[1], args[2], args[3], args[4]);
+       
+                               if (!result) {
+                                       System.exit(2);
+                               }
+                       }else {
                                removeUsage();
-                               System.exit(1);
                        }
-
-                       ProductLogic productLogic = new ProductLogic();
-                       boolean result = productLogic.deleteAllProducts(args[1], args[2], args[3], args[4]);
-
-                       if (result == false) {
-                               System.exit(2);
-                       }
-                       break;
-               default:
-                       removeUsage();
                }
-
        }
 
        private static void removeUsage() {