Fix sonar issues 81/109481/10
authorsebdet <sebastien.determe@intl.att.com>
Wed, 24 Jun 2020 09:32:27 +0000 (11:32 +0200)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Sun, 5 Jul 2020 08:46:33 +0000 (08:46 +0000)
Fix sonar/checkstyle issues in sdc code

Issue-ID: SDC-3158
Change-Id: Ic29f9233838e25c195d2b349bfac8e6d56888609
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ProductLogic.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/RestUtils.java
asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/RestUtilsTest.java

index e60640f..d757d81 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
 package org.openecomp.sdc.asdctool.impl;
 
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.tinkerpop.gremlin.structure.Transaction;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.janusgraph.core.JanusGraph;
 import org.janusgraph.core.JanusGraphFactory;
-import org.janusgraph.core.JanusGraphVertex;
 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
 import org.openecomp.sdc.common.log.wrappers.Logger;
 
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
 /**
  * Created by mlando on 2/23/2016.
  */
 public class ProductLogic {
 
-       private static Logger log = Logger.getLogger(ProductLogic.class.getName());
+    private static final Logger log = Logger.getLogger(ProductLogic.class.getName());
 
-       public boolean deleteAllProducts(String janusGraphFile, String beHost, String bePort, String adminUser) {
-               log.debug("retrieving all products from graph");
-               RestUtils restUtils = null;
+    public boolean deleteAllProducts(String janusGraphFile, String beHost, String bePort, String adminUser) {
+        log.debug("retrieving all products from graph");
         List<String> productList = getAllProducts(janusGraphFile);
-        restUtils = new RestUtils();
-        if (productList != null) {
-            for (String productUid : productList) {
-                restUtils.deleteProduct(productUid, beHost, bePort, adminUser);
-            }
-            return true;
-        }
-        else {
+        if (productList.isEmpty()) {
             log.error("failed to get products from graph");
             return false;
         }
-       }
-
-       private List<String> getAllProducts(String janusGraphFile) {
-               JanusGraph graph = null;
-               try {
-                       graph = openGraph(janusGraphFile);
-                       List<String> productsToDelete = new ArrayList<String>();
-                       Iterable vertices = graph.query()
-                                       .has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Product.getName()).vertices();
-                       if (vertices != null) {
-                               Iterator<JanusGraphVertex> iter = vertices.iterator();
-                               while (iter.hasNext()) {
-                                       Vertex vertex = iter.next();
-                                       String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
-                                       productsToDelete.add(id);
-                               }
-                       }
-
-                       graph.tx().commit();
-                       return productsToDelete;
-               } catch (Exception e) {
-            e.printStackTrace();
-            log.info("get All Products failed - {}" , e);
-                       if(graph != null) {
-                           graph.tx().rollback();
-                       }
-                       return null;
-
-               } finally {
-                       if (graph != null) {
-                               graph.close();
-                       }
-               }
-       }
-
-       private JanusGraph openGraph(String janusGraphFileLocation) {
-
-               return JanusGraphFactory.open(janusGraphFileLocation);
-               
-       }
+        for (String productUid : productList) {
+            new RestUtils().deleteProduct(productUid, beHost, bePort, adminUser);
+        }
+        return true;
+    }
 
+    private List<String> getAllProducts(String janusGraphFile) {
+        List<String> productsToDelete = new ArrayList<>();
+        Transaction transac = null;
+        try (JanusGraph graph = JanusGraphFactory.open(janusGraphFile)) {
+            transac = graph.tx();
+            Iterable vertices = graph.query()
+                    .has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Product.getName()).vertices();
+            if (vertices != null) {
+                for (Vertex vertex : (Iterable<Vertex>) vertices) {
+                    String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
+                    productsToDelete.add(id);
+                }
+            }
+            transac.commit();
+        } catch (Exception e) {
+            log.error("get All Products failed", e);
+            if (transac != null) {
+                transac.rollback();
+            }
+        }
+        return productsToDelete;
+    }
 }
index aaafdd2..3332131 100644 (file)
@@ -51,8 +51,7 @@ public class RestUtils {
             return status;
         } catch (Exception e) {
             log.error("Product uid:{} delete failed with exception", productUid, e);
+            return HttpStatus.SC_INTERNAL_SERVER_ERROR;
         }
-        return null;
     }
-
 }
index 9067273..4afa3a1 100644 (file)
 
 package org.openecomp.sdc.asdctool.impl;
 
-import org.junit.Test;
+
+import org.apache.http.HttpStatus;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import org.junit.jupiter.api.Test;
 
 public class RestUtilsTest {
 
@@ -39,6 +42,9 @@ public class RestUtilsTest {
 
                // default test
                testSubject = createTestSubject();
-               result = testSubject.deleteProduct(productUid, beHost, bePort, adminUser);
+               assertEquals(Integer.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR), testSubject.deleteProduct(productUid,
+                                                                                                                                                                                                                       beHost,
+                                                                                                                                                                                                                       bePort,
+                                                                                                                                                                                                                       adminUser));
        }
 }