/** * This isn't really a test. It just pretty prints the features for the graph for reference. Of course, * if the implementing classes use anonymous inner classes it will */ @Test public void shouldPrintTheFeatureList() { logger.info(String.format("Printing Features of %s for reference: ", g.getClass().getSimpleName())); logger.info(graph.features().toString()); assertTrue(true); }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_UUID_IDS) public void shouldIterateVerticesWithUuidIdSupportUsingVertices() { // if the graph supports id assigned, it should allow it. if the graph does not, it will generate one final Vertex v1 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, UUID.randomUUID()) : graph.addVertex(); final Vertex v2 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, UUID.randomUUID()) : graph.addVertex(); graph.addVertex(); tryCommit(graph, graph -> { assertEquals(2, IteratorUtils.count(graph.vertices(v1, v2))); }); }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldSetValueOnVertexOnAdd() throws Exception { assumeThat(graph.features().supports(VertexPropertyFeatures.class, featureName), is(true)); final Vertex vertex = graph.addVertex("aKey", value); assertPropertyValue(vertex); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldUseActualVertexWhenAdded() { final AtomicBoolean triggered = new AtomicBoolean(false); final AtomicReference<Vertex> eventedVertex = new AtomicReference<>(); final MutationListener listener = new AbstractMutationListener() { @Override public void vertexAdded(final Vertex element) { eventedVertex.set(element); assertEquals("thing", element.label()); assertThat(element.properties("here").hasNext(), is(false)); triggered.set(true); } }; final EventStrategy.Builder builder = EventStrategy.build().addListener(listener).detach(EventStrategy.Detachment.REFERENCE); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final GraphTraversalSource gts = create(eventStrategy); final Vertex v = gts.addV("thing").property("here", "there").next(); tryCommit(graph); assertVertexEdgeCounts(graph, 1, 0); assertThat(triggered.get(), is(true)); assertEquals(v, eventedVertex.get()); }
public Vertex vertexFromJson(final JsonNode json) throws IOException { final Map<String, Object> props = readProperties(json); final Object vertexId = getTypedValueFromJsonNode(json.get(GraphSONTokensTP2._ID)); final Vertex v = vertexFeatures.willAllowId(vertexId) ? g.addVertex(T.id, vertexId) : g.addVertex(); cache.put(vertexId, v); for (Map.Entry<String, Object> entry : props.entrySet()) { v.property(g.features().vertex().getCardinality(entry.getKey()), entry.getKey(), entry.getValue()); } return v; }
/** * Utility method that commits if the graph supports transactions and executes an assertion function before and * after the commit. It assumes that the assertion should be true before and after the commit. */ public void tryCommit(final Graph graph, final Consumer<Graph> assertFunction) { assertFunction.accept(graph); if (graph.features().graph().supportsTransactions()) { graph.tx().commit(); assertFunction.accept(graph); } }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_NUMERIC_IDS) public void shouldIterateVerticesWithNumericIdSupportUsingLongRepresentations() { // if the graph supports id assigned, it should allow it. if the graph does not, it will generate one final Vertex v1 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, 1l) : graph.addVertex(); final Vertex v2 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, 2l) : graph.addVertex(); graph.addVertex(); tryCommit(graph, graph -> { assertEquals(2, IteratorUtils.count(graph.vertices(Long.parseLong(v1.id().toString()), Long.parseLong(v2.id().toString())))); }); }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY) public void shouldEnableFeatureOnVertexIfNotEnabled() throws Exception { assumeThat(graph.features().supports(VertexPropertyFeatures.class, featureName), is(false)); try { graph.addVertex("aKey", value); fail(String.format(INVALID_FEATURE_SPECIFICATION, VertexPropertyFeatures.class.getSimpleName(), featureName)); } catch (Exception e) { validateException(Property.Exceptions.dataTypeOfPropertyValueNotSupported(value), e); } }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldSetValueOnEdgeOnAdd() throws Exception { assumeThat(graph.features().supports(EdgePropertyFeatures.class, featureName), is(true)); final Edge edge = createEdgeForPropertyFeatureTests("aKey", value); assertPropertyValue(edge); }
public static Vertex createVertex(final Attachable<Vertex> attachableVertex, final Graph hostGraph) { final Vertex baseVertex = attachableVertex.get(); final Vertex vertex = hostGraph.features().vertex().willAllowId(baseVertex.id()) ? hostGraph.addVertex(T.id, baseVertex.id(), T.label, baseVertex.label()) : hostGraph.addVertex(T.label, baseVertex.label()); baseVertex.properties().forEachRemaining(vp -> { final VertexProperty vertexProperty = hostGraph.features().vertex().properties().willAllowId(vp.id()) ? vertex.property(hostGraph.features().vertex().getCardinality(vp.key()), vp.key(), vp.value(), T.id, vp.id()) : vertex.property(hostGraph.features().vertex().getCardinality(vp.key()), vp.key(), vp.value()); vp.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value())); }); return vertex; }
public TransactionalEventQueue(final Graph graph) { if (!graph.features().graph().supportsTransactions()) throw new IllegalStateException(String.format("%s requires the graph to support transactions", EventStrategy.class.getName())); // since this is a transactional graph events are enqueued so the events should be fired/reset only after // transaction is committed/rolled back as tied to a graph transaction graph.tx().addTransactionListener(status -> { if (status == Transaction.Status.COMMIT) fireEventQueue(); else if (status == Transaction.Status.ROLLBACK) resetEventQueue(); else throw new RuntimeException(String.format("The %s is not aware of this status: %s", EventQueue.class.getName(), status)); }); }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_UUID_IDS) public void shouldIterateVerticesWithUuidIdSupportUsingStringRepresentations() { // if the graph supports id assigned, it should allow it. if the graph does not, it will generate one final Vertex v1 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, UUID.randomUUID()) : graph.addVertex(); final Vertex v2 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, UUID.randomUUID()) : graph.addVertex(); graph.addVertex(); tryCommit(graph, graph -> { assertEquals(2, IteratorUtils.count(graph.vertices(v1.id().toString(), v2.id().toString()))); }); }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY) public void shouldSetValueOnVertex() throws Exception { assumeThat(graph.features().supports(VertexPropertyFeatures.class, featureName), is(true)); final Vertex vertex = graph.addVertex(); vertex.property(VertexProperty.Cardinality.single, "aKey", value); assertPropertyValue(vertex); }
@Test @FeatureRequirement(featureClass = VertexFeatures.class, feature = FEATURE_USER_SUPPLIED_IDS, supported = false) public void shouldNotAllowAnyIdsIfUserSuppliedIdsIsFalse() throws Exception { assertFalse(graph.features().vertex().willAllowId(graphProvider.convertId(99999943835l, Vertex.class))); }
/** * Commit transactions across all {@link Graph} objects. */ public final void commitAll() { graphs.entrySet().forEach(e -> { final Graph graph = e.getValue(); if (graph.features().graph().supportsTransactions() && graph.tx().isOpen()) graph.tx().commit(); }); }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_STRING_IDS) public void shouldIterateVerticesWithStringIdSupportUsingVertices() { // if the graph supports id assigned, it should allow it. if the graph does not, it will generate one final Vertex v1 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, "1") : graph.addVertex(); final Vertex v2 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, "2") : graph.addVertex(); graph.addVertex(); tryCommit(graph, graph -> { assertEquals(2, IteratorUtils.count(graph.vertices(v1, v2))); }); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldDetachVertexWhenRemoved() { final AtomicBoolean triggered = new AtomicBoolean(false); final Vertex v = graph.addVertex(); final String label = v.label(); final Object id = v.id(); final MutationListener listener = new AbstractMutationListener() { @Override public void vertexRemoved(final Vertex element) { assertThat(element, instanceOf(DetachedVertex.class)); assertEquals(id, element.id()); assertEquals(label, element.label()); triggered.set(true); } }; final EventStrategy.Builder builder = EventStrategy.build().addListener(listener); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final GraphTraversalSource gts = create(eventStrategy); gts.V(v).drop().iterate(); tryCommit(graph); assertVertexEdgeCounts(graph, 0, 0); assertThat(triggered.get(), is(true)); }
@Test @FeatureRequirement(featureClass = EdgeFeatures.class, feature = EdgeFeatures.FEATURE_USER_SUPPLIED_IDS, supported = false) public void shouldNotAllowAnyIdsIfUserSuppliedIdsIsFalse() throws Exception { assertFalse(graph.features().edge().willAllowId(graphProvider.convertId(99999943835l, Edge.class))); }
/** * Rollback transactions across all {@link Graph} objects. */ public final void rollbackAll() { graphs.entrySet().forEach(e -> { final Graph graph = e.getValue(); if (graph.features().graph().supportsTransactions() && graph.tx().isOpen()) graph.tx().rollback(); }); }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_NUMERIC_IDS) public void shouldIterateVerticesWithNumericIdSupportUsingIntegerRepresentations() { // if the graph supports id assigned, it should allow it. if the graph does not, it will generate one final Vertex v1 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, 1l) : graph.addVertex(); final Vertex v2 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, 2l) : graph.addVertex(); graph.addVertex(); tryCommit(graph, graph -> { assertEquals(2, IteratorUtils.count(graph.vertices(Integer.parseInt(v1.id().toString()), Integer.parseInt(v2.id().toString())))); }); }