Search in sources :

Example 91 with ServiceCall

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

the class DistributeBeforeDownloadTest method updateASecondTimeClearsPreviousReleaseCache.

@Test
public void updateASecondTimeClearsPreviousReleaseCache() throws Exception {
    /* Mock first update completed. */
    mockStatic(DistributeUtils.class);
    when(DistributeUtils.getStoredDownloadState()).thenReturn(DOWNLOAD_STATE_COMPLETED);
    when(mReleaseDetails.getVersion()).thenReturn(6);
    when(mReleaseDetails.getId()).thenReturn(1);
    when(DistributeUtils.loadCachedReleaseDetails()).thenReturn(mReleaseDetails);
    ReleaseDownloader cachedReleaseDownloader = mock(ReleaseDownloader.class);
    when(ReleaseDownloaderFactory.create(any(Context.class), same(mReleaseDetails), any(ReleaseDownloadListener.class))).thenReturn(cachedReleaseDownloader);
    when(cachedReleaseDownloader.getReleaseDetails()).thenReturn(mReleaseDetails);
    /* Mock next release. */
    final ReleaseDetails nextReleaseDetails = mock(ReleaseDetails.class);
    when(nextReleaseDetails.getId()).thenReturn(2);
    when(nextReleaseDetails.getVersion()).thenReturn(7);
    when(ReleaseDetails.parse(anyString())).thenReturn(nextReleaseDetails);
    ReleaseDownloader nextReleaseDownloader = mock(ReleaseDownloader.class);
    when(ReleaseDownloaderFactory.create(any(Context.class), same(nextReleaseDetails), any(ReleaseDownloadListener.class))).thenReturn(nextReleaseDownloader);
    when(nextReleaseDownloader.getReleaseDetails()).thenReturn(nextReleaseDetails);
    /* Simulate cache update. */
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            when(DistributeUtils.loadCachedReleaseDetails()).thenReturn(nextReleaseDetails);
            return null;
        }
    }).when(SharedPreferencesManager.class);
    SharedPreferencesManager.putString(eq(PREFERENCE_KEY_RELEASE_DETAILS), anyString());
    /* Mock we receive a second update. */
    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) {
            ((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded(new HttpResponse(200, "mock"));
            return mock(ServiceCall.class);
        }
    });
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mHttpClient).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify prompt is shown. */
    verify(mDialog).show();
    /* Verify previous download canceled. */
    verify(cachedReleaseDownloader).cancel();
}
Also used : SessionContext(com.microsoft.appcenter.utils.context.SessionContext) Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) ReleaseDownloader(com.microsoft.appcenter.distribute.download.ReleaseDownloader) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 92 with ServiceCall

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

the class DistributeHttpTest method getCallTemplate.

private HttpClient.CallTemplate getCallTemplate(String appSecret, String apiToken) {
    /* Configure mock HTTP to get an instance of IngestionCallTemplate. */
    Distribute.getInstance().onStarting(mAppCenterHandler);
    Distribute.getInstance().onStarted(mContext, mock(Channel.class), appSecret, null, true);
    final ServiceCall call = mock(ServiceCall.class);
    final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
    when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).then(new Answer<ServiceCall>() {

        @Override
        public ServiceCall answer(InvocationOnMock invocation) {
            callTemplate.set((HttpClient.CallTemplate) invocation.getArguments()[3]);
            return call;
        }
    });
    Distribute.getInstance().getLatestReleaseDetails("mockGroup", apiToken);
    return callTemplate.get();
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Channel(com.microsoft.appcenter.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString)

Example 93 with ServiceCall

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

the class DistributeBeforeApiSuccessTest method disableWhileCheckingRelease.

@Test
public void disableWhileCheckingRelease() {
    /* Mock we already have token. */
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    ServiceCall firstCall = mock(ServiceCall.class);
    when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenReturn(firstCall).thenReturn(mock(ServiceCall.class));
    /* The call is only triggered when app is resumed. */
    start();
    verify(mHttpClient, never()).callAsync(anyString(), anyString(), eq(Collections.<String, String>emptyMap()), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    Distribute.getInstance().onActivityResumed(mActivity);
    verify(mHttpClient).callAsync(anyString(), anyString(), eq(Collections.<String, String>emptyMap()), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify cancel on disabling. */
    verify(firstCall, never()).cancel();
    Distribute.setEnabled(false);
    verify(firstCall).cancel();
    /* No more call on that one. */
    Distribute.setEnabled(true);
    Distribute.setEnabled(false);
    verify(firstCall).cancel();
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HttpClient(com.microsoft.appcenter.http.HttpClient) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 94 with ServiceCall

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

the class DistributeBeforeApiSuccessTest method releaseSuccessDifferentIds.

@Test
public void releaseSuccessDifferentIds() {
    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 id had changed. */
            Distribute.getInstance().getLatestReleaseDetails("mockGroup", "token");
            ((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 95 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall 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(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) {
            ((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded(new HttpResponse(200, "mock", Collections.<String, String>emptyMap()));
            return mock(ServiceCall.class);
        }
    });
    Map<String, String> headers = new HashMap<>();
    when(ReleaseDetails.parse(anyString())).thenThrow(new JSONException("mock"));
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify on failure we complete workflow. */
    verifyStatic();
    SharedPreferencesManager.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(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) ServiceCall(com.microsoft.appcenter.http.ServiceCall) HashMap(java.util.HashMap) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpResponse(com.microsoft.appcenter.http.HttpResponse) JSONException(org.json.JSONException) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ServiceCall (com.microsoft.appcenter.http.ServiceCall)116 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)116 Matchers.anyString (org.mockito.Matchers.anyString)113 InvocationOnMock (org.mockito.invocation.InvocationOnMock)95 Test (org.junit.Test)91 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)91 Activity (android.app.Activity)59 HttpResponse (com.microsoft.appcenter.http.HttpResponse)57 HashMap (java.util.HashMap)45 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)33 HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)26 Log (com.microsoft.appcenter.ingestion.models.Log)25 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)25 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)24 DialogInterface (android.content.DialogInterface)21 DistributionStartSessionLog (com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog)21 HttpClient (com.microsoft.appcenter.http.HttpClient)21 Answer (org.mockito.stubbing.Answer)20 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)20