[43] | 1 | package geniusweb.voting.votingevaluators;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 | import static org.mockito.Mockito.mock;
|
---|
| 5 |
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 | import java.util.Arrays;
|
---|
| 8 | import java.util.Collections;
|
---|
| 9 | import java.util.HashMap;
|
---|
| 10 | import java.util.HashSet;
|
---|
| 11 | import java.util.List;
|
---|
| 12 | import java.util.Map;
|
---|
| 13 |
|
---|
| 14 | import org.junit.Test;
|
---|
| 15 |
|
---|
| 16 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 17 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 18 |
|
---|
| 19 | import geniusweb.actions.Action;
|
---|
| 20 | import geniusweb.actions.PartyId;
|
---|
| 21 | import geniusweb.actions.Vote;
|
---|
| 22 | import geniusweb.actions.Votes;
|
---|
| 23 | import geniusweb.inform.Agreements;
|
---|
| 24 | import geniusweb.issuevalue.Bid;
|
---|
| 25 | import geniusweb.voting.CollectedVotes;
|
---|
| 26 | import geniusweb.voting.VotingEvaluator;
|
---|
| 27 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 28 |
|
---|
| 29 | public class LargestAgreementsLoopTest
|
---|
| 30 | extends GeneralTests<LargestAgreementsLoop> {
|
---|
| 31 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
| 32 |
|
---|
| 33 | private final Bid a = mock(Bid.class), b = mock(Bid.class),
|
---|
| 34 | c = mock(Bid.class), d = mock(Bid.class);
|
---|
| 35 |
|
---|
| 36 | private LargestAgreementsLoop lal1 = new LargestAgreementsLoop();
|
---|
| 37 | private LargestAgreementsLoop lal1a = new LargestAgreementsLoop();
|
---|
| 38 | private final PartyId party1 = new PartyId("party1");
|
---|
| 39 | private final PartyId party2 = new PartyId("party2");
|
---|
| 40 | private final PartyId party3 = new PartyId("party3");
|
---|
| 41 | private final PartyId party4 = new PartyId("party4");
|
---|
| 42 | private Votes votes1 = mock(Votes.class);
|
---|
| 43 | private LargestAgreementsLoop lal2 = new LargestAgreementsLoop(
|
---|
| 44 | new CollectedVotes(Collections.singletonMap(party1, votes1),
|
---|
| 45 | Collections.singletonMap(party1, 1)));
|
---|
| 46 |
|
---|
| 47 | private String lalstring = "{\"LargestAgreementsLoop\":{}}";
|
---|
| 48 |
|
---|
| 49 | @Override
|
---|
| 50 | public List<List<LargestAgreementsLoop>> getGeneralTestData() {
|
---|
| 51 | return Arrays.asList(Arrays.asList(lal1, lal1a), Arrays.asList(lal2));
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | @Override
|
---|
| 55 | public List<String> getGeneralTestStrings() {
|
---|
| 56 | return Arrays.asList("LargestAgreementsLoop", "LargestAgreementsLoop");
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | @Test
|
---|
| 60 | public void serializeAcceptTest() throws JsonProcessingException {
|
---|
| 61 | // we use lal2 to also check we don't serialize the contents
|
---|
| 62 | System.out.println(jackson.writeValueAsString(lal2));
|
---|
| 63 | assertEquals(lalstring, jackson.writeValueAsString(lal2));
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | @Test
|
---|
| 67 | public void deserializeAcceptTest() throws IOException {
|
---|
| 68 | VotingEvaluator eval = jackson.readValue(lalstring,
|
---|
| 69 | VotingEvaluator.class);
|
---|
| 70 | // it should deserialize as the empty lal1 because we ignore the fields.
|
---|
| 71 | assertEquals(lal1, eval);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | @Test
|
---|
| 75 | public void testCollectVotes() {
|
---|
| 76 | Votes vote1AB = new Votes(party1, new HashSet<>(Arrays
|
---|
| 77 | .asList(new Vote(party1, a, 2, 9), new Vote(party1, b, 2, 9))));
|
---|
| 78 | Votes vote2AB = new Votes(party2, new HashSet<>(Arrays
|
---|
| 79 | .asList(new Vote(party2, a, 2, 9), new Vote(party2, b, 2, 9))));
|
---|
| 80 | Votes vote3C = new Votes(party3,
|
---|
| 81 | Collections.singleton(new Vote(party3, c, 2, 9)));
|
---|
| 82 | Votes vote4AC = new Votes(party4, new HashSet<>(Arrays
|
---|
| 83 | .asList(new Vote(party4, a, 2, 9), new Vote(party4, c, 2, 9))));
|
---|
| 84 | // party 1,2,4 vote for A, party 1,2 vote for B, party 3,4 vote for C.
|
---|
| 85 | // the biggest vote is P,Q,S
|
---|
| 86 | Map<PartyId, Votes> votes = new HashMap<>();
|
---|
| 87 | votes.put(party1, vote1AB);
|
---|
| 88 | votes.put(party2, vote2AB);
|
---|
| 89 | votes.put(party3, vote3C);
|
---|
| 90 | votes.put(party4, vote4AC);
|
---|
| 91 |
|
---|
| 92 | List<List<Action>> actions1 = Arrays
|
---|
| 93 | .asList(Arrays.asList(vote1AB, vote2AB, vote3C, vote4AC));
|
---|
| 94 | Map<PartyId, Integer> power = new HashMap<>();
|
---|
| 95 | power.put(party1, 1);
|
---|
| 96 | power.put(party2, 1);
|
---|
| 97 | power.put(party3, 1);
|
---|
| 98 | power.put(party4, 1);
|
---|
| 99 | Agreements agrees = new LargestAgreementsLoop()
|
---|
| 100 | .create(new CollectedVotes(votes, power)).getAgreements();
|
---|
| 101 | System.out.println(agrees);
|
---|
| 102 |
|
---|
| 103 | // biggest agreement is party 1,2,4 for bid a.
|
---|
| 104 | assertEquals(new HashSet<>(Arrays.asList(party1, party2, party4)),
|
---|
| 105 | agrees.getMap().keySet());
|
---|
| 106 |
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | }
|
---|