package geniusweb.protocol.session.mopac.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.io.IOException; import java.net.URISyntaxException; 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 com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import geniusweb.actions.EndNegotiation; import geniusweb.actions.Offer; import geniusweb.actions.PartyId; import geniusweb.actions.Vote; import geniusweb.actions.Votes; import geniusweb.inform.Voting; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.DiscreteValue; import geniusweb.protocol.ProtocolException; import geniusweb.protocol.session.mopac.PartyStates; import geniusweb.voting.VotingEvaluator; import geniusweb.voting.votingevaluators.LargestAgreement; import geniusweb.voting.votingevaluators.LargestAgreementsLoop; import tudelft.utilities.junit.GeneralTests; /** * We also test defaultPhase here. */ public class VotingPhaseTest extends GeneralTests { private final ObjectMapper jackson = new ObjectMapper(); 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 OfferPhase prevPhase = mock(OfferPhase.class); private final Map powers = new HashMap<>(); private final PartyStates states, states2; private final VotingEvaluator evaluator = new LargestAgreement(); private final VotingEvaluator evaluator2 = new LargestAgreementsLoop(); private final VotingPhase phase, phase1, phase1a, phase2, phase3, phase4; // just a test bid private final Bid bid = new Bid("issue", new DiscreteValue("yes")); private final Vote vote = new Vote(party1, bid, 2, 3); private final Votes votes = new Votes(party1, Collections.singleton(vote)); private final List prevPhase = Arrays.asList(new Offer(party1, bid)); private final String serialized = "{\"VotingPhase\":{\"offers\":[],\"partyStates\":{\"powers\":{\"party2\":3,\"party1\":2,\"party3\":3},\"notYetActed\":[\"party2\",\"party1\",\"party3\"],\"actions\":[],\"agreements\":{},\"walkedAway\":[],\"exceptions\":{}},\"deadline\":10,\"evaluator\":{\"LargestAgreement\":{}}}}"; public VotingPhaseTest() { powers.put(party1, 2); powers.put(party2, 3); states2 = new PartyStates(powers); powers.put(party3, 3); states = new PartyStates(powers); phase = new VotingPhase(prevPhase, states, DEADLINE, evaluator); // in these phases we just don't set prevPhase. // this to avoid serialization troubles. phase1 = new VotingPhase(Collections.emptyList(), states, 10l, evaluator); phase1a = new VotingPhase(Collections.emptyList(), states, 10l, evaluator); phase2 = new VotingPhase(Collections.emptyList(), states2, 10l, evaluator); phase3 = new VotingPhase(Collections.emptyList(), states, 20l, evaluator); phase4 = new VotingPhase(Collections.emptyList(), states, 10l, evaluator2); } @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(phase1, phase1a), Arrays.asList(phase2), Arrays.asList(phase3), Arrays.asList(phase4)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "VotingPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*", "VotingPhase.*PartyStates.*party2, party1.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*", "VotingPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],20,LargestAgreement.*", "VotingPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreementsLoop.*"); } @Test public void smokeTest() { } @Test public void testInitState() { assertEquals(3, phase.getPartyStates().getNotYetActed().size()); } @Test public void testInform() { // when(prevPhase.getOffers()).thenReturn(Arrays.asList(bid)); assertEquals(new Voting(Arrays.asList(new Offer(party1, bid)), powers), 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 OptInPhase); // no remaining parties, since finish kicked all assertTrue(next.getPartyStates().getNotYetActed().isEmpty()); } @Test public void testAllowed() { phase.with(party1, votes, 1); } @Test public void getVotesTest() { assertEquals(Collections.emptyList(), phase.getVotes()); List vts = phase.with(party1, votes, 1).getVotes(); assertEquals(Arrays.asList(votes), vts); } @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); } @Test public void testDeserialize() throws IOException { Phase obj = jackson.readValue(serialized, Phase.class); System.out.println(obj); assertEquals(phase1, obj); } @Test public void testSerialize() throws JsonProcessingException, URISyntaxException { String string = jackson.writeValueAsString(phase1); System.out.println(string); assertEquals(serialized, string); } }