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

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.ws.rs.core.HttpHeaders;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.ocelotds.Constants;
import org.slf4j.Logger;


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

	@InjectMocks
	@Spy
	private OcelotRequestConfigurator ocelotRequestConfigurator;
	
	/**
	 * Test of modifyHandshake method, of class OcelotRequestConfigurator.
	 */
	@Test
	public void testModifyHandshakeWithDefaultAccept() {
		System.out.println("testModifyHandshakeWithDefaultAccept");
		ServerEndpointConfig sec = mock(ServerEndpointConfig.class);
		HandshakeRequest request = mock(HandshakeRequest.class);
		HandshakeResponse response = mock(HandshakeResponse.class);
		Map<String, Object> userProperties = new HashMap<>();
		Map<String, List<String>> headers = new HashMap<>();
		
		when(sec.getUserProperties()).thenReturn(userProperties);
		when(request.getHeaders()).thenReturn(headers);
		
		doReturn(null).when(ocelotRequestConfigurator).getSecurityContext();
		ocelotRequestConfigurator.modifyHandshake(sec, request, response);
		Locale result = (Locale) sec.getUserProperties().get(Constants.LOCALE);
		assertThat(result).isEqualTo(new Locale("en", "US"));
	}
	
	/**
	 * Test of modifyHandshake method, of class OcelotRequestConfigurator.
	 */
	@Test
	public void testModifyHandshake() {
		System.out.println("testModifyHandshake");
		ServerEndpointConfig sec = mock(ServerEndpointConfig.class);
		Map<String, Object> userProperties = new HashMap<>();
		HandshakeRequest request = mock(HandshakeRequest.class);
		HandshakeResponse response = mock(HandshakeResponse.class);
		Map<String, List<String>> headers = new HashMap<>();
		List<String> accepts = Arrays.asList("fr", "fr-FR;q=1");
		headers.put(HttpHeaders.ACCEPT_LANGUAGE, accepts);

		when(sec.getUserProperties()).thenReturn(userProperties);
		when(request.getHeaders()).thenReturn(headers);
		doReturn(null).when(ocelotRequestConfigurator).getSecurityContext();
		
		ocelotRequestConfigurator.modifyHandshake(sec, request, response);
		Locale result = (Locale) sec.getUserProperties().get(Constants.LOCALE);
		assertThat(result).isEqualTo(new Locale("fr", "FR"));
	}
}

@@ -362,24 +362,64 @@
 .util.Map;%0D%0A
+import javax.servlet.http.HttpSession;%0D%0A
 import javax
@@ -697,16 +697,52 @@
 nWith;%0D%0A
+import org.mockito.ArgumentCaptor;%0D%0A
 import o
@@ -963,18 +963,16 @@
 ger;%0D%0A%0D%0A
-%0D%0A
 /**%0D%0A *%0D
@@ -1082,17 +1082,16 @@
 Test %7B%0D%0A
-%09
 %0D%0A%09@Mock
@@ -1180,37 +1180,19 @@
 tor 
-ocelotRequestConfigurator
+instance
 ;%0D%0A
-%09
 %0D%0A%09/
@@ -1696,26 +1696,24 @@
 shMap%3C%3E();%0D%0A
-%09%09
 %0D%0A%09%09when(sec
@@ -1810,26 +1810,24 @@
 (headers);%0D%0A
-%09%09
 %0D%0A%09%09doReturn
@@ -1842,33 +1842,16 @@
 hen(
-ocelotRequestConfigurator
+instance
 ).ge
@@ -1877,33 +1877,16 @@
 %0D%0A%09%09
-ocelotRequestConfigurator
+instance
 .mod
@@ -2060,17 +2060,16 @@
 );%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09/**%0D%0A
@@ -2793,33 +2793,16 @@
 hen(
-ocelotRequestConfigurator
+instance
 ).ge
@@ -2826,39 +2826,20 @@
 );%0D%0A
-%09%09
 %0D%0A%09%09
-ocelotRequestConfigurator
+instance
 .mod
@@ -3012,12 +3012,810 @@
 ));%0D%0A%09%7D%0D
+%0A%0D%0A%09@Test%0D%0A%09public void testGetSessionBeansMap() %7B%0D%0A%09%09HttpSession session = mock(HttpSession.class);%0D%0A%09%09Object result = instance.getSessionBeansMap(null);%0D%0A%09%09assertThat(result).isInstanceOf(Map.class);%0D%0A%09%09result = instance.getSessionBeansMap(%22%22);%0D%0A%09%09assertThat(result).isInstanceOf(Map.class);%0D%0A%09%09when(session.getAttribute(Constants.SESSION_BEANS)).thenReturn(result).thenReturn(null);%0D%0A%09%09result = instance.getSessionBeansMap(session);%0D%0A%09%09assertThat(result).isInstanceOf(Map.class);%0D%0A%09%09result = instance.getSessionBeansMap(session);%0D%0A%0D%0A%09%09ArgumentCaptor%3CMap%3E captureMap = ArgumentCaptor.forClass(Map.class);%0D%0A%09%09verify(session).setAttribute(eq(Constants.SESSION_BEANS), captureMap.capture());%0D%0A%09%09assertThat(result).isInstanceOf(Map.class);%0D%0A%09%09assertThat(result).isEqualTo(captureMap.getValue());%0D%0A%09%7D%0D
 %0A%7D%0D%0A

improve test coverage