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.HashSet;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.junit.Test;
|
---|
13 |
|
---|
14 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
15 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
16 |
|
---|
17 | import geniusweb.actions.PartyId;
|
---|
18 | import geniusweb.actions.Vote;
|
---|
19 | import geniusweb.actions.Votes;
|
---|
20 | import geniusweb.inform.Agreements;
|
---|
21 | import geniusweb.issuevalue.Bid;
|
---|
22 | import geniusweb.voting.CollectedVotes;
|
---|
23 | import geniusweb.voting.VotingEvaluator;
|
---|
24 | import tudelft.utilities.junit.GeneralTests;
|
---|
25 |
|
---|
26 | public class LargestAgreementTest extends GeneralTests<LargestAgreement> {
|
---|
27 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
28 |
|
---|
29 | private LargestAgreement lal1 = new LargestAgreement();
|
---|
30 | private LargestAgreement lal1a = new LargestAgreement();
|
---|
31 | private PartyId party1 = new PartyId("party1");
|
---|
32 | private PartyId party2 = new PartyId("party2");
|
---|
33 | private PartyId party3 = new PartyId("party3");
|
---|
34 | private PartyId party4 = new PartyId("party4");
|
---|
35 | private Votes votes1 = mock(Votes.class);
|
---|
36 | private LargestAgreement lal2 = new LargestAgreement(
|
---|
37 | new CollectedVotes(Collections.singletonMap(party1, votes1),
|
---|
38 | Collections.singletonMap(party1, 1)));
|
---|
39 |
|
---|
40 | private String lalstring = "{\"LargestAgreement\":{}}";
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public List<List<LargestAgreement>> getGeneralTestData() {
|
---|
44 | return Arrays.asList(Arrays.asList(lal1, lal1a), Arrays.asList(lal2));
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public List<String> getGeneralTestStrings() {
|
---|
49 | return Arrays.asList("LargestAgreement", "LargestAgreement");
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Test
|
---|
53 | public void serializeTest() throws JsonProcessingException {
|
---|
54 | // we use lal2 to also check we don't serialize the contents
|
---|
55 | System.out.println(jackson.writeValueAsString(lal2));
|
---|
56 | assertEquals(lalstring, jackson.writeValueAsString(lal2));
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Test
|
---|
60 | public void deserializeTest() throws IOException {
|
---|
61 | VotingEvaluator eval = jackson.readValue(lalstring,
|
---|
62 | VotingEvaluator.class);
|
---|
63 | // it should deserialize as the empty lal1 because we ignore the fields.
|
---|
64 | assertEquals(lal1, eval);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Test
|
---|
68 | public void testCollectVotes() {
|
---|
69 | // we set up a tricky situation where party4 has much more power
|
---|
70 | // and therefore party 1+4 >> p1+2+3
|
---|
71 | Bid bid1 = mock(Bid.class);
|
---|
72 | Bid bid2 = mock(Bid.class);
|
---|
73 |
|
---|
74 | CollectedVotes votes = new CollectedVotes(Collections.emptyMap(),
|
---|
75 | Collections.emptyMap());
|
---|
76 | votes = votes.with(new Votes(party1,
|
---|
77 | new HashSet<>(Arrays.asList(new Vote(party1, bid1, 1, 99),
|
---|
78 | new Vote(party1, bid2, 1, 99)))),
|
---|
79 | 1);
|
---|
80 | votes = votes.with(
|
---|
81 | new Votes(party2,
|
---|
82 | new HashSet<>(
|
---|
83 | Arrays.asList(new Vote(party2, bid1, 1, 99)))),
|
---|
84 | 1);
|
---|
85 | votes = votes.with(
|
---|
86 | new Votes(party3,
|
---|
87 | new HashSet<>(
|
---|
88 | Arrays.asList(new Vote(party3, bid1, 1, 99)))),
|
---|
89 | 1);
|
---|
90 | votes = votes.with(
|
---|
91 | new Votes(party4,
|
---|
92 | new HashSet<>(
|
---|
93 | Arrays.asList(new Vote(party4, bid2, 1, 99)))),
|
---|
94 | 4);
|
---|
95 | LargestAgreement la = new LargestAgreement(votes);
|
---|
96 | Agreements agreement = la.collectVotes();
|
---|
97 | assertEquals(new HashSet<PartyId>(Arrays.asList(party1, party4)),
|
---|
98 | agreement.getMap().keySet());
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|