/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ocelotds.topic;

import java.security.Principal;
import java.util.Map;
import javax.websocket.Session;
import javax.websocket.server.HandshakeRequest;
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;
import org.ocelotds.security.UserContext;

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

	@InjectMocks
	@Spy
	UserContextFactory instance;

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

	/**
	 * Test of addSession method, of class UserContextFactory.
	 */
	@Test
	public void test_createUserContext() {
		System.out.println("createUserContext");
		HandshakeRequest request = mock(HandshakeRequest.class);
		instance.createUserContext(request, "ID");
		assertThat(instance.getMap()).containsKeys("ID");
		instance.getMap().remove("ID");
	}

	@Test
	public void test_createUserContextRequestNull() {
		System.out.println("createUserContext");
		Session session = mock(Session.class);
		when(session.getId()).thenReturn("ID");
		instance.createUserContext(null, "ID");
		assertThat(instance.getMap()).isEmpty();
		verify(instance).destroyUserContext(anyString());
	}

	@Test
	public void test_createUserContextNull() {
		System.out.println("createUserContext");
		HandshakeRequest request = mock(HandshakeRequest.class);
		instance.createUserContext(request, null);
		assertThat(instance.getMap()).isEmpty();
		verify(instance, never()).destroyUserContext(anyString());
	}

	@Test
	public void test_createUserContextNullRequestNull() {
		System.out.println("createUserContext");
		instance.createUserContext(null, null);
		assertThat(instance.getMap()).isEmpty();
		verify(instance).destroyUserContext(anyString());
	}

	/**
	 * Test of removeSession method, of class UserContextFactory.
	 */
	@Test
	public void test_destroyUserContext() {
		System.out.println("removeSession");
		instance.getMap().clear();
		instance.getMap().put("ID1", mock(UserContext.class));
		instance.getMap().put("ID2", mock(UserContext.class));
		instance.getMap().put("ID3", mock(UserContext.class));
		instance.destroyUserContext("ID2");
		assertThat(instance.getMap()).hasSize(2);
		instance.destroyUserContext("ID4");
		assertThat(instance.getMap()).hasSize(2);
	}

	/**
	 * Test of getUserContext method, of class UserContextFactory.
	 */
	@Test
	public void testGetUserContext() {
		System.out.println("getUserContext");
		UserContext uc = mock(UserContext.class);
		instance.getMap().put("ID1", uc);
		Principal principal = mock(Principal.class);
		when(uc.getPrincipal()).thenReturn(principal);
		when(principal.getName()).thenReturn("NAME");
		UserContext result = instance.getUserContext("ID1");
		assertThat(result).isNotNull();
		assertThat(result.getPrincipal()).isNotNull();
		assertThat(result.getPrincipal().getName()).isEqualTo("NAME");
	}

}
@@ -212,40 +212,8 @@
 c;%0A%0A
-import java.security.Principal;%0A
 impo
@@ -2984,285 +2984,540 @@
 ;%0A%09%09
-Principal principal = mock(Principal.class);%0A%09%09when(uc.getPrincipal()).thenReturn(principal);%0A%09%09when(principal.getName()).thenReturn(%22NAME%22);%0A%09%09UserContext result = instance.getUserContext(%22ID1%22);%0A%09%09assertThat(result).isNotNull();%0A%09%09assertThat(result.getPrincipal()).isNotN
+doReturn(true).when(instance).containsKey(eq(%22ID1%22));%0A%09%09doReturn(false).when(instance).containsKey(eq(%22ID2%22));%0A%09%09UserContext result = instance.getUserContext(%22ID1%22);%0A%09%09assertThat(result).isNotNull();%0A%09%09result = instance.getUserContext(%22ID2%22);%0A%09%09assertThat(result).isNull();%0A%09%7D%0A%0A%09/**%0A%09 * Test of containsKey method, of class.%0A%09 */%0A%09@Test%0A%09public void test_containsKey() %7B%0A%09%09System.out.println(%22containsKey%22);%0A%09%09UserContext uc = mock(UserContext.class);%0A%09%09instance.getMap().put(%22ID1%22, uc);%0A%09%09boolean result = instance.containsKey(n
 ull
-(
 );%0A%09
@@ -3538,55 +3538,92 @@
 sult
-.getPrincipal().getName()).isEqualTo(%22NAME%22
+).isFalse();%0A%09%09result = instance.containsKey(%22ID1%22);%0A%09%09assertThat(result).isTrue(
 );%0A%09%7D%0A
-%0A
 %7D

improve test