source: protocol/src/test/java/geniusweb/protocol/session/amop/AMOPStateTest.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 16.3 KB
Line 
1package geniusweb.protocol.session.amop;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6import static org.mockito.Matchers.any;
7import static org.mockito.Mockito.mock;
8import static org.mockito.Mockito.when;
9
10import java.util.Arrays;
11import java.util.Collections;
12import java.util.Date;
13import java.util.HashMap;
14import java.util.HashSet;
15import java.util.LinkedList;
16import java.util.List;
17import java.util.Map;
18
19import org.junit.Before;
20import org.junit.Test;
21
22import geniusweb.actions.Accept;
23import geniusweb.actions.Action;
24import geniusweb.actions.EndNegotiation;
25import geniusweb.actions.Offer;
26import geniusweb.actions.PartyId;
27import geniusweb.actions.Vote;
28import geniusweb.actions.Votes;
29import geniusweb.inform.Agreements;
30import geniusweb.issuevalue.Bid;
31import geniusweb.progress.Progress;
32import geniusweb.progress.ProgressRounds;
33import geniusweb.progress.ProgressTime;
34import geniusweb.protocol.ProtocolException;
35import geniusweb.protocol.partyconnection.ProtocolToPartyConn;
36import geniusweb.protocol.partyconnection.ProtocolToPartyConnections;
37import geniusweb.protocol.session.amop.AMOPState.Phase;
38import geniusweb.references.PartyWithProfile;
39import tudelft.utilities.junit.GeneralTests;
40
41public class AMOPStateTest extends GeneralTests<AMOPState> {
42 private final long NOW = 1000;
43 Bid a = mock(Bid.class), b = mock(Bid.class), c = mock(Bid.class),
44 d = mock(Bid.class);
45
46 private final PartyId party1 = new PartyId("party1");
47 private final PartyId party2 = new PartyId("party2");
48 private final PartyId party3 = new PartyId("party3");
49 private final PartyId party4 = new PartyId("party4");
50 private final List<Action> actions = new LinkedList<>();
51 private final Action action = mock(Action.class);
52 private final List<Action> actions2 = Arrays.asList(action);
53
54 private final ProtocolToPartyConn party1conn = mock(
55 ProtocolToPartyConn.class);
56 private final ProtocolToPartyConn party2conn = mock(
57 ProtocolToPartyConn.class);
58 private final ProtocolToPartyConn party3conn = mock(
59 ProtocolToPartyConn.class);
60
61 private final ProtocolToPartyConnections connections = new ProtocolToPartyConnections(
62 Arrays.asList(party1conn, party2conn));
63 private final ProtocolToPartyConnections connections2 = new ProtocolToPartyConnections(
64 Arrays.asList(party1conn, party3conn));
65 private final ProtocolToPartyConnections connections3 = new ProtocolToPartyConnections(
66 Arrays.asList(party1conn, party2conn, party3conn));
67
68 private final Progress progresstime = mock(ProgressTime.class);
69 private final ProgressRounds progressrounds = mock(ProgressRounds.class);
70 private final ProgressRounds progressrounds1 = mock(ProgressRounds.class);
71 private final AMOPSettings settings = mock(AMOPSettings.class);
72 private final AMOPSettings settings2 = mock(AMOPSettings.class);
73 private AMOPState state1, state1a, state2, state3, state4, state5, state6,
74 state7, state8, state9;
75
76 private final Bid bid1 = mock(Bid.class);
77 private final Bid otherbid = mock(Bid.class);
78 private final Accept accept1 = new Accept(party1, bid1);
79 private final Accept accept2 = new Accept(party2, bid1);
80 private final Accept acceptother = new Accept(party2, otherbid);
81 private final Offer offer1 = new Offer(party1, bid1);
82 private final EndNegotiation endNegotiation1 = new EndNegotiation(party1);
83
84 private final Map<PartyId, PartyWithProfile> partyprofiles1 = mock(
85 HashMap.class);
86 private final Map<PartyId, PartyWithProfile> partyprofiles2 = mock(
87 HashMap.class);
88
89 private final Agreements agreements1 = new Agreements();
90 private final Agreements agreements2 = mock(Agreements.class);
91
92 private final Map<PartyId, ProtocolException> exceptions1 = mock(
93 HashMap.class);
94 private final Map<PartyId, ProtocolException> exceptions2 = mock(
95 HashMap.class);
96
97 private final List<PartyId> walkedaway0 = Arrays.asList();
98 private final List<PartyId> walkedaway1 = Arrays.asList(party1);
99 private final List<PartyId> walkedaway3 = Arrays.asList(party3);
100
101 @Before
102 public void before() {
103 when(party1conn.getParty()).thenReturn(party1);
104 when(party2conn.getParty()).thenReturn(party2);
105 when(party3conn.getParty()).thenReturn(party3);
106 when(party1conn.toString()).thenReturn("conn1");
107 when(party2conn.toString()).thenReturn("conn2");
108 when(party3conn.toString()).thenReturn("conn3");
109 when(action.toString()).thenReturn("act1");
110
111 when(progressrounds.getTerminationTime())
112 .thenReturn(new Date(System.currentTimeMillis() + 3600000l));
113 when(progressrounds1.getTerminationTime())
114 .thenReturn(new Date(System.currentTimeMillis() + 3600000l));
115 when(progressrounds.advance()).thenReturn(progressrounds1);
116
117 state1 = new AMOPState(Phase.INIT, actions, connections, progresstime,
118 settings, partyprofiles1, agreements1, exceptions1,
119 walkedaway0);
120 state1a = new AMOPState(Phase.INIT, actions, connections, progresstime,
121 settings, partyprofiles1, agreements1, exceptions1,
122 walkedaway0);
123 state2 = new AMOPState(Phase.INIT, actions2, connections, progresstime,
124 settings, partyprofiles1, agreements1, exceptions1,
125 walkedaway0);
126 state3 = new AMOPState(Phase.INIT, actions, connections2, progresstime,
127 settings, partyprofiles1, agreements1, exceptions1,
128 walkedaway0);
129 state4 = new AMOPState(Phase.INIT, actions, connections, progressrounds,
130 settings, partyprofiles1, agreements1, exceptions1,
131 walkedaway0);
132 state5 = new AMOPState(Phase.INIT, actions, connections, progresstime,
133 settings2, partyprofiles1, agreements1, exceptions1,
134 walkedaway0);
135 state6 = new AMOPState(Phase.INIT, actions, connections, progresstime,
136 settings, partyprofiles2, agreements1, exceptions1,
137 walkedaway0);
138 state7 = new AMOPState(Phase.INIT, actions, connections, progresstime,
139 settings, partyprofiles1, agreements2, exceptions1,
140 walkedaway0);
141 state8 = new AMOPState(Phase.INIT, actions, connections, progresstime,
142 settings, partyprofiles1, agreements1, exceptions2,
143 walkedaway0);
144 state9 = new AMOPState(Phase.INIT, actions, connections, progresstime,
145 settings, partyprofiles1, agreements1, exceptions1,
146 walkedaway3);
147 }
148
149 @Override
150 public List<List<AMOPState>> getGeneralTestData() {
151 return Arrays.asList(Arrays.asList(state1, state1a),
152 Arrays.asList(state2), Arrays.asList(state3),
153 Arrays.asList(state4), Arrays.asList(state5),
154 Arrays.asList(state6), Arrays.asList(state7),
155 Arrays.asList(state8), Arrays.asList(state9));
156 }
157
158 @Override
159 public List<String> getGeneralTestStrings() {
160 // being lazy, toString is terrible large and everything's mocked
161 return Arrays.asList("AMOPState.*", "AMOPState.*", "AMOPState.*",
162 "AMOPState.*", "AMOPState.*", "AMOPState.*", "AMOPState.*",
163 "AMOPState.*", "AMOPState.*");
164 }
165
166 @Test
167 public void constructWith0Connection() {
168 state1 = new AMOPState(Phase.INIT, actions,
169 new ProtocolToPartyConnections(Arrays.asList()), progresstime,
170 settings, partyprofiles1, agreements1, exceptions1,
171 walkedaway0);
172 }
173
174 @SuppressWarnings("unused")
175 @Test
176 public void constructWithNullConnection() {
177 // is allowed
178 new AMOPState(Phase.INIT, actions, null, progresstime, settings,
179 partyprofiles1, agreements1, exceptions1, walkedaway0);
180 }
181
182 @Test
183 public void isInitFinalTest() {
184 assertFalse(state1.isFinal(NOW));
185 }
186
187 @Test
188 public void isInitFinalTest2() {
189 assertFalse(state1.isFinal(NOW + 12000));
190 }
191
192 @Test
193 public void withExceptionTest() {
194 AMOPState newstate1 = state1.with(party1,
195 mock(ProtocolException.class));
196 // party errors are not terminating in AMOP
197 assertFalse(newstate1.isFinal(NOW));
198 AMOPState newstate2 = newstate1.with(party2,
199 mock(ProtocolException.class));
200 assertFalse(newstate2.isFinal(NOW));
201 }
202
203 @Test
204 public void isOfferAgreementTest1() {
205 List<Action> actions = Arrays.asList(offer1);
206 AMOPState state = new AMOPState(Phase.INIT, actions, connections3,
207 progresstime, settings, partyprofiles1, agreements1,
208 exceptions1, walkedaway0);
209 assertTrue(state.getAgreements().getMap().isEmpty());
210 }
211
212 @Test(expected = ProtocolException.class)
213 public void illegalActionInIllegalState() throws ProtocolException {
214 // party can not do accept. And certainly not in INIT phase.
215 state1.with(party1, offer1).with(party1, accept2);
216 }
217
218 @Test
219 public void isOfferAllowedInOFFER() throws ProtocolException {
220 AMOPState state = new AMOPState(Phase.OFFER, actions, connections,
221 progresstime, settings, partyprofiles1, agreements1,
222 exceptions1, walkedaway0);
223 state = state.with(party1, offer1);
224 }
225
226 @Test(expected = ProtocolException.class)
227 public void isOfferAllowedInINIT() throws ProtocolException {
228 AMOPState state = new AMOPState(Phase.INIT, actions, connections,
229 progresstime, settings, partyprofiles1, agreements1,
230 exceptions1, walkedaway0);
231 state = state.with(party1, offer1);
232 }
233
234 @Test(expected = ProtocolException.class)
235 public void isOfferAllowedInVOTE() throws ProtocolException {
236 AMOPState state = new AMOPState(Phase.VOTE, actions, connections,
237 progresstime, settings, partyprofiles1, agreements1,
238 exceptions1, walkedaway0);
239 state = state.with(party1, offer1);
240 }
241
242 @Test(expected = ProtocolException.class)
243 public void isOfferAcceptAgreementTest2() throws ProtocolException {
244 AMOPState state = new AMOPState(Phase.OFFER, actions, connections,
245 progresstime, settings, partyprofiles1, agreements1,
246 exceptions1, walkedaway0);
247 state = state.with(party1, offer1).with(party1, accept2);
248 }
249
250 @Test
251 public void testWithProgress() {
252 AMOPState state = new AMOPState(Phase.INIT, actions, connections, null,
253 settings, partyprofiles1, agreements1, exceptions1,
254 walkedaway0);
255 state = state.with(progresstime);
256 assertEquals(progresstime, state.getProgress());
257 }
258
259 @Test(expected = IllegalArgumentException.class)
260 public void testWithProgressAlreadySet() {
261 AMOPState state = new AMOPState(Phase.INIT, actions, connections,
262 progressrounds, settings, partyprofiles1, agreements1,
263 exceptions1, walkedaway0);
264 state = state.with(progresstime);
265 }
266
267 @Test
268 public void activePartiesWalkedAwayTest() {
269 AMOPState state = new AMOPState(Phase.INIT, actions, connections3,
270 progressrounds, settings, partyprofiles1, agreements1,
271 exceptions1, walkedaway3);
272 assertEquals(Arrays.asList(party1, party2), state.getActiveParties());
273 }
274
275 @Test
276 public void activePartiesAgreedTest() {
277 Agreements agreements = new Agreements().with(new Agreements(bid1,
278 new HashSet<>(Arrays.asList(party1, party2))));
279 AMOPState state = new AMOPState(Phase.INIT, actions, connections3,
280 progressrounds, settings, partyprofiles1, agreements,
281 exceptions1, walkedaway0);
282 assertEquals(Arrays.asList(party3), state.getActiveParties());
283 }
284
285 @Test
286 public void activePartiesErrorTest() {
287 Map<PartyId, ProtocolException> exceptions = Collections
288 .singletonMap(party2, new ProtocolException("test", party2));
289 AMOPState state = new AMOPState(Phase.INIT, actions, connections3,
290 progressrounds, settings, partyprofiles1, agreements1,
291 exceptions, walkedaway0);
292 assertEquals(Arrays.asList(party1, party3), state.getActiveParties());
293 }
294
295 /************** isFinal tests *****************/
296 @Test
297 public void deadlineProgressTimeDeadlineTest() {
298 Progress progress = mock(Progress.class);
299 when(progress.isPastDeadline(any())).thenReturn(true);
300 AMOPState state = new AMOPState(Phase.VOTE, actions, connections,
301 progress, settings, partyprofiles1, agreements1, exceptions1,
302 walkedaway0);
303 assertTrue(state.isFinal(10));
304 }
305
306 @Test
307 public void deadlineProgressTestOnly1Active() {
308 Progress progress = mock(Progress.class);
309 when(progress.isPastDeadline(any())).thenReturn(false);
310 List<PartyId> walkedaway13 = Arrays.asList(party1, party3);
311
312 AMOPState state = new AMOPState(Phase.VOTE, actions, connections,
313 progress, settings, partyprofiles1, agreements1, exceptions1,
314 walkedaway13);
315 assertTrue(state.isFinal(10));
316 }
317
318 /*************
319 * action tests
320 ***************/
321 @Test
322 public void OfferTest() throws ProtocolException {
323 AMOPState state = new AMOPState(Phase.OFFER, actions, connections,
324 progresstime, settings, partyprofiles1, agreements1,
325 exceptions1, walkedaway0);
326 state = state.with(party1, new Offer(party1, bid1));
327 assertEquals(1, state.getActions().size());
328 }
329
330 @Test(expected = ProtocolException.class)
331 public void OfferTwiceTest() throws ProtocolException {
332 AMOPState state = new AMOPState(Phase.OFFER, actions, connections,
333 progresstime, settings, partyprofiles1, agreements1,
334 exceptions1, walkedaway0);
335 state = state.with(party1, new Offer(party1, bid1));
336 // party1 may offer only once
337 state = state.with(party1, new Offer(party1, bid1));
338 }
339
340 @Test
341 public void endNegoOfferTest() throws ProtocolException {
342 AMOPState state = new AMOPState(Phase.OFFER, actions, connections,
343 progresstime, settings, partyprofiles1, agreements1,
344 exceptions1, walkedaway0);
345 state = state.with(party1, new EndNegotiation(party1));
346 assertEquals(1, state.getActions().size());
347 assertEquals(1, state.getWalkedAway().size());
348 }
349
350 @Test
351 public void endNegoVoteTest() throws ProtocolException {
352 AMOPState state = new AMOPState(Phase.VOTE, actions, connections,
353 progresstime, settings, partyprofiles1, agreements1,
354 exceptions1, walkedaway0);
355 state = state.with(party1, new EndNegotiation(party1));
356 assertEquals(1, state.getActions().size());
357 assertEquals(1, state.getWalkedAway().size());
358 }
359
360 @Test(expected = ProtocolException.class)
361 public void OfferButInactive() throws ProtocolException {
362 // party walked away or error'ed and then votes again
363 AMOPState state = new AMOPState(Phase.OFFER, actions, connections,
364 progresstime, settings, partyprofiles1, agreements1,
365 exceptions1, walkedaway1);
366 state = state.with(party1, new Offer(party1, bid1));
367 }
368
369 /************** NextPhase tests *****************/
370 @Test
371 public void isInitToOfferWorking() throws ProtocolException {
372 AMOPState state = state1.nextPhase();
373 assertEquals(Phase.OFFER, state.getPhase());
374 }
375
376 @Test
377 public void testCollectVotes() {
378 Votes vote1AB = new Votes(party1, new HashSet<>(Arrays
379 .asList(new Vote(party1, a, 2, 9), new Vote(party1, b, 2, 9))));
380 Votes vote2AB = new Votes(party2, new HashSet<>(Arrays
381 .asList(new Vote(party2, a, 2, 9), new Vote(party2, b, 2, 9))));
382 Votes vote3C = new Votes(party3,
383 Collections.singleton(new Vote(party3, c, 2, 9)));
384 Votes vote4AC = new Votes(party4, new HashSet<>(Arrays
385 .asList(new Vote(party4, a, 2, 9), new Vote(party4, c, 2, 9))));
386 // party 1,2,4 vote for A, party 1,2 vote for B, party 3,4 vote for C.
387 // the biggest vote is P,Q,S
388 Map<PartyId, Votes> votes = new HashMap<>();
389 votes.put(party1, vote1AB);
390 votes.put(party2, vote2AB);
391 votes.put(party3, vote3C);
392 votes.put(party4, vote4AC);
393
394 List<Action> actions1 = Arrays.asList(vote1AB, vote2AB, vote3C,
395 vote4AC);
396 AMOPState state = new AMOPState(Phase.VOTE, actions1, connections,
397 progresstime, settings, partyprofiles1, agreements1,
398 exceptions1, walkedaway1);
399 Agreements agrees = state.collectVotes(votes);
400 System.out.println(agrees);
401
402 // biggest agreement is party 1,2,4 for bid a.
403 assertEquals(new HashSet<>(Arrays.asList(party1, party2, party4)),
404 agrees.getMap().keySet());
405
406 }
407
408 @Test
409 public void testNextPhase() {
410 // copy of above. todo cleanup
411 Votes vote1AB = new Votes(party1, new HashSet<>(Arrays
412 .asList(new Vote(party1, a, 2, 9), new Vote(party1, b, 2, 9))));
413 Votes vote2AB = new Votes(party2, new HashSet<>(Arrays
414 .asList(new Vote(party2, a, 2, 9), new Vote(party2, b, 2, 9))));
415 Votes vote3C = new Votes(party3,
416 Collections.singleton(new Vote(party3, c, 2, 9)));
417 Votes vote4AC = new Votes(party4, new HashSet<>(Arrays
418 .asList(new Vote(party4, a, 2, 9), new Vote(party4, c, 2, 9))));
419 // party 1,2,4 vote for A, party 1,2 vote for B, party 3,4 vote for C.
420 // the biggest vote is P,Q,S
421 Map<PartyId, Votes> votes = new HashMap<>();
422 votes.put(party1, vote1AB);
423 votes.put(party2, vote2AB);
424 votes.put(party3, vote3C);
425 votes.put(party4, vote4AC);
426
427 List<Action> actions1 = Arrays.asList(vote1AB, vote2AB, vote3C,
428 vote4AC);
429 AMOPState state = new AMOPState(Phase.VOTE, actions1, connections,
430 progressrounds, settings, partyprofiles1, agreements1,
431 exceptions1, walkedaway1);
432 state = state.nextPhase();
433 assertEquals(Phase.OFFER, state.getPhase());
434 assertEquals(new HashSet<>(Arrays.asList(party1, party2, party4)),
435 state.getAgreements().getMap().keySet());
436 assertEquals(progressrounds.advance(), state.getProgress());
437
438 }
439}
Note: See TracBrowser for help on using the repository browser.