<commit_before>package org.sagebionetworks.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sagebionetworks.client.BaseClientImpl.MAX_RETRY_SERVICE_UNAVAILABLE_COUNT;
import static org.sagebionetworks.client.Method.GET;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.sagebionetworks.client.exceptions.SynapseClientException;
import org.sagebionetworks.client.exceptions.SynapseForbiddenException;
import org.sagebionetworks.client.exceptions.SynapseServerException;
import org.sagebionetworks.reflection.model.PaginatedResults;
import org.sagebionetworks.repo.model.AuthorizationConstants;
import org.sagebionetworks.repo.model.EntityId;
import org.sagebionetworks.repo.model.auth.LoginRequest;
import org.sagebionetworks.repo.model.auth.LoginResponse;
import org.sagebionetworks.schema.adapter.org.json.EntityFactory;
import org.sagebionetworks.simpleHttpClient.Header;
import org.sagebionetworks.simpleHttpClient.SimpleHttpClient;
import org.sagebionetworks.simpleHttpClient.SimpleHttpRequest;
import org.sagebionetworks.simpleHttpClient.SimpleHttpResponse;

public class BaseClientImplTest {
	private static final String USER_AGENT = "JavaClient";
	@Mock
	SimpleHttpClient mockClient;
	@Mock
	SimpleHttpResponse mockResponse;
	@Mock
	SimpleHttpResponse mockResponse2;
	@Mock
	File mockFile;
	@Mock
	Header mockHeader;

	private static final String CONTENT_TYPE = "Content-Type";
	private static final String CONTENT_TYPE_APPLICATION_JSON = "application/json; charset=utf-8";
	
	BaseClientImpl baseClient;
	private final String sessionIdVal = "mySessionIdValue";


	@Before
	public void before(){
		MockitoAnnotations.initMocks(this);
		baseClient = new BaseClientImpl(USER_AGENT);
		baseClient.setSimpleHttpClient(mockClient);
		
		when(mockHeader.getValue()).thenReturn(CONTENT_TYPE_APPLICATION_JSON);
		when(mockResponse.getFirstHeader(CONTENT_TYPE)).thenReturn(mockHeader);

	}

	@Test (expected = IllegalArgumentException.class)
	public void testAppendAgentWithNull() {
		baseClient.appendUserAgent(null);
	}

	@Test
	public void testAppendAgentAlreadyExist() {
		assertEquals(USER_AGENT, baseClient.getUserAgent());
		baseClient.appendUserAgent(USER_AGENT);
		assertEquals(USER_AGENT, baseClient.getUserAgent());
	}

	@Test
	public void testAppendAgent() {
		assertEquals(USER_AGENT, baseClient.getUserAgent());
		baseClient.appendUserAgent("v1");
		assertEquals(USER_AGENT+" v1", baseClient.getUserAgent());
	}

	@Test (expected = IllegalArgumentException.class)
	public void testLoginWithNullRequest() throws Exception{
		baseClient.login(null);
	}

	@Test (expected = IllegalArgumentException.class)
	public void testLoginWithNullUsername() throws Exception{
		LoginRequest request = new LoginRequest();
		request.setPassword("password");
		baseClient.login(request);
	}

	@Test (expected = IllegalArgumentException.class)
	public void testLoginWithNullPassword() throws Exception{
		LoginRequest request = new LoginRequest();
		request.setUsername("username");
		baseClient.login(request);
	}

	@Test
	public void testLogin() throws Exception{
		LoginRequest request = new LoginRequest();
		request.setUsername("username");
		request.setPassword("password");
		when(mockClient.post(any(SimpleHttpRequest.class),anyString()))
				.thenReturn(mockResponse);
		when(mockResponse.getStatusCode()).thenReturn(200);
		when(mockResponse.getContent()).thenReturn("{"
				+ "\"sessionToken\":\"token\","
				+ "\"authenticationReceipt\":\"receipt\","
				+ "\"acceptsTermsOfUse\":\"true\","
				+ "}");
		LoginResponse loginResponse = new LoginResponse();
		loginResponse.setAcceptsTermsOfUse(true);
		loginResponse.setAuthenticationReceipt("receipt");
		loginResponse.setSessionToken("token");
		assertEquals(loginResponse , baseClient.login(request));
		ArgumentCaptor<SimpleHttpRequest> captor = ArgumentCaptor.forClass(SimpleHttpRequest.class);
		verify(mockClient).post(captor.capture(),
				eq(EntityFactory.createJSONObjectForEntity(request).toString()));
		assertEquals("https://repo-prod.prod.sagebase.org/auth/v1/login",
				captor.getValue().getUri());
	}

	@Test
	public void testLoginForAccessToken() throws Exception{
		LoginRequest request = new LoginRequest();
		request.setUsername("username");
		request.setPassword("password");
		when(mockClient.post(any(SimpleHttpRequest.class),anyString()))
				.thenReturn(mockResponse);
		when(mockResponse.getStatusCode()).thenReturn(200);
		when(mockResponse.getContent()).thenReturn("{"
				+ "\"accessToken\":\"token\","
				+ "\"authenticationReceipt\":\"receipt\","
				+ "\"acceptsTermsOfUse\":\"true\","
				+ "}");
		LoginResponse loginResponse = new LoginResponse();
		loginResponse.setAcceptsTermsOfUse(true);
		loginResponse.setAuthenticationReceipt("receipt");
		loginResponse.setAccessToken("token");
		assertEquals(loginResponse , baseClient.loginForAccessToken(request));
		ArgumentCaptor<SimpleHttpRequest> captor = ArgumentCaptor.forClass(SimpleHttpRequest.class);
		verify(mockClient).post(captor.capture(),
				eq(EntityFactory.createJSONObjectForEntity(request).toString()));
		assertEquals("https://repo-prod.prod.sagebase.org/auth/v1/login2",
				captor.getValue().getUri());
		assertEquals("token", baseClient.getAccessToken());
	}

	@Test
	public void testLogout() throws Exception {
		when(mockClient.delete(any(SimpleHttpRequest.class))).thenReturn(mockResponse);
		when(mockResponse.getStatusCode()).thenReturn(200);
		baseClient.logout();
		ArgumentCaptor<SimpleHttpRequest> captor = ArgumentCaptor.forClass(SimpleHttpRequest.class);
		verify(mockClient).delete(captor.capture());
		assertEquals("https://repo-prod.prod.sagebase.org/auth/v1/session",
				captor.getValue().getUri());
	}
	
	@Test
	public void testLogoutForAccessToken() throws Exception {
		baseClient.logoutForAccessToken();

		assertNull(baseClient.getAuthorizationHeader());<commit_msg>PLFM-6678<commit_after>package org.sagebionetworks.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sagebionetworks.client.BaseClientImpl.MAX_RETRY_SERVICE_UNAVAILABLE_COUNT;
import static org.sagebionetworks.client.Method.GET;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.sagebionetworks.client.exceptions.SynapseClientException;
import org.sagebionetworks.client.exceptions.SynapseForbiddenException;
import org.sagebionetworks.client.exceptions.SynapseServerException;
import org.sagebionetworks.reflection.model.PaginatedResults;
import org.sagebionetworks.repo.model.AuthorizationConstants;
import org.sagebionetworks.repo.model.EntityId;
import org.sagebionetworks.repo.model.auth.LoginRequest;
import org.sagebionetworks.repo.model.auth.LoginResponse;
import org.sagebionetworks.schema.adapter.org.json.EntityFactory;
import org.sagebionetworks.simpleHttpClient.Header;
import org.sagebionetworks.simpleHttpClient.SimpleHttpClient;
import org.sagebionetworks.simpleHttpClient.SimpleHttpRequest;
import org.sagebionetworks.simpleHttpClient.SimpleHttpResponse;

public class BaseClientImplTest {
	private static final String USER_AGENT = "JavaClient";
	@Mock
	SimpleHttpClient mockClient;
	@Mock
	SimpleHttpResponse mockResponse;
	@Mock
	SimpleHttpResponse mockResponse2;
	@Mock
	File mockFile;
	@Mock
	Header mockHeader;

	private static final String CONTENT_TYPE = "Content-Type";
	private static final String CONTENT_TYPE_APPLICATION_JSON = "application/json; charset=utf-8";
	
	BaseClientImpl baseClient;
	private final String sessionIdVal = "mySessionIdValue";


	@Before
	public void before(){
		MockitoAnnotations.initMocks(this);
		baseClient = new BaseClientImpl(USER_AGENT);
		baseClient.setSimpleHttpClient(mockClient);
		
		when(mockHeader.getValue()).thenReturn(CONTENT_TYPE_APPLICATION_JSON);
		when(mockResponse.getFirstHeader(CONTENT_TYPE)).thenReturn(mockHeader);

	}

	@Test (expected = IllegalArgumentException.class)
	public void testAppendAgentWithNull() {
		baseClient.appendUserAgent(null);
	}

	@Test
	public void testAppendAgentAlreadyExist() {
		assertEquals(USER_AGENT, baseClient.getUserAgent());
		baseClient.appendUserAgent(USER_AGENT);
		assertEquals(USER_AGENT, baseClient.getUserAgent());
	}

	@Test
	public void testAppendAgent() {
		assertEquals(USER_AGENT, baseClient.getUserAgent());
		baseClient.appendUserAgent("v1");
		assertEquals(USER_AGENT+" v1", baseClient.getUserAgent());
	}

	@Test (expected = IllegalArgumentException.class)
	public void testLoginWithNullRequest() throws Exception{
		baseClient.login(null);
	}

	@Test (expected = IllegalArgumentException.class)
	public void testLoginWithNullUsername() throws Exception{
		LoginRequest request = new LoginRequest();
		request.setPassword("password");
		baseClient.login(request);
	}

	@Test (expected = IllegalArgumentException.class)
	public void testLoginWithNullPassword() throws Exception{
		LoginRequest request = new LoginRequest();
		request.setUsername("username");
		baseClient.login(request);
	}

	@Test
	public void testLogin() throws Exception{
		LoginRequest request = new LoginRequest();
		request.setUsername("username");
		request.setPassword("password");
		when(mockClient.post(any(SimpleHttpRequest.class),anyString()))
				.thenReturn(mockResponse);
		when(mockResponse.getStatusCode()).thenReturn(200);
		when(mockResponse.getContent()).thenReturn("{"
				+ "\"sessionToken\":\"token\","
				+ "\"authenticationReceipt\":\"receipt\","
				+ "\"acceptsTermsOfUse\":\"true\","
				+ "}");
		LoginResponse loginResponse = new LoginResponse();
		loginResponse.setAcceptsTermsOfUse(true);
		loginResponse.setAuthenticationReceipt("receipt");
		loginResponse.setSessionToken("token");
		assertEquals(loginResponse , baseClient.login(request));
		ArgumentCaptor<SimpleHttpRequest> captor = ArgumentCaptor.forClass(SimpleHttpRequest.class);
		verify(mockClient).post(captor.capture(),
				eq(EntityFactory.createJSONObjectForEntity(request).toString()));
		assertEquals("https://repo-prod.prod.sagebase.org/auth/v1/login",
				captor.getValue().getUri());
	}

	@Test
	public void testLoginForAccessToken() throws Exception{
		LoginRequest request = new LoginRequest();
		request.setUsername("username");
		request.setPassword("password");
		when(mockClient.post(any(SimpleHttpRequest.class),anyString()))
				.thenReturn(mockResponse);
		when(mockResponse.getStatusCode()).thenReturn(200);
		when(mockResponse.getContent()).thenReturn("{"
				+ "\"accessToken\":\"token\","
				+ "\"authenticationReceipt\":\"receipt\","
				+ "\"acceptsTermsOfUse\":\"true\","
				+ "}");
		LoginResponse loginResponse = new LoginResponse();
		loginResponse.setAcceptsTermsOfUse(true);
		loginResponse.setAuthenticationReceipt("receipt");
		loginResponse.setAccessToken("token");
		assertEquals(loginResponse , baseClient.loginForAccessToken(request));
		ArgumentCaptor<SimpleHttpRequest> captor = ArgumentCaptor.forClass(SimpleHttpRequest.class);
		verify(mockClient).post(captor.capture(),
				eq(EntityFactory.createJSONObjectForEntity(request).toString()));
		assertEquals("https://repo-prod.prod.sagebase.org/auth/v1/login2",
				captor.getValue().getUri());
		assertEquals("token", baseClient.getAccessToken());
	}

	@Test
	public void testLogout() throws Exception {
		baseClient.setSessionToken("some token");
		when(mockClient.delete(any(SimpleHttpRequest.class))).thenReturn(mockResponse);
		when(mockResponse.getStatusCode()).thenReturn(200);
		baseClient.logout();
		ArgumentCaptor<SimpleHttpRequest> captor = ArgumentCaptor.forClass(SimpleHttpRequest.class);
		verify(mockClient).delete(captor.capture());
		assertEquals("https://repo-prod.prod.sagebase.org/auth/v1/session",
				captor.getValue().getUri());
	}
	
	@Test
	public void testLogoutForAccessToken() throws Exception {
		baseClient.logoutForAccessToken();

		assertNull(baseClient.getAuthorizationHeader());
{'fraction_non_alphanumeric': 0.11984326482330326, 'fraction_numerical': 0.002809404110601804, 'mean_word_length': 6.814558058925477, 'pattern_counts': {'":': 12, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6004320', 'n_tokens_mistral': 4320, 'n_tokens_neox': 4030, 'n_words': 623}