TopicManagerImpl implements TopicManager { @Override public int registerTopicSession(String topic, Session session) throws IllegalAccessException { if (isInconsistenceContext(topic, session)) { return 0; } Collection<Session> sessions; if (map.containsKey(topic)) { sessions = map.get(topic); } else { sessions = Collections.synchronizedCollection(new ArrayList<Session>()); map.put(topic, sessions); } topicAccessManager.checkAccessTopic(userContextFactory.getUserContext(session.getId()), topic); logger.debug("'{}' subscribe to '{}'", session.getId(), topic); if (session.isOpen()) { sessions.add(session); } return getNumberSubscribers(topic); }  @Override Map<String, Collection<Session>> getSessionsByTopic(); @Override int registerTopicSession(String topic, Session session); @Override int unregisterTopicSession(String topic, Session session); @Override Collection<String> removeSessionsToTopic(Collection<Session> sessions); @Override Collection<String> removeSessionToTopics(Session session); @Override Collection<Session> getSessionsForTopic(String topic); @Override int getNumberSubscribers(String topic);  }
@Test public void testRegisterTopicSession() throws IllegalAccessException { System.out.println("registerTopicSession"); Session session = mock(Session.class); when(session.isOpen()).thenReturn(true); int result = instance.registerTopicSession(null, session); assertThat(result).isEqualTo(0); result = instance.registerTopicSession("", session); assertThat(result).isEqualTo(0); result = instance.registerTopicSession(TOPIC1, session); assertThat(result).isEqualTo(1); assertThat(instance.getNumberSubscribers(TOPIC1)).isEqualTo(1); session = mock(Session.class); when(session.isOpen()).thenReturn(true); result = instance.registerTopicSession(TOPIC1, session); assertThat(result).isEqualTo(2); assertThat(instance.getNumberSubscribers(TOPIC1)).isEqualTo(2); result = instance.registerTopicSession(TOPIC1, session); assertThat(result).isEqualTo(3); assertThat(instance.getNumberSubscribers(TOPIC1)).isEqualTo(3); session = mock(Session.class); when(session.isOpen()).thenReturn(false); result = instance.registerTopicSession(TOPIC1, session); assertThat(result).isEqualTo(3); assertThat(instance.getNumberSubscribers(TOPIC1)).isEqualTo(3); }