package org.sagebionetworks.web.unitserver;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import static org.mockito.Mockito.*;

import javax.servlet.http.HttpServletRequest;

import org.mockito.runners.MockitoJUnitRunner;
import org.sagebionetworks.StackConfiguration;
import org.sagebionetworks.client.SynapseClient;
import org.sagebionetworks.web.server.servlet.ServiceUrlProvider;
import org.sagebionetworks.web.server.servlet.SynapseClientBase;
import org.sagebionetworks.web.server.servlet.SynapseProvider;
import org.mockito.internal.util.reflection.Whitebox;
import org.junit.runner.RunWith;

@RunWith (MockitoJUnitRunner.class)
public class SynapseClientBaseTest {
	
	@Mock
	SynapseProvider mockSynapseProvider;
	
	@Mock
	ServiceUrlProvider mockServiceUrlProvider;
	
	@Mock
	SynapseClient mockSynapseClient;
	
	SynapseClientBase synapseClientBase;
	
	String publicAuthBaseUrl;
	String repositoryServiceUrl;
	
	@Mock
	ThreadLocal<HttpServletRequest> mockThreadLocal;
	
	@Mock 
	HttpServletRequest mockRequest;
	
	String userIp = "127.0.0.1";
	
	@Before
	public void setUp() throws Exception {
		repositoryServiceUrl = "asdf.com";
		publicAuthBaseUrl = "qwerty.com";
		synapseClientBase = new SynapseClientBase();
		synapseClientBase.setServiceUrlProvider(mockServiceUrlProvider);
		when(mockSynapseProvider.createNewClient()).thenReturn(mockSynapseClient);
		when(mockServiceUrlProvider.getRepositoryServiceUrl()).thenReturn(repositoryServiceUrl);
		when(mockServiceUrlProvider.getPublicAuthBaseUrl()).thenReturn(publicAuthBaseUrl);
		Whitebox.setInternalState(synapseClientBase, "synapseProvider", mockSynapseProvider);
		Whitebox.setInternalState(synapseClientBase, "perThreadRequest", mockThreadLocal);
		userIp = "127.0.0.1";
		when(mockThreadLocal.get()).thenReturn(mockRequest);
		when(mockRequest.getRemoteAddr()).thenReturn(userIp);
	}

	@Test
	public void testCreateSynapseClient() {
		String sessionToken = "fakeSessionToken";
		SynapseClient createdClient = synapseClientBase.createSynapseClient("fakeSessionToken");
		assertEquals(mockSynapseClient, createdClient);
		verify(mockSynapseClient).setSessionToken(sessionToken);
		verify(mockSynapseClient).setRepositoryEndpoint(repositoryServiceUrl);
		verify(mockSynapseClient).setAuthEndpoint(publicAuthBaseUrl);
		verify(mockSynapseClient).setFileEndpoint(StackConfiguration
				.getFileServiceEndpoint());
		verify(mockSynapseClient).appendUserAgent(SynapseClientBase.PORTAL_USER_AGENT);
		verify(mockSynapseClient).setUserIpAddress(userIp);
		
	}

}

@@ -2070,22 +2070,17 @@
 eClient(
-%22fakeS
+s
 essionTo
@@ -2082,17 +2082,16 @@
 ionToken
-%22
 );%0A%09%09ass
@@ -2567,12 +2567,308 @@
 );%0A%09%09%0A%09%7D
+%0A%09%0A%09@Test%0A%09public void testNullThreadLocalRequest() %7B%0A%09%09when(mockThreadLocal.get()).thenReturn(null);%0A%09%09String sessionToken = %22fakeSessionToken%22;%0A%09%09SynapseClient createdClient = synapseClientBase.createSynapseClient(sessionToken);%0A%09%09verify(mockSynapseClient, never()).setUserIpAddress(userIp);%0A%09%7D
 %0A%0A%7D%0A

test for null request