diff --git a/src/main/java/org/rulez/demokracia/pdengine/Vote.java b/src/main/java/org/rulez/demokracia/pdengine/Vote.java index 0963d59c..b3791834 100644 --- a/src/main/java/org/rulez/demokracia/pdengine/Vote.java +++ b/src/main/java/org/rulez/demokracia/pdengine/Vote.java @@ -29,7 +29,18 @@ public Vote(String voteName, Collection neededAssurances, Collection(); ballots = new ArrayList(); + recordedBallots = new HashMap(); } + + public Integer getRecordedBallots(String key) { + return recordedBallots.containsKey(key) ? recordedBallots.get(key) : 0; + } + + public void increaseRecordedBallots(String key) { + recordedBallots.put(key, getRecordedBallots(key) + 1); + } + + public String addChoice(String choiceName, String user) { Choice choice = new Choice(choiceName, user); diff --git a/src/main/java/org/rulez/demokracia/pdengine/VoteRegistry.java b/src/main/java/org/rulez/demokracia/pdengine/VoteRegistry.java index ae3ed7cb..5060fe28 100644 --- a/src/main/java/org/rulez/demokracia/pdengine/VoteRegistry.java +++ b/src/main/java/org/rulez/demokracia/pdengine/VoteRegistry.java @@ -11,77 +11,79 @@ public class VoteRegistry extends ChoiceManager implements IVoteManager { public VoteRegistry(WebServiceContext wsContext) { super(wsContext); } - + @Override public String obtainBallot(String id, String adminKey) { Vote vote = getVote(id); - - if(adminKey.equals("anon")) { - if(!userHasAllAssurance(vote.neededAssurances)) + vote.checkAdminKey(adminKey); + + if (adminKey.equals(vote.adminKey)) + vote.increaseRecordedBallots("admin"); + else if (adminKey.equals("user")) + if (!userHasAllAssurance(vote.neededAssurances)) throw new IllegalArgumentException("The user does not have all of the needed assurances."); - }else { - vote.checkAdminKey(adminKey); - } - + else + vote.increaseRecordedBallots(getWsUserName()); + String ballot = RandomUtils.createRandomKey(); vote.ballots.add(ballot); return ballot; } - + public boolean userHasAllAssurance(List neededAssuranceList) { for (String neededAssurance : neededAssuranceList) { if (!hasAssurance(neededAssurance)) { return false; } } - return true; + return true; } @Override public void castVote(String voteId, String ballot, List theVote) { Vote vote = getVote(voteId); - if(! vote.canVote) - throw new IllegalArgumentException("This issue cannot be voted on yet"); - - if (! vote.ballots.contains(ballot)) - throw new IllegalArgumentException(String.format("Illegal ballot: %s", ballot)); - - for(RankedChoice choice : theVote){ - if (! vote.choices.containsKey(choice.choiceId)) + if (!vote.canVote) + throw new IllegalArgumentException("This issue cannot be voted on yet"); + + if (!vote.ballots.contains(ballot)) + throw new IllegalArgumentException(String.format("Illegal ballot: %s", ballot)); + + for (RankedChoice choice : theVote) { + if (!vote.choices.containsKey(choice.choiceId)) throw new IllegalArgumentException(String.format("Invalid choiceId: %s", choice.choiceId)); - if (choice.rank < 0) + if (choice.rank < 0) throw new IllegalArgumentException(String.format("Invalid rank: %d", choice.rank)); } - + vote.ballots.remove(ballot); } @Override public void modifyVote(String voteId, String adminKey, String votename) throws ReportedException { - Validate.checkVoteName(votename); + Validate.checkVoteName(votename); Vote vote = getVote(voteId); vote.checkAdminKey(adminKey); - - if(vote.hasIssuedBallots()) + + if (vote.hasIssuedBallots()) throw new IllegalArgumentException("The vote cannot be modified there are ballots issued."); - + vote.name = votename; } - + public void deleteVote(String voteId, String adminKey) throws ReportedException { Vote vote = getVote(voteId); vote.checkAdminKey(adminKey); - - if(vote.hasIssuedBallots()) + + if (vote.hasIssuedBallots()) throw new IllegalArgumentException("This vote cannot be deleted it has issued ballots."); - + session.remove(vote); } - + public JSONObject showVote(String voteId, String adminKey) throws ReportedException { Vote vote = getVote(voteId); vote.checkAdminKey(adminKey); - + return vote.toJson(voteId); } @@ -90,18 +92,19 @@ public String deleteChoice(String voteId, String choiceId, String adminKey) thro Vote vote = getVote(voteId); vote.checkAdminKey(adminKey); Choice votesChoice = vote.getChoice(choiceId); - - if(vote.hasIssuedBallots()) + + if (vote.hasIssuedBallots()) throw new IllegalArgumentException("This choice cannot be deleted the vote has issued ballots."); - - if(adminKey.equals("user")) - if(votesChoice.userName.equals(getWsUserName())) - if(vote.canAddin) + + if (adminKey.equals("user")) + if (votesChoice.userName.equals(getWsUserName())) + if (vote.canAddin) vote.choices.remove(votesChoice.id); else - throw new IllegalArgumentException("The adminKey is \"user\" but canAddin is false."); + throw new IllegalArgumentException("The adminKey is \"user\" but canAddin is false."); else - throw new IllegalArgumentException("The adminKey is \"user\" but the user is not same with that user who added the choice."); + throw new IllegalArgumentException( + "The adminKey is \"user\" but the user is not same with that user who added the choice."); else vote.choices.remove(votesChoice.id); return "OK"; @@ -112,18 +115,19 @@ public void modifyChoice(String voteId, String choiceId, String adminKey, String vote.checkAdminKey(adminKey); Choice votesChoice = vote.getChoice(choiceId); - if(vote.hasIssuedBallots()) + if (vote.hasIssuedBallots()) throw new IllegalArgumentException("Choice modification disallowed: ballots already issued"); - if("user".equals(adminKey)) { - if(! vote.canAddin) - throw new IllegalArgumentException("Choice modification disallowed: adminKey is user, but canAddin is false"); - - if(! votesChoice.userName.equals(getWsUserName())) - throw new IllegalArgumentException(String.format("Choice modification disallowed: adminKey is user, " + - "and the choice was added by a different user: %s, me: %s", - votesChoice.userName, - getWsUserName())); + if ("user".equals(adminKey)) { + if (!vote.canAddin) + throw new IllegalArgumentException( + "Choice modification disallowed: adminKey is user, but canAddin is false"); + + if (!votesChoice.userName.equals(getWsUserName())) + throw new IllegalArgumentException(String.format( + "Choice modification disallowed: adminKey is user, " + + "and the choice was added by a different user: %s, me: %s", + votesChoice.userName, getWsUserName())); } votesChoice.name = choice; @@ -134,9 +138,9 @@ public void setVoteParameters(String voteId, String adminKey, int minEndorsement boolean canEndorse, boolean canVote, boolean canView) throws ReportedException { Vote vote = getVote(voteId); vote.checkAdminKey(adminKey); - - if(minEndorsements >= 0) - vote.setParameters(adminKey, minEndorsements, canAddin, canEndorse, canVote, canView); + + if (minEndorsements >= 0) + vote.setParameters(adminKey, minEndorsements, canAddin, canEndorse, canVote, canView); else throw new IllegalArgumentException(String.format("Illegal minEndorsements: %s", minEndorsements)); } diff --git a/src/main/java/org/rulez/demokracia/pdengine/dataobjects/VoteEntity.java b/src/main/java/org/rulez/demokracia/pdengine/dataobjects/VoteEntity.java index a69ff8f1..f7e3011b 100644 --- a/src/main/java/org/rulez/demokracia/pdengine/dataobjects/VoteEntity.java +++ b/src/main/java/org/rulez/demokracia/pdengine/dataobjects/VoteEntity.java @@ -29,6 +29,8 @@ public abstract class VoteEntity extends BaseEntity { public boolean canUpdate; @ElementCollection public Map choices; + @ElementCollection + public Map recordedBallots; public VoteEntity() { super(); diff --git a/src/test/java/org/rulez/demokracia/pdengine/ObtainBallotAdminKeyTest.java b/src/test/java/org/rulez/demokracia/pdengine/ObtainBallotAdminKeyTest.java index c87194e8..f97805e4 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/ObtainBallotAdminKeyTest.java +++ b/src/test/java/org/rulez/demokracia/pdengine/ObtainBallotAdminKeyTest.java @@ -27,7 +27,7 @@ public void if_the_user_does_not_have_all_the_needed_assurances_then_she_cannot_ vote.neededAssurances.clear(); vote.neededAssurances.add("dontCare"); assertThrows( - () -> voteManager.obtainBallot(adminInfo.voteId, "anon") + () -> voteManager.obtainBallot(adminInfo.voteId, "user") ).assertMessageIs("The user does not have all of the needed assurances."); } @@ -40,7 +40,7 @@ public void if_the_user_does_have_all_the_assurances_then_a_ballot_is_served() { vote.neededAssurances.clear(); vote.neededAssurances.add("magyar"); - String ballot = voteManager.obtainBallot(adminInfo.voteId, "anon"); + String ballot = voteManager.obtainBallot(adminInfo.voteId, "user"); assertTrue(ballot instanceof String); } @@ -52,7 +52,7 @@ public void if_neededAssurances_is_empty_then_a_ballot_is_served_to_anyone() { Vote vote = voteManager.getVote(adminInfo.voteId); vote.neededAssurances.clear(); - String ballot = voteManager.obtainBallot(adminInfo.voteId, "anon"); + String ballot = voteManager.obtainBallot(adminInfo.voteId, "user"); assertTrue(ballot instanceof String); } } diff --git a/src/test/java/org/rulez/demokracia/pdengine/ObtainBallotTest.java b/src/test/java/org/rulez/demokracia/pdengine/ObtainBallotTest.java index ab728062..723f66b1 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/ObtainBallotTest.java +++ b/src/test/java/org/rulez/demokracia/pdengine/ObtainBallotTest.java @@ -75,6 +75,29 @@ public void obtainBallot_stores_the_ballot() { assertTrue(vote.ballots.contains(ballot)); } + @tested_feature("Manage votes") + @tested_operation("Obtain ballot") + @tested_behaviour("the number of ballots obtained with adminKey are recorded for \"admin\"") + @Test + public void obtainBallot_increases_recordedBallots_when_adminKey_is_admin() { + String voteId = adminInfo.voteId; + String adminKey = adminInfo.adminKey; + + Vote vote = voteManager.getVote(voteId); + int originalObtainedBallots = vote.getRecordedBallots(adminKey); + String ballot = voteManager.obtainBallot(voteId, adminKey); + assertEquals(originalObtainedBallots + 1, vote.getRecordedBallots("admin").intValue()); + } - + @tested_feature("Manage votes") + @tested_operation("Obtain ballot") + @tested_behaviour("the number of ballots obtained with anon adminkey are recorded with the proxy id of the user") + @Test + public void obtainBallot_increases_recordedBallots_when_adminKey_is_anon() { + String voteId = adminInfo.voteId; + Vote vote = voteManager.getVote(voteId); + int originalObtainedBallots = vote.getRecordedBallots("test_user_in_ws_context"); + String ballot = voteManager.obtainBallot(voteId, "user"); + assertEquals(originalObtainedBallots + 1, vote.getRecordedBallots("test_user_in_ws_context").intValue()); + } }