Skip to content

Commit bf5bb8b

Browse files
committed
vote receipt contents #203
1 parent cd72cd4 commit bf5bb8b

File tree

12 files changed

+170
-108
lines changed

12 files changed

+170
-108
lines changed

src/main/java/org/rulez/demokracia/pdengine/IVoteManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import javax.xml.ws.WebServiceContext;
77

88
import org.json.JSONObject;
9+
import org.rulez.demokracia.pdengine.dataobjects.CastVote;
910
import org.rulez.demokracia.pdengine.dataobjects.ChoiceEntity;
1011
import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo;
1112
import org.rulez.demokracia.pdengine.dataobjects.VoteParameters;
@@ -35,7 +36,7 @@ VoteAdminInfo createVote(final String voteName, final Set<String> neededAssuranc
3536

3637
String obtainBallot(final String identifier, final String adminKey);
3738

38-
void castVote(final String voteId, final String ballot, final List<RankedChoice> theVote);
39+
CastVote castVote(final String voteId, final String ballot, final List<RankedChoice> theVote);
3940

4041
String getWsUserName();
4142

src/main/java/org/rulez/demokracia/pdengine/Vote.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private JSONObject choiceAsJson(final String key, final Choice choice) {
118118
return obj;
119119
}
120120

121-
protected void addCastVote(final String proxyId, final List<RankedChoice> theVote) {
121+
protected CastVote addCastVote(final String proxyId, final List<RankedChoice> theVote) {
122122
Iterator<CastVote> listIterator = votesCast.iterator();
123123
while (listIterator.hasNext()) {
124124
CastVote element = listIterator.next();
@@ -129,5 +129,6 @@ protected void addCastVote(final String proxyId, final List<RankedChoice> theVot
129129

130130
CastVote castVote = new CastVote(proxyId, theVote);
131131
votesCast.add(castVote);
132+
return castVote;
132133
}
133134
}

src/main/java/org/rulez/demokracia/pdengine/VoteRegistry.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import javax.xml.ws.WebServiceContext;
66

77
import org.json.JSONObject;
8+
import org.rulez.demokracia.pdengine.dataobjects.CastVote;
89
import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo;
910
import org.rulez.demokracia.pdengine.dataobjects.VoteParameters;
1011
import org.rulez.demokracia.pdengine.exception.ReportedException;
@@ -48,20 +49,22 @@ public boolean userHasAllAssurance(final List<String> neededAssuranceList) {
4849
}
4950

5051
@Override
51-
public void castVote(final String voteId, final String ballot, final List<RankedChoice> theVote) {
52+
public CastVote castVote(final String voteId, final String ballot, final List<RankedChoice> theVote) {
5253
Vote vote = getVote(voteId);
5354

5455
checkIfVotingEnabled(vote);
5556
checkIfUpdateConditionsAreConsistent(vote);
5657
validateBallot(ballot, vote);
5758
validatePreferences(theVote, vote);
5859

60+
CastVote receipt;
5961
if (vote.canUpdate)
60-
vote.addCastVote(getWsUserName(), theVote);
62+
receipt = vote.addCastVote(getWsUserName(), theVote);
6163
else
62-
vote.addCastVote(null, theVote);
64+
receipt = vote.addCastVote(null, theVote);
6365

6466
vote.ballots.remove(ballot);
67+
return receipt;
6568
}
6669

6770
private void validatePreferences(final List<RankedChoice> theVote, Vote vote) {

src/test/java/org/rulez/demokracia/pdengine/ObtainBallotInvariantsTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
@TestedOperation("Obtain ballot")
1616
public class ObtainBallotInvariantsTest extends CreatedDefaultChoice{
1717

18-
private Vote vote;
1918
private String originalVoteId;
2019
private String originalAdminKey;
2120
private List<String> originalNeededAssurances;

src/test/java/org/rulez/demokracia/pdengine/ObtainBallotTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
@TestedOperation("Obtain ballot")
1515
public class ObtainBallotTest extends CreatedDefaultChoice{
1616

17-
private Vote vote;
18-
1917
@Before
2018
public void setUp() {
2119
super.setUp();
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.rulez.demokracia.pdengine;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour;
11+
import org.rulez.demokracia.pdengine.annotations.TestedFeature;
12+
import org.rulez.demokracia.pdengine.annotations.TestedOperation;
13+
import org.rulez.demokracia.pdengine.testhelpers.CreatedDefaultChoice;
14+
15+
@TestedFeature("Vote")
16+
@TestedOperation("Cast vote")
17+
@TestedBehaviour("if there was a cast vote from the same user, the old one is deleted")
18+
public class VoteCastAgainTest extends CreatedDefaultChoice {
19+
20+
@Before
21+
public void setUp() {
22+
super.setUp();
23+
initializeVoteCastTest();
24+
}
25+
26+
@Test
27+
public void cast_vote_records_the_vote_with_same_user_votesCast_when_same_user_is_in_the_top_of_the_list() {
28+
vote.canUpdate = true;
29+
30+
addCastVote(TEST_USER_NAME, theCastVote);
31+
addfirstDummies();
32+
addSecondDummies();
33+
34+
voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
35+
36+
assertTrue(vote.votesCast.get(7).proxyId.equals(TEST_USER_NAME));
37+
}
38+
39+
@Test
40+
public void cast_vote_records_the_vote_with_same_user_votesCast_when_same_user_is_in_the_bottom_of_the_list() {
41+
List<RankedChoice> theCastVote1 = new ArrayList<>();
42+
vote.canUpdate = true;
43+
44+
addfirstDummies();
45+
addSecondDummies();
46+
addCastVote(TEST_USER_NAME, theCastVote1);
47+
48+
voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
49+
50+
assertTrue(vote.votesCast.get(7).preferences.equals(theCastVote1));
51+
}
52+
53+
@Test
54+
public void cast_vote_records_the_vote_with_same_user_votesCast_when_same_user_is_in_the_middle_of_the_list() {
55+
vote.canUpdate = true;
56+
57+
addfirstDummies();
58+
addCastVote(TEST_USER_NAME, theCastVote);
59+
addSecondDummies();
60+
61+
voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
62+
63+
assertTrue(vote.votesCast.get(7).proxyId.equals(TEST_USER_NAME));
64+
}
65+
66+
@Test
67+
public void cast_vote_records_the_vote_with_same_user_votesCast_when_the_list_does_not_contain_same_user() {
68+
vote.canUpdate = true;
69+
70+
addCastVote("OtherUser1", theCastVote);
71+
addCastVote("OtherUser2", theCastVote);
72+
addCastVote("OtherUser3", theCastVote);
73+
addCastVote("OtherUser4", theCastVote);
74+
voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
75+
76+
assertTrue(vote.votesCast.get(4).proxyId.equals(TEST_USER_NAME));
77+
}
78+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.rulez.demokracia.pdengine;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour;
8+
import org.rulez.demokracia.pdengine.annotations.TestedFeature;
9+
import org.rulez.demokracia.pdengine.annotations.TestedOperation;
10+
import org.rulez.demokracia.pdengine.dataobjects.CastVote;
11+
import org.rulez.demokracia.pdengine.testhelpers.CreatedDefaultChoice;
12+
13+
@TestedFeature("Vote")
14+
@TestedOperation("Cast vote")
15+
@TestedBehaviour("The vote receipt contains the ballot cast and the cast vote identifier")
16+
public class VoteCastReceiptTest extends CreatedDefaultChoice {
17+
@Before
18+
public void setUp() {
19+
super.setUp();
20+
21+
ballot = voteManager.obtainBallot(adminInfo.voteId, adminInfo.adminKey);
22+
vote = getTheVote();
23+
vote.canVote = true;
24+
vote.canUpdate = true;
25+
vote.votesCast.clear();
26+
}
27+
28+
@Test
29+
public void cast_vote_returns_the_cast_vote_id() {
30+
CastVote receipt = voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
31+
assertEquals(vote.votesCast.get(0).secretId, receipt.secretId);
32+
}
33+
34+
@Test
35+
public void cast_vote_returns_the_cast_vote_preferences() {
36+
CastVote receipt = voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
37+
assertEquals(vote.votesCast.get(0).preferences.get(0).choiceId, receipt.preferences.get(0).choiceId);
38+
}
39+
40+
}

src/test/java/org/rulez/demokracia/pdengine/VoteCastTest.java

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import static org.junit.Assert.*;
44

5-
import java.util.ArrayList;
6-
import java.util.List;
7-
85
import org.junit.Before;
96
import org.junit.Test;
107
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour;
@@ -17,19 +14,10 @@
1714
@TestedOperation("Cast vote")
1815
public class VoteCastTest extends CreatedDefaultChoice {
1916

20-
private String ballot;
21-
private List<RankedChoice> theCastVote;
22-
private Vote vote;
23-
2417
@Before
2518
public void setUp() {
2619
super.setUp();
27-
28-
ballot = voteManager.obtainBallot(adminInfo.voteId, adminInfo.adminKey);
29-
theCastVote = new ArrayList<>();
30-
vote = getTheVote();
31-
vote.canVote = true;
32-
vote.votesCast.clear();
20+
initializeVoteCastTest();
3321
}
3422

3523
@TestedBehaviour("records cast vote with the vote and user's proxy id")
@@ -55,79 +43,4 @@ public void voteCast_records_a_secret_id_with_the_vote_cast() {
5543
voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
5644
assertTrue(vote.votesCast.get(0).secretId instanceof String);
5745
}
58-
59-
@TestedBehaviour("if there was a cast vote from the same user, the old one is deleted")
60-
@Test
61-
public void cast_vote_records_the_vote_with_same_user_votesCast_when_same_user_is_in_the_top_of_the_list() {
62-
vote.canUpdate = true;
63-
64-
addCastVote(TEST_USER_NAME, theCastVote);
65-
addfirstDummies();
66-
addSecondDummies();
67-
68-
voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
69-
70-
assertTrue(vote.votesCast.get(7).proxyId.equals(TEST_USER_NAME));
71-
}
72-
73-
@TestedBehaviour("if there was a cast vote from the same user, the old one is deleted")
74-
@Test
75-
public void cast_vote_records_the_vote_with_same_user_votesCast_when_same_user_is_in_the_bottom_of_the_list() {
76-
List<RankedChoice> theCastVote1 = new ArrayList<>();
77-
vote.canUpdate = true;
78-
79-
addfirstDummies();
80-
addSecondDummies();
81-
addCastVote(TEST_USER_NAME, theCastVote1);
82-
83-
voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
84-
85-
assertTrue(vote.votesCast.get(7).preferences.equals(theCastVote1));
86-
}
87-
88-
@TestedBehaviour("if there was a cast vote from the same user, the old one is deleted")
89-
@Test
90-
public void cast_vote_records_the_vote_with_same_user_votesCast_when_same_user_is_in_the_middle_of_the_list() {
91-
vote.canUpdate = true;
92-
93-
addfirstDummies();
94-
addCastVote(TEST_USER_NAME, theCastVote);
95-
addSecondDummies();
96-
97-
voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
98-
99-
assertTrue(vote.votesCast.get(7).proxyId.equals(TEST_USER_NAME));
100-
}
101-
102-
private void addSecondDummies() {
103-
addCastVote("dummy4", theCastVote);
104-
addCastVote("dummy5", theCastVote);
105-
addCastVote("dummy6", theCastVote);
106-
addCastVote("dummy7", theCastVote);
107-
}
108-
109-
private void addfirstDummies() {
110-
addCastVote("dummy1", theCastVote);
111-
addCastVote("dummy2", theCastVote);
112-
addCastVote("dummy3", theCastVote);
113-
}
114-
115-
@TestedBehaviour("if there was a cast vote from the same user, the old one is deleted")
116-
@Test
117-
public void cast_vote_records_the_vote_with_same_user_votesCast_when_the_list_does_not_contain_same_user() {
118-
vote.canUpdate = true;
119-
120-
addCastVote("OtherUser1", theCastVote);
121-
addCastVote("OtherUser2", theCastVote);
122-
addCastVote("OtherUser3", theCastVote);
123-
addCastVote("OtherUser4", theCastVote);
124-
voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
125-
126-
assertTrue(vote.votesCast.get(4).proxyId.equals(TEST_USER_NAME));
127-
}
128-
129-
private void addCastVote(final String proxyId, final List<RankedChoice> theCastVote) {
130-
vote.votesCast.add(new CastVote(proxyId, theCastVote));
131-
}
132-
13346
}

src/test/java/org/rulez/demokracia/pdengine/VoteCastUpdatableTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.junit.Assert.*;
44

55
import java.util.ArrayList;
6-
import java.util.List;
76

87
import org.junit.Before;
98
import org.junit.Test;
@@ -15,10 +14,6 @@
1514

1615
public class VoteCastUpdatableTest extends CreatedDefaultChoice {
1716

18-
private String ballot;
19-
private List<RankedChoice> theCastVote;
20-
private Vote vote;
21-
2217
@Before
2318
public void setUp() {
2419
super.setUp();

src/test/java/org/rulez/demokracia/pdengine/VoteShowTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.rulez.demokracia.pdengine;
22

33
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.fail;
5-
64
import org.json.JSONArray;
75
import org.json.JSONObject;
86
import org.junit.Before;

src/test/java/org/rulez/demokracia/pdengine/VoteTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ public class VoteTest extends CreatedDefaultChoice {
1919

2020
private static final int LAST_WRONG_RANK = -1;
2121
private static final int FIRST_GOOD_RANK = 0;
22-
private Vote vote;
23-
private String ballot;
24-
private List<RankedChoice> theCastVote;
25-
26-
2722
@Before
2823
public void setUp() {
2924
super.setUp();

0 commit comments

Comments
 (0)