1 | package geniusweb.protocol.session.mopac.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.mockito.Mockito.mock;
|
---|
7 | import static org.mockito.Mockito.when;
|
---|
8 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.net.URISyntaxException;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.Collections;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 |
|
---|
17 | import org.junit.Test;
|
---|
18 |
|
---|
19 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
20 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
21 |
|
---|
22 | import geniusweb.actions.EndNegotiation;
|
---|
23 | import geniusweb.actions.Offer;
|
---|
24 | import geniusweb.actions.PartyId;
|
---|
25 | import geniusweb.actions.Vote;
|
---|
26 | import geniusweb.actions.Votes;
|
---|
27 | import geniusweb.inform.Voting;
|
---|
28 | import geniusweb.issuevalue.Bid;
|
---|
29 | import geniusweb.issuevalue.DiscreteValue;
|
---|
30 | import geniusweb.protocol.ProtocolException;
|
---|
31 | import geniusweb.protocol.session.mopac.PartyStates;
|
---|
32 | import geniusweb.voting.VotingEvaluator;
|
---|
33 | import geniusweb.voting.votingevaluators.LargestAgreement;
|
---|
34 | import geniusweb.voting.votingevaluators.LargestAgreementsLoop;
|
---|
35 | import tudelft.utilities.junit.GeneralTests;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * We also test defaultPhase here.
|
---|
39 | */
|
---|
40 | public class VotingPhaseTest extends GeneralTests<VotingPhase> {
|
---|
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 OfferPhase prevPhase = mock(OfferPhase.class);
|
---|
49 |
|
---|
50 | private final Map<PartyId, Integer> powers = new HashMap<>();
|
---|
51 | private final PartyStates states, states2;
|
---|
52 | private final VotingEvaluator evaluator = new LargestAgreement();
|
---|
53 | private final VotingEvaluator evaluator2 = new LargestAgreementsLoop();
|
---|
54 | private final VotingPhase phase, phase1, phase1a, phase2, phase3, phase4;
|
---|
55 |
|
---|
56 | // just a test bid
|
---|
57 | private final Bid bid = new Bid("issue", new DiscreteValue("yes"));
|
---|
58 | private final Vote vote = new Vote(party1, bid, 2, 3);
|
---|
59 | private final Votes votes = new Votes(party1, Collections.singleton(vote));
|
---|
60 |
|
---|
61 | private final List<Offer> prevPhase = Arrays.asList(new Offer(party1, bid));
|
---|
62 | private final String serialized = "{\"VotingPhase\":{\"offers\":[],\"partyStates\":{\"powers\":{\"party2\":3,\"party1\":2,\"party3\":3},\"notYetActed\":[\"party2\",\"party1\",\"party3\"],\"actions\":[],\"agreements\":{},\"walkedAway\":[],\"exceptions\":{}},\"deadline\":10,\"evaluator\":{\"LargestAgreement\":{}}}}";
|
---|
63 |
|
---|
64 | public VotingPhaseTest() {
|
---|
65 | powers.put(party1, 2);
|
---|
66 | powers.put(party2, 3);
|
---|
67 | states2 = new PartyStates(powers);
|
---|
68 | powers.put(party3, 3);
|
---|
69 | states = new PartyStates(powers);
|
---|
70 |
|
---|
71 | phase = new VotingPhase(prevPhase, states, DEADLINE, evaluator);
|
---|
72 |
|
---|
73 | // in these phases we just don't set prevPhase.
|
---|
74 | // this to avoid serialization troubles.
|
---|
75 | phase1 = new VotingPhase(Collections.emptyList(), states, 10l,
|
---|
76 | evaluator);
|
---|
77 | phase1a = new VotingPhase(Collections.emptyList(), states, 10l,
|
---|
78 | evaluator);
|
---|
79 | phase2 = new VotingPhase(Collections.emptyList(), states2, 10l,
|
---|
80 | evaluator);
|
---|
81 | phase3 = new VotingPhase(Collections.emptyList(), states, 20l,
|
---|
82 | evaluator);
|
---|
83 | phase4 = new VotingPhase(Collections.emptyList(), states, 10l,
|
---|
84 | evaluator2);
|
---|
85 |
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public List<List<VotingPhase>> getGeneralTestData() {
|
---|
90 | return Arrays.asList(Arrays.asList(phase1, phase1a),
|
---|
91 | Arrays.asList(phase2), Arrays.asList(phase3),
|
---|
92 | Arrays.asList(phase4));
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public List<String> getGeneralTestStrings() {
|
---|
97 | return Arrays.asList(
|
---|
98 | "VotingPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
99 | "VotingPhase.*PartyStates.*party2, party1.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
100 | "VotingPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],20,LargestAgreement.*",
|
---|
101 | "VotingPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreementsLoop.*");
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Test
|
---|
105 | public void smokeTest() {
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Test
|
---|
109 | public void testInitState() {
|
---|
110 | assertEquals(3, phase.getPartyStates().getNotYetActed().size());
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Test
|
---|
114 | public void testInform() {
|
---|
115 | // when(prevPhase.getOffers()).thenReturn(Arrays.asList(bid));
|
---|
116 | assertEquals(new Voting(Arrays.asList(new Offer(party1, bid)), powers),
|
---|
117 | phase.getInform());
|
---|
118 |
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Test
|
---|
122 | public void isFinalTest() {
|
---|
123 | assertFalse(phase.isFinal(1l));
|
---|
124 | assertTrue(phase.isFinal(10l));
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Test
|
---|
128 | public void isFinalTestActorNotYetActed() {
|
---|
129 | PartyStates actedstate = mock(PartyStates.class);
|
---|
130 | when(actedstate.getNotYetActed())
|
---|
131 | .thenReturn(Collections.singleton(party1));
|
---|
132 | OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator);
|
---|
133 | assertFalse(testphase.isFinal(1l));
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Test
|
---|
137 | public void isFinalTestAllActorsActed() {
|
---|
138 | PartyStates actedstate = mock(PartyStates.class);
|
---|
139 | when(actedstate.getNotYetActed()).thenReturn(Collections.emptySet());
|
---|
140 | OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator);
|
---|
141 | assertTrue(testphase.isFinal(1l));
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Test(expected = ProtocolException.class)
|
---|
145 | public void checkIncorrectActorTest() throws ProtocolException {
|
---|
146 | phase.checkAction(party1, new EndNegotiation(party2), 1);
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Test(expected = ProtocolException.class)
|
---|
150 | public void checkNotAllowedActionTest() throws ProtocolException {
|
---|
151 | phase.checkAction(party1, new Votes(party2, Collections.emptySet()), 1);
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Test
|
---|
155 | public void testFinish() {
|
---|
156 | Phase ph = phase.finish();
|
---|
157 | assertTrue(ph.getPartyStates().getNotYetActed().isEmpty());
|
---|
158 | assertEquals(3, ph.getPartyStates().getExceptions().size());
|
---|
159 | assertTrue(phase.getPartyStates().getAgreements().getMap().isEmpty());
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Test(expected = IllegalStateException.class)
|
---|
163 | public void testNextWrong() {
|
---|
164 | // state is not final, should not work
|
---|
165 | phase.next(1, 1000);
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Test
|
---|
169 | public void testNextNoParties() {
|
---|
170 | Phase ph = phase.finish();
|
---|
171 | // no parties are left now, all failed
|
---|
172 | // but this is not part of the next() check.
|
---|
173 | Phase next = ph.next(1, 1000);
|
---|
174 | assertTrue(next instanceof OptInPhase);
|
---|
175 | // no remaining parties, since finish kicked all
|
---|
176 | assertTrue(next.getPartyStates().getNotYetActed().isEmpty());
|
---|
177 | }
|
---|
178 |
|
---|
179 | @Test
|
---|
180 | public void testAllowed() {
|
---|
181 | phase.with(party1, votes, 1);
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Test
|
---|
185 | public void getVotesTest() {
|
---|
186 | assertEquals(Collections.emptyList(), phase.getVotes());
|
---|
187 | List<Votes> vts = phase.with(party1, votes, 1).getVotes();
|
---|
188 | assertEquals(Arrays.asList(votes), vts);
|
---|
189 | }
|
---|
190 |
|
---|
191 | @Test(expected = IllegalArgumentException.class)
|
---|
192 | public void testNextTooShortDuration() {
|
---|
193 | phase.next(1, Phase.PHASE_MINTIME - 1);
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Test(expected = IllegalStateException.class)
|
---|
197 | public void testNextNotFinished() {
|
---|
198 | phase.next(1, Phase.PHASE_MINTIME + 1);
|
---|
199 | }
|
---|
200 |
|
---|
201 | @Test(expected = IllegalStateException.class)
|
---|
202 | public void testNext() {
|
---|
203 | phase.next(11, Phase.PHASE_MINTIME + 1);
|
---|
204 | }
|
---|
205 |
|
---|
206 | @Test
|
---|
207 | public void testDeserialize() throws IOException {
|
---|
208 | Phase obj = jackson.readValue(serialized, Phase.class);
|
---|
209 | System.out.println(obj);
|
---|
210 | assertEquals(phase1, obj);
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Test
|
---|
214 | public void testSerialize()
|
---|
215 | throws JsonProcessingException, URISyntaxException {
|
---|
216 |
|
---|
217 | String string = jackson.writeValueAsString(phase1);
|
---|
218 | System.out.println(string);
|
---|
219 | assertEquals(serialized, string);
|
---|
220 | }
|
---|
221 | }
|
---|