From 9c49e4b01ef5300026b62afa4e5b6cc591469dc1 Mon Sep 17 00:00:00 2001 From: "k.kedron" Date: Fri, 13 Mar 2020 16:40:27 +0100 Subject: Added new unit tests Added new unit tests to GraphMLConverter class. Fixed the Sonar issue. Issue-ID: SDC-2327 Signed-off-by: Krystian Kedron Change-Id: I985a860012c0c2b0b6524c0a2346415cb113d83c --- .../java/org/openecomp/sdc/asdctool/Utils.java | 11 +-- .../sdc/asdctool/impl/GraphMLConverter.java | 83 ++++++---------------- 2 files changed, 28 insertions(+), 66 deletions(-) (limited to 'asdctool/src/main/java/org/openecomp') diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java index 378b81aaf2..4c52647f65 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java @@ -20,6 +20,7 @@ package org.openecomp.sdc.asdctool; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.configuration.Configuration; import org.apache.tinkerpop.gremlin.structure.Element; import org.apache.tinkerpop.gremlin.structure.Property; @@ -38,7 +39,10 @@ public class Utils { private static Logger log = Logger.getLogger(Utils.class.getName()); - public static String NEW_LINE = System.getProperty("line.separator"); + public static final String NEW_LINE = System.getProperty("line.separator"); + + private Utils() { + } public static Response buildOkResponse( /* @@ -122,10 +126,9 @@ public class Utils { public static Map getProperties(Element element) { - Map result = new HashMap(); - ; + Map result = new HashMap<>(); - if (element.keys() != null && element.keys().size() > 0) { + if (CollectionUtils.isNotEmpty(element.keys())) { Map propertyMap = ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()])); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java index ae4a55903a..d0eea9a936 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java @@ -23,15 +23,12 @@ package org.openecomp.sdc.asdctool.impl; import com.google.gson.Gson; import org.apache.commons.configuration.BaseConfiguration; import org.apache.commons.lang3.tuple.ImmutablePair; -import org.apache.tinkerpop.gremlin.structure.Element; import org.apache.tinkerpop.gremlin.structure.Graph; -import org.apache.tinkerpop.gremlin.structure.Property; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.apache.tinkerpop.gremlin.structure.io.IoCore; import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper; import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader; import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter; -import org.apache.tinkerpop.gremlin.structure.util.ElementHelper; import org.janusgraph.core.JanusGraph; import org.janusgraph.core.JanusGraphFactory; import org.janusgraph.core.JanusGraphQuery; @@ -49,12 +46,12 @@ import java.io.FileWriter; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import static org.openecomp.sdc.asdctool.Utils.getProperties; + public class GraphMLConverter { private static final String STORAGE_BACKEND = "storage.backend"; @@ -65,7 +62,7 @@ public class GraphMLConverter { private static final String DOT_JSON = ".json"; - private static final String EXPORTED_FILE = "Exported file="; + private static final String EXPORTED_FILE = "Exported file={}"; private static final String NODE_LABEL = "nodeLabel"; @@ -96,7 +93,6 @@ public class GraphMLConverter { return importJsonGraph(graph, inputFile, propertiesCriteriaToDelete); } catch (Exception e) { - e.printStackTrace(); log.info("import graph failed ", e); return false; } finally { @@ -123,7 +119,6 @@ public class GraphMLConverter { log.info(LOG_FORMATTER, EXPORTED_FILE, result); } catch (Exception e) { - e.printStackTrace(); log.info("export graph failed ", e); return false; } finally { @@ -138,7 +133,7 @@ public class GraphMLConverter { public String exportGraphMl(String[] args) { JanusGraph graph = null; - String result = null; + String result; try { String janusGraphFileLocation = args[1]; String outputDirectory = args[2]; @@ -148,7 +143,6 @@ public class GraphMLConverter { log.info(LOG_FORMATTER, EXPORTED_FILE, result); } catch (Exception e) { - e.printStackTrace(); log.info("export exportGraphMl failed ", e); return null; } finally { @@ -176,7 +170,6 @@ public class GraphMLConverter { log.info(LOG_FORMATTER, EXPORTED_FILE, result); } catch (Exception e) { - e.printStackTrace(); log.info("find Error In Json Graph failed ", e); return false; } finally { @@ -189,9 +182,7 @@ public class GraphMLConverter { } public JanusGraph openGraph(String janusGraphFileLocation) { - return JanusGraphFactory.open(janusGraphFileLocation); - } public String exportJsonGraph(JanusGraph graph, String outputDirectory) { @@ -213,10 +204,8 @@ public class GraphMLConverter { result = outputFile; } catch (Exception e) { - e.printStackTrace(); log.info("export Json Graph failed ", e); graph.tx().rollback(); - e.printStackTrace(); } return result; @@ -234,7 +223,6 @@ public class GraphMLConverter { graph.tx().commit(); } catch (Exception e) { graph.tx().rollback(); - e.printStackTrace(); log.info("export Graph Ml failed ", e); } return result; @@ -252,30 +240,26 @@ public class GraphMLConverter { boolean result = false; if (propertiesCriteriaToDelete != null) { - for (Entry entry : propertiesCriteriaToDelete - - ) { + for (Entry entry : propertiesCriteriaToDelete) { String key = entry.getKey(); String value = entry.getValue(); - Iterator iterator = graph.query().has(key, value).vertices().iterator(); - while (iterator.hasNext()) { - Vertex vertex = (Vertex) iterator.next(); - vertex.remove(); - log.info("Remove vertex of type{} ", key, " and value {}", value); + for (JanusGraphVertex janusGraphVertex : graph.query().has(key, value).vertices()) { + janusGraphVertex.remove(); + log.info("Remove vertex of type {} and value {}", key, value); } } } File file = new File(graphJsonFile); if (!file.isFile()) { - log.info("File ", graphJsonFile, " cannot be found."); - return result; + log.info("File {} cannot be found.", graphJsonFile ); + return false; } try (final InputStream is = new BufferedInputStream(new FileInputStream(graphJsonFile))) { - log.info("Before importing file ", graphJsonFile); + log.info("Before importing file {}", graphJsonFile); GraphSONReader create = GraphSONReader.build().create(); create.readGraph(is, graph); @@ -286,9 +270,7 @@ public class GraphMLConverter { } catch (Exception e) { log.info("Failed to import graph ", e); - e.printStackTrace(); graph.tx().rollback(); - e.printStackTrace(); } return result; @@ -297,7 +279,6 @@ public class GraphMLConverter { public String findErrorInJsonGraph(JanusGraph graph, String outputDirectory) { String result = null; - String outputFile = outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + DOT_JSON; try (final OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile))) { @@ -315,23 +296,22 @@ public class GraphMLConverter { graph.tx().rollback(); + result = outputFile; + } catch (Exception e) { - e.printStackTrace(); - log.info("find Error In Json Graph failed ", e); + log.info("Find error In Json Graph ", e); graph.tx().rollback(); - e.printStackTrace(); } + return result; } private void removeNodesByLabel(JanusGraph graph, String label) { Iterable vertices = - graph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), label).vertices(); - Iterator iterator = vertices.iterator(); - while (iterator.hasNext()) { - Vertex next2 = iterator.next(); - next2.remove(); + graph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), label).vertices(); + for (Vertex vertex : vertices) { + vertex.remove(); } } @@ -367,32 +347,12 @@ public class GraphMLConverter { result = outputFile; } catch (Exception e) { - e.printStackTrace(); - log.info("export Users failed ", e); + log.info("Export users failed because ", e); graph.tx().rollback(); - e.printStackTrace(); } - return result; - - } - - public Map getProperties(Element element) { - - Map result = new HashMap<>(); - ; - - if (element.keys() != null && !element.keys().isEmpty()) { - Map propertyMap = - ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()])); - for (Entry entry : propertyMap.entrySet()) { - String key = entry.getKey(); - Object value = entry.getValue().value(); - - result.put(key, value); - } - } return result; + } public boolean exportUsers(String[] args) { @@ -411,8 +371,7 @@ public class GraphMLConverter { log.info(EXPORTED_FILE, result); } catch (Exception e) { - e.printStackTrace(); - log.info("export Users failed ", e); + log.info("Export users failed because", e); return false; } finally { if (graph != null) { -- cgit 1.2.3-korg