/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.ocelotds.web;

import ch.qos.logback.classic.Level;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.ocelotds.core.SessionManager;
import org.ocelotds.messaging.MessageToClient;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
import org.ocelotds.messaging.MessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author hhfrancois
 */
@RunWith(MockitoJUnitRunner.class)
public class TopicsMessagesBroadcasterTest {
	
	@Mock
	private Logger logger;

	@Mock
	private SessionManager sessionManager;

	@InjectMocks
	private TopicsMessagesBroadcaster messagesBroadcaster;
	
	/**
	 * Test of sendMessageToTopic method, of class TopicsMessagesBroadcaster.
	 */
	@Test
	public void testSendMessageToTopicFor1Session() {
		ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(TopicsMessagesBroadcaster.class);
		Level old = logger.getLevel();
		logger.setLevel(Level.DEBUG);
		System.out.println("sendMessageToTopicFor1Session");
		Collection<Session> sessions = new ArrayList<>();
		Session session1 = mock(Session.class);
		RemoteEndpoint.Async async = mock(RemoteEndpoint.Async.class);
		when(session1.isOpen()).thenReturn(true);
		when(session1.getAsyncRemote()).thenReturn(async);
		sessions.add(session1);
		Session session2 = mock(Session.class);
		sessions.add(session2);
		when(sessionManager.getSessionsForTopic(anyString())).thenReturn(sessions);

		MessageToClient msg = new MessageToClient();
		String id = UUID.randomUUID().toString();
		msg.setId(id);
		String expResult = "RESULT";
		msg.setResult(expResult);
		messagesBroadcaster.sendMessageToTopic(msg);

		ArgumentCaptor<MessageToClient> captureMsg = ArgumentCaptor.forClass(MessageToClient.class);
		verify(async).sendObject(captureMsg.capture());
		assertThat(captureMsg.getValue().getResponse()).isEqualTo(expResult);
		assertThat(captureMsg.getValue().getId()).isEqualTo(id);
		assertThat(captureMsg.getValue().getType()).isEqualTo(MessageType.MESSAGE);
		assertThat(captureMsg.getValue().getDeadline()).isEqualTo(0L);
		logger.setLevel(old);
	}
	
	/**
	 * Test of sendMessageToTopic method, of class TopicsMessagesBroadcaster.
	 */
	@Test
	public void testSendMessageToTopicFor0Session() {
		System.out.println("sendMessageToTopicFor0Session");
		Collection<Session> sessions = new ArrayList<>();
		sessions.add(null);
		when(sessionManager.getSessionsForTopic(anyString())).thenReturn(sessions);

		MessageToClient msg = new MessageToClient();
		String id = UUID.randomUUID().toString();
		msg.setId(id);
		String expResult = "RESULT";
		msg.setResult(expResult);
		messagesBroadcaster.sendMessageToTopic(msg);
		assertThat(msg.getType()).isEqualTo(MessageType.MESSAGE);
	}

	/**
	 * Test of sendMessageToTopic method, of class TopicsMessagesBroadcaster.
	 */
	@Test
	public void testSendMessageToTopicForNullSession() {
		System.out.println("sendMessageToTopicForNullSession");
		Collection<Session> sessions = new ArrayList<>();
		when(sessionManager.getSessionsForTopic(anyString())).thenReturn(sessions);

		MessageToClient msg = new MessageToClient();
		String id = UUID.randomUUID().toString();
		msg.setId(id);
		String expResult = "RESULT";
		msg.setResult(expResult);
		messagesBroadcaster.sendMessageToTopic(msg);
		assertThat(msg.getType()).isEqualTo(MessageType.MESSAGE);
	}
}

@@ -234,46 +234,8 @@
 %0D%0A%0D%0A
-import ch.qos.logback.classic.Level;%0D%0A
 impo
@@ -255,24 +255,24 @@
 ArrayList;%0D%0A
+
 import java.
@@ -838,41 +838,8 @@
 er;%0D
-%0Aimport org.slf4j.LoggerFactory;%0D
 %0A%0D%0A/
@@ -1274,201 +1274,61 @@
 %0D%0A%09%09
-ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(TopicsMessagesBroadcaster.class);%0D%0A%09%09Level old = logger.getLevel();%0D%0A%09%09logger.setLevel(Level.DEBUG
+when(logger.isDebugEnabled()).thenReturn(Boolean.TRUE
 );%0D%0A
@@ -2399,24 +2399,24 @@
 .MESSAGE);%0D%0A
+
 %09%09assertThat
@@ -2473,33 +2473,8 @@
 );%0D%0A
-%09%09logger.setLevel(old);%0D%0A
 %09%7D%0D%0A

improve test