diff --git a/CHANGELOG.md b/CHANGELOG.md index eb1914ca91c6518046f6f37a2f1b06db24360a89..c40a58fbb1fa826dd9be6690901133ba9692f09a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ ## 3.2.1 +* Removed `throws exception` from the signature of the TxActions. +* Fixed a bug where the `VertexFrame.toJson()` and `EdgeFrame.toJson()` methods wouldn't encode properties that werent + of the type `Number` or `String` +* When constructing a `DelegatingFramedGraph` if a the delegate graph argument is null the constructor now throws an + exception. +* `AbstractAnnotationFrameFactory.constructClass()` method now throws an exception if the element argument is neither an + `Edge` or a `Vertex`. +* Added several more unit tests bring test coverage up an additional 5%. + ## 3.2.0 * Added nexus staging deployment plugin. diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 09f21a12f45ff1cfc596ae7e7d4847ce13210050..ef9c6ca1dbb395e59802dcfdcdb0397eef1a9fd1 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -10,4 +10,4 @@ * Johannes Schüth <j.schueth@jotschi.de> * Evan Thompson * Joshua Shinavier <josh@fortytwo.net> -* Veselin Iordanov +* Veselin Yordanov <vesko935@gmail.com> diff --git a/src/main/java/com/syncleus/ferma/AbstractEdgeFrame.java b/src/main/java/com/syncleus/ferma/AbstractEdgeFrame.java index 7106cc9c6bc9ca292cf5e4e0db5587fce355141c..f9f2ab59c6b91df4ee2f38dc047b4823fbb3602d 100644 --- a/src/main/java/com/syncleus/ferma/AbstractEdgeFrame.java +++ b/src/main/java/com/syncleus/ferma/AbstractEdgeFrame.java @@ -60,8 +60,12 @@ public abstract class AbstractEdgeFrame extends AbstractElementFrame implements final Object value = getProperty(key); if (value instanceof Number) json.addProperty(key, (Number) value); - else if (value instanceof String) - json.addProperty(key, (String) value); + else if (value instanceof Boolean) + json.addProperty(key, (Boolean) value); + else if (value instanceof Character) + json.addProperty(key, (Character) value); + else + json.addProperty(key, value.toString()); } return json; } diff --git a/src/main/java/com/syncleus/ferma/AbstractVertexFrame.java b/src/main/java/com/syncleus/ferma/AbstractVertexFrame.java index f06abb2e5662366884938ae9106ec1280257a7e6..8da3df5b8552548a9ff6a53dcc8bab63e9a07653 100644 --- a/src/main/java/com/syncleus/ferma/AbstractVertexFrame.java +++ b/src/main/java/com/syncleus/ferma/AbstractVertexFrame.java @@ -245,8 +245,12 @@ public abstract class AbstractVertexFrame extends AbstractElementFrame implement final Object value = getProperty(key); if (value instanceof Number) json.addProperty(key, (Number) value); - else if (value instanceof String) - json.addProperty(key, (String) value); + else if(value instanceof Boolean) + json.addProperty(key, (Boolean) value); + else if(value instanceof Character) + json.addProperty(key, (Character) value); + else + json.addProperty(key, value.toString()); } return json; } diff --git a/src/main/java/com/syncleus/ferma/DelegatingFramedGraph.java b/src/main/java/com/syncleus/ferma/DelegatingFramedGraph.java index 04258735ae6a7785601231ecdacc49124c94c0e5..9fee13f70386a63def309255fb9613e3af35569f 100644 --- a/src/main/java/com/syncleus/ferma/DelegatingFramedGraph.java +++ b/src/main/java/com/syncleus/ferma/DelegatingFramedGraph.java @@ -63,13 +63,14 @@ public class DelegatingFramedGraph<G extends Graph> implements WrappedFramedGrap * The type defaultResolver that will decide the final frame type. */ public DelegatingFramedGraph(final G delegate, final FrameFactory builder, final TypeResolver defaultResolver) { - this.delegate = delegate; - if( builder == null ) throw new IllegalArgumentException("builder can not be null"); else if( defaultResolver == null ) throw new IllegalArgumentException("defaultResolver can not be null"); + else if( delegate == null ) + throw new IllegalArgumentException("delegate can not be null"); + this.delegate = delegate; this.defaultResolver = defaultResolver; this.untypedResolver = new UntypedTypeResolver(); this.builder = builder; diff --git a/src/main/java/com/syncleus/ferma/framefactories/annotation/AbstractAnnotationFrameFactory.java b/src/main/java/com/syncleus/ferma/framefactories/annotation/AbstractAnnotationFrameFactory.java index 38f0985bd2ff06aa181c003ab1d6fe50a9698154..61071199232ed245f3f54a9b6fc3375d51893025 100644 --- a/src/main/java/com/syncleus/ferma/framefactories/annotation/AbstractAnnotationFrameFactory.java +++ b/src/main/java/com/syncleus/ferma/framefactories/annotation/AbstractAnnotationFrameFactory.java @@ -84,9 +84,11 @@ public class AbstractAnnotationFrameFactory implements FrameFactory { else throw new IllegalStateException("class is neither an Edge or a vertex!"); else { - if (element instanceof Vertex && !VertexFrame.class.isAssignableFrom(clazz)) + if( !(element instanceof Vertex || element instanceof Edge) ) + throw new IllegalStateException("element is neither an edge nor a vertex"); + else if (element instanceof Vertex && !VertexFrame.class.isAssignableFrom(clazz)) throw new IllegalStateException(clazz.getName() + " Class is not a type of VertexFrame"); - if (element instanceof Edge && !EdgeFrame.class.isAssignableFrom(clazz)) + else if (element instanceof Edge && !EdgeFrame.class.isAssignableFrom(clazz)) throw new IllegalStateException(clazz.getName() + " Class is not a type of EdgeFrame"); classBuilder = new ByteBuddy().subclass(clazz); } diff --git a/src/main/java/com/syncleus/ferma/tx/TxAction.java b/src/main/java/com/syncleus/ferma/tx/TxAction.java index ab73b8346e959ae38f2817dff55be4621230b87e..9784ee0f26d8a258f2ba1386f636895c6691d61a 100644 --- a/src/main/java/com/syncleus/ferma/tx/TxAction.java +++ b/src/main/java/com/syncleus/ferma/tx/TxAction.java @@ -18,6 +18,6 @@ package com.syncleus.ferma.tx; @FunctionalInterface public interface TxAction<T> { - T handle(Tx tx) throws Exception; + T handle(Tx tx); } diff --git a/src/main/java/com/syncleus/ferma/tx/TxAction0.java b/src/main/java/com/syncleus/ferma/tx/TxAction0.java index 09bb86506a2e4a32705ff2d25274f60925ffcb3e..4f74760fc89f10d1b00997f536f9a4a1de644dca 100644 --- a/src/main/java/com/syncleus/ferma/tx/TxAction0.java +++ b/src/main/java/com/syncleus/ferma/tx/TxAction0.java @@ -18,6 +18,6 @@ package com.syncleus.ferma.tx; @FunctionalInterface public interface TxAction0 { - void handle() throws Exception; + void handle(); } diff --git a/src/main/java/com/syncleus/ferma/tx/TxAction1.java b/src/main/java/com/syncleus/ferma/tx/TxAction1.java index b38ff3423ac853f3f300897ce0d81dc6f179ce0b..ed1527acd51820b77d50e5a6171948e4013e593f 100644 --- a/src/main/java/com/syncleus/ferma/tx/TxAction1.java +++ b/src/main/java/com/syncleus/ferma/tx/TxAction1.java @@ -18,6 +18,6 @@ package com.syncleus.ferma.tx; @FunctionalInterface public interface TxAction1<T> { - T handle() throws Exception; + T handle(); } diff --git a/src/main/java/com/syncleus/ferma/tx/TxAction2.java b/src/main/java/com/syncleus/ferma/tx/TxAction2.java index 723d18f33c422ae9bd757434e68d20fe802fd2ac..692964b8aa543850eac97567556637476b6b7c17 100644 --- a/src/main/java/com/syncleus/ferma/tx/TxAction2.java +++ b/src/main/java/com/syncleus/ferma/tx/TxAction2.java @@ -18,6 +18,6 @@ package com.syncleus.ferma.tx; @FunctionalInterface public interface TxAction2 { - void handle(Tx tx) throws Exception; + void handle(Tx tx); } diff --git a/src/test/java/com/syncleus/ferma/AbstractElementFrameTest.java b/src/test/java/com/syncleus/ferma/AbstractElementFrameTest.java index a840bdc689e6792405579a094cdd1f033c7f583b..a3355dd031ebb28d3c0c7d572cc825f85b8fdbbb 100644 --- a/src/test/java/com/syncleus/ferma/AbstractElementFrameTest.java +++ b/src/test/java/com/syncleus/ferma/AbstractElementFrameTest.java @@ -25,6 +25,9 @@ import org.mockito.MockitoAnnotations; import com.google.common.collect.Sets; import com.google.gson.JsonObject; import com.syncleus.ferma.graphtypes.javaclass.JavaAccessModifier; +import java.io.IOException; +import org.apache.tinkerpop.gremlin.structure.T; +import org.junit.After; public class AbstractElementFrameTest { @@ -44,6 +47,11 @@ public class AbstractElementFrameTest { e1 = p1.addKnows(p2); e1.setYears(15); } + + @After + public void deinit() throws IOException { + fg.close(); + } @Test public void testGetId() { @@ -182,6 +190,27 @@ public class AbstractElementFrameTest { Assert.assertEquals(p1.getName(), actual.get("name").getAsString()); } + @Test + public void testVtoJson2() { + String stringPropName = "custom-string-property"; + String stringPropValue = "custom-string-value"; + String charPropName = "custom-char-property"; + Character charPropValue = 'D'; + String intPropName = "custom-int-property"; + int intPropValue = 1234; + Person expected = fg.addFramedVertex(Person.DEFAULT_INITIALIZER, + T.id, "some-id", + stringPropName, stringPropValue, + charPropName, charPropValue, + intPropName, intPropValue); + JsonObject actual = expected.toJson(); + Assert.assertEquals(expected.getId(String.class), actual.get("id").getAsString()); + Assert.assertEquals("vertex", actual.get("elementClass").getAsString()); + Assert.assertEquals(expected.getProperty(intPropName), (Integer) actual.get(intPropName).getAsInt()); + Assert.assertEquals(expected.getProperty(stringPropName), actual.get(stringPropName).getAsString()); + Assert.assertEquals(expected.getProperty(charPropName), (Character) actual.get(charPropName).getAsCharacter()); + } + @Test public void testEtoJson() { JsonObject actual = e1.toJson(); @@ -189,4 +218,37 @@ public class AbstractElementFrameTest { Assert.assertEquals("edge", actual.get("elementClass").getAsString()); Assert.assertEquals(e1.getYears(), actual.get("years").getAsInt()); } + + @Test + public void testEtoJson2() { + String stringPropName = "custom-string-property"; + String stringPropValue = "custom-string-value"; + String charPropName = "custom-char-property"; + Character charPropValue = 'D'; + Person p3 = fg.addFramedVertex(Person.DEFAULT_INITIALIZER); + Knows expected = fg.addFramedEdge(p1, p3, "knows", Knows.DEFAULT_INITIALIZER, + "years", 15, + T.id, "some-id", + stringPropName, stringPropValue, + charPropName, charPropValue); + JsonObject actual = expected.toJson(); + Assert.assertEquals(expected.getId(String.class), actual.get("id").getAsString()); + Assert.assertEquals("edge", actual.get("elementClass").getAsString()); + Assert.assertEquals(expected.getYears(), actual.get("years").getAsInt()); + Assert.assertEquals(expected.getProperty(stringPropName), actual.get(stringPropName).getAsString()); + Assert.assertEquals(expected.getProperty(charPropName), (Character) actual.get(charPropName).getAsCharacter()); + } + + @Test + public void testEquals() { + Assert.assertFalse(p1.equals(null)); + Programmer g1 = fg.addFramedVertex(Programmer.class); + Assert.assertFalse(p1.equals(g1)); + Person p4 = fg.frameElement(p1.getElement(), Person.class); + Assert.assertTrue(p1.equals(p4)); + Person p3 = fg.addFramedVertex(Person.DEFAULT_INITIALIZER); + Assert.assertFalse(p1.equals(p3)); + p1.setElement(null); + Assert.assertFalse(p1.equals(p3)); + } } diff --git a/src/test/java/com/syncleus/ferma/ComparatorsTest.java b/src/test/java/com/syncleus/ferma/ComparatorsTest.java index 851563298026e9719af8cf4fea3ef4094e02ed0c..9f2a70851512900295fce3105aa0bc1711fa5ddd 100644 --- a/src/test/java/com/syncleus/ferma/ComparatorsTest.java +++ b/src/test/java/com/syncleus/ferma/ComparatorsTest.java @@ -15,9 +15,11 @@ */ package com.syncleus.ferma; +import java.io.IOException; import java.util.Comparator; import org.apache.tinkerpop.gremlin.structure.T; import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -30,16 +32,22 @@ public class ComparatorsTest { private Person p1; private Person p2; + private FramedGraph g; @Before public void setUp() { - FramedGraph g = new DelegatingFramedGraph(TinkerGraph.open()); + g = new DelegatingFramedGraph(TinkerGraph.open()); p1 = g.addFramedVertex(Person.DEFAULT_INITIALIZER, T.id, "1"); p1.setName("Alice"); p2 = g.addFramedVertex(Person.DEFAULT_INITIALIZER, T.id, "2"); p2.setName("Bob"); } + @After + public void tearDown() throws IOException { + g.close(); + } + @Test public void testIdComparator() { Comparator<ElementFrame> comparator = Comparators.id(); diff --git a/src/test/java/com/syncleus/ferma/DelegatingFrameGraphSanityTest.java b/src/test/java/com/syncleus/ferma/DelegatingFrameGraphSanityTest.java new file mode 100644 index 0000000000000000000000000000000000000000..af00440e99dcf9367417de0fcc85f3755bd9dd12 --- /dev/null +++ b/src/test/java/com/syncleus/ferma/DelegatingFrameGraphSanityTest.java @@ -0,0 +1,90 @@ +/** + * Copyright 2004 - 2017 Syncleus, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.syncleus.ferma; + +import com.syncleus.ferma.typeresolvers.TypeResolver; +import com.syncleus.ferma.typeresolvers.UntypedTypeResolver; +import java.util.HashSet; +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests if DelegatingFramedGraph constructors create usable instance + * + * @author rqpa + */ +public class DelegatingFrameGraphSanityTest { + + private Graph g; + private TypeResolver resolver; + + @Before + public void setUp() { + g = TinkerGraph.open(); + resolver = new UntypedTypeResolver(); + } + + @After + public void tearDown() throws Exception { + g.close(); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullDelegate() { + new DelegatingFramedGraph(null, resolver); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullResolver() { + new DelegatingFramedGraph(g, (TypeResolver) null); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullBuilder() { + new DelegatingFramedGraph(g, null, resolver); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullReflectionsCache() { + new DelegatingFramedGraph(g, null, true, true); + } + + @Test + public void testNoTypeResolutionNoAnnotations() { + assertSanity(new DelegatingFramedGraph(g, new ReflectionCache(), false, false)); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullTypes() { + new DelegatingFramedGraph(g, true, null); + } + + @Test + public void testEmptyTypesSet() { + assertSanity(new DelegatingFramedGraph(g, false, new HashSet<>())); + } + + private void assertSanity(DelegatingFramedGraph framed) { + Assert.assertNotNull(framed.getBaseGraph()); + Assert.assertNotNull(framed.getBuilder()); + Assert.assertNotNull(framed.getTypeResolver()); + } + +} diff --git a/src/test/java/com/syncleus/ferma/DelegatingFrameGraphTest.java b/src/test/java/com/syncleus/ferma/DelegatingFrameGraphTest.java index d4094df24911d395960c93c3b2a94e9135b304f9..10bfe79f8f8b9207765575887b2c5cb6917e0309 100644 --- a/src/test/java/com/syncleus/ferma/DelegatingFrameGraphTest.java +++ b/src/test/java/com/syncleus/ferma/DelegatingFrameGraphTest.java @@ -228,4 +228,5 @@ public class DelegatingFrameGraphTest extends NetworkGraphTestHelper { NetworkConnectionEdge.class); assertAllConnections(actual); } + } diff --git a/src/test/java/com/syncleus/ferma/DelegatingTransactionTest.java b/src/test/java/com/syncleus/ferma/DelegatingTransactionTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a7265aa4074b4fc5c2e16bc5142ef8fc44eeb0db --- /dev/null +++ b/src/test/java/com/syncleus/ferma/DelegatingTransactionTest.java @@ -0,0 +1,177 @@ +/** + * Copyright 2004 - 2017 Syncleus, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.syncleus.ferma; + +import java.util.function.Consumer; + +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.apache.tinkerpop.gremlin.structure.Transaction; +import org.apache.tinkerpop.gremlin.structure.util.wrapped.WrappedGraph; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import static org.mockito.Mockito.when; + +/** + * + * @author rqpa + */ +public class DelegatingTransactionTest { + + private Transaction gremlinTx; + private WrappedFramedGraph<Graph> framedGraph; + private Graph baseGraph; + private DelegatingTransaction delegatingTx; + + @Before + public void setUp() { + gremlinTx = Mockito.mock(Transaction.class); + framedGraph = Mockito.mock(WrappedFramedGraph.class, Mockito.RETURNS_MOCKS); + baseGraph = Mockito.mock(Graph.class, Mockito.RETURNS_MOCKS); + + when(gremlinTx.createThreadedTx()).thenReturn(baseGraph); + when(framedGraph.getBaseGraph()).thenReturn(baseGraph); + + delegatingTx = new DelegatingTransaction(gremlinTx, framedGraph); + } + + @Test + public void testAddTxListener() { + + Consumer<Transaction.Status> txListener = Mockito.mock(Consumer.class, "Foo"); + delegatingTx.addTransactionListener(txListener); + + // Only delegating so the same listener should be passed + Mockito.verify(gremlinTx, Mockito.times(1)).addTransactionListener(txListener); + Mockito.verifyZeroInteractions(framedGraph); + Mockito.verifyZeroInteractions(txListener); + Mockito.verifyNoMoreInteractions(gremlinTx); + } + + @Test + public void testClearTxListeners() { + delegatingTx.clearTransactionListeners(); + + Mockito.verify(gremlinTx, Mockito.times(1)).clearTransactionListeners(); + Mockito.verifyZeroInteractions(framedGraph); + Mockito.verifyNoMoreInteractions(gremlinTx); + } + + @Test + public void testAutoClosable() { + try (DelegatingTransaction tx = new DelegatingTransaction(gremlinTx, framedGraph)){ + + } + + Mockito.verify(gremlinTx, Mockito.times(1)).close(); + Mockito.verifyZeroInteractions(framedGraph); + Mockito.verifyNoMoreInteractions(gremlinTx); + } + + @Test + public void testCommit() { + delegatingTx.commit(); + + Mockito.verify(gremlinTx, Mockito.times(1)).commit(); + Mockito.verifyZeroInteractions(framedGraph); + Mockito.verifyNoMoreInteractions(gremlinTx); + } + + @Test + public void testCreateThreadedTx() { + delegatingTx.createThreadedTx(); + + Mockito.verify(gremlinTx, Mockito.times(1)).createThreadedTx(); + // A new wrapped graph may need to be constructed so calls to simple + // getters are OK + Mockito.verify(framedGraph, Mockito.atLeast(0)).getBaseGraph(); + Mockito.verify(framedGraph, Mockito.atLeast(0)).getBuilder(); + Mockito.verify(framedGraph, Mockito.atLeast(0)).getRawTraversal(); + Mockito.verify(framedGraph, Mockito.atLeast(0)).getTypeResolver(); + // No other interactions. We're only delegating + Mockito.verifyNoMoreInteractions(gremlinTx, framedGraph); + } + + @Test + public void testGetDelegate() { + Transaction actualDelegate = delegatingTx.getDelegate(); + Mockito.verifyZeroInteractions(gremlinTx, framedGraph); + Assert.assertEquals(gremlinTx, actualDelegate); + } + + @Test + public void testGetGraph() { + WrappedGraph actualDelegate = delegatingTx.getGraph(); + Mockito.verifyZeroInteractions(gremlinTx, framedGraph); + Assert.assertEquals(framedGraph, actualDelegate); + } + + @Test + public void testIsOpen() { + assertDelegatingIsOpenUsage(true); + assertDelegatingIsOpenUsage(false); + } + + private void assertDelegatingIsOpenUsage(boolean expectedValue) { + Transaction tx = Mockito.mock(Transaction.class); + WrappedFramedGraph<?> graph = Mockito.mock(WrappedFramedGraph.class); + when(tx.isOpen()).thenReturn(expectedValue); + DelegatingTransaction delTx = new DelegatingTransaction(tx, graph); + Assert.assertEquals(expectedValue, delTx.isOpen()); + + Mockito.verify(tx, Mockito.times(1)).isOpen(); + Mockito.verifyNoMoreInteractions(tx, graph); + } + + @Test + public void testOpenTx() { + delegatingTx.open(); + + Mockito.verify(gremlinTx, Mockito.times(1)).open(); + Mockito.verifyZeroInteractions(framedGraph); + Mockito.verifyNoMoreInteractions(gremlinTx); + } + + @Test + public void testReadWrite() { + delegatingTx.readWrite(); + + Mockito.verify(gremlinTx, Mockito.times(1)).readWrite(); + Mockito.verifyZeroInteractions(framedGraph); + Mockito.verifyNoMoreInteractions(gremlinTx); + } + + @Test + public void testRollback() { + delegatingTx.rollback(); + + Mockito.verify(gremlinTx, Mockito.times(1)).rollback(); + Mockito.verifyZeroInteractions(framedGraph); + Mockito.verifyNoMoreInteractions(gremlinTx); + } + + @Test + public void testRemoveTxListener() { + Consumer<Transaction.Status> txListener = Mockito.mock(Consumer.class, "Foo"); + delegatingTx.removeTransactionListener(txListener); + + Mockito.verify(gremlinTx, Mockito.times(1)).removeTransactionListener(txListener); + Mockito.verifyZeroInteractions(framedGraph, txListener); + Mockito.verifyNoMoreInteractions(gremlinTx); + } +} diff --git a/src/test/java/com/syncleus/ferma/FramedEdgeTest.java b/src/test/java/com/syncleus/ferma/FramedEdgeTest.java index ba88420c897ded64d4b341b4d4fb313483395334..755c855eb13a7cc17d2a56a22bf2d971892bc420 100644 --- a/src/test/java/com/syncleus/ferma/FramedEdgeTest.java +++ b/src/test/java/com/syncleus/ferma/FramedEdgeTest.java @@ -15,9 +15,11 @@ */ package com.syncleus.ferma; +import java.io.IOException; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; import org.apache.tinkerpop.gremlin.structure.Graph; import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -43,6 +45,11 @@ public class FramedEdgeTest { e1.setYears(15); } + + @After + public void deinit() throws IOException { + fg.close(); + } @Test public void testLabel() { diff --git a/src/test/java/com/syncleus/ferma/FramedVertexTest.java b/src/test/java/com/syncleus/ferma/FramedVertexTest.java index 01d060b4862939bced633c7c98605fdf613b208b..2351e12ff0d99f58fe851b3673eea3dfbe44c0cb 100644 --- a/src/test/java/com/syncleus/ferma/FramedVertexTest.java +++ b/src/test/java/com/syncleus/ferma/FramedVertexTest.java @@ -28,8 +28,10 @@ import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; import com.google.common.collect.Lists; +import java.io.IOException; import javax.annotation.Nullable; +import org.junit.After; public class FramedVertexTest { private static final Function<GraphTraversal<Vertex, Vertex>, GraphTraversal<?, ?>> OUT_TRAVERSAL = input -> input.out(); @@ -52,6 +54,11 @@ public class FramedVertexTest { e1.setYears(15); } + + @After + public void deinit() throws IOException { + fg.close(); + } @Test public void testOut() { diff --git a/src/test/java/com/syncleus/ferma/annotations/AdjacencyHandlerWithNetworkGraphTest.java b/src/test/java/com/syncleus/ferma/annotations/AdjacencyHandlerWithNetworkGraphTest.java index f60342ea4ee6bff8ddc4f38ad1d1feea961a7d70..55be58fe331053411b8db339bfff6fec756aeb97 100644 --- a/src/test/java/com/syncleus/ferma/annotations/AdjacencyHandlerWithNetworkGraphTest.java +++ b/src/test/java/com/syncleus/ferma/annotations/AdjacencyHandlerWithNetworkGraphTest.java @@ -124,7 +124,7 @@ public class AdjacencyHandlerWithNetworkGraphTest extends NetworkGraphTestHelper testAddVertexBothVertexTypedEdgeTyped(); ComputerVertex newDev = findDeviceByName(graph, newDevName); // Now make it linked only to devices 2 and 4 - newDev.setTwoWayConnectionsWith(Arrays.asList(dev2, dev4).iterator()); + newDev.setTwoWayConnectionsWithIterator(Arrays.asList(dev2, dev4).iterator()); assertTwoWayConnection(newDev, dev2); assertTwoWayConnection(newDev, dev4); assertNoConnection(newDev, dev1); @@ -136,7 +136,7 @@ public class AdjacencyHandlerWithNetworkGraphTest extends NetworkGraphTestHelper testAddVertexBothVertexTypedEdgeTyped(); ComputerVertex newDev = findDeviceByName(graph, newDevName); // Now make it linked only to devices 2 and 4 - newDev.setTwoWayConnectionsWith(Arrays.asList(dev2, dev4)); + newDev.setTwoWayConnectionsWithIterable(Arrays.asList(dev2, dev4)); assertTwoWayConnection(newDev, dev2); assertTwoWayConnection(newDev, dev4); assertNoConnection(newDev, dev1); diff --git a/src/test/java/com/syncleus/ferma/annotations/AdjacencyMethodHandlerTest.java b/src/test/java/com/syncleus/ferma/annotations/AdjacencyMethodHandlerTest.java index f0bb49cf4dc60b4d964c42711f7a23bfdee3697a..750230d46d26a6165760c653819ead0fd3706345 100644 --- a/src/test/java/com/syncleus/ferma/annotations/AdjacencyMethodHandlerTest.java +++ b/src/test/java/com/syncleus/ferma/annotations/AdjacencyMethodHandlerTest.java @@ -22,20 +22,34 @@ import org.junit.Assert; import org.junit.Test; import java.util.*; +import org.junit.After; +import org.junit.Before; public class AdjacencyMethodHandlerTest { private static final Set<Class<?>> TEST_TYPES = new HashSet<>(Arrays.asList(new Class<?>[]{God.class, FatherEdge.class, GodExtended.class, GodAlternative.class})); + private TinkerGraph godGraph; + + @Before + public void setUp() { + godGraph = TinkerGraph.open(); + } + + @After + public void tearDown() { + godGraph.close(); + } + @Test public void testGetSonsDefault() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -53,13 +67,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testGetSonsListDefault() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -77,13 +91,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testGetSonsSetDefault() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -101,13 +115,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testGetSonsByType() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -125,13 +139,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testGetSonsListByType() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -149,13 +163,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testGetSonsSetByType() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -173,13 +187,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testObtainSonsByType() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -197,13 +211,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testGetSonsExtended() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -221,13 +235,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testGetSonsForceAlternative() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends GodAlternative> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(GodAlternative.class); + input -> input.V().has("name", "jupiter")).toList(GodAlternative.class); final GodAlternative father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -246,13 +260,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testGetSonsNoLabel() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends GodAlternative> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(GodAlternative.class); + input -> input.V().has("name", "jupiter")).toList(GodAlternative.class); final GodAlternative father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -265,13 +279,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testGetSonDefault() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -288,13 +302,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testGetSonByType() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -311,13 +325,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testAddSonDefault() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -330,13 +344,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testAddSonByTypeUntypedEdge() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -351,13 +365,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testAddSonByObjectUntypedEdge() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -374,13 +388,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testAddSonByTypeTypedEdge() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -395,13 +409,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testincludeSon() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -416,13 +430,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testAddSonByObjectTypedEdge() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -439,13 +453,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testSetSonsEmpty() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -465,13 +479,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testSetSons() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -495,13 +509,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testSetSon() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -530,13 +544,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testSetSonsList() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -560,13 +574,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testApplySons() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -590,13 +604,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testRemoveSon() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -616,13 +630,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testRemoveEverySon() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -635,13 +649,13 @@ public class AdjacencyMethodHandlerTest { @Test public void testDeleteSon() { - final TinkerGraph godGraph = TinkerGraph.open(); + GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, TEST_TYPES); final List<? extends God> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(God.class); + input -> input.V().has("name", "jupiter")).toList(God.class); final God father = gods.iterator().next(); Assert.assertTrue(father != null); @@ -664,13 +678,12 @@ public class AdjacencyMethodHandlerTest { final Set<Class<?>> exceptionTypes = new HashSet<>(Arrays.asList(new Class<?>[]{BadGetSonsArgumentClass.class})); - final TinkerGraph godGraph = TinkerGraph.open(); GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, exceptionTypes); final List<? extends BadGetSonsArgumentClass> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentClass.class); + input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentClass.class); final BadGetSonsArgumentClass father = gods.iterator().next(); } @@ -680,13 +693,12 @@ public class AdjacencyMethodHandlerTest { final Set<Class<?>> exceptionTypes = new HashSet<>(Arrays.asList(new Class<?>[]{BadGetSonsArgumentInterface.class})); - final TinkerGraph godGraph = TinkerGraph.open(); GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, exceptionTypes); final List<? extends BadGetSonsArgumentInterface> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentInterface.class); + input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentInterface.class); final BadGetSonsArgumentInterface father = gods.iterator().next(); } @@ -696,13 +708,12 @@ public class AdjacencyMethodHandlerTest { final Set<Class<?>> exceptionTypes = new HashSet<>(Arrays.asList(new Class<?>[]{BadAddSonNoArguments.class})); - final TinkerGraph godGraph = TinkerGraph.open(); GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, exceptionTypes); final List<? extends BadGetSonsArgumentInterface> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentInterface.class); + input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentInterface.class); final BadGetSonsArgumentInterface father = gods.iterator().next(); } @@ -712,13 +723,12 @@ public class AdjacencyMethodHandlerTest { final Set<Class<?>> exceptionTypes = new HashSet<>(Arrays.asList(new Class<?>[]{BadAddSonArgument.class})); - final TinkerGraph godGraph = TinkerGraph.open(); GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, exceptionTypes); final List<? extends BadGetSonsArgumentInterface> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentInterface.class); + input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentInterface.class); final BadGetSonsArgumentInterface father = gods.iterator().next(); } @@ -728,13 +738,12 @@ public class AdjacencyMethodHandlerTest { final Set<Class<?>> exceptionTypes = new HashSet<>(Arrays.asList(new Class<?>[]{BadAddSonExtraArgument.class})); - final TinkerGraph godGraph = TinkerGraph.open(); GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, exceptionTypes); final List<? extends BadGetSonsArgumentInterface> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentInterface.class); + input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentInterface.class); final BadGetSonsArgumentInterface father = gods.iterator().next(); } @@ -744,13 +753,12 @@ public class AdjacencyMethodHandlerTest { final Set<Class<?>> exceptionTypes = new HashSet<>(Arrays.asList(new Class<?>[]{BadSonMethodName.class})); - final TinkerGraph godGraph = TinkerGraph.open(); GodGraphLoader.load(godGraph); final FramedGraph framedGraph = new DelegatingFramedGraph(godGraph, exceptionTypes); final List<? extends BadGetSonsArgumentInterface> gods = framedGraph.traverse( - input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentInterface.class); + input -> input.V().has("name", "jupiter")).toList(BadGetSonsArgumentInterface.class); final BadGetSonsArgumentInterface father = gods.iterator().next(); } diff --git a/src/test/java/com/syncleus/ferma/annotations/NetworkGraphTestHelper.java b/src/test/java/com/syncleus/ferma/annotations/NetworkGraphTestHelper.java index 7c44628d6339a7d182194bb58d8340dcbfbd11fd..7a16bf2b93256688de7ea89502cf041672de2588 100644 --- a/src/test/java/com/syncleus/ferma/annotations/NetworkGraphTestHelper.java +++ b/src/test/java/com/syncleus/ferma/annotations/NetworkGraphTestHelper.java @@ -26,6 +26,8 @@ import java.util.stream.Collectors; import org.junit.Before; import com.syncleus.ferma.graphtypes.network.ComputerVertex; import com.syncleus.ferma.graphtypes.network.NetworkDeviceVertex; +import java.io.IOException; +import org.junit.After; import org.junit.Assert; /** @@ -62,6 +64,11 @@ public class NetworkGraphTestHelper { dev1InConnections = dev1OutConnections; } + @After + public void tearDown() throws IOException { + graph.close(); + } + protected ComputerVertex findDeviceByName(FramedGraph graph, String deviceName) { return graph .traverse(input -> input.V().has("name", deviceName)) diff --git a/src/test/java/com/syncleus/ferma/annotations/PropertyMethodHandlerTest.java b/src/test/java/com/syncleus/ferma/annotations/PropertyMethodHandlerTest.java index d253915cf69c3bad8f2b994a4e3a858917ef7beb..4c8cb7fff5ec217b02a1c836f02b19d45104873b 100644 --- a/src/test/java/com/syncleus/ferma/annotations/PropertyMethodHandlerTest.java +++ b/src/test/java/com/syncleus/ferma/annotations/PropertyMethodHandlerTest.java @@ -168,6 +168,8 @@ public class PropertyMethodHandlerTest { Assert.assertEquals(JavaAccessModifier.PUBLIC, collectionVertex.getAccessModifier()); collectionVertex.setAccessModifier(JavaAccessModifier.PROTECTED); Assert.assertEquals(JavaAccessModifier.PROTECTED, collectionVertex.getAccessModifier()); + collectionVertex.setAccessModifier(null); + Assert.assertNull(collectionVertex.getAccessModifier()); } @Test (expected = IllegalStateException.class) diff --git a/src/test/java/com/syncleus/ferma/framefactories/annotation/AnnotationFrameFactoryTest.java b/src/test/java/com/syncleus/ferma/framefactories/annotation/AnnotationFrameFactoryTest.java index 02a4fa7f4262137d676c3c097da59fe0e9fbfb55..736899f5c11d18ae1254f989d7b340dbe83fd61a 100644 --- a/src/test/java/com/syncleus/ferma/framefactories/annotation/AnnotationFrameFactoryTest.java +++ b/src/test/java/com/syncleus/ferma/framefactories/annotation/AnnotationFrameFactoryTest.java @@ -19,11 +19,24 @@ import com.syncleus.ferma.DelegatingFramedGraph; import com.syncleus.ferma.ReflectionCache; import com.syncleus.ferma.TEdge; import com.syncleus.ferma.TVertex; +import com.syncleus.ferma.annotations.Adjacency; +import com.syncleus.ferma.annotations.Friend; +import com.syncleus.ferma.annotations.God; import com.syncleus.ferma.graphtypes.javaclass.invalid.InvalidFrame; import com.syncleus.ferma.graphtypes.javaclass.invalid.OneArgConstructorVertex; +import com.syncleus.ferma.typeresolvers.PolymorphicTypeResolver; +import java.io.IOException; +import java.lang.annotation.Annotation; +import java.util.Collections; +import net.bytebuddy.dynamic.DynamicType; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Element; +import org.apache.tinkerpop.gremlin.structure.Vertex; import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph; +import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; /** * @@ -40,6 +53,11 @@ public class AnnotationFrameFactoryTest { fg = new DelegatingFramedGraph(TinkerGraph.open()); } + @After + public void tearDown() throws IOException { + fg.close(); + } + @Test (expected = IllegalArgumentException.class) public void testOneArgNonAbstractFrame() { frameFactory.create(fg.addFramedVertex().getElement(), OneArgConstructorVertex.class); @@ -57,4 +75,44 @@ public class AnnotationFrameFactoryTest { TEdge e1 = fg.addFramedEdge(v1, v2, "some_label"); frameFactory.create(e1.getElement(), InvalidFrame.class); } + + @Test + public void testCustomHandlers() { + MethodHandler custom = Mockito.mock(AbstractMethodHandler.class, Mockito.CALLS_REAL_METHODS); + Class<? extends Annotation> annotation = Adjacency.class; + custom = Mockito.when(custom.getAnnotationType()).then(inv -> annotation).getMock(); + custom = Mockito + .when(custom.processMethod(Mockito.any(), Mockito.any(), Mockito.any())) + .thenAnswer(inv -> inv.getArgumentAt(0, DynamicType.Builder.class)) + .getMock(); + AnnotationFrameFactory frameFactory = new AnnotationFrameFactory(new ReflectionCache(), Collections.singleton(custom)); + DelegatingFramedGraph framedGraph = new DelegatingFramedGraph(fg.getBaseGraph(), frameFactory, new PolymorphicTypeResolver()); + framedGraph.addFramedVertex(God.class); + Mockito.verify(custom, Mockito.atLeast(0)).getAnnotationType(); + Mockito.verify(custom, Mockito.atLeastOnce()).processMethod(Mockito.any(), Mockito.any(), Mockito.any()); + } + + @Test (expected = IllegalStateException.class) + public void testBadElementInterfaceFrame() { + Element badElement = Mockito.mock(Element.class); + frameFactory.create(badElement, God.class); + } + + @Test (expected = IllegalStateException.class) + public void testBadElementAbstractClassFrame() { + Element badElement = Mockito.mock(Element.class); + frameFactory.create(badElement, Friend.class); + } + + @Test (expected = IllegalStateException.class) + public void testVertexBadFrame() { + Vertex okElement = Mockito.mock(Vertex.class); + frameFactory.create(okElement, InvalidFrame.class); + } + + @Test (expected = IllegalStateException.class) + public void testEdgeBadFrame() { + Edge okElement = Mockito.mock(Edge.class); + frameFactory.create(okElement, InvalidFrame.class); + } } diff --git a/src/test/java/com/syncleus/ferma/framefactories/annotation/ReflectionUtilityTest.java b/src/test/java/com/syncleus/ferma/framefactories/annotation/ReflectionUtilityTest.java index 96c02dce40679fb98a667d8cc59b0aba6090b06b..f39b0757ee9a41569bfa46051e58e3cf5df60d11 100644 --- a/src/test/java/com/syncleus/ferma/framefactories/annotation/ReflectionUtilityTest.java +++ b/src/test/java/com/syncleus/ferma/framefactories/annotation/ReflectionUtilityTest.java @@ -15,6 +15,8 @@ */ package com.syncleus.ferma.framefactories.annotation; +import com.syncleus.ferma.EdgeFrame; +import com.syncleus.ferma.VertexFrame; import com.syncleus.ferma.annotations.God; import com.syncleus.ferma.graphtypes.javaclass.JavaClassVertex; import com.syncleus.ferma.graphtypes.javaclass.JavaTypeVertex; @@ -34,21 +36,45 @@ import org.junit.Test; * @author rqpa */ public class ReflectionUtilityTest { - + private static class SomeMockClass<N extends Number> { - + public void foo(Function<? extends Number, ? super Comparable> f) { // Implementation is irrelevant } + + public void noWildcardFunction(Function<Number, Comparable> f) { + // Implementation is irrelevant + } + + public Function<Number, Comparable> functionReturningMethod() { + return null; + } + + public Boolean isFoo() { + return true; + } + + public boolean canFoo() { + return true; + } + + public void doesAcceptVertexFrame(VertexFrame frame) { + // Implementation is irrelevant + } + + public void doesAcceptEdgeFrame(EdgeFrame frame) { + // Implementation is irrelevant + } }; - + private static Method getMethod(Class<?> clazz, String methodName) { return Stream.of(clazz.getMethods()) .filter(m -> m.getName().equals(methodName)) .findFirst() .orElse(null); } - + @Test public void testActualTypeWithCollection() throws NoSuchMethodException { assertGetActualType( @@ -56,38 +82,149 @@ public class ReflectionUtilityTest { Collection.class.getTypeParameters()[0], 0); } - + + @Test + public void testActualTypeWithArray() throws NoSuchMethodException { + assertGetActualType( + Double.class, + Double[].class, + 0); + } + + @Test + public void testActualTypeWithNull() { + assertGetActualType( + null, + null, + 0); + } + @Test public void testReturnsMap() throws NoSuchMethodException { Assert.assertTrue(ReflectionUtility.returnsSet(Collections.class.getMethod("emptySet"))); Assert.assertFalse(ReflectionUtility.returnsSet(Collections.class.getMethod("reverseOrder"))); } - + @Test public void testActualTypeWithParameterizedType() throws NoSuchMethodException, NoSuchFieldException { assertGetActualType( - Number.class, // List has no upper bounds + Number.class, getMethod(SomeMockClass.class, "foo").getGenericParameterTypes()[0], 0); } - + @Test - public void testActualTypeWithWildcardType() throws NoSuchMethodException, NoSuchFieldException { + public void testActualTypeWithWildcardTypeUpperBounds() throws NoSuchMethodException, NoSuchFieldException { ParameterizedType paramType = (ParameterizedType) getMethod(SomeMockClass.class, "foo").getGenericParameterTypes()[0]; Type wildCardType = paramType.getActualTypeArguments()[0]; assertGetActualType( - Number.class, // List has no upper bounds + Number.class, + wildCardType, + 0); + } + + @Test + public void testActualTypeParamTypeNoWildcard() throws NoSuchMethodException, NoSuchFieldException { + ParameterizedType paramType = (ParameterizedType) getMethod(SomeMockClass.class, "noWildcardFunction").getGenericParameterTypes()[0]; + assertGetActualType( + Comparable.class, + paramType, + 1); + } + + @Test + public void testActualTypeWithWildcardTypeLowerBounds() throws NoSuchMethodException, NoSuchFieldException { + ParameterizedType paramType = (ParameterizedType) getMethod(SomeMockClass.class, "foo").getGenericParameterTypes()[0]; + Type wildCardType = paramType.getActualTypeArguments()[1]; + assertGetActualType( + Comparable.class, wildCardType, 0); } - + + @Test + public void testGetGenericType() throws NoSuchMethodException, NoSuchFieldException { + // Maybe we want to add position to getGenericType and not just assume the first + // generic type is required? + Class actual = ReflectionUtility.getGenericClass(getMethod(SomeMockClass.class, "functionReturningMethod")); + Assert.assertEquals(Number.class, actual); + actual = ReflectionUtility.getGenericClass(getMethod(SomeMockClass.class, "foo")); + Assert.assertEquals(void.class, actual); + } + + @Test + public void testAcceptsIterator() { + Assert.assertFalse(ReflectionUtility.acceptsIterator(getMethod(ComputerVertex.class, "addAndConnectOutVertexTypedEdgeTyped"), 0)); + Assert.assertTrue(ReflectionUtility.acceptsIterator(getMethod(ComputerVertex.class, "setTwoWayConnectionsWithIterator"), 0)); + Assert.assertFalse(ReflectionUtility.acceptsIterator(getMethod(ComputerVertex.class, "setTwoWayConnectionsWithIterator"), 1)); + Assert.assertFalse(ReflectionUtility.acceptsIterator(getMethod(ComputerVertex.class, "setTwoWayConnectionsWithIterable"), 0)); + } + + @Test + public void testAcceptsIterable() { + Assert.assertFalse(ReflectionUtility.acceptsIterable(getMethod(ComputerVertex.class, "addAndConnectOutVertexTypedEdgeTyped"), 0)); + Assert.assertTrue(ReflectionUtility.acceptsIterable(getMethod(ComputerVertex.class, "setTwoWayConnectionsWithIterable"), 0)); + Assert.assertFalse(ReflectionUtility.acceptsIterable(getMethod(ComputerVertex.class, "setTwoWayConnectionsWithIterable"), 1)); + Assert.assertFalse(ReflectionUtility.acceptsIterable(getMethod(ComputerVertex.class, "setTwoWayConnectionsWithIterator"), 0)); + } + + @Test + public void testAcceptsVertexFrame() { + Assert.assertFalse(ReflectionUtility.acceptsVertexFrame(getMethod(SomeMockClass.class, "doesAcceptEdgeFrame"), 0)); + Assert.assertFalse(ReflectionUtility.acceptsVertexFrame(getMethod(SomeMockClass.class, "doesAcceptVertexFrame"), 1)); + Assert.assertTrue(ReflectionUtility.acceptsVertexFrame(getMethod(SomeMockClass.class, "doesAcceptVertexFrame"), 0)); + } + + @Test + public void testGetTypes() { + Type[] expected = new Type[]{ + Integer.TYPE, + Double.TYPE, + Character.TYPE + }; + + for (int i = 0; i < expected.length; i++) { + Assert.assertEquals(ReflectionUtility.getType(expected, i), expected[i]); + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetTypesOutOfBounds() { + Type[] expected = new Type[]{ + Integer.TYPE, + Double.TYPE, + Character.TYPE + }; + + ReflectionUtility.getType(expected, expected.length); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetTypesOutOfBounds2() { + Type[] expected = new Type[]{ + Integer.TYPE, + Double.TYPE, + Character.TYPE + }; + + ReflectionUtility.getType(expected, -3); + } + + @Test + public void testIsGetMethod() { + Assert.assertTrue(ReflectionUtility.isGetMethod(getMethod(SomeMockClass.class, "isFoo"))); + Assert.assertTrue(ReflectionUtility.isGetMethod(getMethod(SomeMockClass.class, "canFoo"))); + Assert.assertTrue(ReflectionUtility.isGetMethod(getMethod(God.class, "getSons"))); + Assert.assertFalse(ReflectionUtility.isGetMethod(getMethod(ComputerVertex.class, "disconnectFromNetwork"))); + } + @Test public void testIsSetMethod() { Assert.assertTrue(ReflectionUtility.isSetMethod(getMethod(ComputerVertex.class, "setName"))); Assert.assertTrue(ReflectionUtility.isSetMethod(getMethod(God.class, "applyName"))); Assert.assertFalse(ReflectionUtility.isSetMethod(getMethod(ComputerVertex.class, "disconnectFromNetwork"))); } - + @Test public void testIsRemoveMethod() { Assert.assertTrue(ReflectionUtility.isRemoveMethod(getMethod(God.class, "removeSonEdge"))); @@ -96,8 +233,7 @@ public class ReflectionUtilityTest { Assert.assertFalse(ReflectionUtility.isRemoveMethod(getMethod(JavaTypeVertex.class, "getFullyQualifiedName"))); Assert.assertFalse(ReflectionUtility.isRemoveMethod(getMethod(JavaClassVertex.class, "implementNewInterface"))); } - - + private void assertGetActualType(Class<?> expected, Type toTest, int pos) { Class<?> actualType = ReflectionUtility.getActualType(toTest, pos); Assert.assertEquals("Types mismatch!", expected, actualType); diff --git a/src/test/java/com/syncleus/ferma/graphtypes/network/ComputerVertex.java b/src/test/java/com/syncleus/ferma/graphtypes/network/ComputerVertex.java index 54ee891420f67442eaa5be6095185f0e6e70fc14..55d67fcdc83d90f7a0eced5c2b789f63c696fdde 100644 --- a/src/test/java/com/syncleus/ferma/graphtypes/network/ComputerVertex.java +++ b/src/test/java/com/syncleus/ferma/graphtypes/network/ComputerVertex.java @@ -78,10 +78,10 @@ public interface ComputerVertex extends NetworkDeviceVertex { ClassInitializer<E> edgeInitializer); @Adjacency(label = "connects", direction = Direction.BOTH) - <T extends NetworkDeviceVertex> void setTwoWayConnectionsWith(Iterator<T> connectees); + <T extends NetworkDeviceVertex> void setTwoWayConnectionsWithIterator(Iterator<T> connectees); @Adjacency(label = "connects", direction = Direction.BOTH) - <T extends NetworkDeviceVertex> void setTwoWayConnectionsWith(Iterable<T> connectees); + <T extends NetworkDeviceVertex> void setTwoWayConnectionsWithIterable(Iterable<T> connectees); @Adjacency(label = "connects", direction = Direction.OUT) <T extends NetworkDeviceVertex> void setOutConnectionsWith(Iterable<T> connectees); diff --git a/src/test/java/com/syncleus/ferma/tx/DummyGraph.java b/src/test/java/com/syncleus/ferma/tx/DummyGraph.java index 73af6361bddefd57c284e36ee51130d83497d6c2..bfe17ebe482ab12048fe8ce80fe2f800ae21f629 100644 --- a/src/test/java/com/syncleus/ferma/tx/DummyGraph.java +++ b/src/test/java/com/syncleus/ferma/tx/DummyGraph.java @@ -21,10 +21,12 @@ import com.syncleus.ferma.DelegatingFramedGraph; public class DummyGraph extends DelegatingFramedGraph<Graph> implements FramedTxGraph { + private int totalTxCreated = 0; + public DummyGraph(Graph delegate) { super(delegate); } - + @Override public Tx tx() { return FramedTxGraph.super.tx(); @@ -32,6 +34,13 @@ public class DummyGraph extends DelegatingFramedGraph<Graph> implements FramedTx @Override public Tx createTx() { - return new DummyTransaction(null, this); + Tx created = new DummyTransaction(null, this); + totalTxCreated += 1; + return created; + } + + public int getTotalTxCreated() { + return totalTxCreated; } + } diff --git a/src/test/java/com/syncleus/ferma/tx/TxFactoryTest.java b/src/test/java/com/syncleus/ferma/tx/TxFactoryTest.java index 44ed86f76f048714f14b4f48018ae9b098011ab8..35d9e2d61d1c3dc79363e17b3d7feaf74be8a17e 100644 --- a/src/test/java/com/syncleus/ferma/tx/TxFactoryTest.java +++ b/src/test/java/com/syncleus/ferma/tx/TxFactoryTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertNull; import static org.mockito.Mockito.verify; import org.apache.tinkerpop.gremlin.structure.Transaction; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -123,19 +124,24 @@ public class TxFactoryTest implements TxFactory { verify(graphMock.tx(), Mockito.never()).commit(); } + @Test + public void testActiveTxRetrieval() { + Tx tx = tx(); + mock = Mockito.mock(DummyTransaction.class); + Assert.assertSame(tx, tx()); + Assert.assertSame(tx, tx()); + Assert.assertSame(tx, tx()); + } + @Override public Tx createTx() { return mock; } - + @Override public <T> T tx(TxAction<T> txHandler) { try (Tx tx = tx()) { - try { - return txHandler.handle(mock); - } catch (Exception e) { - throw new RuntimeException(e); - } + return txHandler.handle(mock); } } diff --git a/src/test/java/com/syncleus/ferma/tx/TxFramedGraphTest.java b/src/test/java/com/syncleus/ferma/tx/TxFramedGraphTest.java new file mode 100644 index 0000000000000000000000000000000000000000..22bd91f52bd943ef23963b2b923d1546b1b1df94 --- /dev/null +++ b/src/test/java/com/syncleus/ferma/tx/TxFramedGraphTest.java @@ -0,0 +1,45 @@ +/** + * Copyright 2004 - 2017 Syncleus, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.syncleus.ferma.tx; + +import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * + * @author rqpa + */ +public class TxFramedGraphTest { + + private DummyGraph graph; + + @Before + public void setUp() { + graph = new DummyGraph(TinkerGraph.open()); + } + + @Test + public void testSingleTxCreation() { + Tx expected = graph.tx(); + Assert.assertSame(expected, graph.tx()); + Assert.assertSame(expected, graph.tx()); + Assert.assertSame(expected, graph.tx()); + Assert.assertSame(expected, graph.tx()); + } + +} diff --git a/src/test/java/com/syncleus/ferma/typeresolvers/UntypedTypeResolverTest.java b/src/test/java/com/syncleus/ferma/typeresolvers/UntypedTypeResolverTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e3aa92941b2f0572f126346dd4b37d8e9d554040 --- /dev/null +++ b/src/test/java/com/syncleus/ferma/typeresolvers/UntypedTypeResolverTest.java @@ -0,0 +1,106 @@ +/** + * Copyright 2004 - 2017 Syncleus, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.syncleus.ferma.typeresolvers; + +import com.syncleus.ferma.AbstractEdgeFrame; +import com.syncleus.ferma.AbstractVertexFrame; +import com.syncleus.ferma.DelegatingFramedGraph; +import com.syncleus.ferma.EdgeFrame; +import com.syncleus.ferma.TEdge; +import com.syncleus.ferma.TVertex; +import com.syncleus.ferma.VertexFrame; +import com.syncleus.ferma.annotations.God; +import com.syncleus.ferma.graphtypes.network.ComputerVertex; +import com.syncleus.ferma.graphtypes.network.NetworkGraphLoader; +import java.util.List; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; +import org.apache.tinkerpop.gremlin.structure.Element; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +/** + * + * @author rqpa + */ +public class UntypedTypeResolverTest { + + private UntypedTypeResolver resolver; + + @Before + public void setUp() { + resolver = new UntypedTypeResolver(); + } + + @Test + public void testResolve() { + Element mockElement = Mockito.mock(Element.class); + // THis one should do no resolution. Mock should not be used and null returned. + Assert.assertNull(resolver.resolve(mockElement)); + Mockito.verifyZeroInteractions(mockElement); + } + + @Test + public void testResolveWithDefault() { + Element mockElement = Mockito.mock(Element.class); + Assert.assertEquals(resolver.resolve(mockElement, VertexFrame.class), TVertex.class); + Mockito.verifyZeroInteractions(mockElement); + Assert.assertEquals(resolver.resolve(mockElement, AbstractVertexFrame.class), TVertex.class); + Mockito.verifyZeroInteractions(mockElement); + Assert.assertEquals(resolver.resolve(mockElement, EdgeFrame.class), TEdge.class); + Mockito.verifyZeroInteractions(mockElement); + Assert.assertEquals(resolver.resolve(mockElement, AbstractEdgeFrame.class), TEdge.class); + Mockito.verifyZeroInteractions(mockElement); + Assert.assertEquals(resolver.resolve(mockElement, God.class), God.class); + Mockito.verifyZeroInteractions(mockElement); + } + + @Test + public void testInit() { + Element mockElement = Mockito.mock(Element.class); + resolver.init(mockElement, God.class); + Mockito.verifyZeroInteractions(mockElement); + } + + @Test + public void testDenit() { + Element mockElement = Mockito.mock(Element.class); + resolver.deinit(mockElement); + Mockito.verifyZeroInteractions(mockElement); + } + + @Test + public void testHasNotType() { + // Resolver knows nothing about types so it should not filter any elements + // and return the same traverser + GraphTraversal<Element, Element> expected = Mockito.mock(GraphTraversal.class); + GraphTraversal<Element, Element> actual = resolver.hasNotType(expected, God.class); + Assert.assertSame(expected, actual); + Mockito.verifyZeroInteractions(expected); + } + + @Test + public void testHasType() { + // Does not know about types so should filter out all elements + DelegatingFramedGraph<?> fg = NetworkGraphLoader.INSTANCE.load(); + List<Vertex> actualVertices = resolver + .hasType(fg.getBaseGraph().traversal().V(), ComputerVertex.class) + .toList(); + Assert.assertTrue(actualVertices.isEmpty()); + } +}