/* 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 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;

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

	@InjectMocks
	private TopicsMessagesBroadcaster messagesBroadcaster = new TopicsMessagesBroadcaster();
	
	/**
	 * Test of sendMessageToTopic method, of class TopicsMessagesBroadcaster.
	 */
	@Test
	public void testSendMessageToTopic() {
		System.out.println("sendMessageToTopic");
		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);

	}
	
}

@@ -1224,16 +1224,27 @@
 eToTopic
+For1Session
 () %7B%0D%0A%09%09
@@ -1281,16 +1281,27 @@
 eToTopic
+For1Session
 %22);%0D%0A%09%09C
@@ -2387,16 +2387,1291 @@
 );%0D%0A
-%0D%0A
 %09%7D%0D%0A%09%0D%0A
+%09/**%0D%0A%09 * Test of sendMessageToTopic method, of class TopicsMessagesBroadcaster.%0D%0A%09 */%0D%0A%09@Test%0D%0A%09public void testSendMessageToTopicFor0Session() %7B%0D%0A%09%09System.out.println(%22sendMessageToTopicFor0Session%22);%0D%0A%09%09Collection%3CSession%3E sessions = new ArrayList%3C%3E();%0D%0A%09%09sessions.add(null);%0D%0A%09%09when(sessionManager.getSessionsForTopic(anyString())).thenReturn(sessions);%0D%0A%0D%0A%09%09MessageToClient msg = new MessageToClient();%0D%0A%09%09String id = UUID.randomUUID().toString();%0D%0A%09%09msg.setId(id);%0D%0A%09%09String expResult = %22RESULT%22;%0D%0A%09%09msg.setResult(expResult);%0D%0A%09%09messagesBroadcaster.sendMessageToTopic(msg);%0D%0A%09%09assertThat(msg.getType()).isEqualTo(MessageType.MESSAGE);%0D%0A%09%7D%0D%0A%0D%0A%09/**%0D%0A%09 * Test of sendMessageToTopic method, of class TopicsMessagesBroadcaster.%0D%0A%09 */%0D%0A%09@Test%0D%0A%09public void testSendMessageToTopicForNullSession() %7B%0D%0A%09%09System.out.println(%22sendMessageToTopicForNullSession%22);%0D%0A%09%09Collection%3CSession%3E sessions = new ArrayList%3C%3E();%0D%0A%09%09when(sessionManager.getSessionsForTopic(anyString())).thenReturn(sessions);%0D%0A%0D%0A%09%09MessageToClient msg = new MessageToClient();%0D%0A%09%09String id = UUID.randomUUID().toString();%0D%0A%09%09msg.setId(id);%0D%0A%09%09String expResult = %22RESULT%22;%0D%0A%09%09msg.setResult(expResult);%0D%0A%09%09messagesBroadcaster.sendMessageToTopic(msg);%0D%0A%09%09assertThat(msg.getType()).isEqualTo(MessageType.MESSAGE);%0D%0A%09%7D%0D%0A
 %7D%0D%0A

improve test TopicsMessagesBroadcaster