1 | package geniusweb.protocol.session.saop;
|
---|
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.assertNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 | import static org.mockito.Mockito.mock;
|
---|
9 | import static org.mockito.Mockito.when;
|
---|
10 |
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.net.URISyntaxException;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.Date;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | import org.junit.Before;
|
---|
19 | import org.junit.Test;
|
---|
20 |
|
---|
21 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
22 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
23 |
|
---|
24 | import geniusweb.actions.Accept;
|
---|
25 | import geniusweb.actions.Action;
|
---|
26 | import geniusweb.actions.EndNegotiation;
|
---|
27 | import geniusweb.actions.Offer;
|
---|
28 | import geniusweb.actions.PartyId;
|
---|
29 | import geniusweb.deadline.Deadline;
|
---|
30 | import geniusweb.deadline.DeadlineTime;
|
---|
31 | import geniusweb.issuevalue.Bid;
|
---|
32 | import geniusweb.progress.Progress;
|
---|
33 | import geniusweb.progress.ProgressRounds;
|
---|
34 | import geniusweb.progress.ProgressTime;
|
---|
35 | import geniusweb.protocol.NegoState;
|
---|
36 | import geniusweb.protocol.ProtocolException;
|
---|
37 | import geniusweb.protocol.partyconnection.ProtocolToPartyConn;
|
---|
38 | import geniusweb.protocol.session.TeamInfo;
|
---|
39 | import geniusweb.references.Parameters;
|
---|
40 | import geniusweb.references.PartyRef;
|
---|
41 | import geniusweb.references.PartyWithParameters;
|
---|
42 | import geniusweb.references.PartyWithProfile;
|
---|
43 | import geniusweb.references.ProfileRef;
|
---|
44 | import tudelft.utilities.junit.GeneralTests;
|
---|
45 |
|
---|
46 | public class SAOPStateTest extends GeneralTests<SAOPState> {
|
---|
47 | private final long NOW = 1000;
|
---|
48 |
|
---|
49 | private PartyId party1 = new PartyId("party1");
|
---|
50 | private PartyId party2 = new PartyId("party2");
|
---|
51 | private PartyId party3 = new PartyId("party3");
|
---|
52 | private List<Action> actions1 = new LinkedList<>();
|
---|
53 | private Action action = mock(Action.class);
|
---|
54 | private List<Action> actions2 = Arrays.asList(action);
|
---|
55 |
|
---|
56 | private ProtocolToPartyConn party1conn = mock(ProtocolToPartyConn.class);
|
---|
57 | private ProtocolToPartyConn party2conn = mock(ProtocolToPartyConn.class);
|
---|
58 | private ProtocolToPartyConn party3conn = mock(ProtocolToPartyConn.class);
|
---|
59 |
|
---|
60 | private List<PartyId> connections = Arrays.asList(party1, party2);
|
---|
61 | private List<PartyId> connections2 = Arrays.asList(party1, party3);
|
---|
62 | private List<PartyId> connections3 = Arrays.asList(party1, party2, party3);
|
---|
63 |
|
---|
64 | private Progress progresstime = mock(ProgressTime.class);
|
---|
65 | private ProgressRounds progressrounds = mock(ProgressRounds.class);
|
---|
66 | private ProgressRounds progressrounds1 = mock(ProgressRounds.class);
|
---|
67 | private SAOPSettings settings = mock(SAOPSettings.class);
|
---|
68 | private SAOPState state, state1, state1a, state2, state3, state4;
|
---|
69 |
|
---|
70 | private Bid bid1 = mock(Bid.class);
|
---|
71 | private Bid otherbid = mock(Bid.class);
|
---|
72 | private Accept accept1 = new Accept(party1, bid1);
|
---|
73 | private Accept accept2 = new Accept(party2, bid1);
|
---|
74 | private Accept acceptother = new Accept(party2, otherbid);
|
---|
75 | private Offer offer1 = new Offer(party1, bid1);
|
---|
76 | private EndNegotiation endNegotiation1 = new EndNegotiation(party1);
|
---|
77 |
|
---|
78 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
79 |
|
---|
80 | private String serialized = "{\"SAOPState\":{\"actions\":[],\"connections\":[\"party1\",\"party2\"],\"progress\":{\"ProgressTime\":{\"duration\":1000000,\"start\":2000000}},\"settings\":{\"SAOPSettings\":{\"participants\":[{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"party1ref\",\"parameters\":{}},\"profile\":\"prof1\"}]}}],\"deadline\":{\"DeadlineTime\":{\"durationms\":123000}}}},\"partyprofiles\":{},\"error\":null}}";
|
---|
81 |
|
---|
82 | @Before
|
---|
83 | public void before() throws URISyntaxException {
|
---|
84 | when(party1conn.getParty()).thenReturn(party1);
|
---|
85 | when(party2conn.getParty()).thenReturn(party2);
|
---|
86 | when(party3conn.getParty()).thenReturn(party3);
|
---|
87 | when(party1conn.toString()).thenReturn("conn1");
|
---|
88 | when(party2conn.toString()).thenReturn("conn2");
|
---|
89 | when(party3conn.toString()).thenReturn("conn3");
|
---|
90 | when(action.toString()).thenReturn("act1");
|
---|
91 |
|
---|
92 | when(progressrounds.getTerminationTime())
|
---|
93 | .thenReturn(new Date(System.currentTimeMillis() + 3600000l));
|
---|
94 | when(progressrounds1.getTerminationTime())
|
---|
95 | .thenReturn(new Date(System.currentTimeMillis() + 3600000l));
|
---|
96 | when(progressrounds.advance()).thenReturn(progressrounds1);
|
---|
97 |
|
---|
98 | state1 = new SAOPState(actions1, connections, progresstime, settings,
|
---|
99 | null, null);
|
---|
100 | state1a = new SAOPState(actions1, connections, progresstime, settings,
|
---|
101 | null, null);
|
---|
102 | state2 = new SAOPState(actions2, connections, progresstime, settings,
|
---|
103 | null, null);
|
---|
104 | state3 = new SAOPState(actions1, connections2, progresstime, settings,
|
---|
105 | null, null);
|
---|
106 | state4 = new SAOPState(actions1, connections, progressrounds, settings,
|
---|
107 | null, null);
|
---|
108 |
|
---|
109 | List<PartyId> conns = Arrays.asList(party1, party2);
|
---|
110 | PartyWithParameters pwithp = new PartyWithParameters(
|
---|
111 | new PartyRef("party1ref"), new Parameters());
|
---|
112 | List<PartyWithProfile> teams = Arrays
|
---|
113 | .asList(new PartyWithProfile(pwithp, new ProfileRef("prof1")));
|
---|
114 | List<TeamInfo> participants = Arrays.asList(new TeamInfo(teams));
|
---|
115 | Deadline deadline = new DeadlineTime(123000);
|
---|
116 | SAOPSettings setts = new SAOPSettings(participants, deadline);
|
---|
117 | state = new SAOPState(actions1, conns,
|
---|
118 | new ProgressTime(1000000l, new Date(2000000l)), setts, null,
|
---|
119 | null);
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public List<List<SAOPState>> getGeneralTestData() {
|
---|
125 | return Arrays.asList(Arrays.asList(state1, state1a),
|
---|
126 | Arrays.asList(state2), Arrays.asList(state3),
|
---|
127 | Arrays.asList(state4));
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Override
|
---|
131 | public List<String> getGeneralTestStrings() {
|
---|
132 | return Arrays.asList(
|
---|
133 | "SAOPState.*\\[\\].*party1.*party2.*ProgressTime.*",
|
---|
134 | "SAOPState.*\\[act1\\].*party1.*party2.*ProgressTime.*",
|
---|
135 | "SAOPState.*party1.*party3.*ProgressTime.*",
|
---|
136 | "SAOPState.*party1.*party2.*ProgressRounds.*");
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Test
|
---|
140 | public void constructWith0Connection() {
|
---|
141 | new SAOPState(actions1, Arrays.asList(), progresstime, settings, null,
|
---|
142 | null);
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Test
|
---|
146 | public void constructWithNullConnection() {
|
---|
147 | SAOPState state = new SAOPState(actions1, null, progresstime, settings,
|
---|
148 | null, null);
|
---|
149 | assertEquals(0, state.getConnections().size());
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Test
|
---|
153 | public void testNextActor() {
|
---|
154 | assertEquals(party1, state1.getNextActor());
|
---|
155 | }
|
---|
156 |
|
---|
157 | @Test
|
---|
158 | public void testNextActor2() {
|
---|
159 | List<PartyId> conns = Arrays.asList(party2, party3);
|
---|
160 | SAOPState state = new SAOPState(actions1, conns, progresstime, settings,
|
---|
161 | null, null);
|
---|
162 | assertEquals(party2, state.getNextActor());
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Test
|
---|
166 | public void testNextActorAfterAction() {
|
---|
167 | List<Action> actions = Arrays.asList(action);
|
---|
168 | SAOPState state = new SAOPState(actions, connections, progresstime,
|
---|
169 | settings, null, null);
|
---|
170 | assertEquals(party2, state.getNextActor());
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Test
|
---|
174 | public void testNextActorAfter2Actions() {
|
---|
175 | List<Action> actions = Arrays.asList(action, action);
|
---|
176 | SAOPState state = new SAOPState(actions, connections, progresstime,
|
---|
177 | settings, null, null);
|
---|
178 | assertEquals(party1, state.getNextActor());
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Test
|
---|
182 | public void isFinalTest() {
|
---|
183 | assertFalse(state1.isFinal(NOW));
|
---|
184 | SAOPState state = new SAOPState(actions1, connections, progresstime,
|
---|
185 | settings, null, mock(ProtocolException.class));
|
---|
186 | assertTrue(state.isFinal(NOW));
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Test
|
---|
190 | public void withExceptionTest() {
|
---|
191 | SAOPState finalstate1 = state1.with(mock(ProtocolException.class));
|
---|
192 | assertTrue(finalstate1.isFinal(NOW));
|
---|
193 | SAOPState finalstate2 = finalstate1.with(mock(ProtocolException.class));
|
---|
194 | assertTrue(finalstate2.isFinal(NOW));
|
---|
195 | }
|
---|
196 |
|
---|
197 | @Test
|
---|
198 | public void isOfferAgreementTest1() {
|
---|
199 | List<Action> actions = Arrays.asList(offer1);
|
---|
200 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
201 | settings, null, null);
|
---|
202 | assertTrue(state.getAgreements().getMap().isEmpty());
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Test
|
---|
206 | public void isOfferAcceptAgreementTest() {
|
---|
207 | List<Action> actions = Arrays.asList(offer1, accept2);
|
---|
208 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
209 | settings, null, null);
|
---|
210 | assertTrue(state.getAgreements().getMap().isEmpty());
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Test
|
---|
214 | public void isOfferAcceptOfferAgreementTest() {
|
---|
215 | List<Action> actions = Arrays.asList(offer1, accept1, offer1);
|
---|
216 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
217 | settings, null, null);
|
---|
218 | assertTrue(state.getAgreements().getMap().isEmpty());
|
---|
219 | }
|
---|
220 |
|
---|
221 | @Test
|
---|
222 | public void isOfferAcceptAcceptAgreementTest() {
|
---|
223 | List<Action> actions = Arrays.asList(offer1, accept1, accept2);
|
---|
224 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
225 | settings, null, null);
|
---|
226 | assertEquals(bid1, state.getAgreements().getMap().get(party1));
|
---|
227 | assertTrue(state.isFinal(NOW));
|
---|
228 | }
|
---|
229 |
|
---|
230 | @Test
|
---|
231 | public void isAcceptOfferAcceptAgreementTest() {
|
---|
232 | List<Action> actions = Arrays.asList(accept1, offer1, accept2);
|
---|
233 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
234 | settings, null, null);
|
---|
235 | assertTrue(state.getAgreements().getMap().isEmpty());
|
---|
236 | }
|
---|
237 |
|
---|
238 | @Test
|
---|
239 | public void isOfferAcceptAcceptotherAgreementTest() {
|
---|
240 | List<Action> actions = Arrays.asList(offer1, accept1, acceptother);
|
---|
241 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
242 | settings, null, null);
|
---|
243 | assertTrue(state.getAgreements().getMap().isEmpty());
|
---|
244 | }
|
---|
245 |
|
---|
246 | @Test(expected = IllegalArgumentException.class)
|
---|
247 | public void withActionTest() {
|
---|
248 | when(action.getActor()).thenReturn(party1);
|
---|
249 | state1.with(party1, action);
|
---|
250 | }
|
---|
251 |
|
---|
252 | @Test
|
---|
253 | public void withConnectionTest() {
|
---|
254 | SAOPState state = state1.with(party3, (PartyWithProfile) null);
|
---|
255 | assertEquals(3, state.getConnections().size());
|
---|
256 | }
|
---|
257 |
|
---|
258 | @Test
|
---|
259 | public void isEndNegotiationFinalTest() {
|
---|
260 | List<Action> actions = Arrays.asList(endNegotiation1);
|
---|
261 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
262 | settings, null, null);
|
---|
263 | assertTrue(state.getAgreements().getMap().isEmpty());
|
---|
264 | assertEquals(null, state.getError());
|
---|
265 | assertTrue(state.isFinal(NOW));
|
---|
266 |
|
---|
267 | }
|
---|
268 |
|
---|
269 | @Test
|
---|
270 | public void isProtocolErrorFinal() {
|
---|
271 | SAOPState state = new SAOPState(actions1, connections3, progressrounds,
|
---|
272 | settings, null,
|
---|
273 | new ProtocolException("test error", new PartyId("test")));
|
---|
274 | assertTrue(state.isFinal(NOW));
|
---|
275 | assertNotNull(state.getError());
|
---|
276 | }
|
---|
277 |
|
---|
278 | @Test(expected = IllegalArgumentException.class)
|
---|
279 | public void RefuseImmediateAccept() {
|
---|
280 | Accept nullaccept = new Accept(party1, null);
|
---|
281 | state1.with(party1, nullaccept);
|
---|
282 | }
|
---|
283 |
|
---|
284 | @Test(expected = IllegalArgumentException.class)
|
---|
285 | public void RefuseNotMyTurn() {
|
---|
286 | List<Action> actions = Arrays.asList(offer1);
|
---|
287 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
288 | settings, null, null);
|
---|
289 |
|
---|
290 | state.with(party1, offer1);
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Test(expected = IllegalArgumentException.class)
|
---|
294 | public void RefuseNullAccept() {
|
---|
295 | List<Action> actions = Arrays.asList(offer1);
|
---|
296 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
297 | settings, null, null);
|
---|
298 |
|
---|
299 | Accept nullaccept = new Accept(party2, null);
|
---|
300 | state = state.with(party2, nullaccept);
|
---|
301 | assertTrue(state.isFinal(NOW));
|
---|
302 | }
|
---|
303 |
|
---|
304 | @Test(expected = IllegalArgumentException.class)
|
---|
305 | public void RefuseNullAction() {
|
---|
306 | List<Action> actions = Arrays.asList(offer1);
|
---|
307 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
308 | settings, null, null);
|
---|
309 |
|
---|
310 | state = state.with(party2, (Action) null);
|
---|
311 | }
|
---|
312 |
|
---|
313 | @Test
|
---|
314 | public void checkGoodAccept() {
|
---|
315 | List<Action> actions = Arrays.asList(offer1, accept2);
|
---|
316 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
317 | settings, null, null);
|
---|
318 |
|
---|
319 | Accept accept = new Accept(party3, bid1);
|
---|
320 | state = state.with(party3, accept);
|
---|
321 | assertTrue(state.isFinal(NOW));
|
---|
322 | assertNull(state.getError());
|
---|
323 | assertEquals(bid1, state.getAgreements().getMap().get(party1));
|
---|
324 |
|
---|
325 | }
|
---|
326 |
|
---|
327 | @Test
|
---|
328 | public void checkGoodSecondAccept() {
|
---|
329 | Bid bid2 = mock(Bid.class);
|
---|
330 | List<Action> actions = Arrays.asList(offer1, new Offer(party2, bid2),
|
---|
331 | new Accept(party3, bid2));
|
---|
332 | SAOPState state = new SAOPState(actions, connections3, progressrounds,
|
---|
333 | settings, null, null);
|
---|
334 |
|
---|
335 | Accept accept = new Accept(party1, bid2);
|
---|
336 | state = state.with(party1, accept);
|
---|
337 | assertTrue(state.isFinal(NOW));
|
---|
338 | assertNull(state.getError());
|
---|
339 | assertEquals(bid2, state.getAgreements().getMap().get(party1));
|
---|
340 |
|
---|
341 | }
|
---|
342 |
|
---|
343 | @Test
|
---|
344 | public void testDeserialize() throws IOException {
|
---|
345 | NegoState obj = jackson.readValue(serialized, NegoState.class);
|
---|
346 | System.out.println(obj);
|
---|
347 | assertEquals(state, obj);
|
---|
348 | }
|
---|
349 |
|
---|
350 | @Test
|
---|
351 | public void testSerialize()
|
---|
352 | throws JsonProcessingException, URISyntaxException {
|
---|
353 |
|
---|
354 | String string = jackson.writeValueAsString(state);
|
---|
355 | System.out.println(string);
|
---|
356 | assertEquals(serialized, string);
|
---|
357 | }
|
---|
358 |
|
---|
359 | }
|
---|