1 | package geniusweb.protocol.session.mopac2.phase;
|
---|
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.junit.Assert.fail;
|
---|
7 | import static org.mockito.Mockito.mock;
|
---|
8 | import static org.mockito.Mockito.when;
|
---|
9 |
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.net.URISyntaxException;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 |
|
---|
18 | import org.junit.Test;
|
---|
19 |
|
---|
20 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
21 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
22 |
|
---|
23 | import geniusweb.actions.Accept;
|
---|
24 | import geniusweb.actions.EndNegotiation;
|
---|
25 | import geniusweb.actions.Offer;
|
---|
26 | import geniusweb.actions.PartyId;
|
---|
27 | import geniusweb.actions.Votes;
|
---|
28 | import geniusweb.inform.YourTurn;
|
---|
29 | import geniusweb.issuevalue.Bid;
|
---|
30 | import geniusweb.issuevalue.DiscreteValue;
|
---|
31 | import geniusweb.protocol.ProtocolException;
|
---|
32 | import geniusweb.protocol.session.mopac2.PartyStates;
|
---|
33 | import geniusweb.voting.VotingEvaluatorWithValue;
|
---|
34 | import geniusweb.voting.evaluatorwithvalue.LargestAgreementWithValue;
|
---|
35 | import tudelft.utilities.junit.GeneralTests;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * We also test defaultPhase here.
|
---|
39 | */
|
---|
40 | public class OfferPhaseTest extends GeneralTests<OfferPhase> {
|
---|
41 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
42 |
|
---|
43 | private static final long DEADLINE = 10l;
|
---|
44 | private final PartyId party1 = new PartyId("party1");
|
---|
45 | private final PartyId party2 = new PartyId("party2");
|
---|
46 | private final PartyId party3 = new PartyId("party3");
|
---|
47 |
|
---|
48 | private final Map<PartyId, Integer> powers = new HashMap<>();
|
---|
49 | private final PartyStates states, states2;
|
---|
50 | private final VotingEvaluatorWithValue evaluator = new LargestAgreementWithValue();
|
---|
51 | private final OfferPhase phase, phase1, phase1a, phase2, phase3;
|
---|
52 |
|
---|
53 | // just a test bid
|
---|
54 | private final Bid bid = new Bid("issue", new DiscreteValue("yes"));
|
---|
55 | private final Offer offer = new Offer(party1, bid);
|
---|
56 |
|
---|
57 | private final String serialized = "{\"OfferPhase\":{\"partyStates\":{\"notYetActed\":[\"party2\",\"party1\",\"party3\"],\"actions\":[],\"agreements\":{},\"walkedAway\":[],\"exceptions\":{},\"powers\":{\"party2\":3,\"party1\":2,\"party3\":3}},\"evaluator\":{\"LargestAgreementWithValue\":{}},\"deadline\":10}}";
|
---|
58 |
|
---|
59 | public OfferPhaseTest() {
|
---|
60 | powers.put(party1, 2);
|
---|
61 | powers.put(party2, 3);
|
---|
62 | states2 = new PartyStates(powers);
|
---|
63 | powers.put(party3, 3);
|
---|
64 | states = new PartyStates(powers);
|
---|
65 | phase = new OfferPhase(states, DEADLINE, evaluator);
|
---|
66 |
|
---|
67 | phase1 = new OfferPhase(states, 10l, evaluator);
|
---|
68 | phase1a = new OfferPhase(states, 10l, evaluator);
|
---|
69 | phase2 = new OfferPhase(states2, 10l, evaluator);
|
---|
70 | phase3 = new OfferPhase(states, 20l, evaluator);
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public List<List<OfferPhase>> getGeneralTestData() {
|
---|
76 | return Arrays.asList(Arrays.asList(phase1, phase1a),
|
---|
77 | Arrays.asList(phase2), Arrays.asList(phase3));
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public List<String> getGeneralTestStrings() {
|
---|
82 | return Arrays.asList(
|
---|
83 | "OfferPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
84 | "OfferPhase.*PartyStates.*party2, party1.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
85 | "OfferPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],20,LargestAgreement.*");
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Test
|
---|
89 | public void smokeTest() {
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Test
|
---|
93 | public void testInitState() {
|
---|
94 | assertEquals(3, phase.getPartyStates().getNotYetActed().size());
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Test
|
---|
98 | public void testInform() {
|
---|
99 | assertEquals(new YourTurn(), phase.getInform());
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Test
|
---|
103 | public void isFinalTest() {
|
---|
104 | assertFalse(phase.isFinal(1l));
|
---|
105 | assertTrue(phase.isFinal(10l));
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Test
|
---|
109 | public void isFinalTestActorNotYetActed() {
|
---|
110 | PartyStates actedstate = mock(PartyStates.class);
|
---|
111 | when(actedstate.getNotYetActed())
|
---|
112 | .thenReturn(Collections.singleton(party1));
|
---|
113 | OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator);
|
---|
114 | assertFalse(testphase.isFinal(1l));
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Test
|
---|
118 | public void isFinalTestAllActorsActed() {
|
---|
119 | PartyStates actedstate = mock(PartyStates.class);
|
---|
120 | when(actedstate.getNotYetActed()).thenReturn(Collections.emptySet());
|
---|
121 | OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator);
|
---|
122 | assertTrue(testphase.isFinal(1l));
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Test(expected = ProtocolException.class)
|
---|
126 | public void checkIncorrectActorTest() throws ProtocolException {
|
---|
127 | phase.checkAction(party1, new EndNegotiation(party2), 1);
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Test(expected = ProtocolException.class)
|
---|
131 | public void checkNotAllowedActionTest() throws ProtocolException {
|
---|
132 | phase.checkAction(party1, new Votes(party2, Collections.emptySet()), 1);
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Test
|
---|
136 | public void testException() {
|
---|
137 | OfferPhase newphase = phase.with(new ProtocolException("bla", party1));
|
---|
138 | assertEquals(10l, newphase.getDeadline());
|
---|
139 | assertEquals(0l, phase.getPartyStates().getActions().size());
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Test
|
---|
143 | public void testFinish() {
|
---|
144 | Phase ph = phase.finish();
|
---|
145 | assertTrue(ph.getPartyStates().getNotYetActed().isEmpty());
|
---|
146 | assertEquals(3, ph.getPartyStates().getExceptions().size());
|
---|
147 | assertTrue(phase.getPartyStates().getAgreements().getMap().isEmpty());
|
---|
148 | }
|
---|
149 |
|
---|
150 | @Test(expected = IllegalStateException.class)
|
---|
151 | public void testNextWrong() {
|
---|
152 | // state is not final, should not work
|
---|
153 | phase.next(1, 1000);
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Test
|
---|
157 | public void testNextNoParties() {
|
---|
158 | Phase ph = phase.finish();
|
---|
159 | // no parties are left now, all failed
|
---|
160 | // but this is not part of the next() check.
|
---|
161 | Phase next = ph.next(1, 1000);
|
---|
162 | assertTrue(next instanceof VotingPhase);
|
---|
163 | // no remaining parties, since finish kicked all
|
---|
164 | assertTrue(next.getPartyStates().getNotYetActed().isEmpty());
|
---|
165 | }
|
---|
166 |
|
---|
167 | @Test
|
---|
168 | public void testAllowed() {
|
---|
169 | phase.with(party1, offer, 1);
|
---|
170 | }
|
---|
171 |
|
---|
172 | @Test
|
---|
173 | public void testAllowedWrongClass() {
|
---|
174 | try {
|
---|
175 | phase.checkAction(party1, new Accept(party1, bid), 1);
|
---|
176 | } catch (ProtocolException e) {
|
---|
177 | assertTrue(e.getMessage().contains("not allowed in OfferPhase"));
|
---|
178 | return;
|
---|
179 | }
|
---|
180 | fail("checkAction did not throw as expected");
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Test
|
---|
184 | public void testAllowedWrongActor() {
|
---|
185 | try {
|
---|
186 | phase.checkAction(party1, new Accept(party2, bid), 1);
|
---|
187 | } catch (ProtocolException e) {
|
---|
188 | assertTrue(
|
---|
189 | e.getMessage().contains("Incorrect actor info in action"));
|
---|
190 | return;
|
---|
191 | }
|
---|
192 | fail("checkAction did not throw as expected");
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Test
|
---|
196 | public void testAllowedActorAlreadyActed() {
|
---|
197 | OfferPhase testphase = phase.with(party1, offer, 1);
|
---|
198 |
|
---|
199 | try {
|
---|
200 | testphase.checkAction(party1, offer, 2);
|
---|
201 | } catch (ProtocolException e) {
|
---|
202 | assertTrue(e.getMessage().contains("can not act anymore"));
|
---|
203 | return;
|
---|
204 | }
|
---|
205 | fail("checkAction did not throw as expected");
|
---|
206 | }
|
---|
207 |
|
---|
208 | @Test
|
---|
209 | public void testAllowedActorAlreadyActed1() {
|
---|
210 | // as theoprevious test, but using with() instead of checkAction
|
---|
211 | OfferPhase testphase = phase.with(party1, offer, 1);
|
---|
212 |
|
---|
213 | OfferPhase newphase = testphase.with(party1, offer, 2);
|
---|
214 | // the party must remain as acted, because we can't retract his
|
---|
215 | // action...
|
---|
216 | assertEquals(1, newphase.getPartyStates().getActions().size());
|
---|
217 | assertEquals(offer, newphase.getPartyStates().getActions().get(0));
|
---|
218 | assertFalse(
|
---|
219 | newphase.getPartyStates().getExceptions().containsKey(party1));
|
---|
220 | }
|
---|
221 |
|
---|
222 | @Test
|
---|
223 | public void testAllowedActingTooLate() {
|
---|
224 |
|
---|
225 | try {
|
---|
226 | phase.checkAction(party1, offer, DEADLINE + 1);
|
---|
227 | } catch (ProtocolException e) {
|
---|
228 | assertTrue(e.getMessage().contains("passed deadline"));
|
---|
229 | return;
|
---|
230 | }
|
---|
231 | fail("checkAction did not throw as expected");
|
---|
232 | }
|
---|
233 |
|
---|
234 | @Test
|
---|
235 | public void getOffersTest() {
|
---|
236 | assertEquals(Collections.emptyList(), phase.getOffers());
|
---|
237 | List<Offer> offs = phase.with(party1, offer, 1).getOffers();
|
---|
238 | assertEquals(1, offs.size());
|
---|
239 | assertEquals(bid, offs.get(0).getBid());
|
---|
240 | }
|
---|
241 |
|
---|
242 | @Test(expected = IllegalArgumentException.class)
|
---|
243 | public void testNextTooShortDuration() {
|
---|
244 | phase.next(1, Phase.PHASE_MINTIME - 1);
|
---|
245 | }
|
---|
246 |
|
---|
247 | @Test(expected = IllegalStateException.class)
|
---|
248 | public void testNextNotFinished() {
|
---|
249 | phase.next(1, Phase.PHASE_MINTIME + 1);
|
---|
250 | }
|
---|
251 |
|
---|
252 | @Test(expected = IllegalStateException.class)
|
---|
253 | public void testNext() {
|
---|
254 | phase.next(11, Phase.PHASE_MINTIME + 1);
|
---|
255 | }
|
---|
256 |
|
---|
257 | @Test
|
---|
258 | public void testDeserialize() throws IOException {
|
---|
259 | Phase obj = jackson.readValue(serialized, Phase.class);
|
---|
260 | System.out.println(obj);
|
---|
261 | assertEquals(phase1, obj);
|
---|
262 | }
|
---|
263 |
|
---|
264 | @Test
|
---|
265 | public void testSerialize()
|
---|
266 | throws JsonProcessingException, URISyntaxException {
|
---|
267 |
|
---|
268 | String string = jackson.writeValueAsString(phase1);
|
---|
269 | System.out.println(string);
|
---|
270 | assertEquals(serialized, string);
|
---|
271 | }
|
---|
272 | }
|
---|