1 | package actions;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.junit.Test;
|
---|
12 |
|
---|
13 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
14 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
15 |
|
---|
16 | import geniusweb.actions.Action;
|
---|
17 | import geniusweb.actions.PartyId;
|
---|
18 | import geniusweb.actions.Vote;
|
---|
19 | import geniusweb.actions.Votes;
|
---|
20 | import geniusweb.issuevalue.Bid;
|
---|
21 | import geniusweb.issuevalue.DiscreteValue;
|
---|
22 | import tudelft.utilities.junit.GeneralTests;
|
---|
23 |
|
---|
24 | public class VotesTest extends GeneralTests<Votes> {
|
---|
25 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
26 | private final String votestring = "{\"Votes\":{\"actor\":\"partyA\",\"votes\":[{\"vote\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"is1\":\"val1\"}},\"minPower\":2,\"maxPower\":9}},{\"vote\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"is1\":\"val2\"}},\"minPower\":2,\"maxPower\":9}}]}}";
|
---|
27 |
|
---|
28 | private PartyId partyA = new PartyId("partyA");
|
---|
29 | private PartyId partyB = new PartyId("partyB");
|
---|
30 | private Bid bid1 = new Bid("is1", new DiscreteValue("val1")),
|
---|
31 | bid2 = new Bid("is1", new DiscreteValue("val2"));
|
---|
32 | private Vote voteA1 = new Vote(partyA, bid1, 2, 9);
|
---|
33 | private Vote voteA2 = new Vote(partyA, bid2, 2, 9);
|
---|
34 | private Vote voteB1 = new Vote(partyB, bid1, 2, 9);
|
---|
35 | private Vote voteB2 = new Vote(partyB, bid2, 2, 9);
|
---|
36 |
|
---|
37 | private Votes votes1 = new Votes(partyA, Arrays.asList(voteA1, voteA2));
|
---|
38 | private Votes votes1a = new Votes(partyA, Arrays.asList(voteA1, voteA2));
|
---|
39 | private Votes votes2 = new Votes(partyB, Arrays.asList(voteB1, voteB2));
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public List<List<Votes>> getGeneralTestData() {
|
---|
43 | return Arrays.asList(Arrays.asList(votes1, votes1a),
|
---|
44 | Arrays.asList(votes2));
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public List<String> getGeneralTestStrings() {
|
---|
49 | return Arrays.asList(
|
---|
50 | "Votes.*Vote.*partyA,Bid.*is1.*val1.*2.*Vote.*partyA,Bid.*is1.*val2.*2.*",
|
---|
51 | "Votes.*Vote.*partyB,Bid.*is1.*val1.*2.*Vote.*partyB,Bid.*is1.*val2.*2.*");
|
---|
52 | }
|
---|
53 |
|
---|
54 | @SuppressWarnings("unused")
|
---|
55 | @Test(expected = IllegalArgumentException.class)
|
---|
56 | public void testRejectVoteWithIncorrectParty() {
|
---|
57 | new Votes(partyA, Arrays.asList(voteB1));
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Test
|
---|
61 | public void serializeTest() throws JsonProcessingException {
|
---|
62 | System.out.println(jackson.writeValueAsString(votes1));
|
---|
63 | assertEquals(votestring, jackson.writeValueAsString(votes1));
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Test
|
---|
67 | public void deserializeTest() throws IOException {
|
---|
68 | Action act = jackson.readValue(votestring, Action.class);
|
---|
69 | assertEquals(votes1, act);
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Test(expected = IllegalArgumentException.class)
|
---|
73 | public void testWrongPartyName() {
|
---|
74 | new Votes(partyA, Arrays.asList(voteB1, voteB2));
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Test
|
---|
78 | public void isExtendingTestExtraVote() {
|
---|
79 | Votes otherVotes = new Votes(partyA, Arrays.asList(voteA1));
|
---|
80 | assertTrue(votes1.isExtending(otherVotes));
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Test
|
---|
84 | public void isExtendingTestNoVotes() {
|
---|
85 | Votes otherVotes = new Votes(partyA, Arrays.asList());
|
---|
86 | assertTrue(votes1.isExtending(otherVotes));
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test
|
---|
90 | public void isExtendingTestMissing() {
|
---|
91 | Votes otherVotes = new Votes(partyA, Arrays.asList(voteA1));
|
---|
92 | assertFalse(otherVotes.isExtending(votes1));
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Test
|
---|
96 | public void isExtendingIdenticalVotes() {
|
---|
97 | assertTrue(votes1.isExtending(votes1));
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Test
|
---|
101 | public void isReallyExtendingVotesMin() {
|
---|
102 | Vote powervoteA1 = new Vote(partyA, bid1, 1, 9);
|
---|
103 | Votes powerVotes = new Votes(partyA,
|
---|
104 | Arrays.asList(powervoteA1, voteA2));
|
---|
105 | assertTrue(powerVotes.isExtending(votes1));
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Test
|
---|
109 | public void isReallyExtendingVotesMax() {
|
---|
110 | Vote powervoteA1 = new Vote(partyA, bid1, 2, 10);
|
---|
111 | Votes powerVotes = new Votes(partyA,
|
---|
112 | Arrays.asList(powervoteA1, voteA2));
|
---|
113 | assertTrue(powerVotes.isExtending(votes1));
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Test
|
---|
117 | public void IsNarrowingOneVoteRemoved() {
|
---|
118 | Votes narrowVotes = new Votes(partyA, Arrays.asList(voteA2));
|
---|
119 | assertFalse(narrowVotes.isExtending(votes1));
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Test
|
---|
123 | public void isNarrowingTestMoreMinPower() {
|
---|
124 | Vote narrowVoteA1 = new Vote(partyA, bid1, 3, 9);
|
---|
125 | Votes narrowVotes = new Votes(partyA,
|
---|
126 | Arrays.asList(narrowVoteA1, voteA2));
|
---|
127 | assertFalse(narrowVotes.isExtending(votes1));
|
---|
128 | assertTrue(votes1.isExtending(narrowVotes));
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Test
|
---|
132 | public void isNarrowingLessMaxPower() {
|
---|
133 | Vote powervoteA1 = new Vote(partyA, bid1, 2, 8);
|
---|
134 | Votes narrowVotes = new Votes(partyA,
|
---|
135 | Arrays.asList(powervoteA1, voteA2));
|
---|
136 | assertFalse(narrowVotes.isExtending(votes1));
|
---|
137 | assertTrue(votes1.isExtending(narrowVotes));
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Test(expected = IllegalArgumentException.class)
|
---|
141 | public void testDuplicateBidsNotAllowed() {
|
---|
142 | Vote voteA1similar = new Vote(partyA, bid1, 3, 9);
|
---|
143 | Votes votes = new Votes(partyA, Arrays.asList(voteA1, voteA1similar));
|
---|
144 | }
|
---|
145 | }
|
---|