source: protocol/src/test/java/geniusweb/protocol/session/mopac/phase/OptInPhaseTest.java@ 32

Last change on this file since 32 was 32, checked in by bart, 3 years ago

Multiple learns with repeated tournament, maven use https.

File size: 6.3 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.PartyId;
19import geniusweb.actions.Vote;
20import geniusweb.actions.Votes;
21import geniusweb.inform.OptIn;
22import geniusweb.issuevalue.Bid;
23import geniusweb.issuevalue.DiscreteValue;
24import geniusweb.protocol.ProtocolException;
25import geniusweb.protocol.session.mopac.PartyStates;
26import geniusweb.voting.VotingEvaluator;
27import geniusweb.voting.votingevaluators.LargestAgreement;
28import geniusweb.voting.votingevaluators.LargestAgreementsLoop;
29import tudelft.utilities.junit.GeneralTests;
30
31/**
32 * We also test defaultPhase here.
33 */
34public class OptInPhaseTest extends GeneralTests<OptInPhase> {
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 VotingPhase prevPhase = mock(VotingPhase.class);
42
43 private final Map<PartyId, Integer> powers = new HashMap<>();
44 private final PartyStates states, states2;
45 private final VotingEvaluator evaluator = new LargestAgreement();
46 private final VotingEvaluator evaluator2 = new LargestAgreementsLoop();
47 private final OptInPhase phase, phase1, phase1a, phase2, phase3, phase4;
48
49 // just a test bid
50 private final Bid bid = new Bid("issue", new DiscreteValue("yes"));
51 private final Vote vote = new Vote(party1, bid, 2, 3);
52 private final Votes votes = new Votes(party1, Collections.singleton(vote));
53
54 private final List<Votes> prevVotes = Arrays.asList(votes);
55
56 public OptInPhaseTest() {
57 powers.put(party1, 2);
58 powers.put(party2, 3);
59 states2 = new PartyStates(powers);
60 powers.put(party3, 3);
61 states = new PartyStates(powers);
62
63 phase = new OptInPhase(prevVotes, states, DEADLINE, evaluator);
64
65 // in these phases we just don't set prevPhase.
66 // this to avoid serialization troubles.
67 phase1 = new OptInPhase(null, states, 10l, evaluator);
68 phase1a = new OptInPhase(null, states, 10l, evaluator);
69 phase2 = new OptInPhase(null, states2, 10l, evaluator);
70 phase3 = new OptInPhase(null, states, 20l, evaluator);
71 phase4 = new OptInPhase(null, states, 10l, evaluator2);
72
73 }
74
75 @Override
76 public List<List<OptInPhase>> getGeneralTestData() {
77 return Arrays.asList(Arrays.asList(phase1, phase1a),
78 Arrays.asList(phase2), Arrays.asList(phase3),
79 Arrays.asList(phase4));
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 "OptInPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreementsLoop.*");
89 }
90
91 @Test
92 public void smokeTest() {
93 }
94
95 @Test
96 public void testInitState() {
97 assertEquals(3, phase.getPartyStates().getNotYetActed().size());
98 }
99
100 @Test
101 public void testInform() {
102 // when(prevPhase.getVotes()).thenReturn(Arrays.asList(votes));
103 assertEquals(new OptIn(Arrays.asList(votes)), 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}
Note: See TracBrowser for help on using the repository browser.