source: protocol/src/test/java/geniusweb/protocol/session/shaop/SHAOPStateTest.java@ 23

Last change on this file since 23 was 23, checked in by bart, 4 years ago

Version 1.5.2

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