Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
9 changes: 7 additions & 2 deletions src/main/java/org/rulez/demokracia/pdengine/CastVote.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ public CastVote(final String proxyId, final List<RankedChoice> preferences) {
super();
this.proxyId = proxyId;
this.preferences = new ArrayList<>(preferences);
secretId = RandomUtils.createRandomKey();
this.secretId = RandomUtils.createRandomKey();
}

public CastVote(final String proxyId, final List<RankedChoice> preferences, final List<String> assurances) {
this(proxyId, preferences);
this.assurances = assurances;
}

@Override
Expand All @@ -23,6 +28,6 @@ public List<RankedChoice> getPreferences() {

@Override
public List<String> getAssurances() {
throw new UnsupportedOperationException();
return assurances;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/rulez/demokracia/pdengine/IVoteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ VoteAdminInfo createVote(final String voteName, final Set<String> neededAssuranc
String getWsUserName();

boolean hasAssurance(final String role);

User getWsUser(String proxyId);

void modifyVote(final VoteAdminInfo voteAdminInfo, final String voteName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ public String getWsUserName() {
public boolean hasAssurance(final String role) {
return getWsContext().isUserInRole(role);
}

public User getWsUser(final String proxyId) {
User user = new User(proxyId);
user.assurances.add("TestAssurances");
return user;
}

}
5 changes: 5 additions & 0 deletions src/main/java/org/rulez/demokracia/pdengine/User.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.rulez.demokracia.pdengine;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;

Expand All @@ -16,5 +17,9 @@ public User(final String proxyId) {
this.proxyId = proxyId;
assurances = new ArrayList<>();
}

public List<String> getAssurances() {
return assurances;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public CastVote castVote(final String voteId, final String ballot, final List<Ra
CastVote receipt;

if (vote.getParameters().canUpdate) {
receipt = vote.addCastVote(getWsUserName(), theVote);
String proxyId = getWsUserName();
receipt = vote.addCastVote(proxyId, theVote, getWsUser(proxyId).getAssurances());
} else {
receipt = vote.addCastVote(null, theVote);
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/rulez/demokracia/pdengine/Voteable.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
public interface Voteable extends VoteInterface {

default CastVote addCastVote(final String proxyId, final List<RankedChoice> theVote) {
CastVote castVote = new CastVote(proxyId, theVote);
getVotesCast().add(castVote);
return castVote;
}

default CastVote addCastVote(final String proxyId, final List<RankedChoice> theVote, final List<String> assurances) {
Iterator<CastVote> listIterator = getVotesCast().iterator();
while (listIterator.hasNext()) {
CastVote element = listIterator.next();

if (element.proxyId != null && element.proxyId.equals(proxyId)) {
if (element.proxyId.equals(proxyId)) {
listIterator.remove();
}
}

CastVote castVote = new CastVote(proxyId, theVote);
CastVote castVote = new CastVote(proxyId, theVote, assurances);
getVotesCast().add(castVote);
return castVote;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public class CastVoteEntity extends BaseEntity {
public List<RankedChoice> preferences;
public String proxyId;
public String secretId;
public List<String> assurances;

}
29 changes: 22 additions & 7 deletions src/test/java/org/rulez/demokracia/pdengine/CastVoteTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.rulez.demokracia.pdengine;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -12,35 +13,49 @@
import org.rulez.demokracia.pdengine.annotations.TestedOperation;
import org.rulez.demokracia.pdengine.testhelpers.CreatedDefaultChoice;

@TestedFeature("Supporting functionality")
@TestedOperation("CastVote")
public class CastVoteTest extends CreatedDefaultChoice {

@Override
@Before
public void setUp() {
super.setUp();
initializeVoteCastTest();
}

@TestedFeature("Supporting functionality")
@TestedOperation("CastVote")
@TestedBehaviour("The preferences described by a cast vote can be obtained")
@Test
public void the_preferences_can_be_obtained_when_they_are_empty() {
List<RankedChoice> theCastVote = new ArrayList<>();
CastVote castVote = new CastVote(TEST_USER_NAME, theCastVote);
List<RankedChoice> preferences = castVote.getPreferences();
assertEquals(new ArrayList<>(), preferences);
}

@TestedFeature("Supporting functionality")
@TestedOperation("CastVote")

@TestedBehaviour("The preferences described by a cast vote can be obtained")
@Test
public void the_preferences_can_be_obtained_when_they_contain_choices() {
List<RankedChoice> theCastVote = new ArrayList<>();
theCastVote.add(new RankedChoice("1", 1));
CastVote castVote = new CastVote(TEST_USER_NAME, theCastVote);
List<RankedChoice> preferences = castVote.getPreferences();
assertEquals(theCastVote, preferences);
}

@TestedBehaviour("The assurances of the voter can be obtained from a cast vote if canupdateis true")
@Test
public void the_assurances_of_the_voter_can_be_obtained_from_a_cast_vote_if_canupdate_is_true() {
vote.parameters.canUpdate = true;
CastVote castVote = voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
assertEquals("TestAssurances", castVote.assurances.get(0));
}

@TestedBehaviour("The assurances of the voter can be obtained from a cast vote if canupdateis true")
@Test
public void the_assurances_of_the_voter_is_null_if_canupdate_is_false() {
vote.parameters.canUpdate = false;
CastVote castVote = voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
System.out.println("castVote: " + castVote);
List<String> assurances = castVote.getAssurances();
assertTrue(assurances == null);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package org.rulez.demokracia.pdengine;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour;
import org.rulez.demokracia.pdengine.annotations.TestedFeature;
import org.rulez.demokracia.pdengine.annotations.TestedOperation;
import org.rulez.demokracia.testhelpers.ThrowableTester;
import org.rulez.demokracia.pdengine.testhelpers.CreatedDefaultChoice;

@TestedFeature("Unimplemented")
@TestedOperation("Unimplemented")
@TestedBehaviour("Unimplemented")
public class UnimplementedTests extends ThrowableTester {
public class UnimplementedTests extends CreatedDefaultChoice {

@Test
public void the_getAssurances_method_is_not_implemented_yet() {
assertUnimplemented(() -> new CastVote("proxyId", new ArrayList<>()).getAssurances());
}
// @Test
// public void the_calculateWinners_method_is_not_implemented_yet() {
// assertUnimplemented(() -> new WinnerCalculatorImpl().calculateWinners(null));
// }
}