source: events/src/test/java/actions/VotesTest.java@ 21

Last change on this file since 21 was 21, checked in by bart, 4 years ago

Version 1.5.

File size: 3.6 KB
RevLine 
[21]1package actions;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6
7import java.io.IOException;
8import java.util.Arrays;
9import java.util.List;
10
11import org.junit.Test;
12
13import com.fasterxml.jackson.core.JsonProcessingException;
14import com.fasterxml.jackson.databind.ObjectMapper;
15
16import geniusweb.actions.Action;
17import geniusweb.actions.PartyId;
18import geniusweb.actions.Vote;
19import geniusweb.actions.Votes;
20import geniusweb.issuevalue.Bid;
21import geniusweb.issuevalue.DiscreteValue;
22import tudelft.utilities.junit.GeneralTests;
23
24public 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}},{\"vote\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"is1\":\"val2\"}},\"minPower\":2}}]}}";
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);
33 private Vote voteA2 = new Vote(partyA, bid2, 2);
34 private Vote voteB1 = new Vote(partyB, bid1, 2);
35 private Vote voteB2 = new Vote(partyB, bid2, 2);
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 isExtendingTestLessPower() {
97 Vote powervoteA1 = new Vote(partyA, bid1, 3);
98 Votes powerVotes = new Votes(partyA, Arrays.asList(powervoteA1));
99 assertFalse(powerVotes.isExtending(votes1));
100 assertTrue(votes1.isExtending(powerVotes));
101 }
102
103 @Test(expected = IllegalArgumentException.class)
104 public void testDuplicateBidsNotAllowed() {
105 Vote voteA1similar = new Vote(partyA, bid1, 3);
106 Votes votes = new Votes(partyA, Arrays.asList(voteA1, voteA1similar));
107 }
108}
Note: See TracBrowser for help on using the repository browser.