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.junit.Assert.fail; 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.Accept; import geniusweb.actions.EndNegotiation; import geniusweb.actions.Offer; import geniusweb.actions.PartyId; import geniusweb.actions.Votes; import geniusweb.inform.YourTurn; 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 OfferPhaseTest 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 Map powers = new HashMap<>(); private final PartyStates states, states2; private final VotingEvaluator evaluator = new LargestAgreement(); private final VotingEvaluator evaluator2 = new LargestAgreementsLoop(); private final OfferPhase phase, phase1, phase1a, phase2, phase3, phase4; // just a test bid private final Bid bid = new Bid("issue", new DiscreteValue("yes")); private final Offer offer = new Offer(party1, bid); private final String serialized = "{\"OfferPhase\":{\"partyStates\":{\"notYetActed\":[\"party2\",\"party1\",\"party3\"],\"actions\":[],\"agreements\":{},\"walkedAway\":[],\"exceptions\":{},\"powers\":{\"party2\":3,\"party1\":2,\"party3\":3}},\"evaluator\":{\"LargestAgreement\":{}},\"deadline\":10}}"; public OfferPhaseTest() { powers.put(party1, 2); powers.put(party2, 3); states2 = new PartyStates(powers); powers.put(party3, 3); states = new PartyStates(powers); phase = new OfferPhase(states, DEADLINE, evaluator); phase1 = new OfferPhase(states, 10l, evaluator); phase1a = new OfferPhase(states, 10l, evaluator); phase2 = new OfferPhase(states2, 10l, evaluator); phase3 = new OfferPhase(states, 20l, evaluator); phase4 = new OfferPhase(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( "OfferPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*", "OfferPhase.*PartyStates.*party2, party1.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*", "OfferPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],20,LargestAgreement.*", "OfferPhase.*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() { assertEquals(new YourTurn(), 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 testException() { OfferPhase newphase = phase.with(new ProtocolException("bla", party1)); assertEquals(10l, newphase.getDeadline()); assertEquals(0l, phase.getPartyStates().getActions().size()); } @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 VotingPhase); // no remaining parties, since finish kicked all assertTrue(next.getPartyStates().getNotYetActed().isEmpty()); } @Test public void testAllowed() { phase.with(party1, offer, 1); } @Test public void testAllowedWrongClass() { try { phase.checkAction(party1, new Accept(party1, bid), 1); } catch (ProtocolException e) { assertTrue(e.getMessage().contains("not allowed in OfferPhase")); return; } fail("checkAction did not throw as expected"); } @Test public void testAllowedWrongActor() { try { phase.checkAction(party1, new Accept(party2, bid), 1); } catch (ProtocolException e) { assertTrue( e.getMessage().contains("Incorrect actor info in action")); return; } fail("checkAction did not throw as expected"); } @Test public void testAllowedActorAlreadyActed() { OfferPhase testphase = phase.with(party1, offer, 1); try { testphase.checkAction(party1, offer, 2); } catch (ProtocolException e) { assertTrue(e.getMessage().contains("can not act anymore")); return; } fail("checkAction did not throw as expected"); } @Test public void testAllowedActorAlreadyActed1() { // as theoprevious test, but using with() instead of checkAction OfferPhase testphase = phase.with(party1, offer, 1); OfferPhase newphase = testphase.with(party1, offer, 2); // the party must remain as acted, because we can't retract his // action... assertEquals(1, newphase.getPartyStates().getActions().size()); assertEquals(offer, newphase.getPartyStates().getActions().get(0)); assertFalse( newphase.getPartyStates().getExceptions().containsKey(party1)); } @Test public void testAllowedActingTooLate() { try { phase.checkAction(party1, offer, DEADLINE + 1); } catch (ProtocolException e) { assertTrue(e.getMessage().contains("passed deadline")); return; } fail("checkAction did not throw as expected"); } @Test public void getOffersTest() { assertEquals(Collections.emptyList(), phase.getOffers()); List offs = phase.with(party1, offer, 1).getOffers(); assertEquals(1, offs.size()); assertEquals(bid, offs.get(0).getBid()); } @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); } }