source: protocol/src/test/java/geniusweb/protocol/session/mopac/MOPACStateTest.java@ 32

Last change on this file since 32 was 32, checked in by bart, 3 years ago

Multiple learns with repeated tournament, maven use https.

File size: 11.0 KB
Line 
1package geniusweb.protocol.session.mopac;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6import static org.mockito.Matchers.any;
7import static org.mockito.Matchers.eq;
8import static org.mockito.Mockito.mock;
9import static org.mockito.Mockito.when;
10
11import java.io.IOException;
12import java.net.URISyntaxException;
13import java.util.Arrays;
14import java.util.Collections;
15import java.util.Date;
16import java.util.HashSet;
17import java.util.List;
18import java.util.Map;
19
20import org.junit.Before;
21import org.junit.Test;
22
23import com.fasterxml.jackson.core.JsonParseException;
24import com.fasterxml.jackson.core.JsonProcessingException;
25import com.fasterxml.jackson.databind.JsonMappingException;
26import com.fasterxml.jackson.databind.ObjectMapper;
27
28import geniusweb.actions.Action;
29import geniusweb.actions.PartyId;
30import geniusweb.progress.Progress;
31import geniusweb.progress.ProgressTime;
32import geniusweb.protocol.ProtocolException;
33import geniusweb.protocol.session.TeamInfo;
34import geniusweb.protocol.session.mopac.phase.OfferPhase;
35import geniusweb.protocol.session.mopac.phase.Phase;
36import geniusweb.references.Parameters;
37import geniusweb.references.PartyWithParameters;
38import geniusweb.references.PartyWithProfile;
39import geniusweb.references.ProfileRef;
40import geniusweb.voting.votingevaluators.LargestAgreement;
41import tudelft.utilities.junit.GeneralTests;
42
43public 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\":{\"notYetActed\":[],\"actions\":[],\"agreements\":{},\"walkedAway\":[],\"exceptions\":{},\"powers\":{}},\"evaluator\":{\"LargestAgreement\":{}},\"deadline\":100}},\"actions\":[],\"progress\":{\"time\":{\"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.getAllParties()).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 Map<PartyId, PartyWithProfile> partyprofiles = mock(Map.class);
190 MOPACState s1 = new MOPACState(phase, actions, progress, settings,
191 partyprofiles);
192
193 PartyStates states = mock(PartyStates.class);
194 when(phase.getPartyStates()).thenReturn(states);
195 when(states.getNegotiatingParties())
196 .thenReturn(new HashSet<>(Arrays.asList(PARTY1, PARTY2)));
197 assertTrue(s1.isNewPhasePossible(10));
198 }
199
200 @Test
201 public void testNewPhasePossibleOnly1Party() {
202
203 Phase phase = mock(Phase.class);
204 List<Action> actions = Collections.emptyList();
205 Progress progress = mock(Progress.class);
206 when(progress.getTerminationTime())
207 .thenReturn(new Date(Phase.PHASE_MAXTIME)); // plenty for new
208 // phase
209 Map<PartyId, PartyWithProfile> partyprofiles = mock(Map.class);
210 MOPACState s1 = new MOPACState(phase, actions, progress, settings,
211 partyprofiles);
212
213 PartyStates states = mock(PartyStates.class);
214 when(phase.getPartyStates()).thenReturn(states);
215 when(states.getNegotiatingParties())
216 .thenReturn(new HashSet<>(Arrays.asList(PARTY1)));
217 assertFalse(s1.isNewPhasePossible(10));
218 }
219
220 @Test
221 public void testNewPhasePossibleNotEnoughTime() {
222
223 Phase phase = mock(Phase.class);
224 List<Action> actions = Collections.emptyList();
225 Progress progress = mock(Progress.class);
226 when(progress.getTerminationTime())
227 .thenReturn(new Date(Phase.PHASE_MINTIME - 10));
228 Map<PartyId, PartyWithProfile> partyprofiles = mock(Map.class);
229 MOPACState s1 = new MOPACState(phase, actions, progress, settings,
230 partyprofiles);
231
232 PartyStates states = mock(PartyStates.class);
233 when(phase.getPartyStates()).thenReturn(states);
234 when(states.getNegotiatingParties())
235 .thenReturn(new HashSet<>(Arrays.asList(PARTY1)));
236 assertFalse(s1.isNewPhasePossible(10));
237 }
238
239 @Test
240 public void nextPhaseTest() {
241 long NOW = 102;
242 Phase phase = mock(Phase.class);
243 List<Action> actions = Collections.emptyList();
244 Progress progress = mock(Progress.class);
245 when(progress.getTerminationTime())
246 .thenReturn(new Date(Phase.PHASE_MAXTIME));
247 Map<PartyId, PartyWithProfile> partyprofiles = mock(Map.class);
248 MOPACState s1 = new MOPACState(phase, actions, progress, settings,
249 partyprofiles);
250
251 // PhaseB is the next phase, with remaining time
252 Phase phaseB = mock(Phase.class);
253 when(phase.next(eq(NOW), eq(Phase.PHASE_MAXTIME - NOW)))
254 .thenReturn(phaseB);
255
256 MOPACState s2 = s1.nextPhase(NOW);
257 assertEquals(phaseB, s2.getPhase());
258 }
259
260 @Test
261 public void exceptionTest() {
262 Phase phase = mock(Phase.class);
263 List<Action> actions = Collections.emptyList();
264 Progress progress = mock(Progress.class);
265 Map<PartyId, PartyWithProfile> partyprofiles = mock(Map.class);
266 MOPACState s1 = new MOPACState(phase, actions, progress, settings,
267 partyprofiles);
268
269 Phase exphase = mock(Phase.class); // supposed phase that contains the
270 // exception
271 when(phase.with(any(ProtocolException.class))).thenReturn(exphase);
272
273 MOPACState errstate = s1.with(new ProtocolException("test", PARTY1));
274 assertEquals(exphase, errstate.getPhase());
275 assertEquals(progress, errstate.getProgress());
276
277 }
278
279}
Note: See TracBrowser for help on using the repository browser.