<commit_before>package net.gini.android.authorization;


import android.test.InstrumentationTestCase;

import org.json.JSONException;

import org.json.JSONObject;
import org.mockito.Mockito;

import bolts.Task;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;


public class UserCenterManagerTest extends InstrumentationTestCase {
    private UserCenterManager mUserCenterManager;
    private UserCenterAPICommunicator mMockUserCenterAPICommunicator;

    public void setUp() {
        // https://code.google.com/p/dexmaker/issues/detail?id=2
        System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());

        mMockUserCenterAPICommunicator = Mockito.mock(UserCenterAPICommunicator.class);
        mUserCenterManager = new UserCenterManager(mMockUserCenterAPICommunicator);
    }

    /**
     * Many tests require a valid response from the Gini API. This method returns as JSONObject
     * which is a valid Gini API response.
     */
    public JSONObject createTestTokenResponse(final String accessToken) throws JSONException{
        return new JSONObject() {{
            put("token_type", "bearer");
            put("access_token", accessToken);
            put("expires_in", 3599);
        }};
    }

    /**
     * Creates and returns a JSONObject which is a valid Gini User Center API response for requesting a user's
     * information.
     */
    public JSONObject createLoginUserResponse(final String email) throws JSONException {
        return new JSONObject() {{
            put("id", "88a28076-18e8-4275-b39c-eaacc240d406");
            put("email", email);
        }};
    }

    public void testLoginClientReturnsFuture() {
        when(mMockUserCenterAPICommunicator.loginClient()).thenReturn(Task.forResult(new JSONObject()));

        assertNotNull(mUserCenterManager.loginClient());
    }

    public void testLoginClientResolvesToCorrectSession() throws JSONException {
        JSONObject responseData = createTestTokenResponse("<PASSWORD>");
        when(mMockUserCenterAPICommunicator.loginClient()).thenReturn(Task.forResult(responseData));

        Session session = mUserCenterManager.loginClient().getResult();

        assertEquals("74c1e7fe-e464-451f-a6eb-8f0998c46ff6", session.getAccessToken());
    }

    public void testGetSessionReusesSession() throws JSONException {
        // First try which sets the first session.
        final JSONObject firstResponseData = createTestTokenResponse("<PASSWORD>");
        when(mMockUserCenterAPICommunicator.loginClient()).thenReturn(Task.forResult(firstResponseData));
        Session firstSession = mUserCenterManager.getUserCenterSession().getResult();

        // Second try which should reuse the old session.
        final JSONObject secondResponseData = new JSONObject() {{
            put("token_type", "bearer");
            put("access_token", "<PASSWORD>");
            put("expires_in", 3599);
        }};
        when(mMockUserCenterAPICommunicator.loginClient()).thenReturn(Task.forResult(secondResponseData));
        Session secondSession = mUserCenterManager.getUserCenterSession().getResult();

        assertEquals(firstSession, secondSession);
    }

    public void testLoginUserShouldReturnTask() throws JSONException {
        UserCredentials userCredentials = new UserCredentials("foobar", "<PASSWORD>");
        JSONObject responseData = createTestTokenResponse("<PASSWORD>");
        when(mMockUserCenterAPICommunicator.loginUser(userCredentials)).thenReturn(Task.forResult(responseData));

        assertNotNull(mUserCenterManager.loginUser(userCredentials));
    }

    public void testLoginUserShouldReturnCorrectSession() throws JSONException {
        UserCredentials userCredentials = new UserCredentials("foobar", "<PASSWORD>");
        JSONObject responseData = createTestTokenResponse("<PASSWORD>4-451f-a6eb-8f0998c46ff6");
        when(mMockUserCenterAPICommunicator.loginUser(userCredentials)).thenReturn(Task.forResult(responseData));

        Session session = mUserCenterManager.loginUser(userCredentials).getResult();
        assertNotNull(session);
        assertEquals("74c1e7fe-e464-451f-a6eb-8f0998c46ff6", session.getAccessToken());
    }

    public void testCreateUserShouldReturnTask() throws JSONException {
        when(mMockUserCenterAPICommunicator.loginClient()).thenReturn(
                Task.forResult(createTestTokenResponse("<PASSWORD>")));
        assertNotNull(mUserCenterManager.createUser(new UserCredentials("foo", "bar")));
    }

    public void testCreateUserShouldResolveToUser() throws JSONException, InterruptedException {
        when(mMockUserCenterAPICommunicator.loginClient())
                .thenReturn(Task.forResult(createTestTokenResponse("<PASSWORD>")));
        UserCredentials userCredentials = new UserCredentials("<EMAIL>", "1234");
        when(mMockUserCenterAPICommunicator.createUser(eq(userCredentials), any(Session.class)))
                .thenReturn(Task.forResult(createLoginUserResponse("<EMAIL>")));

        Task<User> creationTask = mUserCenterManager.createUser(userCredentials);
        creationTask.waitForCompletion();
        User user = creationTask.getResult();
        assertNotNull(user);
        assertEquals("<EMAIL>", user.getUsername());
        assertEquals("88a28076-18e8-4275-b39c-eaacc240d406", user.getUserId());
    }
}<commit_msg>Cleaned up test code.<commit_after>package net.gini.android.authorization;


import android.test.InstrumentationTestCase;

import org.json.JSONException;

import org.json.JSONObject;
import org.mockito.Mockito;

import bolts.Task;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;


public class UserCenterManagerTest extends InstrumentationTestCase {
    private UserCenterManager mUserCenterManager;
    private UserCenterAPICommunicator mMockUserCenterAPICommunicator;

    public void setUp() {
        // https://code.google.com/p/dexmaker/issues/detail?id=2
        System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());

        mMockUserCenterAPICommunicator = Mockito.mock(UserCenterAPICommunicator.class);
        mUserCenterManager = new UserCenterManager(mMockUserCenterAPICommunicator);
    }

    /**
     * Many tests require a valid response from the Gini API. This method returns a task which will resolve to a
     * JSONObject which is a valid Gini API response.
     */
    public Task<JSONObject> createTestTokenResponse(final String accessToken) throws JSONException{
        final JSONObject responseData = new JSONObject() {{
            put("token_type", "bearer");
            put("access_token", accessToken);
            put("expires_in", 3599);
        }};
        return Task.forResult(responseData);
    }

    /**
     * Creates and returns a task which will resolve to a JSONObject which is a valid Gini User Center API response for
     * requesting a user's information.
     */
    public Task<JSONObject> createLoginUserResponse(final String email) throws JSONException {
        final JSONObject responseData = new JSONObject() {{
            put("id", "88a28076-18e8-4275-b39c-eaacc240d406");
            put("email", email);
        }};
        return Task.forResult(responseData);
    }

    public void testLoginClientReturnsFuture() {
        when(mMockUserCenterAPICommunicator.loginClient()).thenReturn(Task.forResult(new JSONObject()));

        assertNotNull(mUserCenterManager.loginClient());
    }

    public void testLoginClientResolvesToCorrectSession() throws JSONException {
        when(mMockUserCenterAPICommunicator.loginClient())
                .thenReturn(createTestTokenResponse("<PASSWORD>"));

        Session session = mUserCenterManager.loginClient().getResult();

        assertEquals("74c1e7fe-e464-451f-a6eb-8f0998c46ff6", session.getAccessToken());
    }

    public void testGetSessionReusesSession() throws JSONException {
        // First try which sets the first session.
        when(mMockUserCenterAPICommunicator.loginClient())
                .thenReturn(createTestTokenResponse("<PASSWORD>"));
        Session firstSession = mUserCenterManager.getUserCenterSession().getResult();

        // Second try which should reuse the old session.
        final JSONObject secondResponseData = new JSONObject() {{
            put("token_type", "bearer");
            put("access_token", "<PASSWORD>");
            put("expires_in", 3599);
        }};
        when(mMockUserCenterAPICommunicator.loginClient()).thenReturn(Task.forResult(secondResponseData));
        Session secondSession = mUserCenterManager.getUserCenterSession().getResult();

        assertEquals(firstSession, secondSession);
    }

    public void testLoginUserShouldReturnTask() throws JSONException {
        UserCredentials userCredentials = new UserCredentials("foobar", "<PASSWORD>");
        when(mMockUserCenterAPICommunicator.loginUser(userCredentials))
                .thenReturn(createTestTokenResponse("<PASSWORD>"));

        assertNotNull(mUserCenterManager.loginUser(userCredentials));
    }

    public void testLoginUserShouldReturnCorrectSession() throws JSONException {
        UserCredentials userCredentials = new UserCredentials("foobar", "<PASSWORD>");
        when(mMockUserCenterAPICommunicator.loginUser(userCredentials))
                .thenReturn(createTestTokenResponse("<PASSWORD>"));

        Session session = mUserCenterManager.loginUser(userCredentials).getResult();
        assertNotNull(session);
        assertEquals("74c1e7fe-e464-451f-a6eb-8f0998c46ff6", session.getAccessToken());
    }

    public void testCreateUserShouldReturnTask() throws JSONException {
        when(mMockUserCenterAPICommunicator.loginClient())
                .thenReturn(createTestTokenResponse("<PASSWORD>"));

        assertNotNull(mUserCenterManager.createUser(new UserCredentials("foo", "bar")));
    }

    public void testCreateUserShouldResolveToUser() throws JSONException, InterruptedException {
        when(mMockUserCenterAPICommunicator.loginClient())
                .thenReturn(createTestTokenResponse("<PASSWORD>"));
        UserCredentials userCredentials = new UserCredentials("<EMAIL>", "1234");
        when(mMockUserCenterAPICommunicator.createUser(eq(userCredentials), any(Session.class)))
                .thenReturn(createLoginUserResponse("<EMAIL>"));

        Task<User> creationTask = mUserCenterManager.createUser(userCredentials);
        creationTask.waitForCompletion();
        User user = creationTask.getResult();
        assertNotNull(user);
        assertEquals("<EMAIL>", user.getUsername());
        assertEquals("88a28076-18e8-4275-b39c-eaacc240d406", user.getUserId());
    }
}
{'fraction_non_alphanumeric': 0.10223082595870206, 'fraction_numerical': 0.018713126843657817, 'mean_word_length': 3.9068294889190414, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 2, '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': '263336', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2864, 'n_words': 518}