package actions; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.Arrays; import java.util.List; import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import geniusweb.actions.Action; import geniusweb.actions.PartyId; import geniusweb.actions.Vote; import geniusweb.actions.Votes; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.DiscreteValue; import tudelft.utilities.junit.GeneralTests; public class VotesTest extends GeneralTests { private final ObjectMapper jackson = new ObjectMapper(); private final String votestring = "{\"Votes\":{\"actor\":\"partyA\",\"votes\":[{\"vote\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"is1\":\"val1\"}},\"minPower\":2}},{\"vote\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"is1\":\"val2\"}},\"minPower\":2}}]}}"; private PartyId partyA = new PartyId("partyA"); private PartyId partyB = new PartyId("partyB"); private Bid bid1 = new Bid("is1", new DiscreteValue("val1")), bid2 = new Bid("is1", new DiscreteValue("val2")); private Vote voteA1 = new Vote(partyA, bid1, 2); private Vote voteA2 = new Vote(partyA, bid2, 2); private Vote voteB1 = new Vote(partyB, bid1, 2); private Vote voteB2 = new Vote(partyB, bid2, 2); private Votes votes1 = new Votes(partyA, Arrays.asList(voteA1, voteA2)); private Votes votes1a = new Votes(partyA, Arrays.asList(voteA1, voteA2)); private Votes votes2 = new Votes(partyB, Arrays.asList(voteB1, voteB2)); @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(votes1, votes1a), Arrays.asList(votes2)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "Votes.*Vote.*partyA,Bid.*is1.*val1.*2.*Vote.*partyA,Bid.*is1.*val2.*2.*", "Votes.*Vote.*partyB,Bid.*is1.*val1.*2.*Vote.*partyB,Bid.*is1.*val2.*2.*"); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testRejectVoteWithIncorrectParty() { new Votes(partyA, Arrays.asList(voteB1)); } @Test public void serializeTest() throws JsonProcessingException { System.out.println(jackson.writeValueAsString(votes1)); assertEquals(votestring, jackson.writeValueAsString(votes1)); } @Test public void deserializeTest() throws IOException { Action act = jackson.readValue(votestring, Action.class); assertEquals(votes1, act); } @Test(expected = IllegalArgumentException.class) public void testWrongPartyName() { new Votes(partyA, Arrays.asList(voteB1, voteB2)); } @Test public void isExtendingTestExtraVote() { Votes otherVotes = new Votes(partyA, Arrays.asList(voteA1)); assertTrue(votes1.isExtending(otherVotes)); } @Test public void isExtendingTestNoVotes() { Votes otherVotes = new Votes(partyA, Arrays.asList()); assertTrue(votes1.isExtending(otherVotes)); } @Test public void isExtendingTestMissing() { Votes otherVotes = new Votes(partyA, Arrays.asList(voteA1)); assertFalse(otherVotes.isExtending(votes1)); } @Test public void isExtendingTestLessPower() { Vote powervoteA1 = new Vote(partyA, bid1, 3); Votes powerVotes = new Votes(partyA, Arrays.asList(powervoteA1)); assertFalse(powerVotes.isExtending(votes1)); assertTrue(votes1.isExtending(powerVotes)); } @Test(expected = IllegalArgumentException.class) public void testDuplicateBidsNotAllowed() { Vote voteA1similar = new Vote(partyA, bid1, 3); Votes votes = new Votes(partyA, Arrays.asList(voteA1, voteA1similar)); } }