Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.umlg.sqlg.test.BaseTest Maven / Gradle / Ivy
package org.umlg.sqlg.test;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang3.time.StopWatch;
import org.apache.tinkerpop.gremlin.AbstractGremlinTest;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.MapHelper;
import org.apache.tinkerpop.gremlin.structure.*;
import org.apache.tinkerpop.gremlin.structure.io.GraphReader;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoReader;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.hamcrest.CoreMatchers;
import org.junit.*;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.umlg.sqlg.structure.SqlgGraph;
import org.umlg.sqlg.util.SqlgUtil;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* Date: 2014/07/12
* Time: 5:44 PM
*/
public abstract class BaseTest {
private static Logger logger = LoggerFactory.getLogger(BaseTest.class.getName());
protected SqlgGraph sqlgGraph;
protected SqlgGraph sqlgGraph1;
protected GraphTraversalSource gt;
protected static Configuration configuration;
private long start;
protected static final int SLEEP_TIME = 1000;
@Rule
public TestRule watcher = new TestWatcher() {
protected void starting(Description description) {
BaseTest.this.start = System.currentTimeMillis();
logger.info("Starting test: " + description.getClassName() + "." + description.getMethodName());
}
protected void finished(Description description) {
long millis = System.currentTimeMillis() - BaseTest.this.start;
String time = String.format("%02d min, %02d sec, %02d mil",
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)),
TimeUnit.MILLISECONDS.toMillis(millis) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millis))
);
logger.info(String.format("Finished test: %s.%s Time taken: %s", description.getClassName(), description.getMethodName(), time));
}
};
@BeforeClass
public static void beforeClass() throws ClassNotFoundException, IOException, PropertyVetoException {
URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
try {
configuration = new PropertiesConfiguration(sqlProperties);
if (!configuration.containsKey("jdbc.url")) {
throw new IllegalArgumentException(String.format("SqlGraph configuration requires that the %s be set", "jdbc.url"));
}
} catch (ConfigurationException e) {
throw new RuntimeException(e);
}
}
@Before
public void before() throws Exception {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
this.sqlgGraph = SqlgGraph.open(configuration);
SqlgUtil.dropDb(this.sqlgGraph);
this.sqlgGraph.tx().commit();
this.sqlgGraph.close();
this.sqlgGraph = SqlgGraph.open(configuration);
this.gt = this.sqlgGraph.traversal();
if (configuration.getBoolean("distributed", false)) {
this.sqlgGraph1 = SqlgGraph.open(configuration);
}
stopWatch.stop();
logger.info("Startup time for test = " + stopWatch.toString());
}
@After
public void after() throws Exception {
try {
this.sqlgGraph.tx().onClose(Transaction.CLOSE_BEHAVIOR.ROLLBACK);
this.sqlgGraph.close();
} catch (Exception e){
logger.error(e.getMessage(), e);
}
try {
if (this.sqlgGraph1 != null) {
this.sqlgGraph1.tx().onClose(Transaction.CLOSE_BEHAVIOR.ROLLBACK);
this.sqlgGraph1.close();
}
} catch (Exception e){
logger.error(e.getMessage(), e);
}
}
/**
* return a clone of the configuration
*
* @return
*/
protected static Configuration getConfigurationClone() {
Configuration conf = new PropertiesConfiguration();
Iterator it = configuration.getKeys();
while (it.hasNext()) {
String s = it.next();
conf.setProperty(s, configuration.getProperty(s));
}
return conf;
}
protected GraphTraversal vertexTraversal(SqlgGraph sqlgGraph, Vertex v) {
return sqlgGraph.traversal().V(v);
}
protected GraphTraversal edgeTraversal(SqlgGraph sqlgGraph, Edge e) {
return sqlgGraph.traversal().E(e);
}
protected void assertDb(String table, int numberOfRows) {
Connection conn = null;
Statement stmt = null;
try {
conn = this.sqlgGraph.getConnection();
stmt = conn.createStatement();
StringBuilder sql = new StringBuilder("SELECT * FROM ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.sqlgGraph.getSqlDialect().getPublicSchema()));
sql.append(".");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(table));
if (this.sqlgGraph.getSqlDialect().needsSemicolon()) {
sql.append(";");
}
if (logger.isDebugEnabled()) {
logger.debug(sql.toString());
}
ResultSet rs = stmt.executeQuery(sql.toString());
int countRows = 0;
while (rs.next()) {
countRows++;
}
assertEquals(numberOfRows, countRows);
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
fail(e.getMessage());
} finally {
//noinspection EmptyCatchBlock
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
fail(se.getMessage());
}
}
}
protected void printTraversalForm(final Traversal traversal) {
final boolean muted = Boolean.parseBoolean(System.getProperty("muteTestLogs", "false"));
if (!muted) System.out.println(" pre-strategy:" + traversal);
traversal.hasNext();
if (!muted) System.out.println(" post-strategy:" + traversal);
}
protected void loadModern(SqlgGraph sqlgGraph) {
final GraphReader gryoReader = GryoReader.build().create();
try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/org/apache/tinkerpop/gremlin/structure/io/gryo/tinkerpop-modern.kryo")) {
gryoReader.readGraph(stream, sqlgGraph);
} catch (IOException e) {
Assert.fail(e.getMessage());
}
}
protected void loadModern() {
loadModern(this.sqlgGraph);
}
protected void loadGratefulDead(SqlgGraph sqlgGraph) {
final GraphReader gryoReader = GryoReader.build().create();
try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/org/apache/tinkerpop/gremlin/structure/io/gryo/grateful-dead.kryo")) {
gryoReader.readGraph(stream, sqlgGraph);
} catch (IOException e) {
Assert.fail(e.getMessage());
}
}
protected void loadGratefulDead() {
loadGratefulDead(this.sqlgGraph);
}
/**
* Looks up the identifier as generated by the current source graph being tested.
*
* @param vertexName a unique string that will identify a graph element within a graph
* @return the id as generated by the graph
*/
public Object convertToVertexId(final String vertexName) {
return convertToVertexId(this.sqlgGraph, vertexName);
}
/**
* Looks up the identifier as generated by the current source graph being tested.
*
* @param g the graph to get the element id from
* @param vertexName a unique string that will identify a graph element within a graph
* @return the id as generated by the graph
*/
public Object convertToVertexId(final Graph g, final String vertexName) {
return convertToVertex(g, vertexName).id();
}
protected Vertex convertToVertex(final Graph graph, final String vertexName) {
// all test graphs have "name" as a unique id which makes it easy to hardcode this...works for now
return graph.traversal().V().has("name", vertexName).next();
}
public Object convertToEdgeId(final Graph graph, final String outVertexName, String edgeLabel, final String inVertexName) {
return graph.traversal().V().has("name", outVertexName).outE(edgeLabel).as("e").inV().has("name", inVertexName).select("e").next().id();
}
public static void assertModernGraph(final Graph g1, final boolean assertDouble, final boolean lossyForId) {
assertToyGraph(g1, assertDouble, lossyForId, true);
}
private static void assertToyGraph(final Graph g1, final boolean assertDouble, final boolean lossyForId, final boolean assertSpecificLabel) {
assertEquals(new Long(6), g1.traversal().V().count().next());
assertEquals(new Long(6), g1.traversal().E().count().next());
final Vertex v1 = g1.traversal().V().has("name", "marko").next();
assertEquals(29, v1.value("age").intValue());
assertEquals(2, v1.keys().size());
assertEquals(assertSpecificLabel ? "person" : Vertex.DEFAULT_LABEL, v1.label());
assertId(g1, lossyForId, v1, 1);
final List v1Edges = g1.traversal().V(v1.id()).bothE().toList();
assertEquals(3, v1Edges.size());
v1Edges.forEach(e -> {
if (g1.traversal().E(e.id()).inV().values("name").next().equals("vadas")) {
assertEquals("knows", e.label());
if (assertDouble)
assertEquals(0.5d, e.value("weight"), 0.0001d);
else
assertEquals(0.5f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 7);
} else if (g1.traversal().E(e.id()).inV().values("name").next().equals("josh")) {
assertEquals("knows", e.label());
if (assertDouble)
assertEquals(1.0, e.value("weight"), 0.0001d);
else
assertEquals(1.0f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 8);
} else if (g1.traversal().E(e.id()).inV().values("name").next().equals("lop")) {
assertEquals("created", e.label());
if (assertDouble)
assertEquals(0.4d, e.value("weight"), 0.0001d);
else
assertEquals(0.4f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 9);
} else {
fail("Edge not expected");
}
});
final Vertex v2 = g1.traversal().V().has("name", "vadas").next();
assertEquals(27, v2.value("age").intValue());
assertEquals(2, v2.keys().size());
assertEquals(assertSpecificLabel ? "person" : Vertex.DEFAULT_LABEL, v2.label());
assertId(g1, lossyForId, v2, 2);
final List v2Edges = g1.traversal().V(v2.id()).bothE().toList();
assertEquals(1, v2Edges.size());
v2Edges.forEach(e -> {
if (g1.traversal().E(e.id()).outV().values("name").next().equals("marko")) {
assertEquals("knows", e.label());
if (assertDouble)
assertEquals(0.5d, e.value("weight"), 0.0001d);
else
assertEquals(0.5f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 7);
} else {
fail("Edge not expected");
}
});
final Vertex v3 = g1.traversal().V().has("name", "lop").next();
assertEquals("java", v3.value("lang"));
assertEquals(2, v2.keys().size());
assertEquals(assertSpecificLabel ? "software" : Vertex.DEFAULT_LABEL, v3.label());
assertId(g1, lossyForId, v3, 3);
final List v3Edges = g1.traversal().V(v3.id()).bothE().toList();
assertEquals(3, v3Edges.size());
v3Edges.forEach(e -> {
if (g1.traversal().E(e.id()).outV().values("name").next().equals("peter")) {
assertEquals("created", e.label());
if (assertDouble)
assertEquals(0.2d, e.value("weight"), 0.0001d);
else
assertEquals(0.2f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 12);
} else if (g1.traversal().E(e.id()).outV().next().value("name").equals("josh")) {
assertEquals("created", e.label());
if (assertDouble)
assertEquals(0.4d, e.value("weight"), 0.0001d);
else
assertEquals(0.4f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 11);
} else if (g1.traversal().E(e.id()).outV().values("name").next().equals("marko")) {
assertEquals("created", e.label());
if (assertDouble)
assertEquals(0.4d, e.value("weight"), 0.0001d);
else
assertEquals(0.4f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 9);
} else {
fail("Edge not expected");
}
});
final Vertex v4 = g1.traversal().V().has("name", "josh").next();
assertEquals(32, v4.value("age").intValue());
assertEquals(2, v4.keys().size());
assertEquals(assertSpecificLabel ? "person" : Vertex.DEFAULT_LABEL, v4.label());
assertId(g1, lossyForId, v4, 4);
final List v4Edges = g1.traversal().V(v4.id()).bothE().toList();
assertEquals(3, v4Edges.size());
v4Edges.forEach(e -> {
if (e.inVertex().values("name").next().equals("ripple")) {
assertEquals("created", e.label());
if (assertDouble)
assertEquals(1.0d, e.value("weight"), 0.0001d);
else
assertEquals(1.0f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 10);
} else if (e.inVertex().values("name").next().equals("lop")) {
assertEquals("created", e.label());
if (assertDouble)
assertEquals(0.4d, e.value("weight"), 0.0001d);
else
assertEquals(0.4f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 11);
} else if (e.outVertex().values("name").next().equals("marko")) {
assertEquals("knows", e.label());
if (assertDouble)
assertEquals(1.0d, e.value("weight"), 0.0001d);
else
assertEquals(1.0f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 8);
} else {
fail("Edge not expected");
}
});
final Vertex v5 = g1.traversal().V().has("name", "ripple").next();
assertEquals("java", v5.value("lang"));
assertEquals(2, v5.keys().size());
assertEquals(assertSpecificLabel ? "software" : Vertex.DEFAULT_LABEL, v5.label());
assertId(g1, lossyForId, v5, 5);
final List v5Edges = IteratorUtils.list(v5.edges(Direction.BOTH));
assertEquals(1, v5Edges.size());
v5Edges.forEach(e -> {
if (e.outVertex().values("name").next().equals("josh")) {
assertEquals("created", e.label());
if (assertDouble)
assertEquals(1.0d, e.value("weight"), 0.0001d);
else
assertEquals(1.0f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 10);
} else {
fail("Edge not expected");
}
});
final Vertex v6 = g1.traversal().V().has("name", "peter").next();
assertEquals(35, v6.value("age").intValue());
assertEquals(2, v6.keys().size());
assertEquals(assertSpecificLabel ? "person" : Vertex.DEFAULT_LABEL, v6.label());
assertId(g1, lossyForId, v6, 6);
final List v6Edges = IteratorUtils.list(v6.edges(Direction.BOTH));
assertEquals(1, v6Edges.size());
v6Edges.forEach(e -> {
if (e.inVertex().values("name").next().equals("lop")) {
assertEquals("created", e.label());
if (assertDouble)
assertEquals(0.2d, e.value("weight"), 0.0001d);
else
assertEquals(0.2f, e.value("weight"), 0.0001f);
assertEquals(1, e.keys().size());
assertId(g1, lossyForId, e, 12);
} else {
fail("Edge not expected");
}
});
}
private static void assertId(final Graph g, final boolean lossyForId, final Element e, final Object expected) {
if (g.features().edge().supportsUserSuppliedIds()) {
if (lossyForId)
assertEquals(expected.toString(), e.id().toString());
else
assertEquals(expected, e.id());
}
}
//copied from TinkerPop
protected static void checkResults(final List expectedResults, final Traversal traversal) {
final List results = traversal.toList();
Assert.assertFalse(traversal.hasNext());
if (expectedResults.size() != results.size()) {
logger.error("Expected results: " + expectedResults);
logger.error("Actual results: " + results);
Assert.assertEquals("Checking result size", expectedResults.size(), results.size());
}
for (T t : results) {
if (t instanceof Map) {
Assert.assertThat("Checking map result existence: " + t, expectedResults.stream().filter(e -> e instanceof Map).filter(e -> internalCheckMap((Map) e, (Map) t)).findAny().isPresent(), CoreMatchers.is(true));
} else {
Assert.assertThat("Checking result existence: " + t, expectedResults.contains(t), CoreMatchers.is(true));
}
}
final Map expectedResultsCount = new HashMap<>();
final Map resultsCount = new HashMap<>();
Assert.assertEquals("Checking indexing is equivalent", expectedResultsCount.size(), resultsCount.size());
expectedResults.forEach(t -> MapHelper.incr(expectedResultsCount, t, 1l));
results.forEach(t -> MapHelper.incr(resultsCount, t, 1l));
expectedResultsCount.forEach((k, v) -> Assert.assertEquals("Checking result group counts", v, resultsCount.get(k)));
Assert.assertThat(traversal.hasNext(), CoreMatchers.is(false));
}
private static boolean internalCheckMap(final Map expectedMap, final Map actualMap) {
final List> actualList = actualMap.entrySet().stream().sorted((a, b) -> a.getKey().toString().compareTo(b.getKey().toString())).collect(Collectors.toList());
final List> expectedList = expectedMap.entrySet().stream().sorted((a, b) -> a.getKey().toString().compareTo(b.getKey().toString())).collect(Collectors.toList());
if (expectedList.size() != actualList.size()) {
return false;
}
for (int i = 0; i < actualList.size(); i++) {
if (!actualList.get(i).getKey().equals(expectedList.get(i).getKey())) {
return false;
}
if (!actualList.get(i).getValue().equals(expectedList.get(i).getValue())) {
return false;
}
}
return true;
}
}