Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 15 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.rulez.demokracia.pdengine;

import java.util.List;

public class ADAAssuranceManager implements AssuranceManager{

@Override
public List<String> getAssurances(final String proxyId) {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.rulez.demokracia.pdengine;

import java.util.List;

public interface AssuranceManager {
List<String> getAssurances(String proxyId);
}
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;
}
}
10 changes: 8 additions & 2 deletions src/main/java/org/rulez/demokracia/pdengine/IVoteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

public interface IVoteManager {

static IVoteManager getVoteManager(final WebServiceContext wsContext) {
return VoteManagerUtils.getVoteManager(wsContext);
static IVoteManager getVoteManager(final WebServiceContext wsContext, AssuranceManager assuranceManager) {
return VoteManagerUtils.getVoteManager(wsContext, assuranceManager);
}

WebServiceContext getWsContext();
Expand Down Expand Up @@ -49,5 +49,11 @@ VoteAdminInfo createVote(final String voteName, final Set<String> neededAssuranc
JsonObject showVote(final VoteAdminInfo adminInfo);

void setVoteParameters(final VoteAdminInfo adminInfo, final VoteParameters voteParameters);

List<String> getAssurances();

static AssuranceManager getAssuranceManager() {
return new ADAAssuranceManager();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test it to kill zombie.

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ final public class VoteManagerUtils {
private VoteManagerUtils() {
}

public static IVoteManager getVoteManager(final WebServiceContext wsContext) {
public static IVoteManager getVoteManager(final WebServiceContext wsContext, final AssuranceManager assuranceManager) {
if(!registry.containsKey(wsContext)) {
registry.put(wsContext, new VoteRegistry(wsContext));
registry.put(wsContext, new VoteRegistry(wsContext, assuranceManager));
}
return registry.get(wsContext);
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/rulez/demokracia/pdengine/VoteRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import com.google.gson.JsonObject;

public class VoteRegistry extends ChoiceManager implements IVoteManager {
public VoteRegistry(final WebServiceContext wsContext) {
private final AssuranceManager assuranceManager;

public VoteRegistry(final WebServiceContext wsContext, final AssuranceManager assuranceManager) {
super(wsContext);
this.assuranceManager = assuranceManager;
}

@Override
Expand Down Expand Up @@ -62,7 +65,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, getAssurances());
} else {
receipt = vote.addCastVote(null, theVote);
}
Expand Down Expand Up @@ -207,5 +211,11 @@ public void setVoteParameters(final VoteAdminInfo adminInfo, final VoteParameter
throw new ReportedException("Illegal minEndorsements", Integer.toString(voteParameters.minEndorsements));
}
}

@Override
public List<String> getAssurances() {
String wsUserName = this.getWsUserName();
return assuranceManager.getAssurances(wsUserName);
}
}

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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class VoteManagerInterface {
public Response createVote(final CreateVoteRequest request) {
VoteAdminInfo adminInfo;
try {
adminInfo = IVoteManager.getVoteManager(wsContext).createVote(
adminInfo = IVoteManager.getVoteManager(wsContext, IVoteManager.getAssuranceManager()).createVote(
request.voteName,
request.neededAssurances,
request.countedAssurances,
Expand Down
28 changes: 21 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,48 @@
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("magyar", 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);
List<String> assurances = castVote.getAssurances();
assertTrue(assurances == null);
}
}
4 changes: 2 additions & 2 deletions src/test/java/org/rulez/demokracia/pdengine/SessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void you_get_the_same_hibernate_session_by_calling_getDBSession_on_DBSess
@Test
public void two_voteManagers_for_the_same_session_context_are_the_same() {
WebServiceContext wsContext = mock(WebServiceContext.class);
IVoteManager voteManager1 = IVoteManager.getVoteManager(wsContext);
IVoteManager voteManager2 = IVoteManager.getVoteManager(wsContext);
IVoteManager voteManager1 = IVoteManager.getVoteManager(wsContext, fakeAssuranceManager);
IVoteManager voteManager2 = IVoteManager.getVoteManager(wsContext, fakeAssuranceManager);
assertEquals(voteManager1, voteManager2);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.xml.ws.WebServiceContext;

import org.junit.Before;
import org.rulez.demokracia.pdengine.AssuranceManager;
import org.rulez.demokracia.pdengine.IVoteManager;
import org.rulez.demokracia.pdengine.Vote;
import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo;
Expand All @@ -28,11 +29,14 @@ public class CreatedDefaultVoteRegistry extends ThrowableTester{
protected Set<String> countedAssurances;
protected boolean isPrivate;
protected int minEndorsements;
protected AssuranceManager fakeAssuranceManager;

@Before
public void setUp() {
WebServiceContext wsContext = setupMockWsContext();
voteManager = IVoteManager.getVoteManager(wsContext);
fakeAssuranceManager = mock(AssuranceManager.class);
when(fakeAssuranceManager.getAssurances(TEST_USER_NAME)).thenReturn(Arrays.asList("magyar", "német"));
voteManager = IVoteManager.getVoteManager(wsContext, fakeAssuranceManager);
neededAssurances = new HashSet<>();
countedAssurances = new HashSet<>();
isPrivate = true;
Expand All @@ -55,7 +59,7 @@ private WebServiceContext setupMockWsContext() {
public void setupUnauthenticatedMockWsContext() {
WebServiceContext wsContext = mock(WebServiceContext.class);
when(wsContext.getUserPrincipal()).thenReturn(null);
voteManager = IVoteManager.getVoteManager(wsContext);
voteManager = IVoteManager.getVoteManager(wsContext, fakeAssuranceManager);
}

protected VoteAdminInfo createAVote() {
Expand Down