Implement help command, add more tests, improve messages
[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
35 /**
36 * @author Jan Dittberner
37 */
38 public class CAcertVoteMechanicsTest {
39 private CAcertVoteMechanics subject;
40 private ResourceBundle messages;
41
42 @Test
43 public void testStateEnum() {
44 State[] values = State.values();
45 Assert.assertEquals(2, values.length);
46 List<State> states = Arrays.asList(values);
47 Assert.assertTrue(states.contains(RUNNING));
48 Assert.assertTrue(states.contains(IDLE));
49 }
50
51 @Before
52 public void setup() {
53 messages = ResourceBundle.getBundle("messages");
54 subject = new CAcertVoteMechanics();
55 }
56
57 @Test
58 public void testNoVote() {
59 String response = subject.evaluateVote("alice", "test");
60 Assert.assertEquals(MessageFormat.format(messages.getString("no_vote_running"), "alice"), response);
61 }
62
63 @Test
64 public void testCallVote() {
65 String response = subject.callVote("test vote");
66 Assert.assertEquals(messages.getString("vote_started"), response);
67 Assert.assertEquals("test vote", subject.getTopic());
68 Assert.assertEquals(RUNNING, subject.getState());
69 }
70
71 @Test
72 public void testRefuseParallelCallVote() {
73 subject.callVote("first");
74 String response = subject.callVote("second");
75 Assert.assertEquals(messages.getString("vote_running"), response);
76 Assert.assertEquals("first", subject.getTopic());
77 Assert.assertEquals(RUNNING, subject.getState());
78 }
79
80 @Test
81 public void testFreshVoteResult() {
82 subject.callVote("fresh vote");
83 Assert.assertEquals("{}", subject.getCurrentResult());
84 }
85
86 @Test
87 public void testVote() {
88 subject.callVote("test");
89 String response = subject.evaluateVote("alice", "aye");
90 Assert.assertEquals(
91 MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
92 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
93 }
94
95 @Test
96 public void testProxyVote() {
97 subject.callVote("test");
98 String response = subject.evaluateVote("alice", "proxy bob aye");
99 Assert.assertEquals(
100 MessageFormat.format(messages.getString("count_proxy_vote"), "alice", "bob", "AYE"),
101 response);
102 Assert.assertEquals("{bob=AYE}", subject.getCurrentResult());
103 }
104
105 @Test
106 public void testInvalidVote() {
107 subject.callVote("test");
108 String response = subject.evaluateVote("alice", "moo");
109 Assert.assertEquals(
110 MessageFormat.format(messages.getString("vote_not_understood"), "alice"), response);
111 Assert.assertEquals("{}", subject.getCurrentResult());
112 }
113
114 @Test
115 public void testChangeVote() {
116 subject.callVote("test");
117 String response = subject.evaluateVote("alice", "aye");
118 Assert.assertEquals(
119 MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
120 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
121 response = subject.evaluateVote("alice", "naye");
122 Assert.assertEquals(
123 MessageFormat.format(messages.getString("count_vote"), "alice", "NAYE"), response);
124 Assert.assertEquals("{alice=NAYE}", subject.getCurrentResult());
125 }
126
127 @Test
128 public void testNoChangeForInvalidVote() {
129 subject.callVote("test");
130 String response = subject.evaluateVote("alice", "aye");
131 Assert.assertEquals(
132 MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
133 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
134 response = subject.evaluateVote("alice", "moo");
135 Assert.assertEquals(
136 MessageFormat.format(messages.getString("vote_not_understood"), "alice"), response);
137 Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
138 }
139
140 @Test
141 public void testInvalidProxyVote() {
142 subject.callVote("test");
143 String response = subject.evaluateVote("alice", "proxy bob moo");
144 Assert.assertEquals(
145 MessageFormat.format(messages.getString("vote_not_understood"), "alice"), response);
146 Assert.assertEquals("{}", subject.getCurrentResult());
147 }
148
149 @Test
150 public void testInvalidProxyVoteTokenCount() {
151 subject.callVote("test");
152 String response = subject.evaluateVote("alice", "proxy ");
153 Assert.assertEquals(
154 MessageFormat.format(messages.getString("invalid_proxy_vote"), "alice"), response);
155 Assert.assertEquals("{}", subject.getCurrentResult());
156 }
157
158 @Test
159 public void testCloseFreshVote() {
160 subject.callVote("fresh vote");
161 String[] response = subject.closeVote();
162 Assert.assertArrayEquals(new String[]{"AYE: 0", "NAYE: 0", "ABSTAIN: 0"}, response);
163 Assert.assertEquals("", subject.getTopic());
164 Assert.assertEquals(IDLE, subject.getState());
165 }
166
167 @Test
168 public void testCloseVote() {
169 subject.callVote("fresh vote");
170 subject.evaluateVote("alice", "AyE");
171 subject.evaluateVote("bob", "NaYe");
172 subject.evaluateVote("claire", "yes");
173 subject.evaluateVote("debra", "abs");
174 subject.evaluateVote("alice", "proxy mike no");
175 subject.evaluateVote("debra", "ja");
176 subject.evaluateVote("malory", "evil");
177 String[] response = subject.closeVote();
178 Assert.assertArrayEquals(new String[]{"AYE: 3", "NAYE: 2", "ABSTAIN: 0"}, response);
179 Assert.assertEquals("", subject.getTopic());
180 Assert.assertEquals(IDLE, subject.getState());
181 }
182 }