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.Offer;
|
---|
19 | import geniusweb.actions.PartyId;
|
---|
20 | import geniusweb.actions.VoteWithValue;
|
---|
21 | import geniusweb.actions.VotesWithValue;
|
---|
22 | import geniusweb.inform.Voting;
|
---|
23 | import geniusweb.issuevalue.Bid;
|
---|
24 | import geniusweb.issuevalue.DiscreteValue;
|
---|
25 | import geniusweb.protocol.ProtocolException;
|
---|
26 | import geniusweb.protocol.session.mopac2.PartyStates;
|
---|
27 | import geniusweb.voting.VotingEvaluatorWithValue;
|
---|
28 | import geniusweb.voting.evaluatorwithvalue.LargestAgreementWithValue;
|
---|
29 | import tudelft.utilities.junit.GeneralTests;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * We also test defaultPhase here.
|
---|
33 | */
|
---|
34 | public class VotingPhaseTest extends GeneralTests<VotingPhase> {
|
---|
35 |
|
---|
36 | private static final long DEADLINE = 10l;
|
---|
37 | private final PartyId party1 = new PartyId("party1");
|
---|
38 | private final PartyId party2 = new PartyId("party2");
|
---|
39 | private final PartyId party3 = new PartyId("party3");
|
---|
40 |
|
---|
41 | // private final OfferPhase prevPhase = mock(OfferPhase.class);
|
---|
42 |
|
---|
43 | private final Map<PartyId, Integer> powers = new HashMap<>();
|
---|
44 | private final PartyStates states, states2;
|
---|
45 | private final VotingEvaluatorWithValue evaluator = new LargestAgreementWithValue();
|
---|
46 | private final VotingPhase phase, phase1, phase1a, phase2, phase3;
|
---|
47 |
|
---|
48 | // just a test bid
|
---|
49 | private final Bid bid = new Bid("issue", new DiscreteValue("yes"));
|
---|
50 | private final VoteWithValue vote = new VoteWithValue(party1, bid, 2, 3,
|
---|
51 | 100);
|
---|
52 | private final VotesWithValue votes = new VotesWithValue(party1,
|
---|
53 | Collections.singleton(vote));
|
---|
54 |
|
---|
55 | private final List<Offer> prevPhase = Arrays.asList(new Offer(party1, bid));
|
---|
56 |
|
---|
57 | public VotingPhaseTest() {
|
---|
58 | powers.put(party1, 2);
|
---|
59 | powers.put(party2, 3);
|
---|
60 | states2 = new PartyStates(powers);
|
---|
61 | powers.put(party3, 3);
|
---|
62 | states = new PartyStates(powers);
|
---|
63 |
|
---|
64 | phase = new VotingPhase(prevPhase, states, DEADLINE, evaluator);
|
---|
65 |
|
---|
66 | // in these phases we just don't set prevPhase.
|
---|
67 | // this to avoid serialization troubles.
|
---|
68 | phase1 = new VotingPhase(null, states, 10l, evaluator);
|
---|
69 | phase1a = new VotingPhase(null, states, 10l, evaluator);
|
---|
70 | phase2 = new VotingPhase(null, states2, 10l, evaluator);
|
---|
71 | phase3 = new VotingPhase(null, states, 20l, evaluator);
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public List<List<VotingPhase>> getGeneralTestData() {
|
---|
77 | return Arrays.asList(Arrays.asList(phase1, phase1a),
|
---|
78 | Arrays.asList(phase2), Arrays.asList(phase3));
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public List<String> getGeneralTestStrings() {
|
---|
83 | return Arrays.asList(
|
---|
84 | "VotingPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
85 | "VotingPhase.*PartyStates.*party2, party1.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
86 | "VotingPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],20,LargestAgreement.*");
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test
|
---|
90 | public void smokeTest() {
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Test
|
---|
94 | public void testInitState() {
|
---|
95 | assertEquals(3, phase.getPartyStates().getNotYetActed().size());
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Test
|
---|
99 | public void testInform() {
|
---|
100 | // when(prevPhase.getOffers()).thenReturn(Arrays.asList(bid));
|
---|
101 | assertEquals(new Voting(Arrays.asList(new Offer(party1, bid)), powers),
|
---|
102 | phase.getInform());
|
---|
103 |
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Test
|
---|
107 | public void isFinalTest() {
|
---|
108 | assertFalse(phase.isFinal(1l));
|
---|
109 | assertTrue(phase.isFinal(10l));
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Test
|
---|
113 | public void isFinalTestActorNotYetActed() {
|
---|
114 | PartyStates actedstate = mock(PartyStates.class);
|
---|
115 | when(actedstate.getNotYetActed())
|
---|
116 | .thenReturn(Collections.singleton(party1));
|
---|
117 | OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator);
|
---|
118 | assertFalse(testphase.isFinal(1l));
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Test
|
---|
122 | public void isFinalTestAllActorsActed() {
|
---|
123 | PartyStates actedstate = mock(PartyStates.class);
|
---|
124 | when(actedstate.getNotYetActed()).thenReturn(Collections.emptySet());
|
---|
125 | OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator);
|
---|
126 | assertTrue(testphase.isFinal(1l));
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Test(expected = ProtocolException.class)
|
---|
130 | public void checkIncorrectActorTest() throws ProtocolException {
|
---|
131 | phase.checkAction(party1, new EndNegotiation(party2), 1);
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Test(expected = ProtocolException.class)
|
---|
135 | public void checkNotAllowedActionTest() throws ProtocolException {
|
---|
136 | phase.checkAction(party2,
|
---|
137 | new VotesWithValue(party1, Collections.singleton(vote)), 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 OptInPhase);
|
---|
161 | // no remaining parties, since finish kicked all
|
---|
162 | assertTrue(next.getPartyStates().getNotYetActed().isEmpty());
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Test
|
---|
166 | public void testAllowed() {
|
---|
167 | phase.with(party1, votes, 1);
|
---|
168 | }
|
---|
169 |
|
---|
170 | @Test
|
---|
171 | public void getVotesTest() {
|
---|
172 | assertEquals(Collections.emptyList(), phase.getVotes());
|
---|
173 | List<VotesWithValue> vts = phase.with(party1, votes, 1).getVotes();
|
---|
174 | assertEquals(Arrays.asList(votes), vts);
|
---|
175 | }
|
---|
176 |
|
---|
177 | @Test(expected = IllegalArgumentException.class)
|
---|
178 | public void testNextTooShortDuration() {
|
---|
179 | phase.next(1, Phase.PHASE_MINTIME - 1);
|
---|
180 | }
|
---|
181 |
|
---|
182 | @Test(expected = IllegalStateException.class)
|
---|
183 | public void testNextNotFinished() {
|
---|
184 | phase.next(1, Phase.PHASE_MINTIME + 1);
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Test(expected = IllegalStateException.class)
|
---|
188 | public void testNext() {
|
---|
189 | phase.next(11, Phase.PHASE_MINTIME + 1);
|
---|
190 | }
|
---|
191 |
|
---|
192 | }
|
---|