1 | package geniusweb.voting.evaluatorwithvalue;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.mockito.Mockito.mock;
|
---|
5 | import static org.mockito.Mockito.when;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.List;
|
---|
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.PartyId;
|
---|
20 | import geniusweb.actions.VoteWithValue;
|
---|
21 | import geniusweb.actions.VotesWithValue;
|
---|
22 | import geniusweb.inform.Agreements;
|
---|
23 | import geniusweb.issuevalue.Bid;
|
---|
24 | import geniusweb.voting.CollectedVotesWithValue;
|
---|
25 | import geniusweb.voting.VotingEvaluatorWithValue;
|
---|
26 | import tudelft.utilities.junit.GeneralTests;
|
---|
27 |
|
---|
28 | public class LargestAgreementWithValueTest
|
---|
29 | extends GeneralTests<LargestAgreementWithValue> {
|
---|
30 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
31 |
|
---|
32 | private LargestAgreementWithValue lal1 = new LargestAgreementWithValue();
|
---|
33 | private LargestAgreementWithValue lal1a = new LargestAgreementWithValue();
|
---|
34 | private PartyId party1 = new PartyId("party1");
|
---|
35 | private PartyId party2 = new PartyId("party2");
|
---|
36 | private PartyId party3 = new PartyId("party3");
|
---|
37 | private PartyId party4 = new PartyId("party4");
|
---|
38 | private VotesWithValue votes1 = mock(VotesWithValue.class);
|
---|
39 | private LargestAgreementWithValue lal2 = new LargestAgreementWithValue(
|
---|
40 | new CollectedVotesWithValue(
|
---|
41 | Collections.singletonMap(party1, votes1),
|
---|
42 | Collections.singletonMap(party1, 1)));
|
---|
43 |
|
---|
44 | private String lalstring = "{\"LargestAgreementWithValue\":{}}";
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public List<List<LargestAgreementWithValue>> getGeneralTestData() {
|
---|
48 | return Arrays.asList(Arrays.asList(lal1, lal1a), Arrays.asList(lal2));
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public List<String> getGeneralTestStrings() {
|
---|
53 | return Arrays.asList("LargestAgreement", "LargestAgreement");
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Test
|
---|
57 | public void serializeTest() throws JsonProcessingException {
|
---|
58 | // we use lal2 to also check we don't serialize the contents
|
---|
59 | System.out.println(jackson.writeValueAsString(lal2));
|
---|
60 | assertEquals(lalstring, jackson.writeValueAsString(lal2));
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Test
|
---|
64 | public void deserializeTest() throws IOException {
|
---|
65 | VotingEvaluatorWithValue eval = jackson.readValue(lalstring,
|
---|
66 | VotingEvaluatorWithValue.class);
|
---|
67 | // it should deserialize as the empty lal1 because we ignore the fields.
|
---|
68 | assertEquals(lal1, eval);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Test
|
---|
72 | public void testCollectVotes() {
|
---|
73 | Bid bid1 = mock(Bid.class);
|
---|
74 | when(bid1.toString()).thenReturn("bid1");
|
---|
75 | Bid bid2 = mock(Bid.class);
|
---|
76 | when(bid2.toString()).thenReturn("bid2");
|
---|
77 |
|
---|
78 | VotesWithValue votes1 = new VotesWithValue(party1,
|
---|
79 | new HashSet<>(Arrays.asList(
|
---|
80 | new VoteWithValue(party1, bid1, 2, 99, 40),
|
---|
81 | new VoteWithValue(party1, bid2, 2, 99, 60))));
|
---|
82 | VotesWithValue votes2 = new VotesWithValue(party2, new HashSet<>(
|
---|
83 | Arrays.asList(new VoteWithValue(party2, bid1, 2, 99, 100))));
|
---|
84 | VotesWithValue votes3 = new VotesWithValue(party3, new HashSet<>(
|
---|
85 | Arrays.asList(new VoteWithValue(party3, bid1, 2, 99, 100))));
|
---|
86 | VotesWithValue votes4 = new VotesWithValue(party4, new HashSet<>(
|
---|
87 | Arrays.asList(new VoteWithValue(party4, bid1, 2, 99, 100))));
|
---|
88 | HashMap<PartyId, VotesWithValue> votesmap = new HashMap<>();
|
---|
89 | votesmap.put(party1, votes1);
|
---|
90 | votesmap.put(party2, votes2);
|
---|
91 | votesmap.put(party3, votes3);
|
---|
92 | votesmap.put(party4, votes4);
|
---|
93 | HashMap<PartyId, Integer> powermap = new HashMap<>();
|
---|
94 | powermap.put(party1, 1);
|
---|
95 | powermap.put(party2, 1);
|
---|
96 | powermap.put(party3, 1);
|
---|
97 | powermap.put(party4, 4);
|
---|
98 |
|
---|
99 | CollectedVotesWithValue votes = new CollectedVotesWithValue(votesmap,
|
---|
100 | powermap);
|
---|
101 |
|
---|
102 | LargestAgreementWithValue la = new LargestAgreementWithValue(votes);
|
---|
103 | Agreements agreement = la.getAgreements();
|
---|
104 | assertEquals(
|
---|
105 | new HashSet<PartyId>(
|
---|
106 | Arrays.asList(party1, party2, party3, party4)),
|
---|
107 | agreement.getMap().keySet());
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Test
|
---|
111 | public void testEmptyCollectVotes() {
|
---|
112 |
|
---|
113 | CollectedVotesWithValue votes = new CollectedVotesWithValue(
|
---|
114 | Collections.emptyMap(), Collections.emptyMap());
|
---|
115 |
|
---|
116 | LargestAgreementWithValue la = new LargestAgreementWithValue(votes);
|
---|
117 | Agreements agreement = la.getAgreements();
|
---|
118 | }
|
---|
119 | }
|
---|