@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldReferenceVertexWhenAdded() { final AtomicBoolean triggered = new AtomicBoolean(false); final MutationListener listener = new AbstractMutationListener() { @Override public void vertexAdded(final Vertex element) { assertThat(element, instanceOf(ReferenceVertex.class)); 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); gts.addV("thing").property("here", "there").iterate(); tryCommit(graph); assertVertexEdgeCounts(graph, 1, 0); assertThat(triggered.get(), is(true)); }
public static String featureString(final Graph.Features features) { final StringBuilder sb = new StringBuilder("FEATURES"); final Predicate<Method> supportMethods = (m) -> m.getModifiers() == Modifier.PUBLIC && m.getName().startsWith(featuresStartWith) && !m.getName().equals(featuresStartWith); sb.append(LINE_SEPARATOR); Stream.of(Pair.with(Graph.Features.GraphFeatures.class, features.graph()), Pair.with(Graph.Features.VariableFeatures.class, features.graph().variables()), Pair.with(Graph.Features.VertexFeatures.class, features.vertex()), Pair.with(Graph.Features.VertexPropertyFeatures.class, features.vertex().properties()), Pair.with(Graph.Features.EdgeFeatures.class, features.edge()), Pair.with(Graph.Features.EdgePropertyFeatures.class, features.edge().properties())).forEach(p -> { printFeatureTitle(p.getValue0(), sb); Stream.of(p.getValue0().getMethods()) .filter(supportMethods) .map(createTransform(p.getValue1())) .forEach(sb::append); }); return sb.toString(); }
private static void assumeRequirementsAreMetForTest(final Set<FeatureRequirement> featureRequirementSet, final Graph.Features features, final boolean staticCheck) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { for (FeatureRequirement fr : featureRequirementSet) { try { assumeThat(String.format("Features of the graph do not support all of the features required by this test so it will be ignored: %s.%s=%s", fr.featureClass().getSimpleName(), fr.feature(), fr.supported()), features.supports(fr.featureClass(), fr.feature()), is(fr.supported())); } catch (NoSuchMethodException nsme) { throw new NoSuchMethodException(String.format("[supports%s] is not a valid feature on %s", fr.feature(), fr.featureClass())); } catch (UnsupportedOperationException uoe) { // no worries if this is a check of static features - it just means that we can't use the cache to // support this check and will have to incur the cost of instantiating a graph instance directly. but, // if this is not a static check then something else is amiss and we should throw. if (staticCheck) throw uoe; } } }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) public void shouldTriggerAddEdgePropertyAdded() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex v = graph.addVertex(); v.addEdge("self", v); final GraphTraversalSource gts = create(eventStrategy); gts.V(v).as("v").addE("self").to("v").property("some", "thing").next(); tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.E().has("some", "thing")))); assertEquals(0, listener1.addVertexEventRecorded()); assertEquals(0, listener2.addVertexEventRecorded()); assertEquals(1, listener1.addEdgeEventRecorded()); assertEquals(1, listener2.addEdgeEventRecorded()); assertEquals(0, listener2.edgePropertyChangedEventRecorded()); assertEquals(0, listener1.edgePropertyChangedEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES) public void shouldTriggerAddVertexPropertyPropertyChanged() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex vSome = graph.addVertex("some", "thing"); vSome.property(VertexProperty.Cardinality.single, "that", "thing", "is", "good"); final GraphTraversalSource gts = create(eventStrategy); final Vertex vAny = gts.V().addV().property("any", "thing").next(); gts.V(vAny).properties("any").property("is", "bad").next(); tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.V().has("any", "thing")))); assertEquals(1, listener1.addVertexEventRecorded()); assertEquals(1, listener2.addVertexEventRecorded()); assertEquals(1, listener2.vertexPropertyPropertyChangedEventRecorded()); assertEquals(1, listener1.vertexPropertyPropertyChangedEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldDetachVertexWhenAdded() { final AtomicBoolean triggered = new AtomicBoolean(false); final MutationListener listener = new AbstractMutationListener() { @Override public void vertexAdded(final Vertex element) { assertThat(element, instanceOf(DetachedVertex.class)); assertEquals("thing", element.label()); assertEquals("there", element.value("here")); 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.addV("thing").property("here", "there").iterate(); tryCommit(graph); assertVertexEdgeCounts(graph, 1, 0); assertThat(triggered.get(), is(true)); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) public void shouldTriggerAddEdge() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex v = graph.addVertex(); v.addEdge("self", v); final GraphTraversalSource gts = create(eventStrategy); gts.V(v).as("v").addE("self").to("v").next(); tryCommit(graph, g -> assertEquals(2, IteratorUtils.count(gts.E()))); assertEquals(0, listener1.addVertexEventRecorded()); assertEquals(0, listener2.addVertexEventRecorded()); assertEquals(1, listener1.addEdgeEventRecorded()); assertEquals(1, listener2.addEdgeEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) public void shouldTriggerAddEdgeByPath() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex v = graph.addVertex(); v.addEdge("self", v); final GraphTraversalSource gts = create(eventStrategy); gts.V(v).as("a").addE("self").to("a").next(); tryCommit(graph, g -> assertEquals(2, IteratorUtils.count(gts.E()))); assertEquals(0, listener1.addVertexEventRecorded()); assertEquals(0, listener2.addVertexEventRecorded()); assertEquals(1, listener1.addEdgeEventRecorded()); assertEquals(1, listener2.addEdgeEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldTriggerAddVertexPropertyChanged() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex vSome = graph.addVertex("some", "thing"); vSome.property(VertexProperty.Cardinality.single, "that", "thing"); final GraphTraversalSource gts = create(eventStrategy); final Vertex vAny = gts.V().addV().property("any", "thing").next(); gts.V(vAny).property(VertexProperty.Cardinality.single, "any", "thing else").next(); tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.V().has("any", "thing else")))); assertEquals(1, listener1.addVertexEventRecorded()); assertEquals(1, listener2.addVertexEventRecorded()); assertEquals(1, listener2.vertexPropertyChangedEventRecorded()); assertEquals(1, listener1.vertexPropertyChangedEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldTriggerAddVertexWithPropertyThenPropertyAdded() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex vSome = graph.addVertex("some", "thing"); vSome.property(VertexProperty.Cardinality.single, "that", "thing"); final GraphTraversalSource gts = create(eventStrategy); gts.V().addV().property("any", "thing").property(VertexProperty.Cardinality.single, "this", "thing").next(); tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.V().has("this", "thing")))); assertEquals(1, listener1.addVertexEventRecorded()); assertEquals(1, listener2.addVertexEventRecorded()); assertEquals(1, listener2.vertexPropertyChangedEventRecorded()); assertEquals(1, listener1.vertexPropertyChangedEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_REMOVE_PROPERTY) public void shouldTriggerRemoveEdgeProperty() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex v = graph.addVertex(); v.addEdge("self", v, "some", "thing"); final GraphTraversalSource gts = create(eventStrategy); gts.E().properties().drop().iterate(); tryCommit(graph, g -> assertEquals(0, IteratorUtils.count(gts.E().properties()))); assertEquals(1, listener1.edgePropertyRemovedEventRecorded()); assertEquals(1, listener2.edgePropertyRemovedEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldTriggerAddVertexPropertyAdded() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex vSome = graph.addVertex("some", "thing"); vSome.property(VertexProperty.Cardinality.single, "that", "thing"); final GraphTraversalSource gts = create(eventStrategy); gts.V().addV().property("this", "thing").next(); tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.V().has("this", "thing")))); assertEquals(1, listener1.addVertexEventRecorded()); assertEquals(1, listener2.addVertexEventRecorded()); assertEquals(0, listener2.vertexPropertyChangedEventRecorded()); assertEquals(0, listener1.vertexPropertyChangedEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_REMOVE_VERTICES) public void shouldTriggerRemoveVertex() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); graph.addVertex("some", "thing"); final GraphTraversalSource gts = create(eventStrategy); gts.V().drop().iterate(); tryCommit(graph, g -> assertEquals(0, IteratorUtils.count(gts.V()))); assertEquals(1, listener1.vertexRemovedEventRecorded()); assertEquals(1, listener2.vertexRemovedEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_REMOVE_EDGES) public void shouldTriggerRemoveEdge() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex v = graph.addVertex("some", "thing"); v.addEdge("self", v); final GraphTraversalSource gts = create(eventStrategy); gts.E().drop().iterate(); tryCommit(graph); assertEquals(1, listener1.edgeRemovedEventRecorded()); assertEquals(1, listener2.edgeRemovedEventRecorded()); }
private void closeTx(final Set<String> graphSourceNamesToCloseTxOn, final Transaction.Status tx) { final Set<Graph> graphsToCloseTxOn = new HashSet<>(); graphSourceNamesToCloseTxOn.forEach(name -> { if (this.graphs.containsKey(name)) { graphsToCloseTxOn.add(this.graphs.get(name)); } }); graphsToCloseTxOn.forEach(graph -> { if (graph.features().graph().supportsTransactions() && graph.tx().isOpen()) { if (tx == Transaction.Status.COMMIT) { graph.tx().commit(); } else { graph.tx().rollback(); } } }); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldTriggerAddVertexFromStart() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); graph.addVertex("some", "thing"); final GraphTraversalSource gts = create(eventStrategy); gts.addV().property("any", "thing").next(); tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.V().has("any", "thing")))); assertEquals(1, listener1.addVertexEventRecorded()); assertEquals(1, listener2.addVertexEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_REMOVE_PROPERTY) public void shouldTriggerRemoveVertexProperty() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); graph.addVertex("some", "thing"); final GraphTraversalSource gts = create(eventStrategy); gts.V().properties().drop().iterate(); tryCommit(graph, g -> assertEquals(0, IteratorUtils.count(gts.V().properties()))); assertEquals(1, listener1.vertexPropertyRemovedEventRecorded()); assertEquals(1, listener2.vertexPropertyRemovedEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldTriggerAddVertex() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); graph.addVertex("some", "thing"); final GraphTraversalSource gts = create(eventStrategy); gts.V().addV().property("any", "thing").next(); tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.V().has("any", "thing")))); assertEquals(1, listener1.addVertexEventRecorded()); assertEquals(1, listener2.addVertexEventRecorded()); }
/** * In this case, the feature requirement for sideEffects is checked, because it means that at least one aspect of * sideEffects is supported so we need to test other features to make sure they work properly. */ @Test @FeatureRequirement(featureClass = Graph.Features.VariableFeatures.class, feature = Graph.Features.VariableFeatures.FEATURE_VARIABLES) public void shouldEnableFeatureOnGraphIfNotEnabled() throws Exception { assumeThat(graph.features().supports(Graph.Features.VariableFeatures.class, featureName), is(false)); try { final Graph.Variables variables = graph.variables(); variables.set("aKey", value); fail(String.format(INVALID_FEATURE_SPECIFICATION, Graph.Features.VariableFeatures.class.getSimpleName(), featureName)); } catch (Exception e) { validateException(Graph.Variables.Exceptions.dataTypeOfVariableValueNotSupported(value), e); } } }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = VertexFeatures.class, feature = FEATURE_USER_SUPPLIED_IDS) @FeatureRequirement(featureClass = VertexFeatures.class, feature = FEATURE_STRING_IDS, supported = false) public void shouldSupportUserSuppliedIdsOfTypeString() throws Exception { final String id = "this-is-a-valid-id"; // a graph can "allow" an id without internally supporting it natively and therefore doesn't need // to throw the exception assumeFalse(graph.features().edge().willAllowId(id)); try { final Vertex v = graph.addVertex(); v.addEdge("test", v, T.id, id); fail(String.format(INVALID_FEATURE_SPECIFICATION, EdgeFeatures.class.getSimpleName(), FEATURE_STRING_IDS)); } catch (Exception e) { validateException(Edge.Exceptions.userSuppliedIdsOfThisTypeNotSupported(), e); } }