Fix misinterpretation of voteBot.warnSecs
[cacert-votebot.git] / src / test / java / org / cacert / votebot / shared / CAcertVoteMechanicsTest.java
1 /*
2 * Copyright (c) 2016, 2018. 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.text.MessageFormat;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.ResourceBundle;
31
32 import static org.cacert.votebot.shared.CAcertVoteMechanics.State.IDLE;
33 import static org.cacert.votebot.shared.CAcertVoteMechanics.State.RUNNING;
34 import static org.cacert.votebot.shared.CAcertVoteMechanics.State.STOPPING;
35 import static org.hamcrest.Matchers.equalTo;
36 import static org.junit.Assert.assertThat;
37 import static org.junit.Assert.fail;
38
39 /**
40 * @author Jan Dittberner
41 */
42 public class CAcertVoteMechanicsTest {
43 private CAcertVoteMechanics subject;
44 private ResourceBundle messages;
45 private static final long TEST_TIMEOUT = 120;
46 private static final long TEST_WARN = 30;
47
48 @Test
49 public void testStateEnum() {
50 State[] values = State.values();
51 Assert.assertEquals(3, values.length);
52 List<State> states = Arrays.asList(values);
53 Assert.assertTrue(states.contains(RUNNING));
54 Assert.assertTrue(states.contains(IDLE));
55 Assert.assertTrue(states.contains(STOPPING));
56 }
57
58 @Before
59 public void setup() {
60 messages = ResourceBundle.getBundle("messages");
61 subject = new CAcertVoteMechanics();
62 }
63
64 @Test
65 public void testNoVote() {
66 String response = subject.evaluateVote("alice", "test");
67 Assert.assertEquals(MessageFormat.format(messages.getString("no_vote_running"), "alice"), response);
68 }
69
70 @Test
71 public void testCallVote() {
72 String response = subject.callVote("test vote", TEST_WARN, TEST_TIMEOUT);
73 Assert.assertEquals(messages.getString("vote_started"), response);
74 Assert.assertEquals("test vote", subject.getTopic());
75 Assert.assertEquals(RUNNING, subject.getState());
76 }
77
78 @Test
79 public void testRefuseParallelCallVote() {
80 subject.callVote("first", 30, TEST_TIMEOUT);
81 String response = subject.callVote("second", TEST_WARN, TEST_TIMEOUT);
82 Assert.assertEquals(messages.getString("vote_running"), response);
83 Assert.assertEquals("first", subject.getTopic());
84 Assert.assertEquals(RUNNING, subject.getState());
85 }
86
87 @Test
88 public void testFreshVoteResult() {
89 subject.callVote("fresh vote", TEST_WARN, TEST_TIMEOUT);
90 Assert.assertEquals("{}", subject.getCurrentResult());
91 }
92
93 @Test
94 public void testVote() {
95 subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
96 String response = subject.evaluateVote("alice", "aye");
97 Assert.assertEquals(
98 MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
99 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
100 }
101
102 @Test
103 public void testProxyVote() {
104 subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
105 String response = subject.evaluateVote("alice", "proxy bob aye");
106 Assert.assertEquals(
107 MessageFormat.format(messages.getString("count_proxy_vote"), "alice", "bob", "AYE"),
108 response);
109 Assert.assertEquals("{bob=AYE}", subject.getCurrentResult());
110 }
111
112 @Test
113 public void testInvalidVote() {
114 subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
115 String response = subject.evaluateVote("alice", "moo");
116 Assert.assertEquals(
117 MessageFormat.format(messages.getString("vote_not_understood"), "alice"), response);
118 Assert.assertEquals("{}", subject.getCurrentResult());
119 }
120
121 @Test
122 public void testChangeVote() {
123 subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
124 String response = subject.evaluateVote("alice", "aye");
125 Assert.assertEquals(
126 MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
127 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
128 response = subject.evaluateVote("alice", "naye");
129 Assert.assertEquals(
130 MessageFormat.format(messages.getString("count_vote"), "alice", "NAYE"), response);
131 Assert.assertEquals("{alice=NAYE}", subject.getCurrentResult());
132 }
133
134 @Test
135 public void testNoChangeForInvalidVote() {
136 subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
137 String response = subject.evaluateVote("alice", "aye");
138 Assert.assertEquals(
139 MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
140 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
141 response = subject.evaluateVote("alice", "moo");
142 Assert.assertEquals(
143 MessageFormat.format(messages.getString("vote_not_understood"), "alice"), response);
144 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
145 }
146
147 @Test
148 public void testInvalidProxyVote() {
149 subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
150 String response = subject.evaluateVote("alice", "proxy bob moo");
151 Assert.assertEquals(
152 MessageFormat.format(messages.getString("vote_not_understood"), "alice"), response);
153 Assert.assertEquals("{}", subject.getCurrentResult());
154 }
155
156 @Test
157 public void testInvalidProxyVoteTokenCount() {
158 subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
159 String response = subject.evaluateVote("alice", "proxy ");
160 Assert.assertEquals(
161 MessageFormat.format(messages.getString("invalid_proxy_vote"), "alice"), response);
162 Assert.assertEquals("{}", subject.getCurrentResult());
163 }
164
165 @Test
166 public void testCloseFreshVote() {
167 subject.callVote("fresh vote", TEST_WARN, TEST_TIMEOUT);
168 String stopResponse = subject.stopVote("timeout");
169 assertThat(stopResponse, equalTo(
170 MessageFormat.format(
171 messages.getString("finishing_vote"), subject.getTopic(), "timeout")));
172 String[] response = subject.closeVote();
173 assertThat(response, equalTo(new String[]{"AYE: 0", "NAYE: 0", "ABSTAIN: 0"}));
174 assertThat(subject.getTopic(), equalTo(""));
175 assertThat(subject.getState(), equalTo(IDLE));
176 }
177
178 @Test
179 public void testFailStopNoVote() {
180 try {
181 subject.stopVote("test");
182 fail("Expected IllegalStateException has not been thrown.");
183 } catch (IllegalStateException e) {
184 assertThat(e.getMessage(), equalTo(messages.getString("no_vote_running_private")));
185 }
186 }
187
188 @Test
189 public void testFailCloseRunningVote() {
190 subject.callVote("running vote", TEST_WARN, TEST_TIMEOUT);
191 try {
192 subject.closeVote();
193 fail("Expected IllegalStateException has not been thrown.");
194 } catch (IllegalStateException e) {
195 assertThat(e.getMessage(), equalTo(messages.getString("cannot_close_running_vote")));
196 }
197 }
198
199 @Test
200 public void testCloseVote() {
201 subject.callVote("fresh vote", TEST_WARN, TEST_TIMEOUT);
202 subject.evaluateVote("alice", "AyE");
203 subject.evaluateVote("bob", "NaYe");
204 subject.evaluateVote("claire", "yes");
205 subject.evaluateVote("debra", "abs");
206 subject.evaluateVote("alice", "proxy mike no");
207 subject.evaluateVote("debra", "ja");
208 subject.evaluateVote("malory", "evil");
209 subject.stopVote("test");
210 String[] response = subject.closeVote();
211 Assert.assertArrayEquals(new String[]{"AYE: 3", "NAYE: 2", "ABSTAIN: 0"}, response);
212 Assert.assertEquals("", subject.getTopic());
213 Assert.assertEquals(IDLE, subject.getState());
214 }
215 }