source: protocol/src/test/java/geniusweb/protocol/session/mopac/phase/VotingPhaseTest.java@ 27

Last change on this file since 27 was 27, checked in by bart, 4 years ago
  • party capabilities now include profile information. Existing parties may need small fix * plot layout shows accepts * VotingEvaluator use group power instead of group size * runsession you can now also select MOPAC-only parties
File size: 6.1 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.mockito.Mockito.mock;
7import static org.mockito.Mockito.when;
8
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.List;
13import java.util.Map;
14
15import org.junit.Test;
16
17import geniusweb.actions.EndNegotiation;
18import geniusweb.actions.Offer;
19import geniusweb.actions.PartyId;
20import geniusweb.actions.Vote;
21import geniusweb.actions.Votes;
22import geniusweb.inform.Voting;
23import geniusweb.issuevalue.Bid;
24import geniusweb.issuevalue.DiscreteValue;
25import geniusweb.protocol.ProtocolException;
26import geniusweb.protocol.session.mopac.PartyStates;
27import geniusweb.voting.VotingEvaluator;
28import geniusweb.voting.votingevaluators.LargestAgreement;
29import geniusweb.voting.votingevaluators.LargestAgreementsLoop;
30import tudelft.utilities.junit.GeneralTests;
31
32/**
33 * We also test defaultPhase here.
34 */
35public class VotingPhaseTest extends GeneralTests<VotingPhase> {
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 OfferPhase prevPhase = mock(OfferPhase.class);
43
44 private final Map<PartyId, Integer> powers = new HashMap<>();
45 private final PartyStates states, states2;
46 private final VotingEvaluator evaluator = new LargestAgreement();
47 private final VotingEvaluator evaluator2 = new LargestAgreementsLoop();
48 private final VotingPhase phase, phase1, phase1a, phase2, phase3, phase4;
49
50 // just a test bid
51 private final Bid bid = new Bid("issue", new DiscreteValue("yes"));
52 private final Vote vote = new Vote(party1, bid, 2, 3);
53 private final Votes votes = new Votes(party1, 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 phase4 = new VotingPhase(null, states, 10l, evaluator2);
73
74 }
75
76 @Override
77 public List<List<VotingPhase>> getGeneralTestData() {
78 return Arrays.asList(Arrays.asList(phase1, phase1a),
79 Arrays.asList(phase2), Arrays.asList(phase3),
80 Arrays.asList(phase4));
81 }
82
83 @Override
84 public List<String> getGeneralTestStrings() {
85 return Arrays.asList(
86 "VotingPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
87 "VotingPhase.*PartyStates.*party2, party1.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
88 "VotingPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],20,LargestAgreement.*",
89 "VotingPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreementsLoop.*");
90 }
91
92 @Test
93 public void smokeTest() {
94 }
95
96 @Test
97 public void testInitState() {
98 assertEquals(3, phase.getPartyStates().getNotYetActed().size());
99 }
100
101 @Test
102 public void testInform() {
103 // when(prevPhase.getOffers()).thenReturn(Arrays.asList(bid));
104 assertEquals(new Voting(Arrays.asList(new Offer(party1, bid)), powers),
105 phase.getInform());
106
107 }
108
109 @Test
110 public void isFinalTest() {
111 assertFalse(phase.isFinal(1l));
112 assertTrue(phase.isFinal(10l));
113 }
114
115 @Test
116 public void isFinalTestActorNotYetActed() {
117 PartyStates actedstate = mock(PartyStates.class);
118 when(actedstate.getNotYetActed())
119 .thenReturn(Collections.singleton(party1));
120 OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator);
121 assertFalse(testphase.isFinal(1l));
122 }
123
124 @Test
125 public void isFinalTestAllActorsActed() {
126 PartyStates actedstate = mock(PartyStates.class);
127 when(actedstate.getNotYetActed()).thenReturn(Collections.emptySet());
128 OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator);
129 assertTrue(testphase.isFinal(1l));
130 }
131
132 @Test(expected = ProtocolException.class)
133 public void checkIncorrectActorTest() throws ProtocolException {
134 phase.checkAction(party1, new EndNegotiation(party2), 1);
135 }
136
137 @Test(expected = ProtocolException.class)
138 public void checkNotAllowedActionTest() throws ProtocolException {
139 phase.checkAction(party1, new Votes(party2, Collections.emptySet()), 1);
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 OptInPhase);
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, votes, 1);
170 }
171
172 @Test
173 public void getVotesTest() {
174 assertEquals(Collections.emptyList(), phase.getVotes());
175 List<Votes> vts = phase.with(party1, votes, 1).getVotes();
176 assertEquals(Arrays.asList(votes), vts);
177 }
178
179 @Test(expected = IllegalArgumentException.class)
180 public void testNextTooShortDuration() {
181 phase.next(1, Phase.PHASE_MINTIME - 1);
182 }
183
184 @Test(expected = IllegalStateException.class)
185 public void testNextNotFinished() {
186 phase.next(1, Phase.PHASE_MINTIME + 1);
187 }
188
189 @Test(expected = IllegalStateException.class)
190 public void testNext() {
191 phase.next(11, Phase.PHASE_MINTIME + 1);
192 }
193
194}
Note: See TracBrowser for help on using the repository browser.