
com.tinkerpop.gremlin.structure.GraphTest Maven / Gradle / Ivy
package com.tinkerpop.gremlin.structure;
import com.tinkerpop.gremlin.AbstractGremlinSuite;
import com.tinkerpop.gremlin.AbstractGremlinTest;
import com.tinkerpop.gremlin.GraphManager;
import com.tinkerpop.gremlin.GraphProvider;
import org.junit.Test;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static com.tinkerpop.gremlin.structure.Graph.Features.GraphFeatures.FEATURE_PERSISTENCE;
import static org.junit.Assert.*;
/**
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
public class GraphTest extends AbstractGremlinTest {
/**
* Ensure compliance with Features by checking that all Features are exposed by the implementation.
*/
@Test
public void shouldImplementAndExposeFeatures() {
final Graph.Features features = g.getFeatures();
assertNotNull(features);
final AtomicInteger counter = new AtomicInteger(0);
// get all features.
final List methods = Arrays.asList(features.getClass().getMethods()).stream()
.filter(m -> Graph.Features.FeatureSet.class.isAssignableFrom(m.getReturnType()))
.collect(Collectors.toList());
methods.forEach(m -> {
try {
assertNotNull(m.invoke(features));
counter.incrementAndGet();
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception while dynamically checking compliance on Feature implementation");
}
});
// always should be some feature methods
assertTrue(methods.size() > 0);
// ensure that every method exposed was checked
assertEquals(methods.size(), counter.get());
}
/**
* Graphs should have a standard toString representation where the value starts with the lower case representation
* of the class name of the Graph instance.
*/
@Test
public void shouldHaveStandardStringRepresentation() throws Exception {
assertNotNull(g.toString());
assertTrue(g.toString().startsWith(g.getClass().getSimpleName().toLowerCase()));
}
/**
* Test graph counts with addition and removal of vertices.
*/
// todo: double check neo4j at 2.2.x to see if this is resolved
@Test
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_FULLY_ISOLATED_TRANSACTIONS)
public void shouldProperlyCountVerticesAndEdgesOnAddRemove() {
final Vertex v = g.addVertex();
StructureStandardSuite.assertVertexEdgeCounts(1, 0).accept(g);
assertEquals(v, g.V().next());
assertEquals(v.id(), g.V().next().id());
assertEquals(v.label(), g.V().next().label());
v.remove();
tryCommit(g, StructureStandardSuite.assertVertexEdgeCounts(0, 0));
g.addVertex();
g.addVertex();
tryCommit(g, StructureStandardSuite.assertVertexEdgeCounts(2, 0));
g.V().forEach(Vertex::remove);
tryCommit(g, StructureStandardSuite.assertVertexEdgeCounts(0, 0));
final String edgeLabel = GraphManager.get().convertLabel("test");
Vertex v1 = g.addVertex();
Vertex v2 = g.addVertex();
Edge e = v1.addEdge(edgeLabel, v2);
tryCommit(g, StructureStandardSuite.assertVertexEdgeCounts(2, 1));
// test removal of the edge itself
e.remove();
tryCommit(g, StructureStandardSuite.assertVertexEdgeCounts(2, 0));
v1.addEdge(edgeLabel, v2);
tryCommit(g, StructureStandardSuite.assertVertexEdgeCounts(2, 1));
// test removal of the out vertex to remove the edge
v1.remove();
tryCommit(g, StructureStandardSuite.assertVertexEdgeCounts(1, 0));
// test removal of the in vertex to remove the edge
v1 = g.addVertex();
v1.addEdge(edgeLabel, v2);
tryCommit(g, StructureStandardSuite.assertVertexEdgeCounts(2, 1));
v2.remove();
tryCommit(g, StructureStandardSuite.assertVertexEdgeCounts(1, 0));
}
/**
* Generate a graph with lots of edges and vertices, then test vertex/edge counts on removal of each edge.
*/
@Test
public void shouldRemoveEdges() {
final int vertexCount = 100;
final int edgeCount = 200;
final List vertices = new ArrayList<>();
final List edges = new ArrayList<>();
final Random random = new Random();
IntStream.range(0, vertexCount).forEach(i -> vertices.add(g.addVertex()));
tryCommit(g, AbstractGremlinSuite.assertVertexEdgeCounts(vertexCount, 0));
IntStream.range(0, edgeCount).forEach(i -> {
boolean created = false;
while (!created) {
final Vertex a = vertices.get(random.nextInt(vertices.size()));
final Vertex b = vertices.get(random.nextInt(vertices.size()));
if (a != b) {
edges.add(a.addEdge(GraphManager.get().convertLabel("a" + UUID.randomUUID()), b));
created = true;
}
}
});
tryCommit(g, AbstractGremlinSuite.assertVertexEdgeCounts(vertexCount, edgeCount));
int counter = 0;
for (Edge e : edges) {
counter = counter + 1;
e.remove();
final int currentCounter = counter;
tryCommit(g, AbstractGremlinSuite.assertVertexEdgeCounts(vertexCount, edgeCount - currentCounter));
}
}
/**
* Generate a graph with lots of edges and vertices, then test vertex/edge counts on removal of each vertex.
*/
@Test
public void shouldRemoveVertices() {
final int vertexCount = 500;
final List vertices = new ArrayList<>();
final List edges = new ArrayList<>();
IntStream.range(0, vertexCount).forEach(i -> vertices.add(g.addVertex()));
tryCommit(g, AbstractGremlinSuite.assertVertexEdgeCounts(vertexCount, 0));
for (int i = 0; i < vertexCount; i = i + 2) {
final Vertex a = vertices.get(i);
final Vertex b = vertices.get(i + 1);
edges.add(a.addEdge(GraphManager.get().convertLabel("a" + UUID.randomUUID()), b));
}
tryCommit(g, AbstractGremlinSuite.assertVertexEdgeCounts(vertexCount, vertexCount / 2));
int counter = 0;
for (Vertex v : vertices) {
counter = counter + 1;
v.remove();
if ((counter + 1) % 2 == 0) {
final int currentCounter = counter;
tryCommit(g, AbstractGremlinSuite.assertVertexEdgeCounts(
vertexCount - currentCounter, edges.size() - ((currentCounter + 1) / 2)));
}
}
}
/**
* Create a small {@link com.tinkerpop.gremlin.structure.Graph} and ensure that counts of edges per vertex are correct.
*/
@Test
public void shouldEvaluateConnectivityPatterns() {
final GraphProvider graphProvider = GraphManager.get();
final Graph graph = this.g;
final Vertex a;
final Vertex b;
final Vertex c;
final Vertex d;
if (graph.getFeatures().vertex().supportsUserSuppliedIds()) {
a = graph.addVertex(Element.ID, graphProvider.convertId("1"));
b = graph.addVertex(Element.ID, graphProvider.convertId("2"));
c = graph.addVertex(Element.ID, graphProvider.convertId("3"));
d = graph.addVertex(Element.ID, graphProvider.convertId("4"));
} else {
a = graph.addVertex();
b = graph.addVertex();
c = graph.addVertex();
d = graph.addVertex();
}
tryCommit(graph, AbstractGremlinSuite.assertVertexEdgeCounts(4, 0));
final Edge e = a.addEdge(graphProvider.convertLabel("knows"), b);
final Edge f = b.addEdge(graphProvider.convertLabel("knows"), c);
final Edge g = c.addEdge(graphProvider.convertLabel("knows"), d);
final Edge h = d.addEdge(graphProvider.convertLabel("knows"), a);
tryCommit(graph, AbstractGremlinSuite.assertVertexEdgeCounts(4, 4));
for (Vertex v : graph.V().toList()) {
assertEquals(new Long(1), v.outE().count().next());
assertEquals(new Long(1), v.inE().count().next());
}
for (Edge x : graph.E().toList()) {
assertEquals(graphProvider.convertLabel("knows"), x.label());
}
if (graph.getFeatures().vertex().supportsUserSuppliedIds()) {
final Vertex va = graph.v(graphProvider.convertId("1"));
final Vertex vb = graph.v(graphProvider.convertId("2"));
final Vertex vc = graph.v(graphProvider.convertId("3"));
final Vertex vd = graph.v(graphProvider.convertId("4"));
assertEquals(a, va);
assertEquals(b, vb);
assertEquals(c, vc);
assertEquals(d, vd);
assertEquals(new Long(1), va.inE().count().next());
assertEquals(new Long(1), va.outE().count().next());
assertEquals(new Long(1), vb.inE().count().next());
assertEquals(new Long(1), vb.outE().count().next());
assertEquals(new Long(1), vc.inE().count().next());
assertEquals(new Long(1), vc.outE().count().next());
assertEquals(new Long(1), vd.inE().count().next());
assertEquals(new Long(1), vd.outE().count().next());
final Edge i = a.addEdge(graphProvider.convertLabel("hates"), b);
assertEquals(new Long(1), va.inE().count().next());
assertEquals(new Long(2), va.outE().count().next());
assertEquals(new Long(2), vb.inE().count().next());
assertEquals(new Long(1), vb.outE().count().next());
assertEquals(new Long(1), vc.inE().count().next());
assertEquals(new Long(1), vc.outE().count().next());
assertEquals(new Long(1), vd.inE().count().next());
assertEquals(new Long(1), vd.outE().count().next());
for (Edge x : a.outE().toList()) {
assertTrue(x.label().equals(graphProvider.convertId("knows")) || x.label().equals(graphProvider.convertId("hates")));
}
assertEquals(graphProvider.convertId("hates"), i.label());
assertEquals(graphProvider.convertId("2"), i.inV().id().next().toString());
assertEquals(graphProvider.convertId("1"), i.outV().id().next().toString());
}
final Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy