This repository was archived by the owner on Oct 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathChoiceDeleteAdminKeyIsUserTest.java
More file actions
125 lines (108 loc) · 4.43 KB
/
ChoiceDeleteAdminKeyIsUserTest.java
File metadata and controls
125 lines (108 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package org.rulez.demokracia.pdengine.choice;
import static org.mockito.Mockito.*;
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;
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 =
"if \"user\" is used as adminKey, then the user must be the one who added the choice and canAddIn be true";
private static final String USER = "user";
private static final String CHOICE1 = "choice1";
private static final String TEST_USER_NAME = "teszt_elek";
@Override
@Before
public void setUp() {
super.setUp();
}
@TestedBehaviour(IF_USER_IS_USED_AS_ADMIN_KEY)
@Test
public void if_canAddin_is_false_then_other_users_cannot_delete_choices() {
final Choice choiceToDelete = createChoice(TEST_USER_NAME, 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() {
final Choice choiceToDelete = createChoice(USER, true);
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() {
final Choice choiceToDelete = createChoice(TEST_USER_NAME, true);
choiceService.deleteChoice(
new VoteAdminInfo(vote.getId(), USER), choiceToDelete.getId()
);
assertThrows(
() -> choiceService.getChoice(vote.getId(), choiceToDelete.getId())
)
.assertMessageIs("Illegal choiceId");
}
@TestedBehaviour(IF_USER_IS_USED_AS_ADMIN_KEY)
@Test
public void deleteChoice_saves_vote_if_the_choice_is_deleted() {
final Choice choiceToDelete = createChoice(TEST_USER_NAME, true);
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() {
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");
}
private Choice
createChoice(final String userName, final boolean isAddinable) {
vote.getParameters().setAddinable(isAddinable);
final Choice choiceToDelete = new Choice(CHOICE1, userName);
vote.addChoice(choiceToDelete);
return choiceToDelete;
}
}