summaryrefslogtreecommitdiffstats
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
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
-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
-rw-r--r--asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/GraphMLConverterTest.java271
-rw-r--r--asdctool/src/test/resources/config/janusgraph.properties1
-rw-r--r--asdctool/src/test/resources/graphSON.json1
5 files changed, 152 insertions, 215 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) {
diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/GraphMLConverterTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/GraphMLConverterTest.java
index 87eb40d10b..96369e045d 100644
--- a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/GraphMLConverterTest.java
+++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/GraphMLConverterTest.java
@@ -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.
@@ -20,156 +20,129 @@
package org.openecomp.sdc.asdctool.impl;
-import org.apache.commons.lang3.tuple.ImmutablePair;
-import org.apache.tinkerpop.gremlin.structure.Element;
import org.janusgraph.core.JanusGraph;
import org.junit.Test;
-import java.util.List;
-import java.util.Map;
+import java.io.File;
+import java.util.Collections;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
public class GraphMLConverterTest {
-
- public GraphMLConverter createTestSubject() {
- return new GraphMLConverter();
- }
-
- @Test
- public void testImportGraph() throws Exception {
- GraphMLConverter testSubject;
- String[] args = new String[] { "" };
- boolean result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.importGraph(args);
- }
-
- @Test
- public void testExportGraph() throws Exception {
- GraphMLConverter testSubject;
- String[] args = new String[] { "" };
- boolean result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.exportGraph(args);
- }
-
- @Test
- public void testExportGraphMl() throws Exception {
- GraphMLConverter testSubject;
- String[] args = new String[] { "" };
- String result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.exportGraphMl(args);
- }
-
- @Test
- public void testFindErrorInJsonGraph() throws Exception {
- GraphMLConverter testSubject;
- String[] args = new String[] { "" };
- boolean result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.findErrorInJsonGraph(args);
- }
-
- @Test(expected=IllegalArgumentException.class)
- public void testOpenGraph() throws Exception {
- GraphMLConverter testSubject;
- String janusGraphFileLocation = "";
- JanusGraph result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.openGraph("src/main/resources/config/janusgraph.properties");
- }
-
- @Test(expected=NullPointerException.class)
- public void testExportJsonGraph() throws Exception {
- GraphMLConverter testSubject;
- JanusGraph graph = null;
- String outputDirectory = "";
- String result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.exportJsonGraph(graph, outputDirectory);
- }
-
- @Test(expected=NullPointerException.class)
- public void testExportGraphMl_1() throws Exception {
- GraphMLConverter testSubject;
- JanusGraph graph = null;
- String outputDirectory = "";
- String result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.exportGraphMl(graph, outputDirectory);
- }
-
- @Test
- public void testImportJsonGraph() throws Exception {
- GraphMLConverter testSubject;
- JanusGraph graph = null;
- String graphJsonFile = "";
- List<ImmutablePair<String, String>> propertiesCriteriaToDelete = null;
- boolean result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.importJsonGraph(graph, graphJsonFile, propertiesCriteriaToDelete);
- }
-
- @Test(expected=NullPointerException.class)
- public void testFindErrorInJsonGraph_1() throws Exception {
- GraphMLConverter testSubject;
- JanusGraph graph = null;
- String outputDirectory = "";
- String result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.findErrorInJsonGraph(graph, outputDirectory);
- }
-
-
- @Test(expected=NullPointerException.class)
- public void testExportUsers() throws Exception {
- GraphMLConverter testSubject;
- JanusGraph graph = null;
- String outputDirectory = "";
- String result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.exportUsers(graph, outputDirectory);
- }
-
- @Test(expected=NullPointerException.class)
- public void testGetProperties() throws Exception {
- GraphMLConverter testSubject;
- Element element = null;
- Map<String, Object> result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.getProperties(element);
- }
-
- @Test
- public void testExportUsers_1() throws Exception {
- GraphMLConverter testSubject;
- String[] args = new String[] { "" };
- boolean result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.exportUsers(args);
- }
+
+ @Test
+ public void testImportGraph() {
+ String[] args = getInputArgs();
+ GraphMLConverter testSubject = new GraphMLConverter();
+ assertTrue(testSubject.importGraph(args));
+ }
+
+ @Test
+ public void testExportGraph() {
+ String[] args = getOutputArgs();
+ GraphMLConverter testSubject = new GraphMLConverter();
+ assertTrue(testSubject.exportGraph(args));
+ }
+
+ @Test
+ public void testExportGraphMl() {
+ String[] args = getOutputArgs();
+ GraphMLConverter testSubject = new GraphMLConverter();
+
+ String result = testSubject.exportGraphMl(args);
+ assertNotNull(result);
+ assertTrue(result.startsWith(args[2]));
+ }
+
+ @Test
+ public void testFindErrorInJsonGraph() {
+ String[] args = getOutputArgs();
+ GraphMLConverter testSubject = new GraphMLConverter();
+ assertTrue(testSubject.findErrorInJsonGraph(args));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testOpenGraphWithBadPath() {
+
+ GraphMLConverter testSubject = new GraphMLConverter();
+ testSubject.openGraph("badPath");
+ }
+
+ @Test
+ public void testExportJsonGraphWithBadOutputDir() {
+
+ GraphMLConverter testSubject = new GraphMLConverter();
+ JanusGraph graph = testSubject.openGraph(getJanusGraphConfig());
+ assertNull(testSubject.exportJsonGraph(graph, "badOutputDir"));
+ }
+
+ @Test
+ public void testImportJsonGraph() {
+ GraphMLConverter testSubject = new GraphMLConverter();
+ JanusGraph graph = testSubject.openGraph(getJanusGraphConfig());
+
+ assertTrue(testSubject.importJsonGraph(graph, getGraphSON(), Collections.emptyList()));
+ }
+
+ @Test
+ public void testImportJsonGraphNoGraphSONFile() {
+ GraphMLConverter testSubject = new GraphMLConverter();
+ JanusGraph graph = testSubject.openGraph(getJanusGraphConfig());
+
+ assertFalse(testSubject.importJsonGraph(graph, "noFile", Collections.emptyList()));
+ }
+
+
+ @Test
+ public void testExportUsers() {
+ GraphMLConverter testSubject = new GraphMLConverter();
+
+ JanusGraph graph = testSubject.openGraph(getJanusGraphConfig());
+ String outputDirectory = getOutputJanusGraph();
+
+ String result = testSubject.exportUsers(graph, outputDirectory);
+ assertNotNull(result);
+ assertTrue(result.startsWith(outputDirectory));
+ }
+
+ @Test
+ public void testExportUsersWithBadOutputDir() {
+ GraphMLConverter testSubject = new GraphMLConverter();
+
+ JanusGraph graph = testSubject.openGraph(getJanusGraphConfig());
+ assertNull(testSubject.exportUsers(graph, "badOutputDir"));
+ }
+
+ @Test
+ public void testExportUsersFromArgs() {
+ String[] args = getOutputArgs();
+ GraphMLConverter testSubject = new GraphMLConverter();
+ assertTrue(testSubject.exportUsers(args));
+ }
+
+ private String getJanusGraphConfig() {
+ return getClass().getClassLoader().getResource("config/janusgraph.properties").getPath();
+ }
+
+ private String getOutputJanusGraph() {
+ return new File(getClass().getClassLoader().getResource("graphSON.json").getFile())
+ .getAbsolutePath()
+ .replace(File.separator + "graphSON.json", "");
+ }
+
+ private String getGraphSON() {
+ return getClass().getClassLoader().getResource("graphSON.json").getPath();
+ }
+
+ private String[] getOutputArgs() {
+ return new String[]{"", getJanusGraphConfig(), getOutputJanusGraph()};
+ }
+
+ private String[] getInputArgs() {
+ return new String[]{"", getJanusGraphConfig(), getGraphSON()};
+ }
}
diff --git a/asdctool/src/test/resources/config/janusgraph.properties b/asdctool/src/test/resources/config/janusgraph.properties
new file mode 100644
index 0000000000..4f8d3ad79c
--- /dev/null
+++ b/asdctool/src/test/resources/config/janusgraph.properties
@@ -0,0 +1 @@
+storage.backend=inmemory
diff --git a/asdctool/src/test/resources/graphSON.json b/asdctool/src/test/resources/graphSON.json
new file mode 100644
index 0000000000..726c20af09
--- /dev/null
+++ b/asdctool/src/test/resources/graphSON.json
@@ -0,0 +1 @@
+{"id":0,"label":"node","outE":{"relation_is":[{"id":0,"inV":1,"properties":{"value":1}}]},"properties":{"node0":[{"id":0,"value":"node0"}],"node1":[{"id":1,"value":"node1"}]}} \ No newline at end of file