Unverified Commit e885dba9 authored by rqpa's avatar rqpa Committed by Jeffrey Phillips Freeman
Browse files

test: Added additional new unittests bringing coverage up another 5%.

parent e696c116
......@@ -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>
......@@ -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
public void testNullDelegate() {
try {
assertSanity(new DelegatingFramedGraph(null, resolver));
} catch (IllegalArgumentException e) {
// Illegal args is ok
}
}
@Test
public void testNullResolver() {
try {
assertSanity(new DelegatingFramedGraph(g, (TypeResolver) null));
} catch (IllegalArgumentException e) {
// Illegal args is ok
}
}
@Test
public void testNullBuilder() {
try {
assertSanity(new DelegatingFramedGraph(g, null, resolver));
} catch (IllegalArgumentException e) {
// Illegal args is ok
}
}
@Test
public void testNullReflectionsCache() {
try {
assertSanity(new DelegatingFramedGraph(g, null, true, true));
} catch (IllegalArgumentException e) {
// Illegal args is ok
}
}
@Test
public void testNoTypeResolutionNoAnnotations() {
try {
assertSanity(new DelegatingFramedGraph(g, new ReflectionCache(), false, false));
} catch (IllegalArgumentException e) {
// Illegal args is ok
}
}
@Test
public void testNullTypes() {
try {
assertSanity(new DelegatingFramedGraph(g, true, null));
} catch (IllegalArgumentException e) {
// Illegal args is ok
}
}
@Test
public void testEmptyTypesSet() {
try {
assertSanity(new DelegatingFramedGraph(g, false, new HashSet<>()));
} catch (IllegalArgumentException e) {
// Illegal args is ok
}
}
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.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;
/**
*
* @author rqpa
*/
public class DelegatingTransactionTest {
private Transaction gremlinTx;
private WrappedFramedGraph<?> framedGraph;
private DelegatingTransaction delegatingTx;
@Before
public void setUp() {
gremlinTx = Mockito.mock(Transaction.class);
framedGraph = Mockito.mock(WrappedFramedGraph.class, Mockito.RETURNS_MOCKS);
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);
Mockito.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();
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);
......
......@@ -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))
......
......@@ -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)
......
......@@ -19,11 +19,22 @@ 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.Element;
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 +51,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 +73,39 @@ 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)