Skip to content

Spring refactor of #242 #324

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

Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.rulez.demokracia.pdengine.assurance;

import java.util.List;

import org.springframework.stereotype.Service;

@Service
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,11 @@
package org.rulez.demokracia.pdengine.assurance;

import java.util.List;

import org.springframework.stereotype.Service;

@Service
public interface AssuranceManager {

List<String> getAssurances(String proxyId);
}
27 changes: 15 additions & 12 deletions src/main/java/org/rulez/demokracia/pdengine/votecast/CastVote.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@
import org.rulez.demokracia.pdengine.RandomUtils;
import org.rulez.demokracia.pdengine.choice.RankedChoice;


public class CastVote extends CastVoteEntity implements CastVoteInterface {
private static final long serialVersionUID = 1L;

public CastVote(final String proxyId, final List<RankedChoice> preferences) {
super();
this.setProxyId(proxyId);
this.setPreferences(new ArrayList<>(preferences));
setSecretId(RandomUtils.createRandomKey());
}
private static final long serialVersionUID = 1L;

public CastVote(final String proxyId, final List<RankedChoice> preferences) {
super();
setProxyId(proxyId);
setPreferences(new ArrayList<>(preferences));
setSecretId(RandomUtils.createRandomKey());
}

@Override
public List<String> getAssurances() {
throw new UnsupportedOperationException();
}
public CastVote(
final String proxyId, final List<RankedChoice> preferences,
final List<String> assurances
) {
this(proxyId, preferences);
setAssurances(assurances);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
@Setter
@Entity
public class CastVoteEntity extends BaseEntity {
private static final long serialVersionUID = 1L;
@ElementCollection
private List<RankedChoice> preferences;
private String proxyId;
private String secretId;
private String signature;

private static final long serialVersionUID = 1L;
@ElementCollection
private List<RankedChoice> preferences;
private String proxyId;
private String secretId;
private String signature;
private List<String> assurances;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Objects;

import org.rulez.demokracia.pdengine.assurance.AssuranceManager;
import org.rulez.demokracia.pdengine.authentication.AuthenticatedUserService;
import org.rulez.demokracia.pdengine.choice.RankedChoice;
import org.rulez.demokracia.pdengine.vote.Vote;
Expand All @@ -22,6 +23,9 @@ public class CastVoteServiceImpl implements CastVoteService {
@Autowired
private VoteService voteService;

@Autowired
private AssuranceManager assuranceManager;

@Override
public CastVote castVote(
final String voteId, final String ballot,
Expand All @@ -45,11 +49,21 @@ public CastVote castVote(
vote.getVotesCast()
.removeIf(castVote -> proxyId.equals(castVote.getProxyId()));

final CastVote castVote = new CastVote(proxyId, rankedChoices);
final CastVote castVote = createCastVote(rankedChoices, proxyId);
vote.getVotesCast().add(castVote);
return castVote;
}

private CastVote
createCastVote(
final List<RankedChoice> rankedChoices, final String proxyId
) {
return Objects.isNull(proxyId) ? new CastVote(proxyId, rankedChoices) :
new CastVote(
proxyId, rankedChoices, assuranceManager.getAssurances(proxyId)
);
}

private void validateInput(
final String ballot, final List<RankedChoice> rankedChoices,
final Vote vote
Expand Down

This file was deleted.

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
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.pdengine.testhelpers.ThrowableTester;

@TestedFeature("Supporting functionality")
@TestedOperation("Assurance management")
@TestedBehaviour("Unimplemented")
@RunWith(MockitoJUnitRunner.class)
public class AssuranceManagerTest extends ThrowableTester {

private static final String PROXY_ID = "proxyId";

@InjectMocks
private ADAAssuranceManager assuranceManager;

@Test
public void getAssurances_is_unimplemented_yet() throws Exception {
assertUnimplemented(
() -> assuranceManager.getAssurances(PROXY_ID)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ public class CastVoteTestHelper {

public static final Choice CHOICE_A = new Choice("A", "user");
public static final Choice CHOICE_B = new Choice("B", "user");
public static final List<RankedChoice> RANKED_CHOICES =
List.of(new RankedChoice(CHOICE_A.getId(), 1));

public static void fillVoteWithDummyCastVotes(final Vote vote) {
vote.addChoice(CHOICE_A);
vote.addChoice(CHOICE_B);
final List<RankedChoice> rankedChoices =
List.of(new RankedChoice(CHOICE_A.getId(), 1));
vote.getVotesCast().add(new CastVote("user1", rankedChoices));
vote.getVotesCast().add(new CastVote("user2", rankedChoices));
vote.getVotesCast().add(new CastVote(null, rankedChoices));
vote.getVotesCast().add(new CastVote("user3", rankedChoices));
vote.getVotesCast().add(new CastVote("user1", RANKED_CHOICES));
vote.getVotesCast().add(new CastVote("user2", RANKED_CHOICES));
vote.getVotesCast().add(new CastVote(null, RANKED_CHOICES));
vote.getVotesCast().add(new CastVote("user3", RANKED_CHOICES));
}

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

import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
import static org.rulez.demokracia.pdengine.testhelpers.CastVoteTestHelper.RANKED_CHOICES;

import java.util.Objects;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour;
import org.rulez.demokracia.pdengine.annotations.TestedFeature;
import org.rulez.demokracia.pdengine.annotations.TestedOperation;

@TestedFeature("Supporting functionality")
@TestedOperation("CastVote")
@RunWith(MockitoJUnitRunner.class)
public class CastVoteAssuranceTest extends CastVoteTestBase {

@Override
@Before
public void setUp() {
super.setUp();
when(authService.getAuthenticatedUserName()).thenReturn(USER_NAME);
when(assuranceManager.getAssurances(USER_NAME))
.thenReturn(ASSURANCES);
}

@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.getParameters().setUpdatable(true);
final CastVote castVote = castVoteService
.castVote(vote.getId(), ballot, RANKED_CHOICES);
assertEquals(ASSURANCES, castVote.getAssurances());
}

@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.getParameters().setUpdatable(false);
final CastVote castVote = castVoteService
.castVote(vote.getId(), ballot, RANKED_CHOICES);
assertTrue(Objects.isNull(castVote.getAssurances()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import static org.mockito.Mockito.when;

import java.util.List;

import org.apache.catalina.connector.CoyotePrincipal;
import org.junit.Before;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.rulez.demokracia.pdengine.assurance.AssuranceManager;
import org.rulez.demokracia.pdengine.authentication.AuthenticatedUserService;
import org.rulez.demokracia.pdengine.testhelpers.CastVoteTestHelper;
import org.rulez.demokracia.pdengine.testhelpers.ThrowableTester;
Expand All @@ -15,25 +18,32 @@

public class CastVoteTestBase extends ThrowableTester {

@InjectMocks
protected CastVoteServiceImpl castVoteService;
protected static final String USER_NAME = "name";

protected static final List<String> ASSURANCES = List.of("madjare", "inglis");

@InjectMocks
protected CastVoteServiceImpl castVoteService;

@Mock
protected VoteService voteService;
@Mock
protected AuthenticatedUserService authService;
@Mock
protected VoteService voteService;
@Mock
protected AuthenticatedUserService authService;
@Mock
protected AssuranceManager assuranceManager;

protected Vote vote = new VariantVote();
protected Vote vote = new VariantVote();

protected String ballot = "ballotgyorgy";
protected String ballot = "ballotgyorgy";

@Before
public void setUp() {
when(voteService.getVote(vote.getId())).thenReturn(vote);
when(authService.getUserPrincipal()).thenReturn(new CoyotePrincipal("name"));
vote.getParameters().setVotable(true);
vote.getParameters().setUpdatable(true);
CastVoteTestHelper.fillVoteWithDummyCastVotes(vote);
vote.addBallot(ballot);
}
@Before
public void setUp() {
when(voteService.getVote(vote.getId())).thenReturn(vote);
when(authService.getUserPrincipal())
.thenReturn(new CoyotePrincipal(USER_NAME));
vote.getParameters().setVotable(true);
vote.getParameters().setUpdatable(true);
CastVoteTestHelper.fillVoteWithDummyCastVotes(vote);
vote.addBallot(ballot);
}
}