1 | package geniusweb.protocol.session.shaop;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertNotNull;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 | import static org.mockito.Mockito.mock;
|
---|
8 | import static org.mockito.Mockito.when;
|
---|
9 |
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Date;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 |
|
---|
17 | import org.junit.Before;
|
---|
18 | import org.junit.Ignore;
|
---|
19 | import org.junit.Test;
|
---|
20 |
|
---|
21 | import geniusweb.actions.Accept;
|
---|
22 | import geniusweb.actions.Action;
|
---|
23 | import geniusweb.actions.EndNegotiation;
|
---|
24 | import geniusweb.actions.Offer;
|
---|
25 | import geniusweb.actions.PartyId;
|
---|
26 | import geniusweb.issuevalue.Bid;
|
---|
27 | import geniusweb.progress.Progress;
|
---|
28 | import geniusweb.progress.ProgressRounds;
|
---|
29 | import geniusweb.progress.ProgressTime;
|
---|
30 | import geniusweb.protocol.ProtocolException;
|
---|
31 | import geniusweb.protocol.partyconnection.ProtocolToPartyConn;
|
---|
32 | import geniusweb.protocol.partyconnection.ProtocolToPartyConnections;
|
---|
33 | import geniusweb.protocol.session.SessionResult;
|
---|
34 | import geniusweb.protocol.session.TeamInfo;
|
---|
35 | import tudelft.utilities.junit.GeneralTests;
|
---|
36 |
|
---|
37 | public class SHAOPStateTest extends GeneralTests<SHAOPState> {
|
---|
38 | private final long NOW = 1000;
|
---|
39 |
|
---|
40 | private PartyId party1 = new PartyId("party1");
|
---|
41 | private PartyId party2 = new PartyId("party2");
|
---|
42 | private PartyId party3 = new PartyId("party3");
|
---|
43 | private PartyId party4 = new PartyId("party4");
|
---|
44 | private List<Action> actions = new LinkedList<>();
|
---|
45 | private Action action = mock(Action.class);
|
---|
46 | private List<Action> actions2 = Arrays.asList(action);
|
---|
47 |
|
---|
48 | private ProtocolToPartyConn party1conn = mock(ProtocolToPartyConn.class);
|
---|
49 | private ProtocolToPartyConn party2conn = mock(ProtocolToPartyConn.class);
|
---|
50 | private ProtocolToPartyConn party3conn = mock(ProtocolToPartyConn.class);
|
---|
51 | private ProtocolToPartyConn party4conn = mock(ProtocolToPartyConn.class);
|
---|
52 |
|
---|
53 | private ProtocolToPartyConnections connections = new ProtocolToPartyConnections(
|
---|
54 | Arrays.asList(party1conn, party2conn));
|
---|
55 | private ProtocolToPartyConnections connections2 = new ProtocolToPartyConnections(
|
---|
56 | Arrays.asList(party1conn, party3conn));
|
---|
57 | private ProtocolToPartyConnections connections3 = new ProtocolToPartyConnections(
|
---|
58 | Arrays.asList(party1conn, party2conn, party3conn));
|
---|
59 | private ProtocolToPartyConnections connections4 = new ProtocolToPartyConnections(
|
---|
60 | Arrays.asList(party1conn, party2conn, party3conn, party4conn));
|
---|
61 |
|
---|
62 | private Progress progresstime = mock(ProgressTime.class);
|
---|
63 | private ProgressRounds progressrounds = mock(ProgressRounds.class);
|
---|
64 | private ProgressRounds progressrounds1 = mock(ProgressRounds.class);
|
---|
65 | private SHAOPSettings settings = mock(SHAOPSettings.class);
|
---|
66 | private SHAOPState state1, state1a, state2, state3, state4;
|
---|
67 |
|
---|
68 | private Bid bid1 = mock(Bid.class);
|
---|
69 | private Bid otherbid = mock(Bid.class);
|
---|
70 | private Accept accept1 = new Accept(party1, bid1);
|
---|
71 | private Accept accept2 = new Accept(party2, bid1);
|
---|
72 | private Accept acceptother = new Accept(party2, otherbid);
|
---|
73 | private Offer offer1 = new Offer(party1, bid1);
|
---|
74 | private EndNegotiation endNegotiation1 = new EndNegotiation(party1);
|
---|
75 | private Map<PartyId, Integer> partynrs = new HashMap<>();
|
---|
76 |
|
---|
77 | private TeamInfo team1 = mock(TeamInfo.class);
|
---|
78 | private TeamInfo team2 = mock(TeamInfo.class);
|
---|
79 |
|
---|
80 | @Before
|
---|
81 | public void before() {
|
---|
82 | when(party1conn.getParty()).thenReturn(party1);
|
---|
83 | when(party2conn.getParty()).thenReturn(party2);
|
---|
84 | when(party3conn.getParty()).thenReturn(party3);
|
---|
85 | when(party1conn.toString()).thenReturn("conn1");
|
---|
86 | when(party2conn.toString()).thenReturn("conn2");
|
---|
87 | when(party3conn.toString()).thenReturn("conn3");
|
---|
88 | when(party4conn.toString()).thenReturn("conn4");
|
---|
89 | when(action.toString()).thenReturn("act1");
|
---|
90 | when(settings.toString()).thenReturn("settings");
|
---|
91 | when(progressrounds.toString()).thenReturn("progressrounds");
|
---|
92 | when(progresstime.toString()).thenReturn("progresstime");
|
---|
93 |
|
---|
94 | when(progressrounds.getTerminationTime())
|
---|
95 | .thenReturn(new Date(System.currentTimeMillis() + 3600000l));
|
---|
96 | when(progressrounds1.getTerminationTime())
|
---|
97 | .thenReturn(new Date(System.currentTimeMillis() + 3600000l));
|
---|
98 | when(progressrounds.advance()).thenReturn(progressrounds1);
|
---|
99 |
|
---|
100 | partynrs.put(party1, 0);
|
---|
101 | partynrs.put(party2, 1);
|
---|
102 | partynrs.put(party3, 2);
|
---|
103 | partynrs.put(party4, 3);
|
---|
104 | Map<PartyId, Double> totalspent = new HashMap<>();
|
---|
105 |
|
---|
106 | state1 = new SHAOPState(actions, connections, progresstime, settings,
|
---|
107 | null, 0, partynrs, totalspent);
|
---|
108 | state1a = new SHAOPState(actions, connections, progresstime, settings,
|
---|
109 | null, 0, partynrs, totalspent);
|
---|
110 | state2 = new SHAOPState(actions2, connections, progresstime, settings,
|
---|
111 | null, 0, partynrs, totalspent);
|
---|
112 | state3 = new SHAOPState(actions, connections, progressrounds, settings,
|
---|
113 | null, 0, partynrs, totalspent);
|
---|
114 | state4 = new SHAOPState(actions, connections, progresstime, settings,
|
---|
115 | null, 1, partynrs, totalspent);
|
---|
116 |
|
---|
117 | when(settings.getTeams()).thenReturn(Arrays.asList(team1, team2));
|
---|
118 |
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | public List<List<SHAOPState>> getGeneralTestData() {
|
---|
123 | return Arrays.asList(Arrays.asList(state1, state1a),
|
---|
124 | Arrays.asList(state2), Arrays.asList(state3),
|
---|
125 | Arrays.asList(state4));
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public List<String> getGeneralTestStrings() {
|
---|
130 | return Arrays.asList(
|
---|
131 | "SHAOPState.*\\[\\].*ConnectionWithParties.*conn1.*conn2.*progresstime.*settings.*",
|
---|
132 | "SHAOPState.*\\[act1\\].*ConnectionWithParties.*conn1.*conn2.*progresstime.*settings.*",
|
---|
133 | "SHAOPState.*\\[\\].*ConnectionWithParties.*conn1.*conn2.*progressrounds.*settings.*",
|
---|
134 | "SHAOPState.*\\[\\].*ConnectionWithParties.*conn1.*conn2.*progresstime.*settings.*");
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Test
|
---|
138 | public void constructWith0Connection() {
|
---|
139 | new SHAOPState(actions, new ProtocolToPartyConnections(Arrays.asList()),
|
---|
140 | progresstime, settings, null, 0, null, null);
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Test
|
---|
144 | public void constructWithNullConnection() {
|
---|
145 | SHAOPState state = new SHAOPState(actions, null, progresstime, settings,
|
---|
146 | null, 0, null, null);
|
---|
147 | assertEquals(0, state.getConnections().size());
|
---|
148 | }
|
---|
149 |
|
---|
150 | @Test
|
---|
151 | public void testCurrentActor() {
|
---|
152 | assertEquals(party1, state1.getCurrentTeam());
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Test
|
---|
156 | public void testNextActor2() {
|
---|
157 | ProtocolToPartyConnections conns = new ProtocolToPartyConnections(
|
---|
158 | Arrays.asList(party2conn, party3conn));
|
---|
159 | SHAOPState state = new SHAOPState(actions, conns, progresstime,
|
---|
160 | settings, null, 0, partynrs, null);
|
---|
161 | assertEquals(party2, state.getCurrentTeam());
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Test
|
---|
165 | public void testNextActorAfterAction() {
|
---|
166 | List<Action> actions = Arrays.asList(action);
|
---|
167 | SHAOPState state = new SHAOPState(actions, connections, progresstime,
|
---|
168 | settings, null, 0, null, null);
|
---|
169 | assertEquals(party1, state.getCurrentTeam());
|
---|
170 | }
|
---|
171 |
|
---|
172 | @Test
|
---|
173 | public void isFinalTest() {
|
---|
174 | assertFalse(state1.isFinal(NOW));
|
---|
175 | SHAOPState state = new SHAOPState(actions, connections, progresstime,
|
---|
176 | settings, mock(ProtocolException.class), 0, null, null);
|
---|
177 | assertTrue(state.isFinal(NOW));
|
---|
178 | }
|
---|
179 |
|
---|
180 | @Test
|
---|
181 | public void withExceptionTest() {
|
---|
182 | SHAOPState finalstate1 = state1.with(mock(ProtocolException.class));
|
---|
183 | assertTrue(finalstate1.isFinal(NOW));
|
---|
184 | SHAOPState finalstate2 = finalstate1
|
---|
185 | .with(mock(ProtocolException.class));
|
---|
186 | assertTrue(finalstate2.isFinal(NOW));
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Test
|
---|
190 | public void isOfferAgreementTest1() {
|
---|
191 | List<Action> actions = Arrays.asList(offer1);
|
---|
192 | SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
|
---|
193 | settings, null, 0, null, null);
|
---|
194 | assertTrue(state.getAgreements().getMap().isEmpty());
|
---|
195 | }
|
---|
196 |
|
---|
197 | @Test
|
---|
198 | public void isOfferAcceptAgreementTest() {
|
---|
199 | List<Action> actions = Arrays.asList(offer1, accept2);
|
---|
200 | SHAOPState state = new SHAOPState(actions, connections4, progressrounds,
|
---|
201 | settings, null, 0, partynrs, null);
|
---|
202 | assertFalse(state.getAgreements().getMap().isEmpty());
|
---|
203 | assertTrue(state.isFinal(NOW));
|
---|
204 | }
|
---|
205 |
|
---|
206 | @Test
|
---|
207 | public void isOfferAcceptOfferAgreementTest() {
|
---|
208 | List<Action> actions = Arrays.asList(offer1, accept1, offer1);
|
---|
209 | SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
|
---|
210 | settings, null, 0, null, null);
|
---|
211 | assert (state.getAgreements().getMap().isEmpty());
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Test
|
---|
215 | public void isOfferAcceptAcceptotherAgreementTest() {
|
---|
216 | List<Action> actions = Arrays.asList(offer1, accept1, acceptother);
|
---|
217 | SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
|
---|
218 | settings, null, 0, null, null);
|
---|
219 | assertTrue(state.getAgreements().getMap().isEmpty());
|
---|
220 | }
|
---|
221 |
|
---|
222 | @Test(expected = IllegalArgumentException.class)
|
---|
223 | public void withWrongActionTest() {
|
---|
224 | // wrong because action has actor null which does not match party1
|
---|
225 | SHAOPState state = state1.with(party1, action);
|
---|
226 | }
|
---|
227 |
|
---|
228 | @Test(expected = IllegalArgumentException.class)
|
---|
229 | public void withActionTest() {
|
---|
230 | when(action.getActor()).thenReturn(party1);
|
---|
231 | state1.with(party1, action);
|
---|
232 | }
|
---|
233 |
|
---|
234 | @Test
|
---|
235 | public void withConnectionTest() {
|
---|
236 | SHAOPState state = state1.with(party3conn);
|
---|
237 | assertEquals(3, state.getConnections().size());
|
---|
238 | }
|
---|
239 |
|
---|
240 | @Test
|
---|
241 | public void isEndNegotiationFinalTest() {
|
---|
242 | List<Action> actions = Arrays.asList(endNegotiation1);
|
---|
243 | SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
|
---|
244 | settings, null, 0, null, null);
|
---|
245 | assertTrue(state.getAgreements().getMap().isEmpty());
|
---|
246 | assertEquals(null, state.getError());
|
---|
247 | assertTrue(state.isFinal(NOW));
|
---|
248 |
|
---|
249 | }
|
---|
250 |
|
---|
251 | @Test
|
---|
252 | public void isProtocolErrorFinal() {
|
---|
253 | SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
|
---|
254 | settings,
|
---|
255 | new ProtocolException("test error", new PartyId("test")), 0,
|
---|
256 | null, null);
|
---|
257 | assertTrue(state.isFinal(NOW));
|
---|
258 | assertNotNull(state.getError());
|
---|
259 | }
|
---|
260 |
|
---|
261 | @Test(expected = IllegalArgumentException.class)
|
---|
262 | public void RefuseImmediateAccept() {
|
---|
263 | Accept nullaccept = new Accept(party1, null);
|
---|
264 | SHAOPState state = state1.with(party1, nullaccept);
|
---|
265 | }
|
---|
266 |
|
---|
267 | @Test(expected = IllegalArgumentException.class)
|
---|
268 | public void RefuseNotMyTurn() {
|
---|
269 | List<Action> actions = Arrays.asList(offer1);
|
---|
270 | SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
|
---|
271 | settings, null, 1, partynrs, null);
|
---|
272 |
|
---|
273 | state.with(party1, offer1);
|
---|
274 | }
|
---|
275 |
|
---|
276 | @Test(expected = IllegalArgumentException.class)
|
---|
277 | public void RefuseNullAccept() {
|
---|
278 | List<Action> actions = Arrays.asList(offer1);
|
---|
279 | SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
|
---|
280 | settings, null, 0, partynrs, null);
|
---|
281 |
|
---|
282 | Accept nullaccept = new Accept(party2, null);
|
---|
283 | state = state.with(party2, nullaccept);
|
---|
284 | assertTrue(state.isFinal(NOW));
|
---|
285 | }
|
---|
286 |
|
---|
287 | @Test(expected = IllegalArgumentException.class)
|
---|
288 | public void RefuseNullAction() {
|
---|
289 | List<Action> actions = Arrays.asList(offer1);
|
---|
290 | SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
|
---|
291 | settings, null, 0, null, null);
|
---|
292 |
|
---|
293 | state = state.with(party2, null);
|
---|
294 | }
|
---|
295 |
|
---|
296 | @Test
|
---|
297 | public void testRoundNumberAfterActions() {
|
---|
298 |
|
---|
299 | SHAOPState state = new SHAOPState(actions, connections4,
|
---|
300 | new ProgressRounds(10, 0, null), settings, null, 0, partynrs,
|
---|
301 | null);
|
---|
302 |
|
---|
303 | assertEquals((Double) 0d,
|
---|
304 | state.getProgress().get(System.currentTimeMillis()));
|
---|
305 | Offer act1 = new Offer(party1, bid1);
|
---|
306 | state = state.with(party1, act1);
|
---|
307 | assertEquals((Double) 0d,
|
---|
308 | state.getProgress().get(System.currentTimeMillis()));
|
---|
309 | Offer act2 = new Offer(party3, bid1);
|
---|
310 | state = state.with(party3, act2);
|
---|
311 | assertEquals((Double) 0.1d,
|
---|
312 | state.getProgress().get(System.currentTimeMillis()));
|
---|
313 |
|
---|
314 | }
|
---|
315 |
|
---|
316 | @Ignore // FIXME
|
---|
317 | @Test
|
---|
318 | public void getResultTest() {
|
---|
319 |
|
---|
320 | Map<PartyId, Double> spent = new HashMap<>();
|
---|
321 | spent.put(party1, 0.1d);
|
---|
322 | SHAOPState state = new SHAOPState(actions, connections4,
|
---|
323 | new ProgressRounds(10, 0, null), settings, null, 0, partynrs,
|
---|
324 | spent);
|
---|
325 | SessionResult res = state.getResults().get(0);
|
---|
326 | }
|
---|
327 |
|
---|
328 | }
|
---|