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

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import javax.websocket.Session;
import org.junit.Test;
import static org.mockito.Mockito.*;
import static org.assertj.core.api.Assertions.*;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

/**
 *
 * @author hhfrancois
 */
@RunWith(MockitoJUnitRunner.class)
public class SessionManagerImplTest {

	@InjectMocks
	@Spy
	SessionManagerImpl instance;

	/**
	 * Test of getMap method, of class SessionManager.
	 */
	@Test
	public void testGetMap() {
		System.out.println("getMap");
		Map<String, Session> result = instance.getMap();
		assertThat(result).isNotNull();
	}

	/**
	 * Test of addSession method, of class SessionManager.
	 */
	@Test
	public void testAddSession() {
		System.out.println("addSession");
		instance.getMap().clear();
		String id = "ID0";
		Session session = mock(Session.class);
		instance.addSession(id, session);
		assertThat(instance.getMap()).isNotEmpty();
		instance.getMap().clear();
	}
	
	/**
	 * Test of closeOldSessionForHttp method, of class.
	 */
	@Test
	public void closeOldSessionForHttpTest() {
		System.out.println("closeOldSessionForHttp");
		instance.getMap().clear();
		Session session = mock(Session.class);
		String id = "ID0";
		instance.getMap().put(id, session);
		Session closed = instance.closeOldSessionForHttp(id);
		try {
			verify(session).close();
		} catch (IOException ex) {
		}
		assertThat(closed).isNotNull();
	}

	/**
	 * Test of removeSession method, of class SessionManager.
	 */
	@Test
	public void testRemoveSession_Session() {
		System.out.println("removeSession");
		instance.getMap().clear();
		Session session = mock(Session.class);
		instance.getMap().put("ID0", session);
		instance.getMap().put("ID1", session);

		Collection<String> removeds = instance.removeSession(session);

		verify(instance, times(2)).removeSession(any(String.class));
		
		assertThat(removeds).contains("ID0", "ID1");
	}

	/**
	 * Test of removeSession method, of class SessionManager.
	 */
	@Test
	public void testRemoveSession_String() {
		System.out.println("removeSession");
		instance.getMap().clear();
		String id = "ID0";
		Session session = mock(Session.class);
		instance.addSession(id, session);
		assertThat(instance.getMap()).isNotEmpty();
		instance.removeSession(id);
		assertThat(instance.getMap()).isEmpty();
	}

	/**
	 * Test of getSessionById method, of class SessionManager.
	 */
	@Test
	public void testGetSessionById() {
		System.out.println("getSessionById");
		instance.getMap().clear();
		String id = "ID0";
		Session session = mock(Session.class);
		instance.addSession(id, session);
		assertThat(instance.getMap()).isNotEmpty();

		Session result = instance.getSessionById(id);
		assertThat(result).isEqualTo(session);
	}

}
@@ -251,24 +251,56 @@
 OException;%0A
+import java.security.Principal;%0A
 import java.
@@ -312,24 +312,24 @@
 Collection;%0A
-
 import java.
@@ -614,16 +614,47 @@
 tRunner;
+%0Aimport org.ocelotds.Constants;
 %0A%0A/**%0A *
@@ -1817,32 +1817,521 @@
 sNotNull();%0A%09%7D%0A%0A
+%09/**%0A%09 * Test of closeOldSessionForHttp method, of class.%0A%09 */%0A%09@Test%0A%09public void closeOldSessionForHttpTestFailed() throws IOException %7B%0A%09%09System.out.println(%22closeOldSessionForHttp%22);%0A%09%09instance.getMap().clear();%0A%09%09Session session = mock(Session.class);%0A%09%09doThrow(IOException.class).when(session).close();%0A%09%09String id = %22ID0%22;%0A%09%09instance.getMap().put(id, session);%0A%09%09Session closed = instance.closeOldSessionForHttp(id);%0A%09%09verify(session).close();%0A%09%09assertThat(closed).isNotNull();%0A%09%7D%0A%0A
 %09/**%0A%09 * Test of
@@ -3586,24 +3586,24 @@
 onById(id);%0A
-
 %09%09assertThat
@@ -3633,11 +3633,620 @@
 ion);%0A%09%7D
+%0A%09%0A%09/**%0A%09 * Test of getUsername method, of class.%0A%09 */%0A%09@Test%0A%09public void getUsernameTest() %7B%0A%09%09System.out.println(%22getUsername%22);%0A%09%09Session session = mock(Session.class);%0A%09%09Principal p = mock(Principal.class);%0A%09%09when(session.getUserPrincipal()).thenReturn(null).thenReturn(p);%0A%09%09when(p.getName()).thenReturn(%22USERNAME%22);%0A%09%09%0A%09%09String result = instance.getUsername(null);%0A%09%09assertThat(result).isEqualTo(Constants.ANONYMOUS);%0A%09%09result = instance.getUsername(session);%0A%09%09assertThat(result).isEqualTo(Constants.ANONYMOUS);%0A%09%09result = instance.getUsername(session);%0A%09%09assertThat(result).isEqualTo(%22USERNAME%22);%0A%0A%09%7D
 %0A%0A%7D

test getUsername method and improve test closeOldSessionForHttp