Coverage Summary for Class: TestTinkerPopEvent (org.umlg.sqlg.test.event)
Class |
Method, %
|
Branch, %
|
Line, %
|
TestTinkerPopEvent |
100%
(6/6)
|
50%
(2/4)
|
94%
(47/50)
|
TestTinkerPopEvent$1 |
100%
(2/2)
|
100%
(4/4)
|
TestTinkerPopEvent$AbstractMutationListener |
9.1%
(1/11)
|
9.1%
(1/11)
|
TestTinkerPopEvent$StubMutationListener |
21.7%
(5/23)
|
33.3%
(18/54)
|
Total |
33.3%
(14/42)
|
50%
(2/4)
|
58.8%
(70/119)
|
package org.umlg.sqlg.test.event;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;
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.event.MutationListener;
import org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy;
import org.apache.tinkerpop.gremlin.structure.*;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.umlg.sqlg.test.BaseTest;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.addV;
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.unfold;
import static org.junit.Assert.assertEquals;
/**
* @author <a href="https://github.com/pietermartin">Pieter Martin</a>
* Date: 2017/06/13
*/
public class TestTinkerPopEvent extends BaseTest {
@BeforeClass
public static void beforeClass() {
URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
try {
Configurations configs = new Configurations();
configuration = configs.properties(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);
}
}
@Test
public void test() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "John");
this.sqlgGraph.tx().commit();
a1 = this.sqlgGraph.traversal().V(a1).next();
Vertex a1Again = this.sqlgGraph.traversal().V(a1).next();
a1Again.property("name", "Peter");
this.sqlgGraph.tx().commit();
//This used to work when we had a vertex transaction cached.
// Assert.assertEquals("Peter", a1.value("name"));
Assert.assertNotEquals("Peter", a1.value("name"));
}
@Test
public void shouldDetachVertexPropertyWhenRemoved() {
final AtomicBoolean triggered = new AtomicBoolean(false);
final Vertex v = this.sqlgGraph.addVertex();
final VertexProperty vp = v.property("to-remove","blah");
final String label = vp.label();
final Object value = vp.value();
final VertexProperty vpToKeep = v.property("to-keep","dah");
final MutationListener listener = new AbstractMutationListener() {
@Override
public void vertexPropertyRemoved(final VertexProperty element) {
Assert.assertEquals(label, element.label());
Assert.assertEquals(value, element.value());
triggered.set(true);
}
};
final EventStrategy.Builder builder = EventStrategy.build().addListener(listener);
if (this.sqlgGraph.features().graph().supportsTransactions())
builder.eventQueue(new EventStrategy.TransactionalEventQueue(this.sqlgGraph));
final EventStrategy eventStrategy = builder.create();
final GraphTraversalSource gts = create(eventStrategy);
gts.V(v).properties("to-remove").drop().iterate();
this.sqlgGraph.tx().commit();
Vertex vAgain = gts.V(v).next();
Assert.assertEquals(1, IteratorUtils.count(vAgain.properties()));
Assert.assertEquals(vpToKeep.value(), v.value("to-keep"));
MatcherAssert.assertThat(triggered.get(), CoreMatchers.is(true));
}
@Test
public void shouldTriggerAddVertexAndPropertyUpdateWithCoalescePattern() {
final StubMutationListener listener1 = new StubMutationListener();
final StubMutationListener listener2 = new StubMutationListener();
final EventStrategy.Builder builder = EventStrategy.build()
.addListener(listener1)
.addListener(listener2);
builder.eventQueue(new EventStrategy.TransactionalEventQueue(this.sqlgGraph));
final EventStrategy eventStrategy = builder.create();
final GraphTraversalSource gts = create(eventStrategy);
Traversal<Vertex, Vertex> traversal = gts.V().has("some","thing").fold().coalesce(unfold(), addV()).property("some", "thing");
traversal.iterate();
this.sqlgGraph.tx().commit();
assertEquals(1, IteratorUtils.count(gts.V().has("some", "thing")));
assertEquals(1, listener1.addVertexEventRecorded());
assertEquals(1, listener2.addVertexEventRecorded());
assertEquals(1, listener1.vertexPropertyChangedEventRecorded());
assertEquals(1, listener2.vertexPropertyChangedEventRecorded());
}
private GraphTraversalSource create(final EventStrategy strategy) {
return this.sqlgGraph.traversal().withStrategies(strategy);
}
static abstract class AbstractMutationListener implements MutationListener {
@Override
public void vertexAdded(final Vertex vertex) {
}
@Override
public void vertexRemoved(final Vertex vertex) {
}
@Override
public void vertexPropertyChanged(Vertex element, VertexProperty oldValue, Object setValue, Object... vertexPropertyKeyValues) {
}
@Override
public void vertexPropertyRemoved(final VertexProperty vertexProperty) {
}
@Override
public void edgeAdded(final Edge edge) {
}
@Override
public void edgeRemoved(final Edge edge) {
}
@Override
public void edgePropertyChanged(final Edge element, final Property oldValue, final Object setValue) {
}
@Override
public void edgePropertyRemoved(final Edge element, final Property property) {
}
@Override
public void vertexPropertyPropertyChanged(final VertexProperty element, final Property oldValue, final Object setValue) {
}
@Override
public void vertexPropertyPropertyRemoved(final VertexProperty element, final Property property) {
}
}
static class StubMutationListener implements MutationListener {
private final AtomicLong addEdgeEvent = new AtomicLong(0);
private final AtomicLong addVertexEvent = new AtomicLong(0);
private final AtomicLong vertexRemovedEvent = new AtomicLong(0);
private final AtomicLong edgePropertyChangedEvent = new AtomicLong(0);
private final AtomicLong vertexPropertyChangedEvent = new AtomicLong(0);
private final AtomicLong vertexPropertyPropertyChangedEvent = new AtomicLong(0);
private final AtomicLong edgePropertyRemovedEvent = new AtomicLong(0);
private final AtomicLong vertexPropertyPropertyRemovedEvent = new AtomicLong(0);
private final AtomicLong edgeRemovedEvent = new AtomicLong(0);
private final AtomicLong vertexPropertyRemovedEvent = new AtomicLong(0);
private final ConcurrentLinkedQueue<String> order = new ConcurrentLinkedQueue<>();
public void reset() {
addEdgeEvent.set(0);
addVertexEvent.set(0);
vertexRemovedEvent.set(0);
edgePropertyChangedEvent.set(0);
vertexPropertyChangedEvent.set(0);
vertexPropertyPropertyChangedEvent.set(0);
vertexPropertyPropertyRemovedEvent.set(0);
edgePropertyRemovedEvent.set(0);
edgeRemovedEvent.set(0);
vertexPropertyRemovedEvent.set(0);
order.clear();
}
public List<String> getOrder() {
return new ArrayList<>(this.order);
}
@Override
public void vertexAdded(final Vertex vertex) {
addVertexEvent.incrementAndGet();
order.add("v-added-" + vertex.id());
}
@Override
public void vertexRemoved(final Vertex vertex) {
vertexRemovedEvent.incrementAndGet();
order.add("v-removed-" + vertex.id());
}
@Override
public void edgeAdded(final Edge edge) {
addEdgeEvent.incrementAndGet();
order.add("e-added-" + edge.id());
}
@Override
public void edgePropertyRemoved(final Edge element, final Property o) {
edgePropertyRemovedEvent.incrementAndGet();
order.add("e-property-removed-" + element.id() + "-" + o);
}
@Override
public void vertexPropertyPropertyRemoved(final VertexProperty element, final Property o) {
vertexPropertyPropertyRemovedEvent.incrementAndGet();
order.add("vp-property-removed-" + element.id() + "-" + o);
}
@Override
public void edgeRemoved(final Edge edge) {
edgeRemovedEvent.incrementAndGet();
order.add("e-removed-" + edge.id());
}
@Override
public void vertexPropertyRemoved(final VertexProperty vertexProperty) {
vertexPropertyRemovedEvent.incrementAndGet();
order.add("vp-property-removed-" + vertexProperty.id());
}
@Override
public void edgePropertyChanged(final Edge element, final Property oldValue, final Object setValue) {
edgePropertyChangedEvent.incrementAndGet();
order.add("e-property-chanaged-" + element.id());
}
@Override
public void vertexPropertyPropertyChanged(final VertexProperty element, final Property oldValue, final Object setValue) {
vertexPropertyPropertyChangedEvent.incrementAndGet();
order.add("vp-property-changed-" + element.id());
}
@Override
public void vertexPropertyChanged(final Vertex element, final VertexProperty oldValue, final Object setValue, final Object... vertexPropertyKeyValues) {
vertexPropertyChangedEvent.incrementAndGet();
order.add("v-property-changed-" + element.id());
}
public long addEdgeEventRecorded() {
return addEdgeEvent.get();
}
public long addVertexEventRecorded() {
return addVertexEvent.get();
}
public long vertexRemovedEventRecorded() {
return vertexRemovedEvent.get();
}
public long edgeRemovedEventRecorded() {
return edgeRemovedEvent.get();
}
public long edgePropertyRemovedEventRecorded() {
return edgePropertyRemovedEvent.get();
}
public long vertexPropertyRemovedEventRecorded() {
return vertexPropertyRemovedEvent.get();
}
public long vertexPropertyPropertyRemovedEventRecorded() {
return vertexPropertyPropertyRemovedEvent.get();
}
public long edgePropertyChangedEventRecorded() {
return edgePropertyChangedEvent.get();
}
public long vertexPropertyChangedEventRecorded() {
return vertexPropertyChangedEvent.get();
}
public long vertexPropertyPropertyChangedEventRecorded() {
return vertexPropertyPropertyChangedEvent.get();
}
}
}