package geniusweb.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.Collections; import java.util.HashSet; import java.util.List; import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.DiscreteValue; import tudelft.utilities.junit.GeneralTests; public class VotesWithValueTest extends GeneralTests { private final ObjectMapper jackson = new ObjectMapper(); private final String votestring = "{\"VotesWithValue\":{\"actor\":\"partyA\",\"votes\":[{\"VoteWithValue\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"is1\":\"val1\"}},\"minPower\":2,\"maxPower\":9,\"value\":30}},{\"VoteWithValue\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"is1\":\"val2\"}},\"minPower\":2,\"maxPower\":9,\"value\":70}}]}}"; 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 VoteWithValue voteA1 = new VoteWithValue(partyA, bid1, 2, 9, 30); private VoteWithValue voteA2 = new VoteWithValue(partyA, bid2, 2, 9, 70); private VoteWithValue voteA1_100 = new VoteWithValue(partyA, bid1, 2, 9, 100); private VoteWithValue voteA2_100 = new VoteWithValue(partyA, bid2, 2, 9, 100); private VoteWithValue voteB1 = new VoteWithValue(partyB, bid1, 2, 9, 40); private VoteWithValue voteB2 = new VoteWithValue(partyB, bid2, 2, 9, 60); private VotesWithValue votes1 = new VotesWithValue(partyA, new HashSet<>(Arrays.asList(voteA1, voteA2))); private VotesWithValue votes1a = new VotesWithValue(partyA, new HashSet<>(Arrays.asList(voteA1, voteA2))); private VotesWithValue votes2 = new VotesWithValue(partyB, new HashSet<>(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.*val2.*2.*Vote.*partyB,Bid.*is1.*val1.*2.*"); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testRejectVoteWithIncorrectParty() { new VotesWithValue(partyA, Collections.singleton(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 VotesWithValue(partyA, new HashSet<>(Arrays.asList(voteB1, voteB2))); } @Test public void isExtendingTestExtraVote() { VotesWithValue otherVotes = new VotesWithValue(partyA, Collections.singleton(voteA1_100)); assertTrue(votes1.isExtending(otherVotes)); } @Test public void isExtendingTestMissing() { VotesWithValue otherVotes = new VotesWithValue(partyA, Collections.singleton(voteA1_100)); assertFalse(otherVotes.isExtending(votes1)); } @Test public void isExtendingIdenticalVotes() { assertTrue(votes1.isExtending(votes1)); } @Test public void isReallyExtendingVotesMin() { VoteWithValue powervoteA1 = new VoteWithValue(partyA, bid1, 1, 9, 30); VotesWithValue powerVotes = new VotesWithValue(partyA, new HashSet<>(Arrays.asList(powervoteA1, voteA2))); assertTrue(powerVotes.isExtending(votes1)); } @Test public void isReallyExtendingVotesMax() { VoteWithValue powervoteA1 = new VoteWithValue(partyA, bid1, 2, 10, 30); VotesWithValue powerVotes = new VotesWithValue(partyA, new HashSet<>(Arrays.asList(powervoteA1, voteA2))); assertTrue(powerVotes.isExtending(votes1)); } @Test public void IsNarrowingOneVoteRemoved() { VotesWithValue narrowVotes = new VotesWithValue(partyA, Collections.singleton(voteA2_100)); assertFalse(narrowVotes.isExtending(votes1)); } @Test public void isNarrowingTestMoreMinPower() { VoteWithValue narrowVoteA1 = new VoteWithValue(partyA, bid1, 3, 9, 30); VotesWithValue narrowVotes = new VotesWithValue(partyA, new HashSet<>(Arrays.asList(narrowVoteA1, voteA2))); assertFalse(narrowVotes.isExtending(votes1)); assertTrue(votes1.isExtending(narrowVotes)); } @Test public void isNarrowingLessMaxPower() { VoteWithValue powervoteA1 = new VoteWithValue(partyA, bid1, 2, 8, 30); VotesWithValue narrowVotes = new VotesWithValue(partyA, new HashSet<>(Arrays.asList(powervoteA1, voteA2))); assertFalse(narrowVotes.isExtending(votes1)); assertTrue(votes1.isExtending(narrowVotes)); } @Test(expected = IllegalArgumentException.class) public void testDuplicateBidsNotAllowed() { VoteWithValue voteA1similar = new VoteWithValue(partyA, bid1, 3, 9, 30); VotesWithValue votes = new VotesWithValue(partyA, new HashSet<>(Arrays.asList(voteA1, voteA1similar))); } }