diff --git a/src/main/java/org/rulez/demokracia/pdengine/choice/ChoiceModificationValidatorService.java b/src/main/java/org/rulez/demokracia/pdengine/choice/ChoiceModificationValidatorService.java new file mode 100644 index 00000000..ed78e900 --- /dev/null +++ b/src/main/java/org/rulez/demokracia/pdengine/choice/ChoiceModificationValidatorService.java @@ -0,0 +1,16 @@ +package org.rulez.demokracia.pdengine.choice; + +import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo; +import org.rulez.demokracia.pdengine.vote.Vote; +import org.springframework.stereotype.Service; + +@Service +public interface ChoiceModificationValidatorService { + + void validateModification( + VoteAdminInfo voteAdminInfo, Vote vote, Choice choice + ); + + void validateDeletion(VoteAdminInfo voteAdminInfo, Vote vote, Choice choice); + +} diff --git a/src/main/java/org/rulez/demokracia/pdengine/choice/ChoiceModificationValidatorServiceImpl.java b/src/main/java/org/rulez/demokracia/pdengine/choice/ChoiceModificationValidatorServiceImpl.java new file mode 100644 index 00000000..26176e32 --- /dev/null +++ b/src/main/java/org/rulez/demokracia/pdengine/choice/ChoiceModificationValidatorServiceImpl.java @@ -0,0 +1,68 @@ +package org.rulez.demokracia.pdengine.choice; + +import org.rulez.demokracia.pdengine.authentication.AuthenticatedUserService; +import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo; +import org.rulez.demokracia.pdengine.exception.ReportedException; +import org.rulez.demokracia.pdengine.vote.Vote; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ChoiceModificationValidatorServiceImpl + implements ChoiceModificationValidatorService { + + private static final String MODIFICATION = "modification"; + private static final String DELETION = "deletion"; + private static final String CAN_ADDIN_IS_FALSE_MESSAGE = + "Choice %s disallowed: adminKey is user, but canAddin is false"; + private static final String DIFFERENT_USER_MESSAGE = + "Choice %s disallowed: adminKey is user, and the choice was added by a different user"; + @Autowired + private AuthenticatedUserService authenticationService; + + @Override + public void validateModification( + final VoteAdminInfo voteAdminInfo, final Vote vote, final Choice choice + ) { + validate(voteAdminInfo, vote, choice, MODIFICATION); + } + + @Override + public void validateDeletion( + final VoteAdminInfo voteAdminInfo, final Vote vote, final Choice choice + ) { + validate(voteAdminInfo, vote, choice, DELETION); + } + + private void validate( + final VoteAdminInfo voteAdminInfo, + final Vote vote, + final Choice choice, + final String operation + ) { + if (voteAdminInfo.isUserAdminKey()) { + validateAddinability(vote, operation); + validateMatchingUser(choice, operation); + } + } + + private void + validateMatchingUser(final Choice choice, final String operation) { + if (!choice.getUserName().equals(getUserName())) + throw new ReportedException( + String.format(DIFFERENT_USER_MESSAGE, operation) + ); + } + + private void validateAddinability(final Vote vote, final String operation) { + if (!vote.getParameters().isAddinable()) + throw new ReportedException( + String.format(CAN_ADDIN_IS_FALSE_MESSAGE, operation) + ); + } + + private String getUserName() { + return authenticationService.getAuthenticatedUserName(); + } + +} diff --git a/src/main/java/org/rulez/demokracia/pdengine/choice/ChoiceServiceImpl.java b/src/main/java/org/rulez/demokracia/pdengine/choice/ChoiceServiceImpl.java index 14c180c5..df88c9bc 100644 --- a/src/main/java/org/rulez/demokracia/pdengine/choice/ChoiceServiceImpl.java +++ b/src/main/java/org/rulez/demokracia/pdengine/choice/ChoiceServiceImpl.java @@ -13,17 +13,8 @@ public class ChoiceServiceImpl implements ChoiceService { private static final String USER = "user"; - private static final String MODIFY_NOT_SAME_USER_MESSAGE = - "Choice modification disallowed: adminKey is user, " - + "and the choice was added by a different user"; - - private static final String MODIFY_CANT_ADDIN_MESSAGE = - "Choice modification disallowed: adminKey is user, but canAddin is false"; - - private static final String DELETE_CANT_ADDIN = "The adminKey is \"user\" but canAddin is false."; - - private static final String DELETE_NOT_SAME_USER_MESSAGE = - "The adminKey is \"user\" but the user is not same with that user who added the choice."; + @Autowired + private ChoiceModificationValidatorService choiceModificationValidatorService; @Autowired private VoteService voteService; @@ -32,8 +23,10 @@ public class ChoiceServiceImpl implements ChoiceService { private AuthenticatedUserService authenticatedUserService; @Override - public Choice addChoice(final VoteAdminInfo voteAdminInfo, final String choiceName, - final String user) { + public Choice addChoice( + final VoteAdminInfo voteAdminInfo, final String choiceName, + final String user + ) { final Vote vote = getModifiableVote(voteAdminInfo, voteService); final Choice choice = new Choice(choiceName, user); vote.addChoice(choice); @@ -47,8 +40,10 @@ public Choice getChoice(final String voteId, final String choiceId) { } @Override - public void endorseChoice(final VoteAdminInfo voteAdminInfo, final String choiceId, - final String givenUserName) { + public void endorseChoice( + final VoteAdminInfo voteAdminInfo, final String choiceId, + final String givenUserName + ) { String userName = givenUserName; final Vote vote = voteService.getVote(voteAdminInfo.voteId); if (USER.equals(voteAdminInfo.adminKey)) { @@ -60,39 +55,34 @@ public void endorseChoice(final VoteAdminInfo voteAdminInfo, final String choice } @Override - public void deleteChoice(final VoteAdminInfo voteAdminInfo, final String choiceId) { + public void + deleteChoice(final VoteAdminInfo voteAdminInfo, final String choiceId) { final Vote vote = getModifiableVote(voteAdminInfo, voteService); final Choice votesChoice = getChoice(voteAdminInfo.getVoteId(), choiceId); - if (voteAdminInfo.isUserAdminKey()) - deleteChoiceAsUser(vote, votesChoice); - else - vote.getChoices().remove(votesChoice.getId()); + choiceModificationValidatorService + .validateDeletion(voteAdminInfo, vote, votesChoice); + + vote.getChoices().remove(votesChoice.getId()); voteService.saveVote(vote); } @Override - public void modifyChoice(final VoteAdminInfo adminInfo, final String choiceId, - final String choiceName) { + public void modifyChoice( + final VoteAdminInfo adminInfo, final String choiceId, + final String choiceName + ) { final Vote vote = getModifiableVote(adminInfo, voteService); final Choice votesChoice = vote.getChoice(choiceId); - validateModifyInput(adminInfo, vote, votesChoice); + choiceModificationValidatorService + .validateModification(adminInfo, vote, votesChoice); votesChoice.setName(choiceName); voteService.saveVote(vote); } - private void validateModifyInput(final VoteAdminInfo adminInfo, final Vote vote, - final Choice votesChoice) { - if (!USER.equals(adminInfo.adminKey)) - return; - checkIfVoteIsAddinable(vote, new ReportedException(MODIFY_CANT_ADDIN_MESSAGE)); - checkIfUserIsTheSame(votesChoice, getUserName(), - new ReportedException(MODIFY_NOT_SAME_USER_MESSAGE, votesChoice.getUserName())); - } - private String getUserName() { return authenticatedUserService.getAuthenticatedUserName(); } @@ -102,11 +92,4 @@ private void checkIfVoteIsEndorseable(final Vote vote) { throw new ReportedException("user cannot endorse this vote"); } - private void deleteChoiceAsUser(final Vote vote, final Choice votesChoice) { - checkIfUserIsTheSame(votesChoice, getUserName(), - new IllegalArgumentException(DELETE_NOT_SAME_USER_MESSAGE)); - checkIfVoteIsAddinable(vote, new IllegalArgumentException(DELETE_CANT_ADDIN)); - vote.getChoices().remove(votesChoice.getId()); - } - } diff --git a/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceDeleteAdminKeyIsUserTest.java b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceDeleteAdminKeyIsUserTest.java index a20adef3..7f67ad9f 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceDeleteAdminKeyIsUserTest.java +++ b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceDeleteAdminKeyIsUserTest.java @@ -1,6 +1,7 @@ package org.rulez.demokracia.pdengine.choice; import static org.mockito.Mockito.*; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -9,12 +10,17 @@ import org.rulez.demokracia.pdengine.annotations.TestedFeature; import org.rulez.demokracia.pdengine.annotations.TestedOperation; import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo; +import org.rulez.demokracia.pdengine.exception.ReportedException; @TestedFeature("Manage votes") @TestedOperation("delete choice") @RunWith(MockitoJUnitRunner.class) public class ChoiceDeleteAdminKeyIsUserTest extends ChoiceTestBase { + private static final String NOT_SAME_USER_MESSAGE = + "The adminKey is \"user\" but the user is not same with that user who added the choice."; + private static final String CAN_ADDIN_IS_FALSE_MESSAGE = + "The adminKey is \"user\" but canAddin is false."; private static final String IF_THE_VOTE_HAS_BALLOTS_ISSUED = "if the vote has ballots issued, the choice cannot be deleted"; private static final String IF_USER_IS_USED_AS_ADMIN_KEY = @@ -33,28 +39,54 @@ public void setUp() { @Test public void if_canAddin_is_false_then_other_users_cannot_delete_choices() { final Choice choiceToDelete = createChoice(TEST_USER_NAME, false); - when(authService.getAuthenticatedUserName()).thenReturn(TEST_USER_NAME); - assertThrows(() -> choiceService.deleteChoice(new VoteAdminInfo(vote.getId(), USER), - choiceToDelete.getId())).assertMessageIs("The adminKey is \"user\" but canAddin is false."); + final VoteAdminInfo voteAdminInfo = new VoteAdminInfo(vote.getId(), USER); + doThrow(new ReportedException(CAN_ADDIN_IS_FALSE_MESSAGE)) + .when(choiceModificationValidatorService) + .validateDeletion(voteAdminInfo, vote, choiceToDelete); + assertThrows( + () -> choiceService.deleteChoice( + voteAdminInfo, + choiceToDelete.getId() + ) + ).assertMessageIs("The adminKey is \"user\" but canAddin is false."); } @TestedBehaviour(IF_USER_IS_USED_AS_ADMIN_KEY) @Test - public void if_adminKey_is_user_and_the_user_is_not_the_one_who_added_the_choice_then_the_choice_cannot_be_deleted() { + public void + if_adminKey_is_user_and_the_user_is_not_the_one_who_added_the_choice_then_the_choice_cannot_be_deleted() { final Choice choiceToDelete = createChoice(USER, true); - assertThrows(() -> choiceService.deleteChoice(new VoteAdminInfo(vote.getId(), USER), - choiceToDelete.getId())).assertMessageIs( - "The adminKey is \"user\" but the user is not same with that user who added the choice."); + final VoteAdminInfo voteAdminInfo = new VoteAdminInfo(vote.getId(), USER); + + doThrow(new ReportedException(NOT_SAME_USER_MESSAGE)) + .when(choiceModificationValidatorService) + .validateDeletion(voteAdminInfo, vote, choiceToDelete); + + assertThrows( + () -> { + choiceService.deleteChoice( + voteAdminInfo, + choiceToDelete.getId() + ); + } + ).assertMessageIs( + NOT_SAME_USER_MESSAGE + + ); } @TestedBehaviour(IF_USER_IS_USED_AS_ADMIN_KEY) @Test - public void if_adminKey_is_user_and_canAddin_is_true_then_the_user_who_added_the_choice_is_able_to_delete_it() { + public void + if_adminKey_is_user_and_canAddin_is_true_then_the_user_who_added_the_choice_is_able_to_delete_it() { final Choice choiceToDelete = createChoice(TEST_USER_NAME, true); - when(authService.getAuthenticatedUserName()).thenReturn(TEST_USER_NAME); - choiceService.deleteChoice(new VoteAdminInfo(vote.getId(), USER), choiceToDelete.getId()); + choiceService.deleteChoice( + new VoteAdminInfo(vote.getId(), USER), choiceToDelete.getId() + ); - assertThrows(() -> choiceService.getChoice(vote.getId(), choiceToDelete.getId())) + assertThrows( + () -> choiceService.getChoice(vote.getId(), choiceToDelete.getId()) + ) .assertMessageIs("Illegal choiceId"); } @@ -62,23 +94,29 @@ public void if_adminKey_is_user_and_canAddin_is_true_then_the_user_who_added_the @Test public void deleteChoice_saves_vote_if_the_choice_is_deleted() { final Choice choiceToDelete = createChoice(TEST_USER_NAME, true); - when(authService.getAuthenticatedUserName()).thenReturn(TEST_USER_NAME); - choiceService.deleteChoice(new VoteAdminInfo(vote.getId(), USER), choiceToDelete.getId()); + + choiceService.deleteChoice( + new VoteAdminInfo(vote.getId(), USER), choiceToDelete.getId() + ); verify(voteService).saveVote(vote); } @TestedBehaviour(IF_THE_VOTE_HAS_BALLOTS_ISSUED) @Test - public void if_the_vote_has_issued_ballots_then_the_choice_cannot_be_deleted() { + public void + if_the_vote_has_issued_ballots_then_the_choice_cannot_be_deleted() { final Choice choiceToDelete = createChoice(TEST_USER_NAME, true); vote.getBallots().add("TestBallot"); - assertThrows(() -> choiceService - .deleteChoice(new VoteAdminInfo(vote.getId(), vote.getAdminKey()), choiceToDelete.getId())) - .assertMessageIs("Vote modification disallowed: ballots already issued"); + assertThrows( + () -> choiceService + .deleteChoice(new VoteAdminInfo(vote.getId(), vote.getAdminKey()), choiceToDelete.getId()) + ) + .assertMessageIs("Vote modification disallowed: ballots already issued"); } - private Choice createChoice(final String userName, final boolean isAddinable) { + private Choice + createChoice(final String userName, final boolean isAddinable) { vote.getParameters().setAddinable(isAddinable); final Choice choiceToDelete = new Choice(CHOICE1, userName); vote.addChoice(choiceToDelete); diff --git a/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceDeleteValidatorServiceTest.java b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceDeleteValidatorServiceTest.java new file mode 100644 index 00000000..b3ed7321 --- /dev/null +++ b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceDeleteValidatorServiceTest.java @@ -0,0 +1,62 @@ +package org.rulez.demokracia.pdengine.choice; + +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("Manage votes") +@TestedOperation("delete choice") +@RunWith(MockitoJUnitRunner.class) +public class ChoiceDeleteValidatorServiceTest extends ChoiceValidatorBaseTest { + + @Test + @TestedBehaviour( + "if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true" + ) + public void userAdmin_cannot_delete_choice_if_canAddin_is_false() { + assertFalseCanAddinCauseException( + () -> underTest.validateDeletion(getUserAdminInfo(), vote, choice) + ); + } + + @TestedBehaviour("modifies the string of the choice") + @Test + public void proper_voteId_choiceId_and_adminKey_does_modify_choice() { + assertNotThrows( + () -> { + underTest.validateDeletion(getAdminInfo(), vote, choice); + } + ); + } + + @TestedBehaviour( + "if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true" + ) + @Test + public void + userAdmin_cannot_modify_choice_if_it_is_not_added_by_other_user() { + assertDifferentUserCauseException( + () -> underTest.validateDeletion(getUserAdminInfo(), vote, choice) + ); + } + + @TestedBehaviour( + "if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true" + ) + @Test + public void + userAdmin_can_modify_the_choice_if_canAddin_is_true_and_he_is_the_choice_creator() { + assertNoExceptionWithCorrectParameters( + () -> underTest.validateDeletion(getUserAdminInfo(), vote, choice2) + ); + } + + @Override + protected String getOperation() { + return "deletion"; + } + +} diff --git a/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceModificationValidatorServiceTest.java b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceModificationValidatorServiceTest.java new file mode 100644 index 00000000..f8e1a555 --- /dev/null +++ b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceModificationValidatorServiceTest.java @@ -0,0 +1,63 @@ +package org.rulez.demokracia.pdengine.choice; + +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("Manage votes") +@TestedOperation("modify vote") +@RunWith(MockitoJUnitRunner.class) +public class ChoiceModificationValidatorServiceTest + extends ChoiceValidatorBaseTest { + + private static final String MODIFICATION = "modification"; + + @Test + @TestedBehaviour( + "if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true" + ) + public void userAdmin_cannot_modify_choice_if_canAddin_is_false() { + assertFalseCanAddinCauseException( + () -> underTest.validateModification(getUserAdminInfo(), vote, choice) + ); + } + + @TestedBehaviour("modifies the string of the choice") + @Test + public void proper_voteId_choiceId_and_adminKey_does_modify_choice() { + assertNotThrows( + () -> underTest.validateModification(getAdminInfo(), vote, choice) + ); + } + + @TestedBehaviour( + "if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true" + ) + @Test + public void + userAdmin_cannot_modify_choice_if_it_is_not_added_by_other_user() { + assertDifferentUserCauseException( + () -> underTest.validateModification(getUserAdminInfo(), vote, choice) + ); + } + + @TestedBehaviour( + "if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true" + ) + @Test + public void + userAdmin_can_modify_the_choice_if_canAddin_is_true_and_he_is_the_choice_creator() { + assertNoExceptionWithCorrectParameters( + () -> underTest.validateModification(getUserAdminInfo(), vote, choice2) + ); + } + + @Override + protected String getOperation() { + return MODIFICATION; + } + +} diff --git a/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceModifyValidationTest.java b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceModifyValidationTest.java index 010f1bb3..e4c4fa90 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceModifyValidationTest.java +++ b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceModifyValidationTest.java @@ -1,7 +1,8 @@ package org.rulez.demokracia.pdengine.choice; import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.doThrow; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -17,6 +18,11 @@ @RunWith(MockitoJUnitRunner.class) public class ChoiceModifyValidationTest extends ChoiceTestBase { + private static final String DIFFERENT_USER_MESSAGE = + "Choice modification disallowed: adminKey is user," + + " and the choice was added by a different user"; + private static final String ADDIN_IS_FALSE_MESSAGE = + "Choice modification disallowed: adminKey is user, but canAddin is false"; private static final String VALIDATES_INPUTS = "validates inputs"; private static final String NEW_CHOICE_NAME = "NewChoiceName"; private static final String USER = "user"; @@ -34,37 +40,50 @@ public void setUp() { @TestedBehaviour(VALIDATES_INPUTS) @Test public void invalid_voteId_is_rejected() { - doThrow(new ReportedException("illegal voteId", invalidvoteId)).when(voteService) + doThrow(new ReportedException("illegal voteId", invalidvoteId)) + .when(voteService) .getVote(invalidvoteId); assertThrows( - () -> choiceService.modifyChoice(new VoteAdminInfo(invalidvoteId, vote.getAdminKey()), - choice.getId(), NEW_CHOICE_NAME)).assertMessageIs("illegal voteId"); + () -> choiceService + .modifyChoice(new VoteAdminInfo(invalidvoteId, vote.getAdminKey()), + choice.getId(), NEW_CHOICE_NAME + ) + ).assertMessageIs("illegal voteId"); } @TestedBehaviour(VALIDATES_INPUTS) @Test public void invalid_choiceId_is_rejected() { - String invalidChoiceId = "invalidChoiceId"; + final String invalidChoiceId = "invalidChoiceId"; assertThrows( - () -> choiceService.modifyChoice(new VoteAdminInfo(vote.getId(), vote.getAdminKey()), - invalidChoiceId, NEW_CHOICE_NAME)).assertMessageIs("Illegal choiceId"); + () -> choiceService + .modifyChoice(new VoteAdminInfo(vote.getId(), vote.getAdminKey()), + invalidChoiceId, NEW_CHOICE_NAME + ) + ).assertMessageIs("Illegal choiceId"); } @TestedBehaviour(VALIDATES_INPUTS) @Test public void invalid_adminKey_is_rejected() { - String invalidAdminKey = "invalidAdminKey"; + final String invalidAdminKey = "invalidAdminKey"; - assertThrows(() -> choiceService.modifyChoice(new VoteAdminInfo(vote.getId(), invalidAdminKey), - choice.getId(), NEW_CHOICE_NAME)).assertMessageIs("Illegal adminKey"); + assertThrows( + () -> choiceService + .modifyChoice(new VoteAdminInfo(vote.getId(), invalidAdminKey), + choice.getId(), NEW_CHOICE_NAME + ) + ).assertMessageIs("Illegal adminKey"); } @TestedBehaviour("modifies the string of the choice") @Test public void proper_voteId_choiceId_and_adminKey_does_modify_choice() { - choiceService.modifyChoice(new VoteAdminInfo(vote.getId(), vote.getAdminKey()), choice.getId(), - NEW_CHOICE_NAME); + choiceService.modifyChoice( + new VoteAdminInfo(vote.getId(), vote.getAdminKey()), choice.getId(), + NEW_CHOICE_NAME + ); assertEquals(NEW_CHOICE_NAME, choice.getName()); } @@ -74,43 +93,77 @@ public void proper_voteId_choiceId_and_adminKey_does_modify_choice() { public void when_ballots_are_already_issued_choices_cannot_be_modified() { vote.getBallots().add("Test Ballot"); - assertThrows(() -> choiceService.modifyChoice( - new VoteAdminInfo(vote.getId(), vote.getAdminKey()), choice.getId(), "something else")) - .assertMessageIs("Vote modification disallowed: ballots already issued"); + assertThrows( + () -> choiceService.modifyChoice( + new VoteAdminInfo(vote.getId(), vote.getAdminKey()), choice.getId(), "something else" + ) + ) + .assertMessageIs("Vote modification disallowed: ballots already issued"); } - @TestedBehaviour("if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true") + @TestedBehaviour( + "if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true" + ) @Test public void userAdmin_cannot_modify_choice_if_canAddin_is_false() { - vote.getParameters().setAddinable(false); + final VoteAdminInfo voteAdminInfo = new VoteAdminInfo(vote.getId(), USER); + + doThrow(new ReportedException(ADDIN_IS_FALSE_MESSAGE)).when( + choiceModificationValidatorService + ).validateModification(voteAdminInfo, vote, choice); - assertThrows(() -> choiceService.modifyChoice(new VoteAdminInfo(vote.getId(), USER), - choice.getId(), NEW_CHOICE_NAME)).assertMessageIs( - "Choice modification disallowed: adminKey is user, but canAddin is false"); + assertThrows( + () -> { + choiceService.modifyChoice( + voteAdminInfo, + choice.getId(), NEW_CHOICE_NAME + ); + } + ).assertMessageIs( + ADDIN_IS_FALSE_MESSAGE + ); } - @TestedBehaviour("if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true") + @TestedBehaviour( + "if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true" + ) @Test - public void userAdmin_cannot_modify_choice_if_it_is_not_added_by_other_user() { - vote.getParameters().setAddinable(true); + public void + userAdmin_cannot_modify_choice_if_it_is_added_by_other_user() { + final VoteAdminInfo voteAdminInfo = new VoteAdminInfo(vote.getId(), USER); - assertThrows(() -> choiceService.modifyChoice(new VoteAdminInfo(vote.getId(), USER), - choice.getId(), NEW_CHOICE_NAME)) - .assertMessageIs("Choice modification disallowed: adminKey is user, " - + "and the choice was added by a different user"); + doThrow(new ReportedException(DIFFERENT_USER_MESSAGE)).when( + choiceModificationValidatorService + ).validateModification(voteAdminInfo, vote, choice); + + assertThrows( + () -> { + choiceService.modifyChoice( + voteAdminInfo, + choice.getId(), NEW_CHOICE_NAME + ); + } + ) + .assertMessageIs( + DIFFERENT_USER_MESSAGE + ); } - @TestedBehaviour("if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true") + @TestedBehaviour( + "if 'user' is used as adminKey, then the user must be the one who added the choice and canAddIn be true" + ) @Test - public void userAdmin_can_modify_the_choice_if_canAddin_is_true_and_he_is_the_choice_creator() { + public void + userAdmin_can_modify_the_choice_if_canAddin_is_true_and_he_is_the_choice_creator() { vote.getParameters().setAddinable(true); - Choice choice2 = new Choice("choice2", ADMIN); + final Choice choice2 = new Choice("choice2", ADMIN); vote.addChoice(choice2); - when(authService.getAuthenticatedUserName()).thenReturn(ADMIN); - choiceService.modifyChoice(new VoteAdminInfo(vote.getId(), USER), choice2.getId(), - NEW_CHOICE_NAME); + choiceService + .modifyChoice(new VoteAdminInfo(vote.getId(), USER), choice2.getId(), + NEW_CHOICE_NAME + ); assertEquals(NEW_CHOICE_NAME, choice2.getName()); } diff --git a/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceTestBase.java b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceTestBase.java index 66684ca9..949b5010 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceTestBase.java +++ b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceTestBase.java @@ -1,6 +1,7 @@ package org.rulez.demokracia.pdengine.choice; import static org.mockito.Mockito.when; + import org.junit.Before; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -15,8 +16,12 @@ public class ChoiceTestBase extends ThrowableTester { @InjectMocks protected ChoiceServiceImpl choiceService; + @Mock + protected ChoiceModificationValidatorService choiceModificationValidatorService; + @Mock protected VoteService voteService; + @Mock protected AuthenticatedUserService authService; diff --git a/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceValidatorBaseTest.java b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceValidatorBaseTest.java new file mode 100644 index 00000000..2324fe7a --- /dev/null +++ b/src/test/java/org/rulez/demokracia/pdengine/choice/ChoiceValidatorBaseTest.java @@ -0,0 +1,70 @@ +package org.rulez.demokracia.pdengine.choice; + +import static org.mockito.Mockito.when; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.rulez.demokracia.pdengine.authentication.AuthenticatedUserService; +import org.rulez.demokracia.pdengine.dataobjects.VoteAdminInfo; +import org.rulez.demokracia.pdengine.testhelpers.ThrowableTester; +import org.rulez.demokracia.pdengine.testhelpers.Thrower; +import org.rulez.demokracia.pdengine.testhelpers.VariantVote; +import org.rulez.demokracia.pdengine.vote.Vote; + +public abstract class ChoiceValidatorBaseTest extends ThrowableTester { + + private static final String CHOICE_ADDED_BY_DIFFERENT_USER_MESSAGE = + "Choice %s disallowed: adminKey is user, and the choice was added by a different user"; + private static final String CAN_ADDIN_IS_FALSE_MESSAGE = + "Choice %s disallowed: adminKey is user, but canAddin is false"; + private static final String USER = "user"; + private static final String ADMIN = "admin"; + + protected final Choice choice = new Choice("choiceName", USER); + protected Vote vote = new VariantVote(); + + @InjectMocks + protected ChoiceModificationValidatorServiceImpl underTest; + + @Mock + protected AuthenticatedUserService authenticatedUserService; + + protected Choice choice2 = new Choice("choice2", ADMIN); + + protected abstract String getOperation(); + + protected String getDifferentUserMessage() { + return String + .format(CHOICE_ADDED_BY_DIFFERENT_USER_MESSAGE, getOperation()); + } + + protected String getCanAddinIsFalseMessage() { + return String.format(CAN_ADDIN_IS_FALSE_MESSAGE, getOperation()); + } + + protected void assertDifferentUserCauseException(final Thrower thrower) { + vote.getParameters().setAddinable(true); + assertThrows(thrower).assertMessageIs(getDifferentUserMessage()); + } + + protected void assertNoExceptionWithCorrectParameters(final Thrower thrower) { + vote.getParameters().setAddinable(true); + vote.addChoice(choice2); + when(authenticatedUserService.getAuthenticatedUserName()).thenReturn(ADMIN); + + assertNotThrows(thrower); + } + + protected void assertFalseCanAddinCauseException(final Thrower thrower) { + assertThrows(thrower).assertMessageIs(getCanAddinIsFalseMessage()); + } + + protected VoteAdminInfo getUserAdminInfo() { + return new VoteAdminInfo(vote.getId(), USER); + } + + protected VoteAdminInfo getAdminInfo() { + return new VoteAdminInfo(vote.getId(), vote.getAdminKey()); + } + +} diff --git a/src/test/java/org/rulez/demokracia/pdengine/testhelpers/CastVoteTestHelper.java b/src/test/java/org/rulez/demokracia/pdengine/testhelpers/CastVoteTestHelper.java index 0a102cae..a30fa876 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/testhelpers/CastVoteTestHelper.java +++ b/src/test/java/org/rulez/demokracia/pdengine/testhelpers/CastVoteTestHelper.java @@ -1,6 +1,7 @@ package org.rulez.demokracia.pdengine.testhelpers; import java.util.List; + import org.rulez.demokracia.pdengine.choice.Choice; import org.rulez.demokracia.pdengine.choice.RankedChoice; import org.rulez.demokracia.pdengine.vote.Vote; @@ -14,7 +15,8 @@ public class CastVoteTestHelper { public static void fillVoteWithDummyCastVotes(final Vote vote) { vote.addChoice(CHOICE_A); vote.addChoice(CHOICE_B); - final List rankedChoices = List.of(new RankedChoice(CHOICE_A.getId(), 1)); + final List 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)); diff --git a/src/test/java/org/rulez/demokracia/pdengine/testhelpers/ThrowableTester.java b/src/test/java/org/rulez/demokracia/pdengine/testhelpers/ThrowableTester.java index c4fd4a4b..db2d3f21 100644 --- a/src/test/java/org/rulez/demokracia/pdengine/testhelpers/ThrowableTester.java +++ b/src/test/java/org/rulez/demokracia/pdengine/testhelpers/ThrowableTester.java @@ -1,7 +1,7 @@ package org.rulez.demokracia.pdengine.testhelpers; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; + import org.apache.commons.lang3.exception.ExceptionUtils; public class ThrowableTester { @@ -19,6 +19,17 @@ public ThrowableTester assertThrows(final Thrower thrower) { return this; } + public void assertNotThrows(final Thrower thrower) { + try { + thrower.throwException(); + } catch (final Exception exception) {// NOPMD + fail( + "No exception expected, but " + + exception.getClass().getCanonicalName() + " has been thrown" + ); + } + } + public ThrowableTester assertMessageIs(final String message) { assertEquals(message, thrown.getMessage()); return this; @@ -28,9 +39,14 @@ public Throwable getException() { return thrown; } - public ThrowableTester assertException(final Class klass) { + public ThrowableTester + assertException(final Class klass) { final String message = - String.format("expected %s but got %s", klass, ExceptionUtils.getStackTrace(thrown)); + String + .format( + "expected %s but got %s", klass, + ExceptionUtils.getStackTrace(thrown) + ); assertEquals(message, klass, thrown.getClass()); return this; }