Search in sources :

Example 1 with ServiceCallback

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

the class DefaultChannel method sendLogs.

/**
     * Send logs.
     *
     * @param groupState   The group state.
     * @param currentState The current state.
     * @param batch        The log batch.
     * @param batchId      The batch ID.
     */
@MainThread
private synchronized void sendLogs(final GroupState groupState, final int currentState, List<Log> batch, final String batchId) {
    if (checkStateDidNotChange(groupState, currentState)) {
        /* Send logs. */
        LogContainer logContainer = new LogContainer();
        logContainer.setLogs(batch);
        mIngestion.sendAsync(mAppSecret, mInstallId, logContainer, new ServiceCallback() {

            @Override
            public void onCallSucceeded(String payload) {
                handleSendingSuccess(groupState, currentState, batchId);
            }

            @Override
            public void onCallFailed(Exception e) {
                handleSendingFailure(groupState, currentState, batchId, e);
            }
        });
        /* Check for more pending logs. */
        checkPendingLogs(groupState.mName);
    }
}
Also used : ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) IOException(java.io.IOException) CancellationException(com.microsoft.azure.mobile.CancellationException) MainThread(android.support.annotation.MainThread)

Example 2 with ServiceCallback

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

the class DefaultChannelTest method disableBeforeCheckingPendingLogs.

@Test
@SuppressWarnings("unchecked")
public void disableBeforeCheckingPendingLogs() throws IOException {
    Ingestion ingestion = mock(Ingestion.class);
    Persistence persistence = mock(Persistence.class);
    final DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion);
    when(persistence.getLogs(anyString(), anyInt(), anyList())).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) throws Throwable {
            /* Simulate a service disabled in the middle of network transaction. */
            ServiceCallback callback = (ServiceCallback) invocation.getArguments()[3];
            channel.removeGroup(TEST_GROUP);
            callback.onCallSucceeded("");
            return null;
        }
    });
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* checkPendingLogs is getting called twice from triggerIngestion and a callback for ingestion.
           It can be failed because of timing issue so checking at least once instead. */
    verifyStatic(atLeastOnce());
    MobileCenterLog.info(eq(MobileCenter.LOG_TAG), anyString());
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Persistence(com.microsoft.azure.mobile.persistence.Persistence) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with ServiceCallback

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

the class DistributeBeforeDownloadTest method cancelGetReleaseCallIfDownloadingCachedDialogAfterRestart.

@Test
public void cancelGetReleaseCallIfDownloadingCachedDialogAfterRestart() throws Exception {
    /* Mock some storage calls. */
    mockSomeStorage();
    /* 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 AtomicReference<ServiceCallback> serviceCallbackRef = new AtomicReference<>();
    final ServiceCall serviceCall = mock(ServiceCall.class);
    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 {
            Object serviceCallback = invocation.getArguments()[4];
            if (serviceCallback instanceof ServiceCallback) {
                serviceCallbackRef.set((ServiceCallback) serviceCallback);
            }
            return serviceCall;
        }
    });
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(7);
    when(releaseDetails.isMandatoryUpdate()).thenReturn(false);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    /* Trigger call. */
    Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* Verify dialog. */
    serviceCallbackRef.get().onCallSucceeded("mock");
    verify(mDialogBuilder).setPositiveButton(eq(R.string.mobile_center_distribute_update_dialog_download), any(DialogInterface.OnClickListener.class));
    /* Restart offline. */
    when(mNetworkStateHelper.isNetworkConnected()).thenReturn(false);
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* Verify dialog restored and call scheduled. */
    verify(httpClient, times(2)).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder, times(2)).setPositiveButton(eq(R.string.mobile_center_distribute_update_dialog_download), clickListener.capture());
    /* We are offline and call is scheduled, clicking download must cancel pending call. */
    when(InstallerUtils.isUnknownSourcesEnabled(mContext)).thenReturn(true);
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
    verify(serviceCall).cancel();
}
Also used : ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) DialogInterface(android.content.DialogInterface) Channel(com.microsoft.azure.mobile.channel.Channel) Activity(android.app.Activity) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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 4 with ServiceCallback

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

the class DistributeBeforeDownloadTest method olderVersionCode.

@Test
public void olderVersionCode() 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");
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(5);
    when(releaseDetails.getMinApiLevel()).thenReturn(Build.VERSION_CODES.M);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.N_MR1);
    /* 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);
    verify(mDialogBuilder, never()).create();
    verify(mDialog, never()).show();
    /* 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 release hash was not even considered. */
    verify(releaseDetails, never()).getReleaseHash();
}
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) Build(android.os.Build) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with ServiceCallback

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

the class DistributeBeforeDownloadTest method sameVersionDifferentHashWithHardcodedAppName.

@Test
public void sameVersionDifferentHashWithHardcodedAppName() 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");
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(6);
    when(releaseDetails.getShortVersion()).thenReturn("1.2.3");
    when(releaseDetails.getReleaseHash()).thenReturn("9f52199c986d9210842824df695900e1656180946212bd5e8978501a5b732e60");
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    /* Mock app name to be not localizable. */
    Whitebox.setInternalState(mContext.getApplicationInfo(), "labelRes", 0);
    Whitebox.setInternalState(mContext.getApplicationInfo(), "nonLocalizedLabel", "hardcoded-app-name");
    /* 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 dialog. */
    verify(mDialogBuilder).setTitle(R.string.mobile_center_distribute_update_dialog_title);
    verify(mDialogBuilder).setMessage("hardcoded-app-name1.2.36");
    verify(mDialogBuilder).create();
    verify(mDialog).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) 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

ServiceCallback (com.microsoft.azure.mobile.http.ServiceCallback)28 InvocationOnMock (org.mockito.invocation.InvocationOnMock)26 ServiceCall (com.microsoft.azure.mobile.http.ServiceCall)23 Matchers.anyString (org.mockito.Matchers.anyString)23 HttpClientNetworkStateHandler (com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler)22 Test (org.junit.Test)22 Channel (com.microsoft.azure.mobile.channel.Channel)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 Activity (android.app.Activity)16 HashMap (java.util.HashMap)13 Context (android.content.Context)10 DialogInterface (android.content.DialogInterface)8 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)8 Log (com.microsoft.azure.mobile.ingestion.models.Log)7 UUID (java.util.UUID)7 Semaphore (java.util.concurrent.Semaphore)6 Persistence (com.microsoft.azure.mobile.persistence.Persistence)5 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)5 IngestionHttp (com.microsoft.azure.mobile.ingestion.IngestionHttp)4 ArrayList (java.util.ArrayList)4