summaryrefslogtreecommitdiff
path: root/src/test/java/org/cacert/votebot/shared/CAcertVoteMechanicsTest.java
blob: 6b46b25fc273d5fdd0579db294d85a652703b9f5 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (c) 2016, 2018. Jan Dittberner
 *
 * This file is part of CAcert VoteBot.
 *
 * CAcert VoteBot is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * CAcert VoteBot is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CAcert VoteBot.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.cacert.votebot.shared;

import org.cacert.votebot.shared.CAcertVoteMechanics.State;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

import static org.cacert.votebot.shared.CAcertVoteMechanics.State.IDLE;
import static org.cacert.votebot.shared.CAcertVoteMechanics.State.RUNNING;
import static org.cacert.votebot.shared.CAcertVoteMechanics.State.STOPPING;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

/**
 * @author Jan Dittberner
 */
public class CAcertVoteMechanicsTest {
    private CAcertVoteMechanics subject;
    private ResourceBundle messages;
    private static final long TEST_TIMEOUT = 120;
    private static final long TEST_WARN = 30;

    @Test
    public void testStateEnum() {
        State[] values = State.values();
        Assert.assertEquals(3, values.length);
        List<State> states = Arrays.asList(values);
        Assert.assertTrue(states.contains(RUNNING));
        Assert.assertTrue(states.contains(IDLE));
        Assert.assertTrue(states.contains(STOPPING));
    }

    @Before
    public void setup() {
        messages = ResourceBundle.getBundle("messages");
        subject = new CAcertVoteMechanics();
    }

    @Test
    public void testNoVote() {
        String response = subject.evaluateVote("alice", "test");
        Assert.assertEquals(MessageFormat.format(messages.getString("no_vote_running"), "alice"), response);
    }

    @Test
    public void testCallVote() {
        String response = subject.callVote("test vote", TEST_WARN, TEST_TIMEOUT);
        Assert.assertEquals(messages.getString("vote_started"), response);
        Assert.assertEquals("test vote", subject.getTopic());
        Assert.assertEquals(RUNNING, subject.getState());
    }

    @Test
    public void testRefuseParallelCallVote() {
        subject.callVote("first", 30, TEST_TIMEOUT);
        String response = subject.callVote("second", TEST_WARN, TEST_TIMEOUT);
        Assert.assertEquals(messages.getString("vote_running"), response);
        Assert.assertEquals("first", subject.getTopic());
        Assert.assertEquals(RUNNING, subject.getState());
    }

    @Test
    public void testFreshVoteResult() {
        subject.callVote("fresh vote", TEST_WARN, TEST_TIMEOUT);
        Assert.assertEquals("{}", subject.getCurrentResult());
    }

    @Test
    public void testVote() {
        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
        String response = subject.evaluateVote("alice", "aye");
        Assert.assertEquals(
                MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
        Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
    }

    @Test
    public void testProxyVote() {
        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
        String response = subject.evaluateVote("alice", "proxy bob aye");
        Assert.assertEquals(
                MessageFormat.format(messages.getString("count_proxy_vote"), "alice", "bob", "AYE"),
                response);
        Assert.assertEquals("{bob=AYE}", subject.getCurrentResult());
    }

    @Test
    public void testInvalidVote() {
        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
        String response = subject.evaluateVote("alice", "moo");
        Assert.assertEquals(
                MessageFormat.format(messages.getString("vote_not_understood"), "alice"), response);
        Assert.assertEquals("{}", subject.getCurrentResult());
    }

    @Test
    public void testChangeVote() {
        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
        String response = subject.evaluateVote("alice", "aye");
        Assert.assertEquals(
                MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
        Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
        response = subject.evaluateVote("alice", "naye");
        Assert.assertEquals(
                MessageFormat.format(messages.getString("count_vote"), "alice", "NAYE"), response);
        Assert.assertEquals("{alice=NAYE}", subject.getCurrentResult());
    }

    @Test
    public void testNoChangeForInvalidVote() {
        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
        String response = subject.evaluateVote("alice", "aye");
        Assert.assertEquals(
                MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
        Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
        response = subject.evaluateVote("alice", "moo");
        Assert.assertEquals(
                MessageFormat.format(messages.getString("vote_not_understood"), "alice"), response);
        Assert.assertEquals("{alice=AYE}", subject.getCurrentResult());
    }

    @Test
    public void testInvalidProxyVote() {
        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
        String response = subject.evaluateVote("alice", "proxy bob moo");
        Assert.assertEquals(
                MessageFormat.format(messages.getString("vote_not_understood"), "alice"), response);
        Assert.assertEquals("{}", subject.getCurrentResult());
    }

    @Test
    public void testInvalidProxyVoteTokenCount() {
        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
        String response = subject.evaluateVote("alice", "proxy ");
        Assert.assertEquals(
                MessageFormat.format(messages.getString("invalid_proxy_vote"), "alice"), response);
        Assert.assertEquals("{}", subject.getCurrentResult());
    }

    @Test
    public void testCloseFreshVote() {
        subject.callVote("fresh vote", TEST_WARN, TEST_TIMEOUT);
        String stopResponse = subject.stopVote("timeout");
        assertThat(stopResponse, equalTo(
                MessageFormat.format(
                        messages.getString("finishing_vote"), subject.getTopic(), "timeout")));
        String[] response = subject.closeVote();
        assertThat(response, equalTo(new String[]{"AYE: 0", "NAYE: 0", "ABSTAIN: 0"}));
        assertThat(subject.getTopic(), equalTo(""));
        assertThat(subject.getState(), equalTo(IDLE));
    }

    @Test
    public void testFailStopNoVote() {
        try {
            subject.stopVote("test");
            fail("Expected IllegalStateException has not been thrown.");
        } catch (IllegalStateException e) {
            assertThat(e.getMessage(), equalTo(messages.getString("no_vote_running_private")));
        }
    }

    @Test
    public void testFailCloseRunningVote() {
        subject.callVote("running vote", TEST_WARN, TEST_TIMEOUT);
        try {
            subject.closeVote();
            fail("Expected IllegalStateException has not been thrown.");
        } catch (IllegalStateException e) {
            assertThat(e.getMessage(), equalTo(messages.getString("cannot_close_running_vote")));
        }
    }

    @Test
    public void testCloseVote() {
        subject.callVote("fresh vote", TEST_WARN, TEST_TIMEOUT);
        subject.evaluateVote("alice", "AyE");
        subject.evaluateVote("bob", "NaYe");
        subject.evaluateVote("claire", "yes");
        subject.evaluateVote("debra", "abs");
        subject.evaluateVote("alice", "proxy mike no");
        subject.evaluateVote("debra", "ja");
        subject.evaluateVote("malory", "evil");
        subject.stopVote("test");
        String[] response = subject.closeVote();
        Assert.assertArrayEquals(new String[]{"AYE: 3", "NAYE: 2", "ABSTAIN: 0"}, response);
        Assert.assertEquals("", subject.getTopic());
        Assert.assertEquals(IDLE, subject.getState());
    }
}