Implement help command, add more tests, improve messages
[cacert-votebot.git] / src / test / java / org / cacert / votebot / vote / CAcertVoteBotTest.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 package org.cacert.votebot.vote;
20
21 import org.cacert.votebot.shared.CAcertVoteMechanics;
22 import org.cacert.votebot.shared.IRCClient;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.test.context.ContextConfiguration;
28 import org.springframework.test.context.junit4.SpringRunner;
29 import org.springframework.test.util.ReflectionTestUtils;
30
31 import java.text.MessageFormat;
32 import java.util.ResourceBundle;
33
34 import static org.mockito.Mockito.*;
35
36 @SuppressWarnings("SpringJavaAutowiredMembersInspection")
37 @RunWith(SpringRunner.class)
38 @ContextConfiguration(classes = {TestConfiguration.class})
39 public class CAcertVoteBotTest {
40
41 private static final String TEST_VOTE_TOPIC = "Is it really a vote or just a fake election?";
42
43 @Autowired
44 private IRCClient ircClient;
45 @Autowired
46 private CAcertVoteMechanics mechanics;
47
48 private ResourceBundle messages;
49
50 private CAcertVoteBot bot;
51
52 @Before
53 public void setupTest() {
54 messages = ResourceBundle.getBundle("messages");
55 bot = new CAcertVoteBot(mechanics, ircClient);
56 ReflectionTestUtils.setField(bot, "meetingChannel", "meeting");
57 ReflectionTestUtils.setField(bot, "voteChannel", "vote");
58 ReflectionTestUtils.setField(bot, "timeout", 120);
59 }
60
61 @Test
62 public void testStartVoteBot() throws Exception {
63 when(mechanics.callVote(TEST_VOTE_TOPIC)).thenReturn(messages.getString("vote_started"));
64 when(mechanics.getTopic()).thenReturn(TEST_VOTE_TOPIC);
65 bot.privateMessage("test", String.format("vote %s", TEST_VOTE_TOPIC));
66 verify(ircClient).send(
67 MessageFormat.format(messages.getString("new_vote"), "test", TEST_VOTE_TOPIC),
68 "meeting");
69 verify(ircClient).send(
70 MessageFormat.format(messages.getString("new_vote"), "test", TEST_VOTE_TOPIC),
71 "vote");
72 verify(ircClient).send(
73 MessageFormat.format(messages.getString("cast_vote_in_vote_channel"), "vote"),
74 "meeting");
75 verify(ircClient).send(
76 MessageFormat.format(messages.getString("cast_vote_in_next_seconds"), 120),
77 "vote");
78 }
79
80 @Test
81 public void testHelp() throws Exception {
82 bot.privateMessage("test", "help");
83 verify(ircClient).sendPrivate(messages.getString("help_message"), "test");
84 verifyNoMoreInteractions(ircClient);
85 }
86 }