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

Merge pull request #29 from Syncleus/unittests

Added unit test coverage and fixes
parents 391d2a60 55b639d6
......@@ -23,10 +23,14 @@
* Removed the upper bound on traversal generics. The travese function's argument has changed from
`GraphTraversal<? extends Vertex, ? extends Vertex>` to `GraphTraversal<Vertex, Vertex>`.
* Expanded documentation to cover all the annotations availible in greater detail, including recent changes.
* `AnnotationFrameFactory` was redesigned to make it easier to inhereit from the class and add support for additional
* `AnnotationFrameFactory` was redesigned to make it easier to inherit from the class and add support for additional
custom annotations.
* `AbstractAnnotationFrameFactory` was created to help create custom annotations that replace the existing standard
annotations provided by `AnnotationFrameFactory`.
* Fixed a `NullPointerException` thrown any time `DelegatingFramedGraph.addFramedEdgeExplicit` was called.
* Fixed a `IllegalStateException` thrown anytime `DefaultTraversable.nextOrAdd()` was called.
* Fixed a `IllegalStateException` thrown when `PolymorphicTypeResolver.resolve()` is called on a class without type
information.
* Updated the following dependencies
* gson: 2.8.1 -> 2.8.2
......
......@@ -79,7 +79,7 @@ public class DefaultTraversable<PE, E> implements Traversable<PE, E>{
return this.parentGraph.frameElement((Element) baseTraversal.next(), VertexFrame.class);
}
catch (final NoSuchElementException e) {
return this.parentGraph.addFramedVertex(null, VertexFrame.class);
return this.parentGraph.addFramedVertex(TVertex.DEFAULT_INITIALIZER, null);
}
}
......
......@@ -313,8 +313,14 @@ public class DelegatingFramedGraph<G extends Graph> implements WrappedFramedGrap
@Override
public <T> T addFramedVertex(final ClassInitializer<T> initializer, final Object... keyValues) {
final T framedVertex = frameNewElement(this.getBaseGraph().addVertex(keyValues), initializer);
return framedVertex;
if( keyValues != null ) {
final T framedVertex = frameNewElement(this.getBaseGraph().addVertex(keyValues), initializer);
return framedVertex;
}
else {
final T framedVertex = frameNewElement(this.getBaseGraph().addVertex(), initializer);
return framedVertex;
}
}
@Override
......@@ -358,7 +364,7 @@ public class DelegatingFramedGraph<G extends Graph> implements WrappedFramedGrap
@Override
public <T> T addFramedEdgeExplicit(final VertexFrame source, final VertexFrame destination, final String label, final ClassInitializer<T> initializer) {
final T framedEdge = frameNewElementExplicit(source.getElement().addEdge(label, destination.getElement(), null), initializer);
final T framedEdge = frameNewElementExplicit(source.getElement().addEdge(label, destination.getElement()), initializer);
return framedEdge;
}
......
......@@ -26,6 +26,7 @@ import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Property;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
......@@ -111,11 +112,12 @@ public class PolymorphicTypeResolver implements TypeResolver {
@Override
public Class<?> resolve(final Element element) {
final String typeResolutionName = element.<String>property(this.typeResolutionKey).value();
if (typeResolutionName == null)
return null;
final Property<String> typeResolutionName = element.<String>property(this.typeResolutionKey);
return this.reflectionCache.forName(typeResolutionName);
if( typeResolutionName.isPresent() )
return this.reflectionCache.forName(typeResolutionName.value());
else
return null;
}
@Override
......
......@@ -15,10 +15,7 @@
*/
package com.syncleus.ferma;
import java.util.function.Function;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.junit.Assert;
......@@ -26,8 +23,8 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import com.google.common.collect.Sets;
import javax.annotation.Nullable;
import java.util.Iterator;
import com.google.gson.JsonObject;
import com.syncleus.ferma.graphtypes.javaclass.JavaAccessModifier;
public class AbstractElementFrameTest {
......@@ -46,7 +43,6 @@ public class AbstractElementFrameTest {
p2.setName("Julia");
e1 = p1.addKnows(p2);
e1.setYears(15);
}
@Test
......@@ -60,15 +56,40 @@ public class AbstractElementFrameTest {
}
@Test
public void testGetProperty() {
public void testGetStringProperty() {
Assert.assertEquals("Bryn", p1.getProperty("name"));
}
@Test
public void testSetProperty() {
public void testSetStringProperty() {
p1.setProperty("name", "Bryn Cooke");
Assert.assertEquals("Bryn Cooke", p1.getProperty("name"));
}
@Test
public void testGetSetTypedIntegerProperty() {
String propName = "ageInMonths";
Class<?> type = Integer.class;
Integer value = 12 * 34;
Assert.assertNull(p1.getProperty(propName, type));
p1.setProperty(propName, value);
Assert.assertEquals(p1.getProperty(propName, type), value);
p1.setProperty(propName, null);
Assert.assertNull(p1.getProperty(propName, type));
}
@Test
public void testGetSetTypedEnumProperty() {
// No semantic meaning but we test pure functionality so it's ok
String propName = "foo";
Class<?> type = JavaAccessModifier.class;
JavaAccessModifier value = JavaAccessModifier.PRIVATE;
Assert.assertNull(p1.getProperty(propName, type));
p1.setProperty(propName, value);
Assert.assertEquals(p1.getProperty(propName, type), value);
p1.setProperty(propName, null);
Assert.assertNull(p1.getProperty(propName, type));
}
@Test
public void testSetPropertyNull() {
......@@ -136,15 +157,36 @@ public class AbstractElementFrameTest {
}
@Test
public void testReframeExplicit() {
public void testReframeExplicitVertex() {
final TVertex v1 = p1.reframeExplicit(TVertex.class);
Assert.assertEquals((Long) p1.getId(), (Long) v1.getId());
}
@Test
public void testReframeExplicitEdge() {
final TEdge edgeReframed = e1.reframeExplicit(TEdge.class);
Assert.assertEquals((Long) e1.getId(), (Long) edgeReframed.getId());
}
@Test
public void testVNull() {
final Long count = fg.getRawTraversal().V("noId").count().next();
Assert.assertEquals((Long) 0L, count);
}
@Test
public void testVtoJson() {
JsonObject actual = p1.toJson();
Assert.assertEquals(p1.getId(Long.class).longValue(), actual.get("id").getAsLong());
Assert.assertEquals("vertex", actual.get("elementClass").getAsString());
Assert.assertEquals(p1.getName(), actual.get("name").getAsString());
}
@Test
public void testEtoJson() {
JsonObject actual = e1.toJson();
Assert.assertEquals(e1.getId(Long.class).longValue(), actual.get("id").getAsLong());
Assert.assertEquals("edge", actual.get("elementClass").getAsString());
Assert.assertEquals(e1.getYears(), actual.get("years").getAsInt());
}
}
/**
* Copyright 2004 - 2016 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.annotations.NetworkGraphTestHelper;
import com.syncleus.ferma.graphtypes.network.ComputerVertex;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.util.function.TriFunction;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author rqpa
*/
public abstract class AbstractTraversableTest extends NetworkGraphTestHelper {
private Set<String> dev2AdjacentVerticesNames;
private ComputerVertex dev6;
@Before
@Override
public void setUp() {
super.setUp();
dev2AdjacentVerticesNames = new HashSet<>();
dev2AdjacentVerticesNames.add(dev1.getName());
dev2AdjacentVerticesNames.add(dev5.getName());
dev2AdjacentVerticesNames = Collections.unmodifiableSet(dev2AdjacentVerticesNames);
dev6 = graph.addFramedVertex(ComputerVertex.class);
dev6.setName("DEV6");
graph.addFramedEdge(dev6, dev5, "connects");
}
@Test
public void testNextOrDefault() {
assertNextOrDefault((t, def) -> t.nextOrDefault(ComputerVertex.class, def));
}
@Test
public void testNextOrDefaultExplicit() {
assertNextOrDefault((t, def) -> t.nextOrDefaultExplicit(ComputerVertex.class, def));
}
private void assertNextOrDefault(BiFunction<Traversable<?, ?>, ComputerVertex, ComputerVertex> nextOrDefault) {
Traversable<?, ?> t = createAdjacentVerticesTraversable(dev2);
Set<String> dev2Adjacent = new HashSet<>(dev2AdjacentVerticesNames);
ComputerVertex lastDevice;
while (true) {
lastDevice = nextOrDefault.apply(t, dev6);
Assert.assertNotNull(lastDevice);
Assert.assertNotNull(lastDevice.getName());
boolean existed = dev2Adjacent.remove(lastDevice.getName());
if (!existed) {
break;
}
}
// now all connections dev2 adjacent vertices must have been traversed
// and dev2Adjacent should be empty
Assert.assertTrue(dev2Adjacent.isEmpty());
// Also the last device must be the default one (dev6)
Assert.assertEquals(dev6.getName(), lastDevice.getName());
}
@Test
public void testNextOrAdd() {
Traversable<?, ?> t = createAdjacentVerticesTraversable(dev6);
VertexFrame actualDev5 = t.nextOrAdd();
Assert.assertEquals(dev5.getName(), actualDev5.getProperty("name"));
VertexFrame actualNew = t.nextOrAdd();
Assert.assertNull(actualNew.getProperty("name"));
}
@Test
public void testNextOrAddExplicitWithInitializer() {
assertNextOrAddTyped(t -> t.nextOrAddExplicit(ComputerVertex.DEFAULT_INITIALIZER));
}
@Test
public void testNextOrAddExplicitWithKind() {
assertNextOrAddTyped(t -> t.nextOrAddExplicit(ComputerVertex.class));
}
@Test
public void testNextOrAddWithInitializer() {
assertNextOrAddTyped(t -> t.nextOrAdd(ComputerVertex.DEFAULT_INITIALIZER));
}
private void assertNextOrAddTyped(Function<Traversable<?, ?>, ComputerVertex> nextOrAdd) {
Traversable<?, ?> t = createAdjacentVerticesTraversable(dev6);
ComputerVertex actualDev5 = nextOrAdd.apply(t);
Assert.assertEquals(dev5.getName(), actualDev5.getName());
ComputerVertex actualNew = nextOrAdd.apply(t);
Assert.assertNull(actualNew.getName());
}
@Test
public void testNextOrAddWithKind() {
Traversable<?, ?> t = createAdjacentVerticesTraversable(dev6);
ComputerVertex actualDev5 = t.nextOrAdd(ComputerVertex.class);
Assert.assertEquals(dev5.getName(), actualDev5.getName());
ComputerVertex actualNew = t.nextOrAdd(ComputerVertex.class);
Assert.assertNull(actualNew.getName());
}
@Test
public void testNextAmount() {
assertNextAmount(Traversable::next);
}
@Test
public void testNextAmountExplicit() {
assertNextAmount(Traversable::nextExplicit);
}
private void assertNextAmount(TriFunction<Traversable<?, ?>, Integer, Class<ComputerVertex>, List<? extends ComputerVertex>> listExtractor) {
List<? extends ComputerVertex> l = listExtractor.apply(createAdjacentVerticesTraversable(dev2), 1, ComputerVertex.class);
Assert.assertEquals(1, l.size());
Assert.assertTrue(dev2AdjacentVerticesNames.contains(l.get(0).getName()));
Set<String> dev2Adj = new HashSet<>(dev2AdjacentVerticesNames);
l = listExtractor.apply(createAdjacentVerticesTraversable(dev2), 2, ComputerVertex.class);
Assert.assertEquals(2, l.size());
for (ComputerVertex v : l) {
Assert.assertTrue(dev2Adj.remove(v.getName()));
}
dev2Adj = new HashSet<>(dev2AdjacentVerticesNames);
l = listExtractor.apply(createAdjacentVerticesTraversable(dev2), 4, ComputerVertex.class);
Assert.assertEquals(2, l.size());
for (ComputerVertex v : l) {
Assert.assertTrue(dev2Adj.remove(v.getName()));
}
}
private Traversable<?, ?> createAdjacentVerticesTraversable(ComputerVertex dev) {
return createNewTraversable(graph, dev.traverse(input -> input.both().dedup()).getRawTraversal());
}
protected abstract <PE, E> Traversable<PE, E> createNewTraversable(FramedGraph graph, GraphTraversal<PE, E> graphTraversal);
}
/**
* Copyright 2004 - 2016 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.annotations.NetworkGraphTestHelper;
import com.syncleus.ferma.graphtypes.network.ComputerVertex;
import com.syncleus.ferma.graphtypes.network.NetworkConnectionEdge;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author rqpa
*/
public class AbstractVertexFrameTest extends NetworkGraphTestHelper {
@Before
@Override
public void setUp() {
super.setUp();
}
@Test
public void testAddFramedEdgeExplicit() {
ComputerVertex dev6 = graph.addFramedVertex(ComputerVertex.DEFAULT_INITIALIZER, "name", "DEV6");
dev6.addFramedEdgeExplicit("connects", dev5, NetworkConnectionEdge.class);
assertOneWayExclusiveConnection(dev6, dev5);
}
@Test
public void testSetLinkOutExplicit() {
ComputerVertex dev6 = dev5.setLinkOutExplicit(ComputerVertex.class, "connects");
dev6.setName("DEV6");
assertOneWayExclusiveConnection(dev2, dev5);
assertOneWayExclusiveConnection(dev3, dev5);
assertOneWayExclusiveConnection(dev4, dev5);
assertOneWayExclusiveConnection(dev5, dev6);
}
@Test
public void testSetLinkInExplicit() {
ComputerVertex dev6 = dev5.setLinkInExplicit(ComputerVertex.class, "connects");
dev6.setName("DEV6");
assertOneWayExclusiveConnection(dev5, dev2);
assertOneWayExclusiveConnection(dev5, dev3);
assertOneWayExclusiveConnection(dev5, dev4);
assertOneWayExclusiveConnection(dev6, dev5);
}
@Test
public void testSetLinkBothExplicit() {
ComputerVertex dev6 = dev5.setLinkBothExplicit(ComputerVertex.class, "connects");
dev6.setName("DEV6");
assertNoConnection(dev5, dev2);
assertNoConnection(dev5, dev3);
assertNoConnection(dev5, dev4);
assertTwoWayConnection(dev6, dev5);
}
}
/**
* Copyright 2004 - 2016 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.Comparator;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author rqpa
*/
public class ComparatorsTest {
private Person p1;
private Person p2;
@Before
public void setUp() {
FramedGraph 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");
}
@Test
public void testIdComparator() {
Comparator<ElementFrame> comparator = Comparators.id();
Assert.assertTrue(comparator.compare(p1, p1) == 0);
Assert.assertTrue(comparator.compare(p2, p1) > 0);
Assert.assertTrue(comparator.compare(p1, p2) < 0);
}
@Test
public void testIdAsLongComparator() {
Comparator<ElementFrame> comparator = Comparators.idAsLong();
Assert.assertTrue(comparator.compare(p1, p1) == 0);
Assert.assertTrue(comparator.compare(p2, p1) > 0);
Assert.assertTrue(comparator.compare(p1, p2) < 0);
}
@Test
public void testPropertyComparator() {
Comparator<ElementFrame> comparator = Comparators.property("name");
Assert.assertTrue(comparator.compare(p1, p1) == 0);
Assert.assertTrue(comparator.compare(p2, p1) > 0);
Assert.assertTrue(comparator.compare(p1, p2) < 0);
}
}
/**
* Copyright 2004 - 2016 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 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
/**
*
* @author rqpa
*/
public class DefaultTraversableTest extends AbstractTraversableTest {
@Override
protected <PE, E> Traversable<PE, E> createNewTraversable(FramedGraph graph, GraphTraversal<PE, E> graphTraversal) {
return new DefaultTraversable<>(graphTraversal, graph);
}
}
/**
* Copyright 2004 - 2016 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.annotations.NetworkGraphTestHelper;
import com.syncleus.ferma.graphtypes.network.ComputerVertex;
import com.syncleus.ferma.graphtypes.network.NetworkConnectionEdge;
import com.syncleus.ferma.graphtypes.network.NetworkDeviceVertex;
import com.syncleus.ferma.typeresolvers.PolymorphicTypeResolver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author rqpa
*/
public class DelegatingFrameGraphTest extends NetworkGraphTestHelper {
private Element dev6;
private final String dev6Name = "DEV6";
private Element dev7;
private final String dev7Name = "DEV7";
private final int allDevicesCount = 7;
private final int allConnectionsCount = 12;
@Before
@Override
public void setUp() {
super.setUp();
dev6 = graph.getBaseGraph().addVertex("name", dev6Name);
dev7 = graph.getBaseGraph().addVertex("name", dev7Name);
}