Skip to content

Commit

Permalink
Merge pull request #141 from ProgrammingLife2016/test/test-model-classes
Browse files Browse the repository at this point in the history
Add tests for the model classes
  • Loading branch information
Pathemeous committed May 27, 2016
2 parents 8ee34b5 + 63c6ff8 commit 8177674
Show file tree
Hide file tree
Showing 9 changed files with 793 additions and 51 deletions.
5 changes: 5 additions & 0 deletions PL2/PL2-shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package nl.tudelft.pl2016gr2.model;

import nl.tudelft.pl2016gr2.thirdparty.testing.utility.TestId;

import java.util.ArrayList;
import java.util.Collection;

Expand All @@ -15,7 +17,8 @@
*/
public abstract class AbstractNode implements Node {

private final int identifier;
@TestId(id = "id_field")
private int identifier;

/**
* Construct a bare abstract node with an ID.
Expand Down Expand Up @@ -66,6 +69,6 @@ assert getOutEdges().contains(

@Override
public String toString() {
return "id: " + identifier;
return "id: " + getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public Iterator<GraphNode> iterator() {
public String toString() {
StringBuilder sb = new StringBuilder();
for (GraphNode node : nodes.values()) {
sb.append(node).append('\n');
sb.append(node.toString()).append('\n');
}
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public void setSequence(BaseSequence sequence) {

@Override
public String getSequence() {
if (sequence == null) {
return null;
}
return sequence.getBaseSequence();
}

Expand Down Expand Up @@ -119,7 +122,7 @@ public void addInEdge(int identifier) {
public void removeInEdge(int identifier) {
assert inEdges.contains(
identifier) : "Removing non-existent in-edge: " + identifier + ". NodeID: " + this.getId();
inEdges.remove(identifier);
inEdges.remove((Integer) identifier);
}

/**
Expand Down Expand Up @@ -150,7 +153,7 @@ public void addOutEdge(int identifier) {
public void removeOutEdge(int identifier) {
assert outEdges.contains(
identifier) : "Removing non-existent out-edge: " + identifier + ". NodeID: " + this.getId();
outEdges.remove(identifier);
outEdges.remove((Integer) identifier);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package nl.tudelft.pl2016gr2.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;

import nl.tudelft.pl2016gr2.thirdparty.testing.utility.AccessPrivate;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import java.util.Arrays;

/**
* Tests the {@link AbstractNode} class.
*
* @author Wouter Smit
*/
public class AbstractNodeTest {

@Rule
public final ExpectedException exception = ExpectedException.none();

private AbstractNode instance;

/**
* Sets up the abstract spied class.
*/
@Before
public void setUp() {
instance = mock(AbstractNode.class);
Mockito.when(instance.getId()).thenCallRealMethod();
Mockito.when(instance.hasChildren()).thenCallRealMethod();
Mockito.when(instance.getChildren()).thenCallRealMethod();
Mockito.when(instance.size()).thenCallRealMethod();
Mockito.when(instance.toString()).thenCallRealMethod();
Mockito.when(instance.getGenomesOverEdge(any())).thenCallRealMethod();
}

@Test
public void testConstructor() {
AbstractNode extendedNode = new SequenceNode(5);
assertEquals(5, extendedNode.getId());
}

@Test
public void getId() {
AccessPrivate.setFieldValue("id_field", AbstractNode.class, instance, 5);
assertEquals(5, instance.getId());
}

@Test
public void hasChildren() {
assertFalse(instance.hasChildren());
}

@Test
public void getChildren() {
assertNull(instance.getChildren());
}

@Test
public void size() {
Mockito.when(instance.getSequence()).thenReturn("ACTG");
assertEquals(4, instance.size());
}

@Test
public void getGenomesOverEdge() {
GraphNode otherNode = mock(GraphNode.class);

Mockito.when(otherNode.getId()).thenReturn(2);
AccessPrivate.setFieldValue("id_field", AbstractNode.class, instance, 5);

Mockito.when(instance.getGenomes()).thenReturn(Arrays.asList("equal", "gen0"));
Mockito.when(otherNode.getGenomes()).thenReturn(Arrays.asList("equal", "gen1"));

Mockito.when(otherNode.getInEdges()).thenReturn(Arrays.asList(1, 5, 8));
Mockito.when(instance.getOutEdges()).thenReturn(Arrays.asList(2, 10, 15));

assertTrue(instance.getGenomesOverEdge(otherNode).contains("equal"));
assertEquals(1, instance.getGenomesOverEdge(otherNode).size());
}

@Test
public void getGenomesOverEdgeThrowsAssertionWhenNotSuccessor() {
GraphNode otherNode = mock(GraphNode.class);

Mockito.when(otherNode.getId()).thenReturn(2);
AccessPrivate.setFieldValue("id_field", AbstractNode.class, instance, 5);

Mockito.when(otherNode.getInEdges()).thenReturn(Arrays.asList(1, 8));
Mockito.when(instance.getOutEdges()).thenReturn(Arrays.asList(10, 15));

exception.expect(AssertionError.class);
instance.getGenomesOverEdge(otherNode);
}

@Test
public void testToString() {
AccessPrivate.setFieldValue("id_field", AbstractNode.class, instance, 5);
assertEquals("id: 5", instance.toString());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package nl.tudelft.pl2016gr2.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
* Tests the {@link HashGraph} class.
*
* @author Wouter Smit
*/
public class HashGraphTest {
private Map<Integer, GraphNode> nodes = new HashMap<>();
private Collection<String> genomes = new ArrayList<>();

/**
* Sets up the graph structure with mocked nodes.
*/
@Before
public void setUp() {
for (int i = 0; i < 5; i++) {
nodes.put(i, SequenceGraphTest.mockNode(i, false));
}
int rootId = 5;
nodes.put(rootId, SequenceGraphTest.mockNode(rootId, true));

for (int i = 0; i < 5; i++) {
genomes.add("genome" + i);
}
}

@Test
public void testHashGraphConstructorForNodesAndGenomes() {
HashGraph graph = new HashGraph(nodes, genomes);

nodes.forEach((id, node) -> assertTrue(graph.contains(id)));
assertTrue(graph.getGenomes().containsAll(genomes));
}

@Test
public void testHashGraphConstructorIncludingRootNodes() {
Collection<Integer> rootNodes = new ArrayList<>(Arrays.asList(5, 10, 25));
HashGraph graph = new HashGraph(nodes, rootNodes, genomes);

nodes.forEach((id, node) -> assertTrue(graph.contains(id)));
assertTrue(graph.getGenomes().containsAll(genomes));
assertTrue(graph.getRootNodes().containsAll(rootNodes));
}

@Test
public void testToString() throws Exception {
HashGraph graph = new HashGraph();
assertEquals("", graph.toString());
graph.add(SequenceGraphTest.mockNode(0, false));
assertNotNull(graph.toString());
}

}
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
//package nl.tudelft.pl2016gr2.model;
//
//import static org.junit.Assert.assertEquals;
//import static org.junit.Assert.assertTrue;
//
//import org.junit.Before;
//import org.junit.Test;
//
//import java.util.ArrayList;
//
//public class NodePositionTest {
//
// private NodePosition nodePosition;
// private AbstractNode node;
//
// @Before
// public void setup() {
// this.node = new Node(5, 3, new ArrayList<String>(), 2);
// this.nodePosition = new NodePosition(node, 6);
// }
//
// @Test
// public void offsetTest() {
// nodePosition.addPositionOffset(4);
// assertEquals(10, nodePosition.getLevel());
// }
//
// @Test
// public void getNodeTest() {
// assertEquals(node, nodePosition.getNode());
// }
//
// @Test
// public void overlapTest() {
// nodePosition.setOverlapping(true);
// assertTrue(nodePosition.isOverlapping());
// }
//
// @Test
// public void compareTest() {
// AbstractNode other = new Node(5, 3, new ArrayList<String>(), 2);
// NodePosition otherPosition = new NodePosition(other, 2);
// assertEquals(4, nodePosition.compareTo(otherPosition));
// }
//
//}
package nl.tudelft.pl2016gr2.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;

public class NodePositionTest {

private NodePosition nodePosition;
private GraphNode node;

@Before
public void setup() {
this.node = mock(GraphNode.class);
this.nodePosition = new NodePosition(node, 6);
}

@Test
public void offsetTest() {
nodePosition.addPositionOffset(4);
assertEquals(10, nodePosition.getLevel());
}

@Test
public void getNodeTest() {
assertEquals(node, nodePosition.getNode());
}

@Test
public void overlapTest() {
nodePosition.setOverlapping(true);
assertTrue(nodePosition.isOverlapping());
}

@Test
public void compareTest() {
GraphNode other = mock(GraphNode.class);
NodePosition otherPosition = new NodePosition(other, 2);
assertEquals(4, nodePosition.compareTo(otherPosition));
}

}
Loading

0 comments on commit 8177674

Please sign in to comment.