Commit bffdf219 authored by Jeffrey Phillips Freeman's avatar Jeffrey Phillips Freeman 💥 Committed by GitHub
Browse files

Merge pull request #35 from Syncleus/moretests

More unit tests and fixes bringing coverage higher.
parents e696c116 124c0a8d
......@@ -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.
......
......@@ -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>
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
......
......@@ -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);
}
......
......@@ -18,6 +18,6 @@ package com.syncleus.ferma.tx;
@FunctionalInterface
public interface TxAction<T> {
T handle(Tx tx) throws Exception;
T handle(Tx tx);
}
......@@ -18,6 +18,6 @@ package com.syncleus.ferma.tx;
@FunctionalInterface
public interface TxAction0 {
void handle() throws Exception;
void handle();
}
......@@ -18,6 +18,6 @@ package com.syncleus.ferma.tx;
@FunctionalInterface
public interface TxAction1<T> {
T handle() throws Exception;
T handle();
}
......@@ -18,6 +18,6 @@ package com.syncleus.ferma.tx;
@FunctionalInterface
public interface TxAction2 {
void handle(Tx tx) throws Exception;
void handle(Tx tx);
}
......@@ -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));
}
}
......@@ -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();
......
/**
* 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());
}
}
......@@ -228,4 +228,5 @@ public class DelegatingFrameGraphTest extends NetworkGraphTestHelper {
NetworkConnectionEdge.class);
assertAllConnections(actual);
}
}
/**
* 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);
}
}
......@@ -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() {
......
......@@ -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() {
......
......@@ -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();