Skip to content

Commit 8177674

Browse files
committed
Merge pull request #141 from ProgrammingLife2016/test/test-model-classes
Add tests for the model classes
2 parents 8ee34b5 + 63c6ff8 commit 8177674

File tree

9 files changed

+793
-51
lines changed

9 files changed

+793
-51
lines changed

PL2/PL2-shared/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@
2525
<version>4.10</version>
2626
<scope>test</scope>
2727
</dependency>
28+
<dependency>
29+
<groupId>org.mockito</groupId>
30+
<artifactId>mockito-core</artifactId>
31+
<version>1.10.19</version>
32+
</dependency>
2833
</dependencies>
2934
</project>

PL2/PL2-shared/src/main/java/nl/tudelft/pl2016gr2/model/AbstractNode.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package nl.tudelft.pl2016gr2.model;
22

3+
import nl.tudelft.pl2016gr2.thirdparty.testing.utility.TestId;
4+
35
import java.util.ArrayList;
46
import java.util.Collection;
57

@@ -15,7 +17,8 @@
1517
*/
1618
public abstract class AbstractNode implements Node {
1719

18-
private final int identifier;
20+
@TestId(id = "id_field")
21+
private int identifier;
1922

2023
/**
2124
* Construct a bare abstract node with an ID.
@@ -66,6 +69,6 @@ assert getOutEdges().contains(
6669

6770
@Override
6871
public String toString() {
69-
return "id: " + identifier;
72+
return "id: " + getId();
7073
}
7174
}

PL2/PL2-shared/src/main/java/nl/tudelft/pl2016gr2/model/HashGraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public Iterator<GraphNode> iterator() {
147147
public String toString() {
148148
StringBuilder sb = new StringBuilder();
149149
for (GraphNode node : nodes.values()) {
150-
sb.append(node).append('\n');
150+
sb.append(node.toString()).append('\n');
151151
}
152152
return sb.toString();
153153
}

PL2/PL2-shared/src/main/java/nl/tudelft/pl2016gr2/model/SequenceNode.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public void setSequence(BaseSequence sequence) {
8888

8989
@Override
9090
public String getSequence() {
91+
if (sequence == null) {
92+
return null;
93+
}
9194
return sequence.getBaseSequence();
9295
}
9396

@@ -119,7 +122,7 @@ public void addInEdge(int identifier) {
119122
public void removeInEdge(int identifier) {
120123
assert inEdges.contains(
121124
identifier) : "Removing non-existent in-edge: " + identifier + ". NodeID: " + this.getId();
122-
inEdges.remove(identifier);
125+
inEdges.remove((Integer) identifier);
123126
}
124127

125128
/**
@@ -150,7 +153,7 @@ public void addOutEdge(int identifier) {
150153
public void removeOutEdge(int identifier) {
151154
assert outEdges.contains(
152155
identifier) : "Removing non-existent out-edge: " + identifier + ". NodeID: " + this.getId();
153-
outEdges.remove(identifier);
156+
outEdges.remove((Integer) identifier);
154157
}
155158

156159
@Override
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package nl.tudelft.pl2016gr2.model;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNull;
6+
import static org.junit.Assert.assertTrue;
7+
import static org.mockito.Matchers.any;
8+
import static org.mockito.Mockito.mock;
9+
10+
import nl.tudelft.pl2016gr2.thirdparty.testing.utility.AccessPrivate;
11+
import org.junit.Before;
12+
import org.junit.Rule;
13+
import org.junit.Test;
14+
import org.junit.rules.ExpectedException;
15+
import org.mockito.Mockito;
16+
17+
import java.util.Arrays;
18+
19+
/**
20+
* Tests the {@link AbstractNode} class.
21+
*
22+
* @author Wouter Smit
23+
*/
24+
public class AbstractNodeTest {
25+
26+
@Rule
27+
public final ExpectedException exception = ExpectedException.none();
28+
29+
private AbstractNode instance;
30+
31+
/**
32+
* Sets up the abstract spied class.
33+
*/
34+
@Before
35+
public void setUp() {
36+
instance = mock(AbstractNode.class);
37+
Mockito.when(instance.getId()).thenCallRealMethod();
38+
Mockito.when(instance.hasChildren()).thenCallRealMethod();
39+
Mockito.when(instance.getChildren()).thenCallRealMethod();
40+
Mockito.when(instance.size()).thenCallRealMethod();
41+
Mockito.when(instance.toString()).thenCallRealMethod();
42+
Mockito.when(instance.getGenomesOverEdge(any())).thenCallRealMethod();
43+
}
44+
45+
@Test
46+
public void testConstructor() {
47+
AbstractNode extendedNode = new SequenceNode(5);
48+
assertEquals(5, extendedNode.getId());
49+
}
50+
51+
@Test
52+
public void getId() {
53+
AccessPrivate.setFieldValue("id_field", AbstractNode.class, instance, 5);
54+
assertEquals(5, instance.getId());
55+
}
56+
57+
@Test
58+
public void hasChildren() {
59+
assertFalse(instance.hasChildren());
60+
}
61+
62+
@Test
63+
public void getChildren() {
64+
assertNull(instance.getChildren());
65+
}
66+
67+
@Test
68+
public void size() {
69+
Mockito.when(instance.getSequence()).thenReturn("ACTG");
70+
assertEquals(4, instance.size());
71+
}
72+
73+
@Test
74+
public void getGenomesOverEdge() {
75+
GraphNode otherNode = mock(GraphNode.class);
76+
77+
Mockito.when(otherNode.getId()).thenReturn(2);
78+
AccessPrivate.setFieldValue("id_field", AbstractNode.class, instance, 5);
79+
80+
Mockito.when(instance.getGenomes()).thenReturn(Arrays.asList("equal", "gen0"));
81+
Mockito.when(otherNode.getGenomes()).thenReturn(Arrays.asList("equal", "gen1"));
82+
83+
Mockito.when(otherNode.getInEdges()).thenReturn(Arrays.asList(1, 5, 8));
84+
Mockito.when(instance.getOutEdges()).thenReturn(Arrays.asList(2, 10, 15));
85+
86+
assertTrue(instance.getGenomesOverEdge(otherNode).contains("equal"));
87+
assertEquals(1, instance.getGenomesOverEdge(otherNode).size());
88+
}
89+
90+
@Test
91+
public void getGenomesOverEdgeThrowsAssertionWhenNotSuccessor() {
92+
GraphNode otherNode = mock(GraphNode.class);
93+
94+
Mockito.when(otherNode.getId()).thenReturn(2);
95+
AccessPrivate.setFieldValue("id_field", AbstractNode.class, instance, 5);
96+
97+
Mockito.when(otherNode.getInEdges()).thenReturn(Arrays.asList(1, 8));
98+
Mockito.when(instance.getOutEdges()).thenReturn(Arrays.asList(10, 15));
99+
100+
exception.expect(AssertionError.class);
101+
instance.getGenomesOverEdge(otherNode);
102+
}
103+
104+
@Test
105+
public void testToString() {
106+
AccessPrivate.setFieldValue("id_field", AbstractNode.class, instance, 5);
107+
assertEquals("id: 5", instance.toString());
108+
}
109+
110+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package nl.tudelft.pl2016gr2.model;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.Collection;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
16+
/**
17+
* Tests the {@link HashGraph} class.
18+
*
19+
* @author Wouter Smit
20+
*/
21+
public class HashGraphTest {
22+
private Map<Integer, GraphNode> nodes = new HashMap<>();
23+
private Collection<String> genomes = new ArrayList<>();
24+
25+
/**
26+
* Sets up the graph structure with mocked nodes.
27+
*/
28+
@Before
29+
public void setUp() {
30+
for (int i = 0; i < 5; i++) {
31+
nodes.put(i, SequenceGraphTest.mockNode(i, false));
32+
}
33+
int rootId = 5;
34+
nodes.put(rootId, SequenceGraphTest.mockNode(rootId, true));
35+
36+
for (int i = 0; i < 5; i++) {
37+
genomes.add("genome" + i);
38+
}
39+
}
40+
41+
@Test
42+
public void testHashGraphConstructorForNodesAndGenomes() {
43+
HashGraph graph = new HashGraph(nodes, genomes);
44+
45+
nodes.forEach((id, node) -> assertTrue(graph.contains(id)));
46+
assertTrue(graph.getGenomes().containsAll(genomes));
47+
}
48+
49+
@Test
50+
public void testHashGraphConstructorIncludingRootNodes() {
51+
Collection<Integer> rootNodes = new ArrayList<>(Arrays.asList(5, 10, 25));
52+
HashGraph graph = new HashGraph(nodes, rootNodes, genomes);
53+
54+
nodes.forEach((id, node) -> assertTrue(graph.contains(id)));
55+
assertTrue(graph.getGenomes().containsAll(genomes));
56+
assertTrue(graph.getRootNodes().containsAll(rootNodes));
57+
}
58+
59+
@Test
60+
public void testToString() throws Exception {
61+
HashGraph graph = new HashGraph();
62+
assertEquals("", graph.toString());
63+
graph.add(SequenceGraphTest.mockNode(0, false));
64+
assertNotNull(graph.toString());
65+
}
66+
67+
}
Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
1-
//package nl.tudelft.pl2016gr2.model;
2-
//
3-
//import static org.junit.Assert.assertEquals;
4-
//import static org.junit.Assert.assertTrue;
5-
//
6-
//import org.junit.Before;
7-
//import org.junit.Test;
8-
//
9-
//import java.util.ArrayList;
10-
//
11-
//public class NodePositionTest {
12-
//
13-
// private NodePosition nodePosition;
14-
// private AbstractNode node;
15-
//
16-
// @Before
17-
// public void setup() {
18-
// this.node = new Node(5, 3, new ArrayList<String>(), 2);
19-
// this.nodePosition = new NodePosition(node, 6);
20-
// }
21-
//
22-
// @Test
23-
// public void offsetTest() {
24-
// nodePosition.addPositionOffset(4);
25-
// assertEquals(10, nodePosition.getLevel());
26-
// }
27-
//
28-
// @Test
29-
// public void getNodeTest() {
30-
// assertEquals(node, nodePosition.getNode());
31-
// }
32-
//
33-
// @Test
34-
// public void overlapTest() {
35-
// nodePosition.setOverlapping(true);
36-
// assertTrue(nodePosition.isOverlapping());
37-
// }
38-
//
39-
// @Test
40-
// public void compareTest() {
41-
// AbstractNode other = new Node(5, 3, new ArrayList<String>(), 2);
42-
// NodePosition otherPosition = new NodePosition(other, 2);
43-
// assertEquals(4, nodePosition.compareTo(otherPosition));
44-
// }
45-
//
46-
//}
1+
package nl.tudelft.pl2016gr2.model;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.mockito.Mockito.mock;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import java.util.ArrayList;
11+
12+
public class NodePositionTest {
13+
14+
private NodePosition nodePosition;
15+
private GraphNode node;
16+
17+
@Before
18+
public void setup() {
19+
this.node = mock(GraphNode.class);
20+
this.nodePosition = new NodePosition(node, 6);
21+
}
22+
23+
@Test
24+
public void offsetTest() {
25+
nodePosition.addPositionOffset(4);
26+
assertEquals(10, nodePosition.getLevel());
27+
}
28+
29+
@Test
30+
public void getNodeTest() {
31+
assertEquals(node, nodePosition.getNode());
32+
}
33+
34+
@Test
35+
public void overlapTest() {
36+
nodePosition.setOverlapping(true);
37+
assertTrue(nodePosition.isOverlapping());
38+
}
39+
40+
@Test
41+
public void compareTest() {
42+
GraphNode other = mock(GraphNode.class);
43+
NodePosition otherPosition = new NodePosition(other, 2);
44+
assertEquals(4, nodePosition.compareTo(otherPosition));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)