source: protocol/src/test/java/geniusweb/protocol/session/mopac/phase/OfferPhaseTest.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

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