Search in sources :

Example 31 with HttpResponse

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

the class DistributeCustomizationTest method distributeNotRecentCoverage.

private void distributeNotRecentCoverage(DistributeListener actualListener, DistributeListener verificationListener, boolean onPause) throws Exception {
    /* Set the package version higher than the portal's one. */
    mockStatic(DeviceInfoHelper.class);
    when(DeviceInfoHelper.getVersionCode(any(PackageInfo.class))).thenReturn(11);
    /* Mock http call. */
    ArgumentCaptor<ServiceCallback> httpCallback = ArgumentCaptor.forClass(ServiceCallback.class);
    when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), httpCallback.capture())).thenReturn(mock(ServiceCall.class));
    /* Mock data model. */
    mockStatic(ReleaseDetails.class);
    ReleaseDetails details = mock(ReleaseDetails.class);
    when(details.getId()).thenReturn(1);
    when(details.getVersion()).thenReturn(10);
    when(details.getShortVersion()).thenReturn("2.3.4");
    when(details.isMandatoryUpdate()).thenReturn(false);
    when(details.getReleaseHash()).thenReturn("some_hash");
    when(ReleaseDetails.parse(anyString())).thenReturn(details);
    /* Mock update token. */
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    /* Start Distribute service. */
    restartProcessAndSdk();
    /* Set Distribute listener and customize it. */
    Distribute.setListener(actualListener);
    /* Resume activity. */
    Distribute.getInstance().onActivityResumed(mActivity);
    if (onPause) {
        Distribute.getInstance().onActivityPaused(mActivity);
    }
    /* Simulate network latency. */
    httpCallback.getValue().onCallSucceeded(new HttpResponse(200, "mock"));
    /* Verify the right listener gets called. */
    verify(verificationListener, never()).onNoReleaseAvailable(any(Activity.class));
    verify(verificationListener, never()).onReleaseAvailable(any(Activity.class), any(ReleaseDetails.class));
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) ServiceCall(com.microsoft.appcenter.http.ServiceCall) PackageInfo(android.content.pm.PackageInfo) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString)

Example 32 with HttpResponse

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

the class DistributeCustomizationTest method distributeNoReleaseAvailableCoverage.

private void distributeNoReleaseAvailableCoverage(DistributeListener actualListener, DistributeListener verificationListener, boolean onPause) throws Exception {
    /* Mock error parsing. */
    ErrorDetails errorDetails = mock(ErrorDetails.class);
    when(errorDetails.getCode()).thenReturn(ErrorDetails.NO_RELEASES_FOUND);
    mockStatic(ErrorDetails.class);
    when(ErrorDetails.parse(anyString())).thenReturn(errorDetails);
    /* Mock http call. */
    final HttpException httpException = new HttpException(new HttpResponse(404, "{code:'not_found'}"));
    /* Mock http call. */
    ArgumentCaptor<ServiceCallback> httpCallback = ArgumentCaptor.forClass(ServiceCallback.class);
    when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), httpCallback.capture())).thenReturn(mock(ServiceCall.class));
    /* Start Distribute service. */
    restartProcessAndSdk();
    /* Set Distribute listener and customize it. */
    Distribute.setListener(actualListener);
    /* Resume activity. */
    Distribute.getInstance().onActivityResumed(mActivity);
    if (onPause) {
        Distribute.getInstance().onActivityPaused(mActivity);
    }
    /* Simulate network latency. */
    httpCallback.getValue().onCallFailed(httpException);
    /* Verify the right listener gets called. */
    verify(verificationListener, never()).onNoReleaseAvailable(any(Activity.class));
    verify(verificationListener, never()).onReleaseAvailable(any(Activity.class), any(ReleaseDetails.class));
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) ServiceCall(com.microsoft.appcenter.http.ServiceCall) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Activity(android.app.Activity) HttpException(com.microsoft.appcenter.http.HttpException) Matchers.anyString(org.mockito.Matchers.anyString)

Example 33 with HttpResponse

use of com.microsoft.appcenter.http.HttpResponse 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 34 with HttpResponse

use of com.microsoft.appcenter.http.HttpResponse 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)

Example 35 with HttpResponse

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

the class DefaultChannelTest method disableBeforeCheckingPendingLogs.

@Test
public void disableBeforeCheckingPendingLogs() {
    Ingestion ingestion = mock(Ingestion.class);
    when(ingestion.isEnabled()).thenReturn(true);
    Persistence persistence = mock(Persistence.class);
    final DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, ingestion, mAppCenterHandler);
    when(persistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(1));
    when(ingestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).thenAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            /* Simulate a service disabled in the middle of network transaction. */
            ServiceCallback callback = (ServiceCallback) invocation.getArguments()[3];
            channel.removeGroup(TEST_GROUP);
            callback.onCallSucceeded(new HttpResponse(200, ""));
            return null;
        }
    });
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Matchers.anyString(org.mockito.Matchers.anyString) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Persistence(com.microsoft.appcenter.persistence.Persistence) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) 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