Search in sources :

Example 11 with HttpClientNetworkStateHandler

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

the class DistributeBeforeDownloadTest method releaseNotes.

@Test
public void releaseNotes() 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);
        }
    });
    /* No release notes. */
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(7);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    /* Trigger call. */
    Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* Verify dialog. */
    verify(mDialogBuilder, never()).setNeutralButton(eq(R.string.mobile_center_distribute_update_dialog_view_release_notes), any(DialogInterface.OnClickListener.class));
    verify(mDialog).show();
    /* Release notes but somehow no URL. */
    when(releaseDetails.getReleaseNotes()).thenReturn("Fix a bug");
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mDialogBuilder, never()).setNeutralButton(eq(R.string.mobile_center_distribute_update_dialog_view_release_notes), any(DialogInterface.OnClickListener.class));
    verify(mDialog, times(2)).show();
    /* Release notes URL this time. */
    final Uri uri = mock(Uri.class);
    Intent intent = mock(Intent.class);
    whenNew(Intent.class).withArguments(Intent.ACTION_VIEW, uri).thenReturn(intent);
    when(releaseDetails.getReleaseNotesUrl()).thenReturn(uri);
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mActivity);
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder).setNeutralButton(eq(R.string.mobile_center_distribute_update_dialog_view_release_notes), clickListener.capture());
    verify(mDialog, times(3)).show();
    /* Click and check navigation. */
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_NEUTRAL);
    verify(mActivity).startActivity(intent);
    /* We thus leave app. */
    Distribute.getInstance().onActivityPaused(mActivity);
    when(mDialog.isShowing()).thenReturn(false);
    /* Going back should restore dialog. */
    Distribute.getInstance().onActivityResumed(mActivity);
    clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder, times(2)).setNeutralButton(eq(R.string.mobile_center_distribute_update_dialog_view_release_notes), clickListener.capture());
    verify(mDialog, times(4)).show();
    /* Do the same test and simulate failed navigation. */
    mockStatic(MobileCenterLog.class);
    ActivityNotFoundException exception = new ActivityNotFoundException();
    doThrow(exception).when(mActivity).startActivity(intent);
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_NEUTRAL);
    verify(mActivity, times(2)).startActivity(intent);
    verifyStatic();
    MobileCenterLog.error(anyString(), anyString(), eq(exception));
}
Also used : ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) DialogInterface(android.content.DialogInterface) Channel(com.microsoft.azure.mobile.channel.Channel) Activity(android.app.Activity) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) Uri(android.net.Uri) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) ActivityNotFoundException(android.content.ActivityNotFoundException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with HttpClientNetworkStateHandler

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

the class IngestionHttpTest method failedSerialization.

@Test
public void failedSerialization() throws Exception {
    /* Build some payload. */
    LogContainer container = new LogContainer();
    Log log = mock(Log.class);
    long logAbsoluteTime = 123L;
    when(log.getToffset()).thenReturn(logAbsoluteTime);
    List<Log> logs = new ArrayList<>();
    logs.add(log);
    container.setLogs(logs);
    LogSerializer serializer = mock(LogSerializer.class);
    JSONException exception = new JSONException("mock");
    when(serializer.serializeContainer(any(LogContainer.class))).thenThrow(exception);
    /* Stable time. */
    mockStatic(System.class);
    long now = 456L;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Configure mock HTTP. */
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    final ServiceCall call = mock(ServiceCall.class);
    final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
    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;
        }
    });
    /* Test calling code. */
    IngestionHttp ingestionHttp = new IngestionHttp(mock(Context.class), serializer);
    ingestionHttp.setLogUrl("http://mock");
    String appSecret = UUIDUtils.randomUUID().toString();
    UUID installId = UUIDUtils.randomUUID();
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertEquals(call, ingestionHttp.sendAsync(appSecret, installId, container, serviceCallback));
    /* Verify call to http client. */
    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.put(APP_SECRET, appSecret);
    expectedHeaders.put(IngestionHttp.INSTALL_ID, installId.toString());
    verify(httpClient).callAsync(eq("http://mock/logs?api_version=1.0.0-preview20160914"), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
    assertNotNull(callTemplate.get());
    try {
        callTemplate.get().buildRequestBody();
        Assert.fail("Expected json exception");
    } catch (JSONException ignored) {
    }
    /* Verify toffset manipulation. */
    verify(log).setToffset(now - logAbsoluteTime);
    verify(log).setToffset(logAbsoluteTime);
    /* Verify close. */
    ingestionHttp.close();
    verify(httpClient).close();
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with HttpClientNetworkStateHandler

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

the class IngestionHttpTest method getCallTemplate.

private HttpClient.CallTemplate getCallTemplate(String appSecret) throws Exception {
    /* Configure mock HTTP to get an instance of IngestionCallTemplate. */
    final ServiceCall call = mock(ServiceCall.class);
    final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
    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;
        }
    });
    IngestionHttp ingestionHttp = new IngestionHttp(mock(Context.class), mock(LogSerializer.class));
    ingestionHttp.setLogUrl("http://mock");
    assertEquals(call, ingestionHttp.sendAsync(appSecret, UUIDUtils.randomUUID(), mock(LogContainer.class), mock(ServiceCallback.class)));
    return callTemplate.get();
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler)

Example 14 with HttpClientNetworkStateHandler

use of com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler in project mobile-center-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().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(apiToken);
    return callTemplate.get();
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) Channel(com.microsoft.azure.mobile.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NetworkStateHelper(com.microsoft.azure.mobile.utils.NetworkStateHelper) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler)

Example 15 with HttpClientNetworkStateHandler

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

the class IngestionHttpTest method sendAsync.

@Test
public void sendAsync() throws Exception {
    /* Build some payload. */
    LogContainer container = new LogContainer();
    Log log = mock(Log.class);
    long logAbsoluteTime = 123L;
    when(log.getToffset()).thenReturn(logAbsoluteTime);
    List<Log> logs = new ArrayList<>();
    logs.add(log);
    container.setLogs(logs);
    LogSerializer serializer = mock(LogSerializer.class);
    when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
    /* Stable time. */
    mockStatic(System.class);
    long now = 456L;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Configure mock HTTP. */
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    final ServiceCall call = mock(ServiceCall.class);
    final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
    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;
        }
    });
    /* Test calling code. */
    IngestionHttp ingestionHttp = new IngestionHttp(mock(Context.class), serializer);
    ingestionHttp.setLogUrl("http://mock");
    String appSecret = UUIDUtils.randomUUID().toString();
    UUID installId = UUIDUtils.randomUUID();
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertEquals(call, ingestionHttp.sendAsync(appSecret, installId, container, serviceCallback));
    /* Verify call to http client. */
    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.put(APP_SECRET, appSecret);
    expectedHeaders.put(IngestionHttp.INSTALL_ID, installId.toString());
    verify(httpClient).callAsync(eq("http://mock" + IngestionHttp.API_PATH), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
    assertNotNull(callTemplate.get());
    assertEquals("mockPayload", callTemplate.get().buildRequestBody());
    /* Verify toffset manipulation. */
    verify(log).setToffset(now - logAbsoluteTime);
    verify(log).setToffset(logAbsoluteTime);
    /* Verify close. */
    ingestionHttp.close();
    verify(httpClient).close();
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

HttpClientNetworkStateHandler (com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler)30 ServiceCallback (com.microsoft.azure.mobile.http.ServiceCallback)30 Matchers.anyString (org.mockito.Matchers.anyString)29 Channel (com.microsoft.azure.mobile.channel.Channel)25 ServiceCall (com.microsoft.azure.mobile.http.ServiceCall)24 InvocationOnMock (org.mockito.invocation.InvocationOnMock)23 Test (org.junit.Test)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 Activity (android.app.Activity)20 HashMap (java.util.HashMap)18 DialogInterface (android.content.DialogInterface)8 Context (android.content.Context)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 UUID (java.util.UUID)4 Semaphore (java.util.concurrent.Semaphore)4 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)3 JSONException (org.json.JSONException)3 ArgumentMatcher (org.mockito.ArgumentMatcher)3 ActivityNotFoundException (android.content.ActivityNotFoundException)2 Build (android.os.Build)2