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

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.junit.Test;
import static org.mockito.Mockito.*;
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.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.ocelotds.Constants;
import org.ocelotds.context.ThreadLocalContextHolder;
import org.ocelotds.core.mtc.RSMessageToClientManager;
import org.ocelotds.messaging.MessageFromClient;
import org.ocelotds.messaging.MessageToClient;
import org.slf4j.Logger;

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

	@InjectMocks
	@Spy
	private RSEndpoint instance;
	
	@Mock
	private HttpServletRequest request;

	@Mock
	private RSMessageToClientManager messageToClientService;
	
	@Mock
	private RequestManager requestManager;
	
	@Mock
	private Logger logger;

	/**
	 * Test of getMessageToClient method, of class RSEndpoint.
	 */
	@Test
	public void testGetMessageToClient() {
		System.out.println("getMessageToClient");
		MessageToClient mtc = mock(MessageToClient.class);
		HttpSession session = mock(HttpSession.class);
		when(request.getSession()).thenReturn(session);
		doNothing().when(instance).setContext(any(HttpSession.class), anyBoolean());
		List<String> args = new ArrayList<>();
		args.add("\"arg\"");
		List<String> argNames = new ArrayList<>();
		argNames.add("\"argName\"");
		MessageFromClient mfc = new MessageFromClient();
		mfc.setDataService("DataServiceClassName");
		mfc.setId(UUID.randomUUID().toString());
		mfc.setOperation("methodName");
		mfc.setParameterNames(argNames);
		mfc.setParameters(args);
		
		when(messageToClientService.createMessageToClient(any(MessageFromClient.class), any(HttpSession.class))).thenReturn(mtc);
		when(mtc.toJson()).thenReturn("RESULT");
		String result = instance.getMessageToClient(mfc.toJson(), true);
		assertThat(result).isEqualTo("RESULT");
		ArgumentCaptor<MessageFromClient> argument = ArgumentCaptor.forClass(MessageFromClient.class);
		verify(messageToClientService).createMessageToClient(argument.capture(), any(HttpSession.class));
		assertThat(argument.getValue()).isEqualTo(mfc);
	}

	/**
	 * Test of getMessageToClient method, of class RSEndpoint.
	 */
	@Test
	public void testSetContext() {
		System.out.println("setContext");
		HttpSession session = mock(HttpSession.class);
		instance.setContext(session, true);
		instance.setContext(session, false);
		ArgumentCaptor<Boolean> argument = ArgumentCaptor.forClass(Boolean.class);
		verify(session, times(2)).setAttribute(eq(Constants.Options.MONITOR), argument.capture());
		assertThat(ThreadLocalContextHolder.get(Constants.HTTPSESSION)).isEqualTo(session);
		assertThat(ThreadLocalContextHolder.get(Constants.HTTPREQUEST)).isEqualTo(request);
		List<Boolean> monitors = argument.getAllValues();
		assertThat(monitors.get(0)).isEqualTo(true);
		assertThat(monitors.get(1)).isEqualTo(false);
	}
}

@@ -229,24 +229,49 @@
 .ArrayList;%0A
+import java.util.Arrays;%0A
 import java.
@@ -1700,218 +1700,198 @@
 ;%0A%09%09
-List%3C
 String
-%3E args = new ArrayList%3C%3E();%0A%09%09args.add(%22%5C%22arg%5C%22%22);%0A%09%09List%3CString%3E argNames = new ArrayList%3C%3E();%0A%09%09argNames.add(%22%5C%22argName%5C%22%22);%0A%09%09MessageFromClient mfc = new MessageFromClient();%0A%09%09mfc.setDataService(
+ json = String.format(%22%7B%5C%22%25s%5C%22:%5C%22%25s%5C%22,%5C%22%25s%5C%22:%5C%22%25s%5C%22,%5C%22%25s%5C%22:%5C%22%25s%5C%22,%5C%22%25s%5C%22:%25s,%5C%22%25s%5C%22:%25s%7D%22,%0A%09%09%09%09  Constants.Message.ID, UUID.randomUUID().toString(),%0A%09%09%09%09  Constants.Message.DATASERVICE, 
 %22Dat
@@ -1912,73 +1912,45 @@
 ame%22
-);%0A%09%09mfc.setId(UUID.randomUUID().toString());%0A%09%09mfc.setOperation(
+,%0A%09%09%09%09  Constants.Message.OPERATION, 
 %22met
@@ -1961,70 +1961,112 @@
 ame%22
-);%0A%09%09mfc.setParameterNames(argNames);%0A%09%09mfc.setParameters(args
+,%0A%09%09%09%09  Constants.Message.ARGUMENTNAMES, %22%5B%5C%22argName%5C%22%5D%22,%0A%09%09%09%09  Constants.Message.ARGUMENTS, %22%5B%5C%22arg%5C%22%5D%22
 );%0A%09
@@ -2284,20 +2284,12 @@
 ent(
-mfc.toJ
+j
 son
-()
 , tr
@@ -2341,250 +2341,113 @@
 ;%0A%09%09
-ArgumentCaptor%3CMessageFromClient%3E argument = ArgumentCaptor.forClass(MessageFromClient.class);%0A%09%09verify(messageToClientService).createMessageToClient(argument.capture(), any(HttpSession.class));%0A%09%09assertThat(argument.getValue()).isEqualTo(mfc
+verify(messageToClientService).createMessageToClient(any(MessageFromClient.class), any(HttpSession.class)
 );%0A%09

fix problem with paramname