org.umlg.sqlg.test.TinkerpopTest Maven / Gradle / Ivy
package org.umlg.sqlg.test;
import org.apache.tinkerpop.gremlin.AbstractGremlinTest;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
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.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.io.GraphReader;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoIo;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoReader;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
/**
* Date: 2014/07/13
* Time: 6:32 PM
*/
public class TinkerpopTest extends BaseTest {
private static final String partition = "gremlin.partitionGraphStrategy.partition";
@Test
public void g_V_chooseXlabel_eq_person__unionX__out_lang__out_nameX__in_labelX() throws IOException {
Graph graph = this.sqlgGraph;
final GraphReader reader = GryoReader.build()
.mapper(graph.io(GryoIo.build()).mapper().create())
.create();
try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/tinkerpop-modern.kryo")) {
reader.readGraph(stream, graph);
}
assertModernGraph(graph, true, false);
GraphTraversalSource g = graph.traversal();
List traversala2 = g.V().hasId(convertToVertexId("marko")).toList();
Assert.assertEquals(1, traversala2.size());
Assert.assertEquals(convertToVertex(graph, "marko"), traversala2.get(0));
}
public static void checkResults(final Map expectedResults, final Traversal, T> traversal) {
final List list = new ArrayList<>();
expectedResults.forEach((k, v) -> {
for (int i = 0; i < v; i++) {
list.add(k);
}
});
checkResults(list, traversal);
}
public static void checkResults(final List expectedResults, final Traversal, T> traversal) {
final List results = traversal.toList();
Assert.assertFalse(traversal.hasNext());
if(expectedResults.size() != results.size()) {
System.err.println("Expected results: " + expectedResults);
System.err.println("Actual results: " + results);
assertEquals("Checking result size", expectedResults.size(), results.size());
}
for (T t : results) {
if (t instanceof Map) {
Assert.assertTrue("Checking map result existence: " + t, expectedResults.stream().filter(e -> e instanceof Map).filter(e -> checkMap((Map) e, (Map) t)).findAny().isPresent());
} else {
Assert.assertTrue("Checking result existence: " + t, expectedResults.contains(t));
}
}
final Map expectedResultsCount = new HashMap<>();
final Map resultsCount = new HashMap<>();
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) -> assertEquals("Checking result group counts", v, resultsCount.get(k)));
Assert.assertFalse(traversal.hasNext());
}
public static boolean checkMap(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;
} else if (actualList.size() > expectedList.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;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy