package geniusweb.protocol.session.mopac2.phase; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.Test; import geniusweb.actions.EndNegotiation; import geniusweb.actions.PartyId; import geniusweb.actions.Vote; import geniusweb.actions.VoteWithValue; import geniusweb.actions.Votes; import geniusweb.actions.VotesWithValue; import geniusweb.inform.OptInWithValue; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.DiscreteValue; import geniusweb.protocol.ProtocolException; import geniusweb.protocol.session.mopac2.PartyStates; import geniusweb.voting.VotingEvaluatorWithValue; import geniusweb.voting.evaluatorwithvalue.LargestAgreementWithValue; import tudelft.utilities.junit.GeneralTests; /** * We also test defaultPhase here. */ public class OptInPhaseTest extends GeneralTests { private static final long DEADLINE = 10l; private final PartyId party1 = new PartyId("party1"); private final PartyId party2 = new PartyId("party2"); private final PartyId party3 = new PartyId("party3"); // private final VotingPhase prevPhase = mock(VotingPhase.class); private final Map powers = new HashMap<>(); private final PartyStates states, states2; private final VotingEvaluatorWithValue evaluator = new LargestAgreementWithValue(); private final OptInPhase phase, phase1, phase1a, phase2, phase3; // just a test bid private final Bid bid = new Bid("issue", new DiscreteValue("yes")); private final VoteWithValue vote = new VoteWithValue(party1, bid, 2, 3, 100); private final VotesWithValue votes = new VotesWithValue(party1, Collections.singleton(vote)); private final List prevVotes = Arrays.asList(votes); public OptInPhaseTest() { powers.put(party1, 2); powers.put(party2, 3); states2 = new PartyStates(powers); powers.put(party3, 3); states = new PartyStates(powers); phase = new OptInPhase(prevVotes, states, DEADLINE, evaluator); // in these phases we just don't set prevPhase. // this to avoid serialization troubles. phase1 = new OptInPhase(null, states, 10l, evaluator); phase1a = new OptInPhase(null, states, 10l, evaluator); phase2 = new OptInPhase(null, states2, 10l, evaluator); phase3 = new OptInPhase(null, states, 20l, evaluator); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(phase1, phase1a), Arrays.asList(phase2), Arrays.asList(phase3)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "OptInPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*", "OptInPhase.*PartyStates.*party2, party1.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*", "OptInPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],20,LargestAgreement.*"); } @Test public void smokeTest() { } @Test public void testInitState() { assertEquals(3, phase.getPartyStates().getNotYetActed().size()); } @Test public void testInform() { // when(prevPhase.getVotes()).thenReturn(Arrays.asList(votes)); assertEquals(new OptInWithValue(Arrays.asList(votes)), phase.getInform()); } @Test public void isFinalTest() { assertFalse(phase.isFinal(1l)); assertTrue(phase.isFinal(10l)); } @Test public void isFinalTestActorNotYetActed() { PartyStates actedstate = mock(PartyStates.class); when(actedstate.getNotYetActed()) .thenReturn(Collections.singleton(party1)); OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator); assertFalse(testphase.isFinal(1l)); } @Test public void isFinalTestAllActorsActed() { PartyStates actedstate = mock(PartyStates.class); when(actedstate.getNotYetActed()).thenReturn(Collections.emptySet()); OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator); assertTrue(testphase.isFinal(1l)); } @Test(expected = ProtocolException.class) public void checkIncorrectActorTest() throws ProtocolException { phase.checkAction(party1, new EndNegotiation(party2), 1); } @Test(expected = ProtocolException.class) public void checkNotAllowedActionTest() throws ProtocolException { phase.checkAction(party1, new Votes(party2, Collections.emptySet()), 1); } @Test public void testFinish() { Phase ph = phase.finish(); assertTrue(ph.getPartyStates().getNotYetActed().isEmpty()); assertEquals(3, ph.getPartyStates().getExceptions().size()); assertTrue(phase.getPartyStates().getAgreements().getMap().isEmpty()); } @Test(expected = IllegalStateException.class) public void testNextWrong() { // state is not final, should not work phase.next(1, 1000); } @Test public void testNextNoParties() { Phase ph = phase.finish(); // no parties are left now, all failed // but this is not part of the next() check. Phase next = ph.next(1, 1000); assertTrue(next instanceof OfferPhase); // no remaining parties, since finish kicked all assertTrue(next.getPartyStates().getNotYetActed().isEmpty()); } @Test public void testAllowed() { // when(prevPhase.getVotes()).thenReturn(Arrays.asList(votes)); phase.with(party1, votes, 1l); } @Test public void testAllowedNarrowingVote() { // the previous vote was maxPower=3, // so this is an illegal narrowing of the previous vote Vote newVote = new Vote(party1, bid, 2, 2); Votes newVotes = new Votes(party1, Collections.singleton(newVote)); // when(prevPhase.getVotes()).thenReturn(Arrays.asList(votes)); // check that party1 was kicked Phase newphase = phase.with(party1, newVotes, 1l); assertTrue( newphase.getPartyStates().getExceptions().containsKey(party1)); } @Test(expected = IllegalArgumentException.class) public void testNextTooShortDuration() { phase.next(1, Phase.PHASE_MINTIME - 1); } @Test(expected = IllegalStateException.class) public void testNextNotFinished() { phase.next(1, Phase.PHASE_MINTIME + 1); } @Test(expected = IllegalStateException.class) public void testNext() { phase.next(11, Phase.PHASE_MINTIME + 1); } }