source: events/src/test/java/geniusweb/actions/VotesWithValueTest.java@ 47

Last change on this file since 47 was 47, checked in by bart, 3 years ago

Refactor to help reusing partiesserver.

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