-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgentMessageTest.java
168 lines (153 loc) · 5.17 KB
/
AgentMessageTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* JUnit test program for methods in AgentMessage Class.
* Course: 5DV133
* Written by: Thomas Sarlin id15tsn
* @Author Group 15 - 5DV133 vt2016
* @Version 1.0
*/
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Test for class AgentMessage.
*/
public class AgentMessageTest {
/**
* Tests if the constructor works as intended.
*
* @throws Exception if agent is not created
*/
@Test
public void shouldBeAbleToCreate() throws Exception{
new AgentMessage(new Node(100,new Position(1,1)));
}
/**
* Tests if the agent is able to move.
* @throws Exception if peek don't give message a
*/
@Test
public void shouldBeAbleToMove() throws Exception{
Node n1 = new Node(1,new Position(1,1));
Node n2 = new Node(1,new Position(1,2));
n1.setNeighbour(n2);
AgentMessage a = new AgentMessage(n1);
a.move(n1);
assertEquals(a,n2.peekAtQueue());
}
/**
* Tests so that the agent stops being added to ques
* after 50 moves.
*
* @throws Exception if nodes queue is not null
*/
@Test
public void shouldNotBeAbleToMove()throws Exception{
Node n1 = new Node(1,new Position(1,1));
Node n2 = new Node(1,new Position(1,2));
n1.setNeighbour(n2);
n2.setNeighbour(n1);
AgentMessage a = new AgentMessage(n1);
for(int i=0;i<101;i++) {
n1.takeNextMessage();
n2.takeNextMessage();
}
assertTrue(n1.peekAtQueue()==null);
assertTrue(n2.peekAtQueue()==null);
}
/**
* Tests if agent successfully withdraws actions and passes them forward.
* @throws Exception if nodes table aren't updated
*/
@Test
public void testSpreadingMultipleActions() throws Exception{
Node n1 = new Node(1,new Position(1,1));
Node n2 = new Node(1,new Position(1,2));
Node n3 = new Node(1,new Position(1,3));
n1.setAction(new Action("action",0,1));
n1.setNeighbour(n2);
n2.setNeighbour(n3);
n2.setAction(new Action("action2",0,2));
n3.setNeighbour(n1);
new AgentMessage(n1);
for(int i=0;i<6;i++){
n1.takeNextMessage();
n2.takeNextMessage();
n3.takeNextMessage();
}
assertEquals("action",n3.getAction("action").getActionID());
assertEquals(2,n3.getAction("action").getSteps());
assertEquals("action2",n3.getAction("action2").getActionID());
assertEquals(1,n3.getAction("action2").getSteps());
}
/**
* Tests if agent successfully withdraws values from the node if the node
* has a shorter path to the target action.
*
* @throws Exception if steps is not 4
*/
@Test
public void testReplacingValuesOnAgent() throws Exception{
Node n1 = new Node(1,new Position(1,1));
Node n2 = new Node(1,new Position(1,2));
Node n3 = new Node(1,new Position(1,3));
n1.setAction(new Action("action",25,1));
n1.setNeighbour(n2);
n2.setNeighbour(n3);
n3.setNeighbour(n1);
n2.setAction(new Action("action",2,1));
AgentMessage a= new AgentMessage(n1);
for(int i=0;i<6;i++){
n1.takeNextMessage();
n2.takeNextMessage();
n3.takeNextMessage();
}
assertEquals(4,a.getAgentActionTable().get("action").getSteps());
}
/**
* Tests if agent successfully replaces values in the node if the Agent
* has a shorter path to the target action.
*
* @throws Exception if steps is not 1
*/
@Test
public void testReplacingValuesOnNode() throws Exception{
Node n1 = new Node(1,new Position(1,1));
Node n2 = new Node(1,new Position(1,2));
Node n3 = new Node(1,new Position(1,3));
n1.setAction(new Action("action",0,1));
n1.setNeighbour(n2);
n2.setNeighbour(n3);
n3.setNeighbour(n1);
n2.setAction(new Action("action",25,1));
AgentMessage a= new AgentMessage(n1);
for(int i=0;i<6;i++){
n1.takeNextMessage();
n2.takeNextMessage();
n3.takeNextMessage();
}
assertEquals(1,n2.getAction("action").getSteps());
}
/**
* Tests so that the agent changes the next position in the nodes action
* if the agent has a shorter path in its table.
* @throws Exception if the position is not (1,1) and (1,2)
*/
@Test
public void shouldChangeNextPosition() throws Exception{
Node n1 = new Node(1,new Position(1,1));
Node n2 = new Node(1,new Position(1,2));
Node n3 = new Node(1,new Position(1,3));
n1.setAction(new Action("action",0,1));
n1.setNeighbour(n2);
n2.setNeighbour(n3);
n3.setNeighbour(n1);
n2.setAction(new Action("action",25,1));
AgentMessage a= new AgentMessage(n1);
for(int i=0;i<6;i++){
n1.takeNextMessage();
n2.takeNextMessage();
n3.takeNextMessage();
}
assertEquals(new Position(1,1),n2.getAction("action").getNextPosition());
assertEquals(new Position(1,2),n3.getAction("action").getNextPosition());
}
}