package geniusweb.voting.votingevaluators; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; 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.Action; import geniusweb.actions.PartyId; import geniusweb.actions.Vote; import geniusweb.actions.Votes; import geniusweb.inform.Agreements; import geniusweb.issuevalue.Bid; import geniusweb.voting.CollectedVotes; import geniusweb.voting.VotingEvaluator; import tudelft.utilities.junit.GeneralTests; public class LargestAgreementsLoopTest extends GeneralTests { private final ObjectMapper jackson = new ObjectMapper(); private final Bid a = mock(Bid.class), b = mock(Bid.class), c = mock(Bid.class), d = mock(Bid.class); private LargestAgreementsLoop lal1 = new LargestAgreementsLoop(); private LargestAgreementsLoop lal1a = new LargestAgreementsLoop(); private final PartyId party1 = new PartyId("party1"); private final PartyId party2 = new PartyId("party2"); private final PartyId party3 = new PartyId("party3"); private final PartyId party4 = new PartyId("party4"); private Votes votes1 = mock(Votes.class); private LargestAgreementsLoop lal2 = new LargestAgreementsLoop( new CollectedVotes(Collections.singletonMap(party1, votes1), Collections.singletonMap(party1, 1))); private String lalstring = "{\"LargestAgreementsLoop\":{}}"; @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(lal1, lal1a), Arrays.asList(lal2)); } @Override public List getGeneralTestStrings() { return Arrays.asList("LargestAgreementsLoop", "LargestAgreementsLoop"); } @Test public void serializeAcceptTest() throws JsonProcessingException { // we use lal2 to also check we don't serialize the contents System.out.println(jackson.writeValueAsString(lal2)); assertEquals(lalstring, jackson.writeValueAsString(lal2)); } @Test public void deserializeAcceptTest() throws IOException { VotingEvaluator eval = jackson.readValue(lalstring, VotingEvaluator.class); // it should deserialize as the empty lal1 because we ignore the fields. assertEquals(lal1, eval); } @Test public void testCollectVotes() { Votes vote1AB = new Votes(party1, new HashSet<>(Arrays .asList(new Vote(party1, a, 2, 9), new Vote(party1, b, 2, 9)))); Votes vote2AB = new Votes(party2, new HashSet<>(Arrays .asList(new Vote(party2, a, 2, 9), new Vote(party2, b, 2, 9)))); Votes vote3C = new Votes(party3, Collections.singleton(new Vote(party3, c, 2, 9))); Votes vote4AC = new Votes(party4, new HashSet<>(Arrays .asList(new Vote(party4, a, 2, 9), new Vote(party4, c, 2, 9)))); // party 1,2,4 vote for A, party 1,2 vote for B, party 3,4 vote for C. // the biggest vote is P,Q,S Map votes = new HashMap<>(); votes.put(party1, vote1AB); votes.put(party2, vote2AB); votes.put(party3, vote3C); votes.put(party4, vote4AC); List> actions1 = Arrays .asList(Arrays.asList(vote1AB, vote2AB, vote3C, vote4AC)); Map power = new HashMap<>(); power.put(party1, 1); power.put(party2, 1); power.put(party3, 1); power.put(party4, 1); Agreements agrees = new LargestAgreementsLoop() .create(new CollectedVotes(votes, power)).getAgreements(); System.out.println(agrees); // biggest agreement is party 1,2,4 for bid a. assertEquals(new HashSet<>(Arrays.asList(party1, party2, party4)), agrees.getMap().keySet()); } }