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