You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServerEventHandlerTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc;
  23. import com.dmdirc.events.AppErrorEvent;
  24. import com.dmdirc.events.DMDircEvent;
  25. import com.dmdirc.events.ServerCtcpEvent;
  26. import com.dmdirc.events.UserErrorEvent;
  27. import com.dmdirc.interfaces.Connection;
  28. import com.dmdirc.interfaces.User;
  29. import com.dmdirc.parser.common.CallbackManager;
  30. import com.dmdirc.parser.common.ParserError;
  31. import com.dmdirc.parser.interfaces.ChannelInfo;
  32. import com.dmdirc.parser.interfaces.ClientInfo;
  33. import com.dmdirc.parser.interfaces.Parser;
  34. import com.dmdirc.parser.interfaces.callbacks.CallbackInterface;
  35. import com.dmdirc.parser.interfaces.callbacks.ChannelSelfJoinListener;
  36. import com.dmdirc.parser.interfaces.callbacks.ErrorInfoListener;
  37. import com.dmdirc.parser.interfaces.callbacks.PrivateActionListener;
  38. import com.dmdirc.parser.interfaces.callbacks.PrivateCtcpListener;
  39. import com.dmdirc.parser.interfaces.callbacks.PrivateMessageListener;
  40. import com.google.common.collect.Lists;
  41. import java.util.Date;
  42. import java.util.Optional;
  43. import org.junit.Before;
  44. import org.junit.Test;
  45. import org.junit.runner.RunWith;
  46. import org.mockito.ArgumentCaptor;
  47. import org.mockito.Mock;
  48. import org.mockito.runners.MockitoJUnitRunner;
  49. import static org.junit.Assert.assertEquals;
  50. import static org.junit.Assert.assertNotNull;
  51. import static org.junit.Assert.assertTrue;
  52. import static org.mockito.Matchers.any;
  53. import static org.mockito.Matchers.anyString;
  54. import static org.mockito.Matchers.eq;
  55. import static org.mockito.Mockito.mock;
  56. import static org.mockito.Mockito.verify;
  57. import static org.mockito.Mockito.when;
  58. @RunWith(MockitoJUnitRunner.class)
  59. public class ServerEventHandlerTest {
  60. @Mock private Server server;
  61. @Mock private Parser parser;
  62. @Mock private CallbackManager callbackManager;
  63. @Mock private DMDircMBassador eventBus;
  64. @Mock private ChannelInfo channelInfo;
  65. @Mock private User user;
  66. @Mock private UserFactory userFactory;
  67. @Mock private Query query;
  68. @Mock private Date date;
  69. @Mock private ClientInfo clientInfo;
  70. @Before
  71. public void setUp() {
  72. when(server.getParser()).thenReturn(Optional.of(parser));
  73. when(server.getState()).thenReturn(ServerState.CONNECTED);
  74. when(parser.getCallbackManager()).thenReturn(callbackManager);
  75. when(server.getConnection()).thenReturn(Optional.ofNullable(server));
  76. when(userFactory.getUser(anyString(), any(Connection.class), eq(clientInfo))).thenReturn(user);
  77. when(userFactory.getUser(anyString(), any(Connection.class), Optional.of(anyString()),
  78. Optional.of(anyString()), Optional.of(anyString()), eq(clientInfo)))
  79. .thenReturn(user);
  80. when(server.getUser(anyString())).thenReturn(user);
  81. when(server.getLocalUser()).thenReturn(user);
  82. final ServerEventHandler handler = new ServerEventHandler(server, eventBus);
  83. handler.registerCallbacks();
  84. when(user.getNickname()).thenReturn("ho");
  85. when(user.getUsername()).thenReturn(Optional.of("st"));
  86. when(user.getHostname()).thenReturn(Optional.of("name"));
  87. }
  88. @Test
  89. public void testSelfJoinAddsChannel() {
  90. final ChannelSelfJoinListener listener = getCallback(ChannelSelfJoinListener.class);
  91. listener.onChannelSelfJoin(parser, date, channelInfo);
  92. verify(server).addChannel(channelInfo);
  93. }
  94. @Test
  95. public void testOnPrivateMessageCreatesQuery() {
  96. when(server.hasQuery("ho!st@name")).thenReturn(false);
  97. when(server.getQuery("ho!st@name")).thenReturn(query);
  98. final PrivateMessageListener listener = getCallback(PrivateMessageListener.class);
  99. listener.onPrivateMessage(parser, date, "message", "ho!st@name");
  100. verify(server).getQuery("ho!st@name");
  101. }
  102. @Test
  103. public void testOnPrivateMessageRelaysMessage() {
  104. when(server.hasQuery("ho!st@name")).thenReturn(false);
  105. when(server.getQuery("ho!st@name")).thenReturn(query);
  106. final PrivateMessageListener listener = getCallback(PrivateMessageListener.class);
  107. listener.onPrivateMessage(parser, date, "message", "ho!st@name");
  108. verify(query).onPrivateMessage(parser, date, "message", "ho!st@name");
  109. }
  110. @Test
  111. public void testOnPrivateActionCreatesQuery() {
  112. when(server.hasQuery("ho!st@name")).thenReturn(false);
  113. when(server.getQuery("ho!st@name")).thenReturn(query);
  114. final PrivateActionListener listener = getCallback(PrivateActionListener.class);
  115. listener.onPrivateAction(parser, date, "message", "ho!st@name");
  116. verify(server).getQuery("ho!st@name");
  117. }
  118. @Test
  119. public void testOnPrivateActionRelaysMessage() {
  120. when(server.hasQuery("ho!st@name")).thenReturn(false);
  121. when(server.getQuery("ho!st@name")).thenReturn(query);
  122. final PrivateActionListener listener = getCallback(PrivateActionListener.class);
  123. listener.onPrivateAction(parser, date, "message", "ho!st@name");
  124. verify(query).onPrivateAction(parser, date, "message", "ho!st@name");
  125. }
  126. @Test
  127. public void testOnErrorInfoEmitsUserErrorEvent() {
  128. final ParserError error = mock(ParserError.class);
  129. when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
  130. when(error.getLastLine()).thenReturn("lastline");
  131. when(server.getAddress()).thenReturn("serveraddress");
  132. when(error.getData()).thenReturn("DATA");
  133. when(error.isUserError()).thenReturn(true);
  134. final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
  135. listener.onErrorInfo(parser, date, error);
  136. final UserErrorEvent event = getAsyncEvent(UserErrorEvent.class);
  137. assertEquals("DATA", event.getMessage());
  138. }
  139. @Test
  140. public void testOnErrorInfoEmitsAppErrorEvent() {
  141. final ParserError error = mock(ParserError.class);
  142. when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
  143. when(error.getLastLine()).thenReturn("lastline");
  144. when(server.getAddress()).thenReturn("serveraddress");
  145. when(error.getData()).thenReturn("DATA");
  146. when(error.isUserError()).thenReturn(false);
  147. final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
  148. listener.onErrorInfo(parser, date, error);
  149. final AppErrorEvent event = getAsyncEvent(AppErrorEvent.class);
  150. assertEquals("DATA", event.getMessage());
  151. }
  152. @Test
  153. public void testOnErrorInfoIncludesConnectionDetails() {
  154. final ParserError error = mock(ParserError.class);
  155. when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
  156. when(error.getLastLine()).thenReturn("lastline");
  157. when(server.getAddress()).thenReturn("serveraddress");
  158. when(error.getData()).thenReturn("DATA");
  159. when(error.isUserError()).thenReturn(false);
  160. final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
  161. listener.onErrorInfo(parser, date, error);
  162. final AppErrorEvent event = getAsyncEvent(AppErrorEvent.class);
  163. assertNotNull(event.getThrowable());
  164. assertTrue(event.getThrowable().getMessage().contains("lastline"));
  165. assertTrue(event.getThrowable().getMessage().contains("iline1"));
  166. assertTrue(event.getThrowable().getMessage().contains("iline2"));
  167. assertTrue(event.getThrowable().getMessage().contains("serveraddress"));
  168. }
  169. @Test
  170. public void testOnErrorInfoIncludesException() {
  171. final ParserError error = mock(ParserError.class);
  172. final Exception exception = mock(Exception.class);
  173. when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
  174. when(error.getLastLine()).thenReturn("lastline");
  175. when(server.getAddress()).thenReturn("serveraddress");
  176. when(error.getData()).thenReturn("DATA");
  177. when(error.isException()).thenReturn(true);
  178. when(error.getException()).thenReturn(exception);
  179. final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
  180. listener.onErrorInfo(parser, date, error);
  181. final AppErrorEvent event = getAsyncEvent(AppErrorEvent.class);
  182. assertNotNull(event.getThrowable());
  183. assertEquals(exception, event.getThrowable());
  184. }
  185. @Test
  186. public void testOnPrivateCTCPRaisesEvent() {
  187. when(server.getUser("ho!st@name")).thenReturn(user);
  188. final PrivateCtcpListener listener = getCallback(PrivateCtcpListener.class);
  189. listener.onPrivateCTCP(parser, date, "type", "message", "ho!st@name");
  190. final ServerCtcpEvent event = getEvent(ServerCtcpEvent.class);
  191. assertEquals("type", event.getType());
  192. assertEquals("message", event.getContent());
  193. assertEquals(user, event.getUser());
  194. }
  195. @Test
  196. public void testOnPrivateCTCPSendsReplyIfEventUnhandled() {
  197. when(server.getUser("ho!st@name")).thenReturn(user);
  198. final PrivateCtcpListener listener = getCallback(PrivateCtcpListener.class);
  199. listener.onPrivateCTCP(parser, date, "type", "message", "ho!st@name");
  200. verify(server).sendCTCPReply("ho", "type", "message");
  201. }
  202. private <T extends CallbackInterface> T getCallback(final Class<T> type) {
  203. final ArgumentCaptor<T> captor = ArgumentCaptor.forClass(type);
  204. verify(callbackManager).addCallback(eq(type), captor.capture());
  205. assertNotNull(captor.getValue());
  206. return captor.getValue();
  207. }
  208. private <T extends DMDircEvent> T getAsyncEvent(final Class<T> type) {
  209. final ArgumentCaptor<T> captor = ArgumentCaptor.forClass(type);
  210. verify(eventBus).publishAsync(captor.capture());
  211. assertNotNull(captor.getValue());
  212. return captor.getValue();
  213. }
  214. private <T extends DMDircEvent> T getEvent(final Class<T> type) {
  215. final ArgumentCaptor<T> captor = ArgumentCaptor.forClass(type);
  216. verify(eventBus).publish(captor.capture());
  217. assertNotNull(captor.getValue());
  218. return captor.getValue();
  219. }
  220. }