Search in sources :

Example 46 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-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. */
    start();
    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.appcenter.http.ServiceCall) HashMap(java.util.HashMap) JSONException(org.json.JSONException) Activity(android.app.Activity) 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 47 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.

the class DistributeBeforeApiSuccessTest method disableBeforeCheckReleaseFails.

@Test
public void disableBeforeCheckReleaseFails() 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]).onCallFailed(new HttpException(403));
                    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 fails. */
    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 ignored. 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));
}
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) HttpException(com.microsoft.appcenter.http.HttpException) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 48 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.

the class DistributeCustomizationTest method mockForCustomizationTest.

private ReleaseDetails mockForCustomizationTest(boolean mandatory) throws Exception {
    /* Mock http call. */
    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);
        }
    });
    /* 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(mandatory);
    when(ReleaseDetails.parse(anyString())).thenReturn(details);
    /* Mock update token. */
    when(StorageHelper.PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    return details;
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) ServiceCall(com.microsoft.appcenter.http.ServiceCall) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler)

Example 49 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.

the class DistributeHttpTest method getCallTemplate.

private HttpClient.CallTemplate getCallTemplate(String appSecret, String apiToken) throws Exception {
    /* Configure mock HTTP to get an instance of IngestionCallTemplate. */
    Distribute.getInstance().onStarting(mAppCenterHandler);
    Distribute.getInstance().onStarted(mContext, appSecret, mock(Channel.class));
    final ServiceCall call = mock(ServiceCall.class);
    final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
    mockStatic(NetworkStateHelper.class);
    when(NetworkStateHelper.getSharedInstance(any(Context.class))).thenReturn(mock(NetworkStateHelper.class));
    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))).then(new Answer<ServiceCall>() {

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

Example 50 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.

the class DistributeWarnUnknownSourcesTest method setUpDialog.

@Before
public void setUpDialog() throws Exception {
    /* Mock we already have redirection parameters. */
    when(PreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
    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);
        }
    });
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(7);
    when(releaseDetails.isMandatoryUpdate()).thenReturn(mMandatoryUpdate);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mFirstActivity);
    /* Mock second dialog. */
    when(mDialogBuilder.create()).thenReturn(mUnknownSourcesDialog);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            when(mUnknownSourcesDialog.isShowing()).thenReturn(true);
            return null;
        }
    }).when(mUnknownSourcesDialog).show();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            when(mUnknownSourcesDialog.isShowing()).thenReturn(false);
            return null;
        }
    }).when(mUnknownSourcesDialog).hide();
    /* Click on first dialog. */
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), clickListener.capture());
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
    when(mDialog.isShowing()).thenReturn(false);
    /* Second should show. */
    verify(mUnknownSourcesDialog).show();
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) DialogInterface(android.content.DialogInterface) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) Before(org.junit.Before)

Aggregations

ServiceCall (com.microsoft.appcenter.http.ServiceCall)69 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)69 Matchers.anyString (org.mockito.Matchers.anyString)68 InvocationOnMock (org.mockito.invocation.InvocationOnMock)61 Test (org.junit.Test)53 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)53 Activity (android.app.Activity)43 HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)39 HashMap (java.util.HashMap)30 HttpResponse (com.microsoft.appcenter.http.HttpResponse)19 DialogInterface (android.content.DialogInterface)16 Context (android.content.Context)15 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 Log (com.microsoft.appcenter.ingestion.models.Log)11 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)11 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)11 UUID (java.util.UUID)11 ArrayList (java.util.ArrayList)9 Answer (org.mockito.stubbing.Answer)9