Move reply mocking into its own helper class.
[xudocci.git] / src / test / java / net / pterodactylus / irc / connection / ChannelNotJoinedHandlerTest.java
1 package net.pterodactylus.irc.connection;
2
3 import static java.util.Arrays.asList;
4 import static net.pterodactylus.irc.connection.Replies.createReply;
5 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.badChannelKey;
6 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.banned;
7 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.inviteOnly;
8 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.registeredNicknamesOnly;
9 import static org.hamcrest.MatcherAssert.assertThat;
10 import static org.hamcrest.Matchers.is;
11 import static org.mockito.ArgumentCaptor.forClass;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.verify;
14
15 import net.pterodactylus.irc.Connection;
16 import net.pterodactylus.irc.event.ChannelNotJoined;
17 import net.pterodactylus.irc.event.ChannelNotJoined.Reason;
18
19 import com.google.common.eventbus.EventBus;
20 import org.junit.Test;
21 import org.mockito.ArgumentCaptor;
22
23 /**
24  * Unit test for {@link ChannelNotJoinedHandler}.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27  */
28 public class ChannelNotJoinedHandlerTest {
29
30         private final EventBus eventBus = mock(EventBus.class);
31         private final Connection connection = mock(Connection.class);
32         private final ChannelNotJoinedHandler handler =
33                         new ChannelNotJoinedHandler(eventBus, connection);
34
35         @Test
36         public void handlerWillOnlyReactToSpecificCommands() {
37                 for (String command : asList("473", "474", "475", "477")) {
38                         assertThat(handler.willHandle(createReply(command)), is(true));
39                 }
40         }
41
42         @Test
43         public void handlerWillNotWantToHandleOtherCommands() {
44                 for (String command : asList("PRIVMSG", "NOTICE", "JOIN", "KICK",
45                                 "PART", "QUIT", "331", "433")) {
46                         assertThat(handler.willHandle(createReply(command)), is(false));
47                 }
48         }
49
50         @Test
51         public void bannedChannelNotJoinedIsRecognizedCorrectly() {
52                 handler.handleReply(createReply("474", "#test"));
53                 verifyReasonIs(banned);
54         }
55
56         private void verifyReasonIs(Reason reason) {
57                 ArgumentCaptor<ChannelNotJoined> channelNotJoined = forClass(
58                                 ChannelNotJoined.class);
59                 verify(eventBus).post(channelNotJoined.capture());
60                 assertThat(channelNotJoined.getValue().channel(), is("#test"));
61                 assertThat(channelNotJoined.getValue().reason(), is(reason));
62                 assertThat(channelNotJoined.getValue().connection(), is(connection));
63         }
64
65         @Test
66         public void inviteOnlyChannelNotJoinedIsRecognizedCorrectly() {
67                 handler.handleReply(createReply("473", "#test"));
68                 verifyReasonIs(inviteOnly);
69         }
70
71         @Test
72         public void channelWithBadChannelKeyNotJoinedIsRecognizedCorrectly() {
73                 handler.handleReply(createReply("475", "#test"));
74                 verifyReasonIs(badChannelKey);
75         }
76
77         @Test
78         public void channelRequiringRegisteredNicknamesNotJoinedIsRecognizedCorrectly() {
79                 handler.handleReply(createReply("477", "#test"));
80                 verifyReasonIs(registeredNicknamesOnly);
81         }
82
83 }