summaryrefslogtreecommitdiffstats
path: root/asdctool/src/main/java/org
diff options
context:
space:
mode:
authork.kedron <k.kedron@partner.samsung.com>2020-03-13 16:40:27 +0100
committerOfir Sonsino <ofir.sonsino@intl.att.com>2020-03-19 16:05:02 +0000
commit9c49e4b01ef5300026b62afa4e5b6cc591469dc1 (patch)
tree35fac34e9f564b53513d5ce64bfbcca2211f929b /asdctool/src/main/java/org
parentb3e9914ce32f3e1b11396f88b3af9ae00c76479d (diff)
Added new unit tests
Added new unit tests to GraphMLConverter class. Fixed the Sonar issue. Issue-ID: SDC-2327 Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com> Change-Id: I985a860012c0c2b0b6524c0a2346415cb113d83c
Diffstat (limited to 'asdctool/src/main/java/org')
-rw-r--r--asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java11
-rw-r--r--asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java83
2 files changed, 28 insertions, 66 deletions
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<String, Object> getProperties(Element element) {
- Map<String, Object> result = new HashMap<String, Object>();
- ;
+ Map<String, Object> result = new HashMap<>();
- if (element.keys() != null && element.keys().size() > 0) {
+ if (CollectionUtils.isNotEmpty(element.keys())) {
Map<String, Property> 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<String, String> entry : propertiesCriteriaToDelete
-
- ) {
+ for (Entry<String, String> 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<JanusGraphVertex> vertices =
- graph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), label).vertices();
- Iterator<JanusGraphVertex> 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<String, Object> getProperties(Element element) {
-
- Map<String, Object> result = new HashMap<>();
- ;
-
- if (element.keys() != null && !element.keys().isEmpty()) {
- Map<String, Property> propertyMap =
- ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()]));
- for (Entry<String, Property> 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) {