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

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

Update to version 1.41

File size: 11.0 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.assertNull;
7import static org.junit.Assert.assertTrue;
8import static org.mockito.Mockito.mock;
9import static org.mockito.Mockito.when;
10
11import java.util.Arrays;
12import java.util.Date;
13import java.util.HashMap;
14import java.util.LinkedList;
15import java.util.List;
16import java.util.Map;
17
18import org.junit.Before;
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.TeamOfPartiesAndProfiles;
34import tudelft.utilities.junit.GeneralTests;
35
36public class SHAOPStateTest extends GeneralTests<SHAOPState> {
37 private final long NOW = 1000;
38
39 private PartyId party1 = new PartyId("party1");
40 private PartyId party2 = new PartyId("party2");
41 private PartyId party3 = new PartyId("party3");
42 private PartyId party4 = new PartyId("party4");
43 private List<Action> actions = new LinkedList<>();
44 private Action action = mock(Action.class);
45 private List<Action> actions2 = Arrays.asList(action);
46
47 private ProtocolToPartyConn party1conn = mock(ProtocolToPartyConn.class);
48 private ProtocolToPartyConn party2conn = mock(ProtocolToPartyConn.class);
49 private ProtocolToPartyConn party3conn = mock(ProtocolToPartyConn.class);
50 private ProtocolToPartyConn party4conn = mock(ProtocolToPartyConn.class);
51
52 private ProtocolToPartyConnections connections = new ProtocolToPartyConnections(
53 Arrays.asList(party1conn, party2conn));
54 private ProtocolToPartyConnections connections2 = new ProtocolToPartyConnections(
55 Arrays.asList(party1conn, party3conn));
56 private ProtocolToPartyConnections connections3 = new ProtocolToPartyConnections(
57 Arrays.asList(party1conn, party2conn, party3conn));
58 private ProtocolToPartyConnections connections4 = new ProtocolToPartyConnections(
59 Arrays.asList(party1conn, party2conn, party3conn, party4conn));
60
61 private Progress progresstime = mock(ProgressTime.class);
62 private ProgressRounds progressrounds = mock(ProgressRounds.class);
63 private ProgressRounds progressrounds1 = mock(ProgressRounds.class);
64 private SHAOPSettings settings = mock(SHAOPSettings.class);
65 private SHAOPState state1, state1a, state2, state3, state4;
66
67 private Bid bid1 = mock(Bid.class);
68 private Bid otherbid = mock(Bid.class);
69 private Accept accept1 = new Accept(party1, bid1);
70 private Accept accept2 = new Accept(party2, bid1);
71 private Accept acceptother = new Accept(party2, otherbid);
72 private Offer offer1 = new Offer(party1, bid1);
73 private EndNegotiation endNegotiation1 = new EndNegotiation(party1);
74 private Map<PartyId, Integer> partynrs = new HashMap<>();
75
76 private TeamOfPartiesAndProfiles team1 = mock(
77 TeamOfPartiesAndProfiles.class);
78 private TeamOfPartiesAndProfiles team2 = mock(
79 TeamOfPartiesAndProfiles.class);
80
81 @Before
82 public void before() {
83 when(party1conn.getParty()).thenReturn(party1);
84 when(party2conn.getParty()).thenReturn(party2);
85 when(party3conn.getParty()).thenReturn(party3);
86 when(party1conn.toString()).thenReturn("conn1");
87 when(party2conn.toString()).thenReturn("conn2");
88 when(party3conn.toString()).thenReturn("conn3");
89 when(party4conn.toString()).thenReturn("conn4");
90 when(action.toString()).thenReturn("act1");
91 when(settings.toString()).thenReturn("settings");
92 when(progressrounds.toString()).thenReturn("progressrounds");
93 when(progresstime.toString()).thenReturn("progresstime");
94
95 when(progressrounds.getTerminationTime())
96 .thenReturn(new Date(System.currentTimeMillis() + 3600000l));
97 when(progressrounds1.getTerminationTime())
98 .thenReturn(new Date(System.currentTimeMillis() + 3600000l));
99 when(progressrounds.advance()).thenReturn(progressrounds1);
100
101 partynrs.put(party1, 0);
102 partynrs.put(party2, 1);
103 partynrs.put(party3, 2);
104 partynrs.put(party4, 3);
105 Map<PartyId, Double> totalspent = new HashMap<>();
106
107 state1 = new SHAOPState(actions, connections, progresstime, settings,
108 null, 0, partynrs, totalspent);
109 state1a = new SHAOPState(actions, connections, progresstime, settings,
110 null, 0, partynrs, totalspent);
111 state2 = new SHAOPState(actions2, connections, progresstime, settings,
112 null, 0, partynrs, totalspent);
113 state3 = new SHAOPState(actions, connections, progressrounds, settings,
114 null, 0, partynrs, totalspent);
115 state4 = new SHAOPState(actions, connections, progresstime, settings,
116 null, 1, partynrs, totalspent);
117
118 when(settings.getTeams()).thenReturn(Arrays.asList(team1, team2));
119
120 }
121
122 @Override
123 public List<List<SHAOPState>> getGeneralTestData() {
124 return Arrays.asList(Arrays.asList(state1, state1a),
125 Arrays.asList(state2), Arrays.asList(state3),
126 Arrays.asList(state4));
127 }
128
129 @Override
130 public List<String> getGeneralTestStrings() {
131 return Arrays.asList(
132 "SHAOPState.*\\[\\].*ConnectionWithParties.*conn1.*conn2.*progresstime.*settings.*",
133 "SHAOPState.*\\[act1\\].*ConnectionWithParties.*conn1.*conn2.*progresstime.*settings.*",
134 "SHAOPState.*\\[\\].*ConnectionWithParties.*conn1.*conn2.*progressrounds.*settings.*",
135 "SHAOPState.*\\[\\].*ConnectionWithParties.*conn1.*conn2.*progresstime.*settings.*");
136 }
137
138 @Test
139 public void constructWith0Connection() {
140 new SHAOPState(actions, new ProtocolToPartyConnections(Arrays.asList()),
141 progresstime, settings, null, 0, null, null);
142 }
143
144 @Test
145 public void constructWithNullConnection() {
146 SHAOPState state = new SHAOPState(actions, null, progresstime, settings,
147 null, 0, null, null);
148 assertEquals(0, state.getConnections().size());
149 }
150
151 @Test
152 public void testCurrentActor() {
153 assertEquals(party1, state1.getCurrentTeam());
154 }
155
156 @Test
157 public void testNextActor2() {
158 ProtocolToPartyConnections conns = new ProtocolToPartyConnections(
159 Arrays.asList(party2conn, party3conn));
160 SHAOPState state = new SHAOPState(actions, conns, progresstime,
161 settings, null, 0, partynrs, null);
162 assertEquals(party2, state.getCurrentTeam());
163 }
164
165 @Test
166 public void testNextActorAfterAction() {
167 List<Action> actions = Arrays.asList(action);
168 SHAOPState state = new SHAOPState(actions, connections, progresstime,
169 settings, null, 0, null, null);
170 assertEquals(party1, state.getCurrentTeam());
171 }
172
173 @Test
174 public void isFinalTest() {
175 assertFalse(state1.isFinal(NOW));
176 SHAOPState state = new SHAOPState(actions, connections, progresstime,
177 settings, mock(ProtocolException.class), 0, null, null);
178 assertTrue(state.isFinal(NOW));
179 }
180
181 @Test
182 public void withExceptionTest() {
183 SHAOPState finalstate1 = state1.with(mock(ProtocolException.class));
184 assertTrue(finalstate1.isFinal(NOW));
185 SHAOPState finalstate2 = finalstate1
186 .with(mock(ProtocolException.class));
187 assertTrue(finalstate2.isFinal(NOW));
188 }
189
190 @Test
191 public void isOfferAgreementTest1() {
192 List<Action> actions = Arrays.asList(offer1);
193 SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
194 settings, null, 0, null, null);
195 assertNull(state.getAgreement());
196 }
197
198 @Test
199 public void isOfferAcceptAgreementTest() {
200 List<Action> actions = Arrays.asList(offer1, accept2);
201 SHAOPState state = new SHAOPState(actions, connections4, progressrounds,
202 settings, null, 0, null, null);
203 assertNotNull(state.getAgreement());
204 assertTrue(state.isFinal(NOW));
205 }
206
207 @Test
208 public void isOfferAcceptOfferAgreementTest() {
209 List<Action> actions = Arrays.asList(offer1, accept1, offer1);
210 SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
211 settings, null, 0, null, null);
212 assertNull(state.getAgreement());
213 }
214
215 @Test
216 public void isOfferAcceptAcceptotherAgreementTest() {
217 List<Action> actions = Arrays.asList(offer1, accept1, acceptother);
218 SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
219 settings, null, 0, null, null);
220 assertEquals(null, state.getAgreement());
221 }
222
223 @Test(expected = IllegalArgumentException.class)
224 public void withWrongActionTest() {
225 // wrong because action has actor null which does not match party1
226 SHAOPState state = state1.with(party1, action);
227 }
228
229 @Test(expected = IllegalArgumentException.class)
230 public void withActionTest() {
231 when(action.getActor()).thenReturn(party1);
232 state1.with(party1, action);
233 }
234
235 @Test
236 public void withConnectionTest() {
237 SHAOPState state = state1.with(party3conn);
238 assertEquals(3, state.getConnections().size());
239 }
240
241 @Test
242 public void isEndNegotiationFinalTest() {
243 List<Action> actions = Arrays.asList(endNegotiation1);
244 SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
245 settings, null, 0, null, null);
246 assertEquals(null, state.getAgreement());
247 assertEquals(null, state.getError());
248 assertTrue(state.isFinal(NOW));
249
250 }
251
252 @Test
253 public void isProtocolErrorFinal() {
254 SHAOPState state = new SHAOPState(actions, connections3, progressrounds,
255 settings, new ProtocolException("test error", "test"), 0, null,
256 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}
Note: See TracBrowser for help on using the repository browser.