Search in sources :

Example 1 with HttpResponse

use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.

the class MSALoginActivity method getToken.

/**
 * Get initial access token.
 */
private void getToken(final String code) {
    Map<String, String> headers = new HashMap<>();
    headers.put(DefaultHttpClient.CONTENT_TYPE_KEY, "application/x-www-form-urlencoded");
    sHttpClient.callAsync(TOKEN_URL, DefaultHttpClient.METHOD_POST, headers, new HttpClient.CallTemplate() {

        @Override
        public String buildRequestBody() {
            return REDIRECT_URI_PARAM + CLIENT_ID_PARAM + "&grant_type=authorization_code" + "&code=" + code;
        }

        @Override
        public void onBeforeCalling(URL url, Map<String, String> headers) {
            AppCenterLog.verbose(AppCenter.LOG_TAG, "Calling " + url + "...");
        }
    }, new ServiceCallback() {

        @Override
        public void onCallSucceeded(HttpResponse httpResponse) {
            try {
                JSONObject response = new JSONObject(httpResponse.getPayload());
                String userId = response.getString(USER_ID);
                mRefreshToken = response.getString(REFRESH_TOKEN);
                mRefreshTokenScope = response.getString(SCOPE);
                registerAppCenterAuthentication(userId);
            } catch (JSONException e) {
                onCallFailed(e);
            }
        }

        @Override
        public void onCallFailed(Exception e) {
            handleCallFailure(e);
        }
    });
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) HttpUtils.createHttpClient(com.microsoft.appcenter.http.HttpUtils.createHttpClient) HttpClient(com.microsoft.appcenter.http.HttpClient) DefaultHttpClient(com.microsoft.appcenter.http.DefaultHttpClient) HttpResponse(com.microsoft.appcenter.http.HttpResponse) JSONException(org.json.JSONException) REDIRECT_URL(com.microsoft.appcenter.sasquatch.MSAAuthenticationProvider.REDIRECT_URL) URL(java.net.URL) AUTHORIZE_URL(com.microsoft.appcenter.sasquatch.MSAAuthenticationProvider.AUTHORIZE_URL) TOKEN_URL(com.microsoft.appcenter.sasquatch.MSAAuthenticationProvider.TOKEN_URL) SIGN_OUT_URL(com.microsoft.appcenter.sasquatch.MSAAuthenticationProvider.SIGN_OUT_URL) HttpException(com.microsoft.appcenter.http.HttpException) JSONException(org.json.JSONException)

Example 2 with HttpResponse

use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method releaseFailureWithDifferentIds.

@Test
public void releaseFailureWithDifferentIds() {
    /* Mock we already have token. */
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {

        @Override
        public ServiceCall answer(InvocationOnMock invocation) {
            /* Do the call so that ids do not match. */
            Distribute.getInstance().getLatestReleaseDetails("mockGroup", "token");
            ((ServiceCallback) invocation.getArguments()[4]).onCallFailed(new HttpException(new HttpResponse(503)));
            return mock(ServiceCall.class);
        }
    }).thenAnswer(new Answer<ServiceCall>() {

        /* On second time we don't answer as it's callback from getLatestReleaseDetails above. */
        @Override
        public ServiceCall answer(InvocationOnMock invocation) {
            return mock(ServiceCall.class);
        }
    });
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* Verify on failure we don't complete workflow if ids don't match. */
    verifyStatic(never());
    SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
}
Also used : PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) ServiceCall(com.microsoft.appcenter.http.ServiceCall) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClient(com.microsoft.appcenter.http.HttpClient) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Activity(android.app.Activity) HttpException(com.microsoft.appcenter.http.HttpException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with HttpResponse

use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method releaseSuccessActivityIsNull.

@Test
public void releaseSuccessActivityIsNull() {
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    /* Update is more recent. */
    when(mReleaseDetails.getVersion()).thenReturn(7);
    when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {

        @Override
        public ServiceCall answer(InvocationOnMock invocation) {
            /* Do the call so that id had changed. */
            Distribute.getInstance().onActivityPaused(mActivity);
            ((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded(new HttpResponse(200, "mock", Collections.<String, String>emptyMap()));
            return mock(ServiceCall.class);
        }
    }).thenAnswer(new Answer<ServiceCall>() {

        /* On second time we don't answer as it's callback from getLatestReleaseDetails above. */
        @Override
        public ServiceCall answer(InvocationOnMock invocation) {
            return mock(ServiceCall.class);
        }
    });
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* Verify on failure we don't complete workflow if ids don't match. */
    verifyStatic(never());
    SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
}
Also used : PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) ServiceCall(com.microsoft.appcenter.http.ServiceCall) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClient(com.microsoft.appcenter.http.HttpClient) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with HttpResponse

use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method checkReleaseFailsWithSome404urlNotFound.

@Test
public void checkReleaseFailsWithSome404urlNotFound() throws Exception {
    /* Mock error parsing. */
    mockStatic(ErrorDetails.class);
    final String errorPayload = "<html>Not Found</html>";
    when(ErrorDetails.parse(errorPayload)).thenThrow(new JSONException("Expected {"));
    final Exception exception = new HttpException(new HttpResponse(404, errorPayload));
    checkReleaseFailure(exception, times(1));
}
Also used : JSONException(org.json.JSONException) HttpResponse(com.microsoft.appcenter.http.HttpResponse) HttpException(com.microsoft.appcenter.http.HttpException) Matchers.anyString(org.mockito.Matchers.anyString) URISyntaxException(java.net.URISyntaxException) HttpException(com.microsoft.appcenter.http.HttpException) JSONException(org.json.JSONException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with HttpResponse

use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method checkReleaseFailsWithSome404noRelease.

@Test
public void checkReleaseFailsWithSome404noRelease() throws Exception {
    /* Mock error parsing. */
    ErrorDetails errorDetails = mock(ErrorDetails.class);
    when(errorDetails.getCode()).thenReturn(ErrorDetails.NO_RELEASES_FOR_USER_CODE);
    mockStatic(ErrorDetails.class);
    String errorPayload = "{code: 'no_releases_for_user'}";
    when(ErrorDetails.parse(errorPayload)).thenReturn(errorDetails);
    checkReleaseFailure(new HttpException(new HttpResponse(404, errorPayload)), never());
}
Also used : HttpResponse(com.microsoft.appcenter.http.HttpResponse) HttpException(com.microsoft.appcenter.http.HttpException) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

HttpResponse (com.microsoft.appcenter.http.HttpResponse)36 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)33 Matchers.anyString (org.mockito.Matchers.anyString)31 Test (org.junit.Test)30 InvocationOnMock (org.mockito.invocation.InvocationOnMock)25 ServiceCall (com.microsoft.appcenter.http.ServiceCall)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Activity (android.app.Activity)18 Context (android.content.Context)12 HttpException (com.microsoft.appcenter.http.HttpException)12 AppCenterIngestion (com.microsoft.appcenter.ingestion.AppCenterIngestion)10 Log (com.microsoft.appcenter.ingestion.models.Log)10 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)10 Persistence (com.microsoft.appcenter.persistence.Persistence)10 HashMap (java.util.HashMap)9 UUID (java.util.UUID)9 DistributionStartSessionLog (com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog)7 Answer (org.mockito.stubbing.Answer)7 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)6 DialogInterface (android.content.DialogInterface)5