TopicsMessagesBroadcaster { int checkAndSendMtcToSession(Session session, JsTopicMessageController msgControl, MessageToClient mtc, Object payload) throws SessionException { if (session != null) { if (session.isOpen()) { try { if (null != msgControl) { checkMessageTopic(userContextFactory.getUserContext(session.getId()), mtc.getId(), payload, msgControl); } mtc.setType(MessageType.MESSAGE); session.getAsyncRemote().sendObject(mtc); return 1; } catch (NotRecipientException ex) { logger.debug("{} is exclude to receive a message in {}", ex.getMessage(), mtc.getId()); } } else { throw new SessionException("CLOSED", null, session); } } return 0; }  int sendMessageToTopic(MessageToClient mtc, Object payload); int sendMessageToTopic(Collection<Session> sessionTargets, MessageToClient mtc, Object payload);  }
@Test public void checkAndSendMtcToSessionNullTest() throws SessionException { System.out.println("checkAndSendMtcToSession"); JsTopicMessageController jtmcmsgControl = mock(JsTopicMessageController.class); MessageToClient mtc = mock(MessageToClient.class); int result = instance.checkAndSendMtcToSession(null, jtmcmsgControl, mtc, null); assertThat(result).isEqualTo(0); }
@Test(expected = SessionException.class) public void checkAndSendMtcToSessionCloseTest() throws SessionException { System.out.println("checkAndSendMtcToSession"); Session session = mock(Session.class); JsTopicMessageController jtmcmsgControl = mock(JsTopicMessageController.class); MessageToClient mtc = mock(MessageToClient.class); when(session.isOpen()).thenReturn(Boolean.FALSE); instance.checkAndSendMtcToSession(session, jtmcmsgControl, mtc, null); }
@Test public void checkAndSendMtcToSessionTest() throws SessionException, NotRecipientException { System.out.println("checkAndSendMtcToSession"); Session session = mock(Session.class); RemoteEndpoint.Async async = mock(RemoteEndpoint.Async.class); when(session.isOpen()).thenReturn(true); when(session.getId()).thenReturn("ID1"); when(session.getAsyncRemote()).thenReturn(async); JsTopicMessageController jtmcmsgControl = mock(JsTopicMessageController.class); MessageToClient mtc = mock(MessageToClient.class); when(session.isOpen()).thenReturn(Boolean.TRUE); when(userContextFactory.getUserContext(eq("ID1"))).thenReturn(mock(UserContext.class)); doNothing().doThrow(NotRecipientException.class).when(instance).checkMessageTopic(any(UserContext.class), anyString(), eq(PAYLOAD), eq(jtmcmsgControl)); int result = instance.checkAndSendMtcToSession(session, jtmcmsgControl, mtc, PAYLOAD); assertThat(result).isEqualTo(1); result = instance.checkAndSendMtcToSession(session, jtmcmsgControl, mtc, PAYLOAD); assertThat(result).isEqualTo(0); }