Bump copyright years
[cacert-votebot.git] / src / test / java / org / cacert / votebot / shared / CAcertVoteMechanicsTest.java
1 /*
2 * Copyright (c) 2016. Jan Dittberner
3 *
4 * This file is part of CAcert votebot.
5 *
6 * CAcert votebot is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
9 * any later version.
10 *
11 * CAcert votebot is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * CAcert votebot. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package org.cacert.votebot.shared;
21
22 import org.cacert.votebot.shared.CAcertVoteMechanics.State;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.ResourceBundle;
30
31 import static org.cacert.votebot.shared.CAcertVoteMechanics.State.IDLE;
32 import static org.cacert.votebot.shared.CAcertVoteMechanics.State.RUNNING;
33
34 /**
35 * @author Jan Dittberner
36 */
37 public class CAcertVoteMechanicsTest {
38 private CAcertVoteMechanics subject;
39 private ResourceBundle resourceBundle;
40
41 @Test
42 public void testStateEnum() {
43 State[] values = State.values();
44 Assert.assertEquals(2, values.length);
45 List<State> states = Arrays.asList(values);
46 Assert.assertTrue(states.contains(RUNNING));
47 Assert.assertTrue(states.contains(IDLE));
48 }
49
50 @Before
51 public void setup() {
52 resourceBundle = ResourceBundle.getBundle("messages");
53 subject = new CAcertVoteMechanics();
54 }
55
56 @Test
57 public void testNoVote() {
58 String response = subject.evaluateVote("alice", "test");
59 Assert.assertEquals(String.format(resourceBundle.getString("no_vote_running"), "alice"), response);
60 }
61
62 @Test
63 public void testCallVote() {
64 String response = subject.callVote("test vote");
65 Assert.assertEquals(resourceBundle.getString("vote_started"), response);
66 Assert.assertEquals("test vote", subject.getTopic());
67 Assert.assertEquals(RUNNING, subject.getState());
68 }
69
70 @Test
71 public void testRefuseParallelCallVote() {
72 subject.callVote("first");
73 String response = subject.callVote("second");
74 Assert.assertEquals(resourceBundle.getString("vote_running"), response);
75 Assert.assertEquals("first", subject.getTopic());
76 Assert.assertEquals(RUNNING, subject.getState());
77 }
78
79 @Test
80 public void testFreshVoteResult() {
81 subject.callVote("fresh vote");
82 Assert.assertEquals("{}", subject.getCurrentResult());
83 }
84
85 @Test
86 public void testVote() {
87 subject.callVote("test");
88 String response = subject.evaluateVote("alice", "aye");
89 Assert.assertEquals(String.format(resourceBundle.getString("count_vote"), "alice", "AYE"), response);
90 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
91 }
92
93 @Test
94 public void testProxyVote() {
95 subject.callVote("test");
96 String response = subject.evaluateVote("alice", "proxy bob aye");
97 Assert.assertEquals(
98 String.format(resourceBundle.getString("count_proxy_vote"), "alice", "bob", "AYE"), response);
99 Assert.assertEquals("{bob=AYE}", subject.getCurrentResult());
100 }
101
102 @Test
103 public void testInvalidVote() {
104 subject.callVote("test");
105 String response = subject.evaluateVote("alice", "moo");
106 Assert.assertEquals(String.format(resourceBundle.getString("vote_not_understood"), "alice"), response);
107 Assert.assertEquals("{}", subject.getCurrentResult());
108 }
109
110 @Test
111 public void testChangeVote() {
112 subject.callVote("test");
113 String response = subject.evaluateVote("alice", "aye");
114 Assert.assertEquals(String.format(resourceBundle.getString("count_vote"), "alice", "AYE"), response);
115 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
116 response = subject.evaluateVote("alice", "naye");
117 Assert.assertEquals(String.format(resourceBundle.getString("count_vote"), "alice", "NAYE"), response);
118 Assert.assertEquals("{alice=NAYE}", subject.getCurrentResult());
119 }
120
121 @Test
122 public void testNoChangeForInvalidVote() {
123 subject.callVote("test");
124 String response = subject.evaluateVote("alice", "aye");
125 Assert.assertEquals(String.format(resourceBundle.getString("count_vote"), "alice", "AYE"), response);
126 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
127 response = subject.evaluateVote("alice", "moo");
128 Assert.assertEquals(String.format(resourceBundle.getString("vote_not_understood"), "alice"), response);
129 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
130 }
131
132 @Test
133 public void testInvalidProxyVote() {
134 subject.callVote("test");
135 String response = subject.evaluateVote("alice", "proxy bob moo");
136 Assert.assertEquals(String.format(resourceBundle.getString("vote_not_understood"), "alice"), response);
137 Assert.assertEquals("{}", subject.getCurrentResult());
138 }
139
140 @Test
141 public void testInvalidProxyVoteTokenCount() {
142 subject.callVote("test");
143 String response = subject.evaluateVote("alice", "proxy ");
144 Assert.assertEquals(String.format(resourceBundle.getString("invalid_proxy_vote"), "alice"), response);
145 Assert.assertEquals("{}", subject.getCurrentResult());
146 }
147
148 @Test
149 public void testCloseFreshVote() {
150 subject.callVote("fresh vote");
151 String[] response = subject.closeVote();
152 Assert.assertArrayEquals(new String[]{"AYE: 0", "NAYE: 0", "ABSTAIN: 0"}, response);
153 Assert.assertEquals("", subject.getTopic());
154 Assert.assertEquals(IDLE, subject.getState());
155 }
156
157 @Test
158 public void testCloseVote() {
159 subject.callVote("fresh vote");
160 subject.evaluateVote("alice", "AyE");
161 subject.evaluateVote("bob", "NaYe");
162 subject.evaluateVote("claire", "yes");
163 subject.evaluateVote("debra", "abs");
164 subject.evaluateVote("alice", "proxy mike no");
165 subject.evaluateVote("debra", "ja");
166 subject.evaluateVote("malory", "evil");
167 String[] response = subject.closeVote();
168 Assert.assertArrayEquals(new String[]{"AYE: 3", "NAYE: 2", "ABSTAIN: 0"}, response);
169 Assert.assertEquals("", subject.getTopic());
170 Assert.assertEquals(IDLE, subject.getState());
171 }
172 }