Search in sources :

Example 21 with HttpClientNetworkStateHandler

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

the class DistributeBeforeApiSuccessTest method reportReleaseInstallForPublicGroupWhenReleaseHashesMatch.

@Test
public void reportReleaseInstallForPublicGroupWhenReleaseHashesMatch() throws Exception {
    when(mMobileCenterPreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN, null)).thenReturn(null);
    when(mMobileCenterPreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, null)).thenReturn("fake-distribution-id");
    when(PreferencesStorage.getString(PREFERENCE_KEY_DOWNLOADED_RELEASE_HASH)).thenReturn(TEST_HASH);
    when(PreferencesStorage.getInt(PREFERENCE_KEY_DOWNLOADED_RELEASE_ID)).thenReturn(4);
    /* Mock install id from AppCenter. */
    final UUID installId = UUID.randomUUID();
    when(mAppCenterFuture.get()).thenReturn(installId);
    when(AppCenter.getInstallId()).thenReturn(mAppCenterFuture);
    /* Mock httpClient. */
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    HashMap<String, String> headers = new HashMap<>();
    /* Primary storage will be missing data. */
    start();
    Distribute.getInstance().onActivityResumed(mActivity);
    ArgumentMatcher<String> urlArg = new ArgumentMatcher<String>() {

        @Override
        public boolean matches(Object argument) {
            return argument.toString().matches("^https://.*?/public/sdk/apps/a/distribution_groups/fake-distribution-id/releases/latest\\?release_hash=" + TEST_HASH + "&install_id=" + installId + "&downloaded_release_id=4$");
        }
    };
    verify(httpClient).callAsync(argThat(urlArg), eq("GET"), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HashMap(java.util.HashMap) ArgumentMatcher(org.mockito.ArgumentMatcher) Matchers.anyString(org.mockito.Matchers.anyString) UUID(java.util.UUID) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with HttpClientNetworkStateHandler

use of com.microsoft.appcenter.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. */
    start();
    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.appcenter.http.ServiceCall) HashMap(java.util.HashMap) Activity(android.app.Activity) Semaphore(java.util.concurrent.Semaphore) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with HttpClientNetworkStateHandler

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

the class DistributeBeforeApiSuccessTest method storePrivateRedirectionBeforeStart.

@Test
public void storePrivateRedirectionBeforeStart() throws Exception {
    /* Setup mock. */
    when(PreferencesStorage.getString(PREFERENCE_KEY_REQUEST_ID)).thenReturn("r");
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    /* Store token before start, start in background, no storage access. */
    Distribute.getInstance().storeRedirectionParameters("r", "g", "some token");
    start();
    verifyStatic(never());
    PreferencesStorage.putString(anyString(), anyString());
    verifyStatic(never());
    PreferencesStorage.remove(anyString());
    /* Unlock the processing by going into foreground. */
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, "g");
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    verify(mDistributeInfoTracker).updateDistributionGroupId("g");
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HashMap(java.util.HashMap) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with HttpClientNetworkStateHandler

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

the class DistributeBeforeApiSuccessTest method willNotReportReleaseInstallForPrivateGroupWithoutStoredReleaseHash.

@Test
public void willNotReportReleaseInstallForPrivateGroupWithoutStoredReleaseHash() throws Exception {
    when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some encrypted token");
    when(mCryptoUtils.decrypt(eq("some encrypted token"), anyBoolean())).thenReturn(new CryptoUtils.DecryptedData("some token", "some better encrypted token"));
    /* Mock httpClient. */
    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. */
    start();
    Distribute.getInstance().onActivityResumed(mActivity);
    ArgumentMatcher<String> urlArg = new ArgumentMatcher<String>() {

        @Override
        public boolean matches(Object argument) {
            return argument.toString().matches("^https://.*?/sdk/apps/a/releases/latest\\?release_hash=" + TEST_HASH + "$");
        }
    };
    verify(httpClient).callAsync(argThat(urlArg), eq("GET"), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HashMap(java.util.HashMap) ArgumentMatcher(org.mockito.ArgumentMatcher) Matchers.anyString(org.mockito.Matchers.anyString) CryptoUtils(com.microsoft.appcenter.utils.crypto.CryptoUtils) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with HttpClientNetworkStateHandler

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

the class DistributeBeforeApiSuccessTest method reportReleaseInstallForPrivateGroupWhenReleaseHashesMatch.

@Test
public void reportReleaseInstallForPrivateGroupWhenReleaseHashesMatch() throws Exception {
    final String distributionGroupId = "fake-distribution-id";
    when(mMobileCenterPreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN, null)).thenReturn("some token MC");
    when(mMobileCenterPreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, null)).thenReturn(distributionGroupId);
    when(PreferencesStorage.getString(PREFERENCE_KEY_DOWNLOADED_RELEASE_HASH)).thenReturn(TEST_HASH);
    when(PreferencesStorage.getInt(PREFERENCE_KEY_DOWNLOADED_RELEASE_ID)).thenReturn(4);
    /* Mock install id from AppCenter. */
    final UUID installId = UUID.randomUUID();
    when(mAppCenterFuture.get()).thenReturn(installId);
    when(AppCenter.getInstallId()).thenReturn(mAppCenterFuture);
    /* Mock httpClient. */
    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 MC");
    /* Primary storage will be missing data. */
    start();
    Distribute.getInstance().onActivityResumed(mActivity);
    ArgumentMatcher<String> urlArg = new ArgumentMatcher<String>() {

        @Override
        public boolean matches(Object argument) {
            return argument.toString().matches("^https://.*?/sdk/apps/a/releases/latest\\?release_hash=" + TEST_HASH + "&distribution_group_id=" + distributionGroupId + "&downloaded_release_id=4$");
        }
    };
    verify(httpClient).callAsync(argThat(urlArg), eq("GET"), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HashMap(java.util.HashMap) ArgumentMatcher(org.mockito.ArgumentMatcher) Matchers.anyString(org.mockito.Matchers.anyString) UUID(java.util.UUID) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)42 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)41 Matchers.anyString (org.mockito.Matchers.anyString)40 Test (org.junit.Test)33 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)33 HashMap (java.util.HashMap)27 ServiceCall (com.microsoft.appcenter.http.ServiceCall)26 InvocationOnMock (org.mockito.invocation.InvocationOnMock)25 Activity (android.app.Activity)23 UUID (java.util.UUID)10 ArgumentMatcher (org.mockito.ArgumentMatcher)9 DialogInterface (android.content.DialogInterface)8 Context (android.content.Context)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Semaphore (java.util.concurrent.Semaphore)4 PackageInfo (android.content.pm.PackageInfo)3 PackageManager (android.content.pm.PackageManager)3 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)3 NetworkStateHelper (com.microsoft.appcenter.utils.NetworkStateHelper)3 JSONException (org.json.JSONException)3