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;
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";
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";
return importJsonGraph(graph, inputFile, propertiesCriteriaToDelete);
} catch (Exception e) {
- e.printStackTrace();
log.info("import graph failed ", e);
return false;
} finally {
log.info(LOG_FORMATTER, EXPORTED_FILE, result);
} catch (Exception e) {
- e.printStackTrace();
log.info("export graph failed ", e);
return false;
} finally {
public String exportGraphMl(String[] args) {
JanusGraph graph = null;
- String result = null;
+ String result;
try {
String janusGraphFileLocation = args[1];
String outputDirectory = args[2];
log.info(LOG_FORMATTER, EXPORTED_FILE, result);
} catch (Exception e) {
- e.printStackTrace();
log.info("export exportGraphMl failed ", e);
return null;
} finally {
log.info(LOG_FORMATTER, EXPORTED_FILE, result);
} catch (Exception e) {
- e.printStackTrace();
log.info("find Error In Json Graph failed ", e);
return false;
} finally {
}
public JanusGraph openGraph(String janusGraphFileLocation) {
-
return JanusGraphFactory.open(janusGraphFileLocation);
-
}
public String exportJsonGraph(JanusGraph graph, String outputDirectory) {
result = outputFile;
} catch (Exception e) {
- e.printStackTrace();
log.info("export Json Graph failed ", e);
graph.tx().rollback();
- e.printStackTrace();
}
return result;
graph.tx().commit();
} catch (Exception e) {
graph.tx().rollback();
- e.printStackTrace();
log.info("export Graph Ml failed ", e);
}
return result;
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);
} catch (Exception e) {
log.info("Failed to import graph ", e);
- e.printStackTrace();
graph.tx().rollback();
- e.printStackTrace();
}
return result;
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))) {
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();
}
}
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) {
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) {
* 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 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()};
+ }
}