1 | package geniusweb.protocol.session.mopac;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 | import static org.mockito.ArgumentMatchers.any;
|
---|
7 | import static org.mockito.ArgumentMatchers.eq;
|
---|
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.Collections;
|
---|
15 | import java.util.Date;
|
---|
16 | import java.util.HashSet;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Map;
|
---|
19 |
|
---|
20 | import org.junit.Before;
|
---|
21 | import org.junit.Test;
|
---|
22 |
|
---|
23 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
24 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
25 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
26 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
27 |
|
---|
28 | import geniusweb.actions.Action;
|
---|
29 | import geniusweb.actions.PartyId;
|
---|
30 | import geniusweb.progress.Progress;
|
---|
31 | import geniusweb.progress.ProgressTime;
|
---|
32 | import geniusweb.protocol.ProtocolException;
|
---|
33 | import geniusweb.protocol.session.TeamInfo;
|
---|
34 | import geniusweb.protocol.session.mopac.phase.OfferPhase;
|
---|
35 | import geniusweb.protocol.session.mopac.phase.Phase;
|
---|
36 | import geniusweb.references.Parameters;
|
---|
37 | import geniusweb.references.PartyWithParameters;
|
---|
38 | import geniusweb.references.PartyWithProfile;
|
---|
39 | import geniusweb.references.ProfileRef;
|
---|
40 | import geniusweb.voting.votingevaluators.LargestAgreement;
|
---|
41 | import tudelft.utilities.junit.GeneralTests;
|
---|
42 |
|
---|
43 | public class MOPACStateTest extends GeneralTests<MOPACState> {
|
---|
44 | private static final Date NOW = new Date(1001);
|
---|
45 |
|
---|
46 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
47 |
|
---|
48 | private final MOPACSettings settings = mock(MOPACSettings.class);
|
---|
49 |
|
---|
50 | private final MOPACState state = new MOPACState(settings);
|
---|
51 |
|
---|
52 | private PartyId PARTY1 = new PartyId("party1");
|
---|
53 | private PartyId PARTY2 = new PartyId("party2");
|
---|
54 | private PartyId PARTY3 = new PartyId("party3");
|
---|
55 |
|
---|
56 | private PartyWithParameters partyparams1 = mock(PartyWithParameters.class);
|
---|
57 | private Parameters params1 = new Parameters();
|
---|
58 |
|
---|
59 | private PartyWithProfile profile1 = new PartyWithProfile(partyparams1,
|
---|
60 | mock(ProfileRef.class));
|
---|
61 |
|
---|
62 | // copy from MopacSettingsTest. We need settings here but don't want to mock
|
---|
63 | // this large structure.
|
---|
64 | private final String mopacsettingstr = "{\"MOPACSettings\":{\"participants\":[{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party1\",\"parameters\":{}},\"profile\":\"http://profile1\"}]}},{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party2\",\"parameters\":{}},\"profile\":\"http://profile2\"}]}}],\"deadline\":{\"DeadlineTime\":{\"durationms\":100}},\"votingevaluator\":{\"LargestAgreement\":{}}}}";
|
---|
65 | // create real objects for the GeneralTests and for serialization tests
|
---|
66 | private PartyStates partyStates = new PartyStates(Collections.emptyMap());
|
---|
67 | private Phase phase1 = new OfferPhase(partyStates, 100l,
|
---|
68 | new LargestAgreement());
|
---|
69 | private Phase phase2 = new OfferPhase(partyStates, 200l,
|
---|
70 | new LargestAgreement());
|
---|
71 | private Progress progress1 = new ProgressTime(100l, NOW);
|
---|
72 | private Progress progress2 = new ProgressTime(200l, NOW);
|
---|
73 |
|
---|
74 | private MOPACState state1, state1a, state2, state3, state4, state5, state6;
|
---|
75 |
|
---|
76 | private String serialized = "{\"MOPACState\":{\"phase\":{\"OfferPhase\":{\"partyStates\":{\"powers\":{},\"notYetActed\":[],\"actions\":[],\"agreements\":{},\"walkedAway\":[],\"exceptions\":{}},\"deadline\":100,\"evaluator\":{\"LargestAgreement\":{}}}},\"actions\":[],\"progress\":{\"ProgressTime\":{\"duration\":100,\"start\":1001}},\"settings\":{\"MOPACSettings\":{\"participants\":[{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party1\",\"parameters\":{}},\"profile\":\"http://profile1\"}]}},{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party2\",\"parameters\":{}},\"profile\":\"http://profile2\"}]}}],\"deadline\":{\"DeadlineTime\":{\"durationms\":100}},\"votingevaluator\":{\"LargestAgreement\":{}}}},\"partyprofiles\":{}}}";
|
---|
77 |
|
---|
78 | @Before
|
---|
79 | public void before()
|
---|
80 | throws JsonParseException, JsonMappingException, IOException {
|
---|
81 | params1 = params1.with("power", 2);
|
---|
82 | when(partyparams1.getParameters()).thenReturn(params1);
|
---|
83 |
|
---|
84 | MOPACSettings realsettings = jackson.readValue(mopacsettingstr,
|
---|
85 | MOPACSettings.class);
|
---|
86 | TeamInfo party = mock(TeamInfo.class);
|
---|
87 | PartyWithParameters pwithp = mock(PartyWithParameters.class);
|
---|
88 | when(pwithp.getParameters()).thenReturn(new Parameters());
|
---|
89 | when(party.getParties()).thenReturn(Arrays
|
---|
90 | .asList(new PartyWithProfile(pwithp, mock(ProfileRef.class))));
|
---|
91 | when(party.getSize()).thenReturn(1);
|
---|
92 | MOPACSettings realsettings2 = realsettings.with(party);
|
---|
93 |
|
---|
94 | state1 = new MOPACState(phase1, Collections.emptyList(), progress1,
|
---|
95 | realsettings, Collections.emptyMap());
|
---|
96 | state1a = new MOPACState(phase1, Collections.emptyList(), progress1,
|
---|
97 | realsettings, Collections.emptyMap());
|
---|
98 | state2 = new MOPACState(phase2, Collections.emptyList(), progress1,
|
---|
99 | realsettings, Collections.emptyMap());
|
---|
100 | state3 = new MOPACState(phase1, Arrays.asList(mock(Action.class)),
|
---|
101 | progress1, realsettings, Collections.emptyMap());
|
---|
102 | state4 = new MOPACState(phase1, Collections.emptyList(), progress2,
|
---|
103 | realsettings, Collections.emptyMap());
|
---|
104 | state5 = new MOPACState(phase1, Collections.emptyList(), progress1,
|
---|
105 | realsettings2, Collections.emptyMap());
|
---|
106 | state6 = new MOPACState(phase1, Collections.emptyList(), progress1,
|
---|
107 | realsettings,
|
---|
108 | Collections.singletonMap(PARTY1, mock(PartyWithProfile.class)));
|
---|
109 |
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | public List<List<MOPACState>> getGeneralTestData() {
|
---|
114 | return Arrays.asList(Arrays.asList(state1, state1a),
|
---|
115 | Arrays.asList(state2), Arrays.asList(state3),
|
---|
116 | Arrays.asList(state4), Arrays.asList(state5),
|
---|
117 | Arrays.asList(state6));
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public List<String> getGeneralTestStrings() {
|
---|
122 | return Arrays.asList(
|
---|
123 | "MOPACState.*OfferPhase.*PartyStates.*,MOPACSettings.*PartyWithProfile.*ProgressTime.*",
|
---|
124 | "MOPACState.*OfferPhase.*PartyStates.*,MOPACSettings.*PartyWithProfile.*ProgressTime.*",
|
---|
125 | "MOPACState.*OfferPhase.*PartyStates.*,MOPACSettings.*PartyWithProfile.*ProgressTime.*",
|
---|
126 | "MOPACState.*OfferPhase.*PartyStates.*,MOPACSettings.*PartyWithProfile.*ProgressTime.*",
|
---|
127 | "MOPACState.*OfferPhase.*PartyStates.*,MOPACSettings.*PartyWithProfile.*ProgressTime.*",
|
---|
128 | "MOPACState.*OfferPhase.*PartyStates.*,MOPACSettings.*PartyWithProfile.*ProgressTime.*");
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Test
|
---|
132 | public void smokeTest() {
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Test
|
---|
136 | public void addProfilesBeforeInit() {
|
---|
137 | MOPACState st = state.with(PARTY1, profile1);
|
---|
138 | assertTrue(st.getPartyProfiles().containsKey(PARTY1));
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Test
|
---|
142 | public void testInit() {
|
---|
143 | // no parties were added, should throw? Go to final mode?
|
---|
144 | // Notice that it's possible to create settings with 0 participants,
|
---|
145 | // because this is needed for Tournament mechanism.
|
---|
146 | MOPACState state1 = state.initPhase(new ProgressTime(100l, new Date(0)),
|
---|
147 | 1);
|
---|
148 | assertTrue(state1.getActions().isEmpty());
|
---|
149 | assertTrue(state1.getAgreements().getMap().isEmpty());
|
---|
150 | assertTrue(state1.getPartyProfiles().isEmpty());
|
---|
151 | Phase phase1 = state1.getPhase();
|
---|
152 | assertTrue(phase1 instanceof OfferPhase);
|
---|
153 | OfferPhase offer1 = (OfferPhase) phase1;
|
---|
154 | assertEquals(new PartyStates(Collections.emptyMap()),
|
---|
155 | offer1.getPartyStates());
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Test
|
---|
159 | public void getAgreementsTest() {
|
---|
160 | MOPACState st = state.initPhase(new ProgressTime(100l, new Date(0)), 1);
|
---|
161 | assertTrue(st.getAgreements().getMap().isEmpty());
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Test
|
---|
165 | public void testDeserialize() throws IOException {
|
---|
166 | MOPACState obj = jackson.readValue(serialized, MOPACState.class);
|
---|
167 | System.out.println(obj);
|
---|
168 | assertEquals(state1, obj);
|
---|
169 | }
|
---|
170 |
|
---|
171 | @Test
|
---|
172 | public void testSerialize()
|
---|
173 | throws JsonProcessingException, URISyntaxException {
|
---|
174 |
|
---|
175 | String string = jackson.writeValueAsString(state1);
|
---|
176 | System.out.println(string);
|
---|
177 | assertEquals(serialized, string);
|
---|
178 | }
|
---|
179 |
|
---|
180 | @Test
|
---|
181 | public void testNewPhasePossible() {
|
---|
182 |
|
---|
183 | Phase phase = mock(Phase.class);
|
---|
184 | List<Action> actions = Collections.emptyList();
|
---|
185 | Progress progress = mock(Progress.class);
|
---|
186 | when(progress.getTerminationTime())
|
---|
187 | .thenReturn(new Date(Phase.PHASE_MAXTIME)); // plenty for new
|
---|
188 | // phase
|
---|
189 | when(progress.isPastDeadline(10 + Phase.PHASE_MINTIME))
|
---|
190 | .thenReturn(false);
|
---|
191 | Map<PartyId, PartyWithProfile> partyprofiles = mock(Map.class);
|
---|
192 | MOPACState s1 = new MOPACState(phase, actions, progress, settings,
|
---|
193 | partyprofiles);
|
---|
194 |
|
---|
195 | PartyStates states = mock(PartyStates.class);
|
---|
196 | when(phase.getPartyStates()).thenReturn(states);
|
---|
197 | when(states.getNegotiatingParties())
|
---|
198 | .thenReturn(new HashSet<>(Arrays.asList(PARTY1, PARTY2)));
|
---|
199 | assertTrue(s1.isNewPhasePossible(10));
|
---|
200 | }
|
---|
201 |
|
---|
202 | @Test
|
---|
203 | public void testNewPhasePossibleOnly1Party() {
|
---|
204 |
|
---|
205 | Phase phase = mock(Phase.class);
|
---|
206 | List<Action> actions = Collections.emptyList();
|
---|
207 | Progress progress = mock(Progress.class);
|
---|
208 | when(progress.getTerminationTime())
|
---|
209 | .thenReturn(new Date(Phase.PHASE_MAXTIME)); // plenty for new
|
---|
210 | // phase
|
---|
211 | Map<PartyId, PartyWithProfile> partyprofiles = mock(Map.class);
|
---|
212 | MOPACState s1 = new MOPACState(phase, actions, progress, settings,
|
---|
213 | partyprofiles);
|
---|
214 |
|
---|
215 | PartyStates states = mock(PartyStates.class);
|
---|
216 | when(phase.getPartyStates()).thenReturn(states);
|
---|
217 | when(states.getNegotiatingParties())
|
---|
218 | .thenReturn(new HashSet<>(Arrays.asList(PARTY1)));
|
---|
219 | assertFalse(s1.isNewPhasePossible(10));
|
---|
220 | }
|
---|
221 |
|
---|
222 | @Test
|
---|
223 | public void testNewPhasePossibleNotEnoughTime() {
|
---|
224 |
|
---|
225 | Phase phase = mock(Phase.class);
|
---|
226 | List<Action> actions = Collections.emptyList();
|
---|
227 | Progress progress = mock(Progress.class);
|
---|
228 | when(progress.getTerminationTime())
|
---|
229 | .thenReturn(new Date(Phase.PHASE_MINTIME - 10));
|
---|
230 | Map<PartyId, PartyWithProfile> partyprofiles = mock(Map.class);
|
---|
231 | MOPACState s1 = new MOPACState(phase, actions, progress, settings,
|
---|
232 | partyprofiles);
|
---|
233 |
|
---|
234 | PartyStates states = mock(PartyStates.class);
|
---|
235 | when(phase.getPartyStates()).thenReturn(states);
|
---|
236 | when(states.getNegotiatingParties())
|
---|
237 | .thenReturn(new HashSet<>(Arrays.asList(PARTY1)));
|
---|
238 | assertFalse(s1.isNewPhasePossible(10));
|
---|
239 | }
|
---|
240 |
|
---|
241 | @Test
|
---|
242 | public void nextPhaseTest() {
|
---|
243 | long NOW = 102;
|
---|
244 | Phase phase = mock(Phase.class);
|
---|
245 | List<Action> actions = Collections.emptyList();
|
---|
246 | Progress progress = mock(Progress.class);
|
---|
247 | when(progress.getTerminationTime())
|
---|
248 | .thenReturn(new Date(Phase.PHASE_MAXTIME));
|
---|
249 | Map<PartyId, PartyWithProfile> partyprofiles = mock(Map.class);
|
---|
250 | MOPACState s1 = new MOPACState(phase, actions, progress, settings,
|
---|
251 | partyprofiles);
|
---|
252 |
|
---|
253 | // PhaseB is the next phase, with remaining time
|
---|
254 | Phase phaseB = mock(Phase.class);
|
---|
255 | when(phase.next(eq(NOW), eq(Phase.PHASE_MAXTIME - NOW)))
|
---|
256 | .thenReturn(phaseB);
|
---|
257 |
|
---|
258 | MOPACState s2 = s1.nextPhase(NOW);
|
---|
259 | assertEquals(phaseB, s2.getPhase());
|
---|
260 | }
|
---|
261 |
|
---|
262 | @Test
|
---|
263 | public void exceptionTest() {
|
---|
264 | Phase phase = mock(Phase.class);
|
---|
265 | List<Action> actions = Collections.emptyList();
|
---|
266 | Progress progress = mock(Progress.class);
|
---|
267 | Map<PartyId, PartyWithProfile> partyprofiles = mock(Map.class);
|
---|
268 | MOPACState s1 = new MOPACState(phase, actions, progress, settings,
|
---|
269 | partyprofiles);
|
---|
270 |
|
---|
271 | Phase exphase = mock(Phase.class); // supposed phase that contains the
|
---|
272 | // exception
|
---|
273 | when(phase.with(any(ProtocolException.class))).thenReturn(exphase);
|
---|
274 |
|
---|
275 | MOPACState errstate = s1.with(new ProtocolException("test", PARTY1));
|
---|
276 | assertEquals(exphase, errstate.getPhase());
|
---|
277 | assertEquals(progress, errstate.getProgress());
|
---|
278 |
|
---|
279 | }
|
---|
280 |
|
---|
281 | }
|
---|