Search in sources :

Example 26 with HttpClientNetworkStateHandler

use of com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method storeBetterEncryptedToken.

@Test
public void storeBetterEncryptedToken() throws Exception {
    /* Mock we already have token. */
    when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some encrypted token");
    when(mCryptoUtils.decrypt("some encrypted token")).thenReturn(new CryptoUtils.DecryptedData("some token", "some better encrypted token"));
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
    /* Trigger call. */
    Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify storage was updated with new encrypted value. */
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some better encrypted token");
}
Also used : ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) HashMap(java.util.HashMap) Channel(com.microsoft.azure.mobile.channel.Channel) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) CryptoUtils(com.microsoft.azure.mobile.utils.crypto.CryptoUtils) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 27 with HttpClientNetworkStateHandler

use of com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method setUrls.

@Test
public void setUrls() throws Exception {
    /* Setup mock. */
    Distribute.setInstallUrl("http://mock");
    Distribute.setApiUrl("https://mock2");
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    UUID requestId = UUID.randomUUID();
    when(UUIDUtils.randomUUID()).thenReturn(requestId);
    when(PreferencesStorage.getString(PREFERENCE_KEY_REQUEST_ID)).thenReturn(requestId.toString());
    /* Start and resume: open browser. */
    Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
    Distribute.getInstance().onActivityResumed(mActivity);
    verifyStatic();
    String url = "http://mock";
    url += String.format(UPDATE_SETUP_PATH_FORMAT, "a");
    url += "?" + PARAMETER_RELEASE_HASH + "=" + TEST_HASH;
    url += "&" + PARAMETER_REDIRECT_ID + "=" + mContext.getPackageName();
    url += "&" + PARAMETER_REQUEST_ID + "=" + requestId.toString();
    url += "&" + PARAMETER_PLATFORM + "=" + PARAMETER_PLATFORM_VALUE;
    BrowserUtils.openBrowser(url, mActivity);
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_REQUEST_ID, requestId.toString());
    /* Store token. */
    Distribute.getInstance().storeUpdateToken("some token", requestId.toString());
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
    verify(httpClient).callAsync(argThat(new ArgumentMatcher<String>() {

        @Override
        public boolean matches(Object argument) {
            return argument.toString().startsWith("https://mock2");
        }
    }), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) HashMap(java.util.HashMap) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) Matchers.anyString(org.mockito.Matchers.anyString) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 28 with HttpClientNetworkStateHandler

use of com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method checkReleaseFailure.

private void checkReleaseFailure(final Exception exception, VerificationMode deleteTokenVerificationMode) throws Exception {
    /* Mock we already have token. */
    when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    when(httpClient.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) throws Throwable {
            ((ServiceCallback) invocation.getArguments()[4]).onCallFailed(exception);
            return mock(ServiceCall.class);
        }
    });
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
    /* Trigger call. */
    Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify on failure we complete workflow. */
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    /* Check token kept or not depending on the test. */
    verifyStatic(deleteTokenVerificationMode);
    PreferencesStorage.remove(PREFERENCE_KEY_UPDATE_TOKEN);
    /* After that if we resume app nothing happens. */
    Distribute.getInstance().onActivityPaused(mock(Activity.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) HashMap(java.util.HashMap) Channel(com.microsoft.azure.mobile.channel.Channel) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler)

Example 29 with HttpClientNetworkStateHandler

use of com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method disableBeforeCheckReleaseSucceed.

@Test
public void disableBeforeCheckReleaseSucceed() throws Exception {
    /* Mock we already have token. */
    when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    final Semaphore beforeSemaphore = new Semaphore(0);
    final Semaphore afterSemaphore = new Semaphore(0);
    when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {

        @Override
        public ServiceCall answer(final InvocationOnMock invocation) throws Throwable {
            new Thread() {

                @Override
                public void run() {
                    beforeSemaphore.acquireUninterruptibly();
                    ((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded("mock");
                    afterSemaphore.release();
                }
            }.start();
            return mock(ServiceCall.class);
        }
    });
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
    /* Trigger call. */
    Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Disable before it succeeds. */
    Distribute.setEnabled(false);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    verifyStatic(never());
    PreferencesStorage.remove(PREFERENCE_KEY_UPDATE_TOKEN);
    beforeSemaphore.release();
    afterSemaphore.acquireUninterruptibly();
    /* Verify complete workflow call skipped. i.e. no more call to delete the state. */
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    /* After that if we resume app nothing happens. */
    Distribute.getInstance().onActivityPaused(mock(Activity.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    verify(mDialog, never()).show();
}
Also used : ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) HashMap(java.util.HashMap) Channel(com.microsoft.azure.mobile.channel.Channel) Activity(android.app.Activity) Semaphore(java.util.concurrent.Semaphore) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 30 with HttpClientNetworkStateHandler

use of com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method checkReleaseFailsParsing.

@Test
public void checkReleaseFailsParsing() throws Exception {
    /* Mock we already have token. */
    when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    when(httpClient.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) throws Throwable {
            ((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded("mock");
            return mock(ServiceCall.class);
        }
    });
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
    when(ReleaseDetails.parse(anyString())).thenThrow(new JSONException("mock"));
    /* Trigger call. */
    Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify on failure we complete workflow. */
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    /* After that if we resume app nothing happens. */
    Distribute.getInstance().onActivityPaused(mock(Activity.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) HashMap(java.util.HashMap) Channel(com.microsoft.azure.mobile.channel.Channel) JSONException(org.json.JSONException) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

HttpClientNetworkStateHandler (com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler)30 ServiceCallback (com.microsoft.azure.mobile.http.ServiceCallback)30 Matchers.anyString (org.mockito.Matchers.anyString)29 Channel (com.microsoft.azure.mobile.channel.Channel)25 ServiceCall (com.microsoft.azure.mobile.http.ServiceCall)24 InvocationOnMock (org.mockito.invocation.InvocationOnMock)23 Test (org.junit.Test)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 Activity (android.app.Activity)20 HashMap (java.util.HashMap)18 DialogInterface (android.content.DialogInterface)8 Context (android.content.Context)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 UUID (java.util.UUID)4 Semaphore (java.util.concurrent.Semaphore)4 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)3 JSONException (org.json.JSONException)3 ArgumentMatcher (org.mockito.ArgumentMatcher)3 ActivityNotFoundException (android.content.ActivityNotFoundException)2 Build (android.os.Build)2