Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

number of issued ballots are recorded #194 #195 #263

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/org/rulez/demokracia/pdengine/Vote.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ public Vote(String voteName, Collection<String> neededAssurances, Collection<Str
creationTime = Instant.now().getEpochSecond();
choices = new HashMap<String,Choice>();
ballots = new ArrayList<String>();
recordedBallots = new HashMap<String, Integer>();
}

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);
Expand Down
106 changes: 55 additions & 51 deletions src/main/java/org/rulez/demokracia/pdengine/VoteRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> neededAssuranceList) {
for (String neededAssurance : neededAssuranceList) {
if (!hasAssurance(neededAssurance)) {
return false;
}
}
return true;
return true;
}

@Override
public void castVote(String voteId, String ballot, List<RankedChoice> 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);
}

Expand All @@ -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";
Expand All @@ -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;
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public abstract class VoteEntity extends BaseEntity {
public boolean canUpdate;
@ElementCollection
public Map<String,Choice> choices;
@ElementCollection
public Map<String, Integer> recordedBallots;

public VoteEntity() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand All @@ -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);
}

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}