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.mockito.Mockito.mock;
|
---|
7 | import static org.mockito.Mockito.when;
|
---|
8 |
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 |
|
---|
15 | import org.junit.Test;
|
---|
16 |
|
---|
17 | import geniusweb.actions.EndNegotiation;
|
---|
18 | import geniusweb.actions.PartyId;
|
---|
19 | import geniusweb.actions.Vote;
|
---|
20 | import geniusweb.actions.VoteWithValue;
|
---|
21 | import geniusweb.actions.Votes;
|
---|
22 | import geniusweb.actions.VotesWithValue;
|
---|
23 | import geniusweb.inform.OptInWithValue;
|
---|
24 | import geniusweb.issuevalue.Bid;
|
---|
25 | import geniusweb.issuevalue.DiscreteValue;
|
---|
26 | import geniusweb.protocol.ProtocolException;
|
---|
27 | import geniusweb.protocol.session.mopac2.PartyStates;
|
---|
28 | import geniusweb.voting.VotingEvaluatorWithValue;
|
---|
29 | import geniusweb.voting.evaluatorwithvalue.LargestAgreementWithValue;
|
---|
30 | import tudelft.utilities.junit.GeneralTests;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * We also test defaultPhase here.
|
---|
34 | */
|
---|
35 | public class OptInPhaseTest extends GeneralTests<OptInPhase> {
|
---|
36 |
|
---|
37 | private static final long DEADLINE = 10l;
|
---|
38 | private final PartyId party1 = new PartyId("party1");
|
---|
39 | private final PartyId party2 = new PartyId("party2");
|
---|
40 | private final PartyId party3 = new PartyId("party3");
|
---|
41 |
|
---|
42 | // private final VotingPhase prevPhase = mock(VotingPhase.class);
|
---|
43 |
|
---|
44 | private final Map<PartyId, Integer> powers = new HashMap<>();
|
---|
45 | private final PartyStates states, states2;
|
---|
46 | private final VotingEvaluatorWithValue evaluator = new LargestAgreementWithValue();
|
---|
47 | private final OptInPhase phase, phase1, phase1a, phase2, phase3;
|
---|
48 |
|
---|
49 | // just a test bid
|
---|
50 | private final Bid bid = new Bid("issue", new DiscreteValue("yes"));
|
---|
51 | private final VoteWithValue vote = new VoteWithValue(party1, bid, 2, 3,
|
---|
52 | 100);
|
---|
53 | private final VotesWithValue votes = new VotesWithValue(party1,
|
---|
54 | Collections.singleton(vote));
|
---|
55 |
|
---|
56 | private final List<VotesWithValue> prevVotes = Arrays.asList(votes);
|
---|
57 |
|
---|
58 | public OptInPhaseTest() {
|
---|
59 | powers.put(party1, 2);
|
---|
60 | powers.put(party2, 3);
|
---|
61 | states2 = new PartyStates(powers);
|
---|
62 | powers.put(party3, 3);
|
---|
63 | states = new PartyStates(powers);
|
---|
64 |
|
---|
65 | phase = new OptInPhase(prevVotes, states, DEADLINE, evaluator);
|
---|
66 |
|
---|
67 | // in these phases we just don't set prevPhase.
|
---|
68 | // this to avoid serialization troubles.
|
---|
69 | phase1 = new OptInPhase(null, states, 10l, evaluator);
|
---|
70 | phase1a = new OptInPhase(null, states, 10l, evaluator);
|
---|
71 | phase2 = new OptInPhase(null, states2, 10l, evaluator);
|
---|
72 | phase3 = new OptInPhase(null, states, 20l, evaluator);
|
---|
73 |
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public List<List<OptInPhase>> getGeneralTestData() {
|
---|
78 | return Arrays.asList(Arrays.asList(phase1, phase1a),
|
---|
79 | Arrays.asList(phase2), Arrays.asList(phase3));
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public List<String> getGeneralTestStrings() {
|
---|
84 | return Arrays.asList(
|
---|
85 | "OptInPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
86 | "OptInPhase.*PartyStates.*party2, party1.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
87 | "OptInPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],20,LargestAgreement.*");
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Test
|
---|
91 | public void smokeTest() {
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Test
|
---|
95 | public void testInitState() {
|
---|
96 | assertEquals(3, phase.getPartyStates().getNotYetActed().size());
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Test
|
---|
100 | public void testInform() {
|
---|
101 | // when(prevPhase.getVotes()).thenReturn(Arrays.asList(votes));
|
---|
102 | assertEquals(new OptInWithValue(Arrays.asList(votes)),
|
---|
103 | phase.getInform());
|
---|
104 |
|
---|
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 testFinish() {
|
---|
142 | Phase ph = phase.finish();
|
---|
143 | assertTrue(ph.getPartyStates().getNotYetActed().isEmpty());
|
---|
144 | assertEquals(3, ph.getPartyStates().getExceptions().size());
|
---|
145 | assertTrue(phase.getPartyStates().getAgreements().getMap().isEmpty());
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Test(expected = IllegalStateException.class)
|
---|
149 | public void testNextWrong() {
|
---|
150 | // state is not final, should not work
|
---|
151 | phase.next(1, 1000);
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Test
|
---|
155 | public void testNextNoParties() {
|
---|
156 | Phase ph = phase.finish();
|
---|
157 | // no parties are left now, all failed
|
---|
158 | // but this is not part of the next() check.
|
---|
159 | Phase next = ph.next(1, 1000);
|
---|
160 | assertTrue(next instanceof OfferPhase);
|
---|
161 | // no remaining parties, since finish kicked all
|
---|
162 | assertTrue(next.getPartyStates().getNotYetActed().isEmpty());
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Test
|
---|
166 | public void testAllowed() {
|
---|
167 | // when(prevPhase.getVotes()).thenReturn(Arrays.asList(votes));
|
---|
168 | phase.with(party1, votes, 1l);
|
---|
169 | }
|
---|
170 |
|
---|
171 | @Test
|
---|
172 | public void testAllowedNarrowingVote() {
|
---|
173 | // the previous vote was maxPower=3,
|
---|
174 | // so this is an illegal narrowing of the previous vote
|
---|
175 | Vote newVote = new Vote(party1, bid, 2, 2);
|
---|
176 | Votes newVotes = new Votes(party1, Collections.singleton(newVote));
|
---|
177 |
|
---|
178 | // when(prevPhase.getVotes()).thenReturn(Arrays.asList(votes));
|
---|
179 |
|
---|
180 | // check that party1 was kicked
|
---|
181 | Phase newphase = phase.with(party1, newVotes, 1l);
|
---|
182 | assertTrue(
|
---|
183 | newphase.getPartyStates().getExceptions().containsKey(party1));
|
---|
184 |
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Test(expected = IllegalArgumentException.class)
|
---|
188 | public void testNextTooShortDuration() {
|
---|
189 | phase.next(1, Phase.PHASE_MINTIME - 1);
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Test(expected = IllegalStateException.class)
|
---|
193 | public void testNextNotFinished() {
|
---|
194 | phase.next(1, Phase.PHASE_MINTIME + 1);
|
---|
195 | }
|
---|
196 |
|
---|
197 | @Test(expected = IllegalStateException.class)
|
---|
198 | public void testNext() {
|
---|
199 | phase.next(11, Phase.PHASE_MINTIME + 1);
|
---|
200 | }
|
---|
201 |
|
---|
202 | }
|
---|