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

test: Added Unit tests to increase code coverage.

parent 391d2a60
......@@ -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);
}
@Test
public void testFrameNullElement() {
Assert.assertNull(graph.frameElement(null, NetworkDeviceVertex.class));
}
@Test
public void testFrameNullElementExplicit() {
Assert.assertNull(graph.frameElementExplicit(null, NetworkDeviceVertex.class));
}
@Test
public void testFrameNewElement() {
ComputerVertex dev6Framed = graph.frameNewElement(dev6, ComputerVertex.class);
Assert.assertNotNull(dev6Framed);
Assert.assertEquals(dev6Name, dev6Framed.getName());
}
@Test
public void testFrameNewElementsIterator() {
Iterator<? extends ComputerVertex> framed = graph.frame(Arrays.asList(dev6, dev7).iterator(), ComputerVertex.class);
assertDev6Dev7Iterator(framed);
}
@Test
public void testFrameNewElementsExplicitIterator() {
Iterator<? extends ComputerVertex> framed = graph.frameExplicit(Arrays.asList(dev6, dev7).iterator(), ComputerVertex.class);
assertDev6Dev7Iterator(framed);
}
private void assertDev6Dev7Iterator(Iterator<? extends ComputerVertex> actual) {
Assert.assertNotNull(actual);
Assert.assertTrue(actual.hasNext());
Assert.assertEquals(dev6Name, actual.next().getName());
Assert.assertTrue(actual.hasNext());
Assert.assertEquals(dev7Name, actual.next().getName());
Assert.assertFalse(actual.hasNext());
}
@Test
public void testFrameNewElementExplicit() {
ComputerVertex actual = graph.frameNewElementExplicit(dev6, ComputerVertex.class);
Assert.assertNotNull(actual);
Assert.assertEquals(dev6Name, actual.getName());
}
@Test
public void testAddFramedEdgeExplicitWithKind() {
NetworkConnectionEdge actual = graph.addFramedEdgeExplicit(
dev1,
dev5,
"connects",
NetworkConnectionEdge.class);
Assert.assertNotNull(actual);
assertOneWayExclusiveConnection(dev1, dev5);
}
@Test
public void testAddFramedEdgeExplicitWithInitializer() {
NetworkConnectionEdge actual = graph.addFramedEdgeExplicit(
dev1,
dev5,
"connects",
NetworkConnectionEdge.DEFAULT_INITIALIZER);
Assert.assertNotNull(actual);
assertOneWayExclusiveConnection(dev1, dev5);
}
@Test
public void testAddFramedEdgeExplicitUntyped() {
TEdge actual = graph.addFramedEdgeExplicit(
dev1,
dev5,
"connects");
Assert.assertNotNull(actual);
assertOneWayExclusiveConnection(dev1, dev5);
}
@Test
public void testGetFramedVertexById() {