Add unit test for the channel-not-joined handler.
[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.event.ChannelNotJoined.Reason.badChannelKey;
5 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.banned;
6 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.inviteOnly;
7 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.registeredNicknamesOnly;
8 import static org.hamcrest.MatcherAssert.assertThat;
9 import static org.hamcrest.Matchers.is;
10 import static org.mockito.ArgumentCaptor.forClass;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14
15 import net.pterodactylus.irc.Connection;
16 import net.pterodactylus.irc.Reply;
17 import net.pterodactylus.irc.event.ChannelNotJoined;
18 import net.pterodactylus.irc.event.ChannelNotJoined.Reason;
19
20 import com.google.common.eventbus.EventBus;
21 import org.junit.Test;
22 import org.mockito.ArgumentCaptor;
23
24 /**
25  * Unit test for {@link ChannelNotJoinedHandler}.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class ChannelNotJoinedHandlerTest {
30
31         private final EventBus eventBus = mock(EventBus.class);
32         private final Connection connection = mock(Connection.class);
33         private final ChannelNotJoinedHandler handler =
34                         new ChannelNotJoinedHandler(eventBus, connection);
35
36         @Test
37         public void handlerWillOnlyReactToSpecificCommands() {
38                 for (String command : asList("473", "474", "475", "477")) {
39                         assertThat(handler.willHandle(createReply(command)), is(true));
40                 }
41         }
42
43         @Test
44         public void handlerWillNotWantToHandleOtherCommands() {
45                 for (String command : asList("PRIVMSG", "NOTICE", "JOIN", "KICK",
46                                 "PART", "QUIT", "331", "433")) {
47                         assertThat(handler.willHandle(createReply(command)), is(false));
48                 }
49         }
50
51         private Reply createReply(String command) {
52                 Reply reply = mock(Reply.class);
53                 return when(reply.command()).thenReturn(command).getMock();
54         }
55
56         @Test
57         public void bannedChannelNotJoinedIsRecognizedCorrectly() {
58                 handler.handleReply(createReply("474", "#test"));
59                 verifyReasonIs(banned);
60         }
61
62         private void verifyReasonIs(Reason reason) {
63                 ArgumentCaptor<ChannelNotJoined> channelNotJoined = forClass(
64                                 ChannelNotJoined.class);
65                 verify(eventBus).post(channelNotJoined.capture());
66                 assertThat(channelNotJoined.getValue().channel(), is("#test"));
67                 assertThat(channelNotJoined.getValue().reason(), is(reason));
68                 assertThat(channelNotJoined.getValue().connection(), is(connection));
69         }
70
71         @Test
72         public void inviteOnlyChannelNotJoinedIsRecognizedCorrectly() {
73                 handler.handleReply(createReply("473", "#test"));
74                 verifyReasonIs(inviteOnly);
75         }
76
77         @Test
78         public void channelWithBadChannelKeyNotJoinedIsRecognizedCorrectly() {
79                 handler.handleReply(createReply("475", "#test"));
80                 verifyReasonIs(badChannelKey);
81         }
82
83         @Test
84         public void channelRequiringRegisteredNicknamesNotJoinedIsRecognizedCorrectly() {
85                 handler.handleReply(createReply("477", "#test"));
86                 verifyReasonIs(registeredNicknamesOnly);
87         }
88
89         private Reply createReply(String command, String channel) {
90                 final Reply reply = mock(Reply.class);
91                 when(reply.command()).thenReturn(command);
92                 when(reply.parameters()).thenReturn(asList(":some.server", channel));
93                 return reply;
94         }
95
96 }