Search in sources :

Example 81 with ServiceCall

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

the class OneCollectorIngestionTest method failedSerialization.

@Test
public void failedSerialization() throws Exception {
    /* Build some payload. */
    final CommonSchemaLog log = mock(CommonSchemaLog.class);
    when(log.getExt()).thenReturn(new Extensions() {

        {
            setProtocol(new ProtocolExtension());
        }
    });
    LogContainer container = new LogContainer() {

        {
            setLogs(new ArrayList<Log>() {

                {
                    add(log);
                }
            });
        }
    };
    LogSerializer serializer = mock(LogSerializer.class);
    JSONException exception = new JSONException("mock");
    when(serializer.serializeLog(log)).thenThrow(exception);
    /* Configure mock HTTP. */
    ServiceCall call = mock(ServiceCall.class);
    ArgumentCaptor<HttpClient.CallTemplate> callTemplate = ArgumentCaptor.forClass(HttpClient.CallTemplate.class);
    when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), callTemplate.capture(), any(ServiceCallback.class))).thenReturn(call);
    /* Test calling code. */
    OneCollectorIngestion ingestion = new OneCollectorIngestion(mHttpClient, serializer);
    ingestion.setLogUrl("http://mock");
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertEquals(call, ingestion.sendAsync(null, null, container, serviceCallback));
    /* Verify call to http client. */
    assertNotNull(callTemplate.getValue());
    try {
        callTemplate.getValue().buildRequestBody();
        Assert.fail("Expected json exception");
    } catch (JSONException ignored) {
    }
    /* Verify close. */
    ingestion.close();
    verify(mHttpClient).close();
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) JSONException(org.json.JSONException) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) HttpClient(com.microsoft.appcenter.http.HttpClient) DefaultHttpClient(com.microsoft.appcenter.http.DefaultHttpClient) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 82 with ServiceCall

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

the class OneCollectorIngestionTest method sendAsync.

@Test
public void sendAsync() throws Exception {
    /* Mock time. */
    mockStatic(System.class);
    when(System.currentTimeMillis()).thenReturn(1234L);
    /* Build some payload. */
    Extensions ext = new Extensions() {

        {
            setProtocol(new ProtocolExtension());
        }
    };
    final CommonSchemaLog log1 = mock(CommonSchemaLog.class);
    when(log1.getExt()).thenReturn(ext);
    when(log1.getTransmissionTargetTokens()).thenReturn(Collections.singleton("token1"));
    final CommonSchemaLog log2 = mock(CommonSchemaLog.class);
    when(log2.getExt()).thenReturn(ext);
    when(log2.getTransmissionTargetTokens()).thenReturn(new HashSet<>(Arrays.asList("token2", "token3")));
    LogContainer container = new LogContainer() {

        {
            setLogs(new ArrayList<Log>() {

                {
                    add(log1);
                    add(log2);
                }
            });
        }
    };
    LogSerializer serializer = mock(LogSerializer.class);
    when(serializer.serializeLog(log1)).thenReturn("mockPayload1");
    when(serializer.serializeLog(log2)).thenReturn("mockPayload2");
    /* Configure mock HTTP. */
    ServiceCall call = mock(ServiceCall.class);
    ArgumentCaptor<HttpClient.CallTemplate> callTemplate = ArgumentCaptor.forClass(HttpClient.CallTemplate.class);
    when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), callTemplate.capture(), any(ServiceCallback.class))).thenReturn(call);
    /* Test calling code. */
    OneCollectorIngestion ingestion = new OneCollectorIngestion(mHttpClient, serializer);
    ingestion.setLogUrl("http://mock");
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertEquals(call, ingestion.sendAsync(null, null, container, serviceCallback));
    /* Verify call to http client. */
    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.put(OneCollectorIngestion.API_KEY, "token1,token2,token3");
    expectedHeaders.put(OneCollectorIngestion.CLIENT_VERSION_KEY, String.format("ACS-Android-Java-no-%s-no", VERSION_NAME));
    expectedHeaders.put(OneCollectorIngestion.UPLOAD_TIME_KEY, "1234");
    expectedHeaders.put(DefaultHttpClient.CONTENT_TYPE_KEY, "application/x-json-stream; charset=utf-8");
    verify(mHttpClient).callAsync(eq("http://mock"), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
    assertNotNull(callTemplate.getValue());
    assertEquals("mockPayload1\nmockPayload2\n", callTemplate.getValue().buildRequestBody());
    /* Verify close. */
    ingestion.close();
    verify(mHttpClient).close();
    /* Verify reopen. */
    ingestion.reopen();
    verify(mHttpClient).reopen();
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) HashMap(java.util.HashMap) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) HttpClient(com.microsoft.appcenter.http.HttpClient) DefaultHttpClient(com.microsoft.appcenter.http.DefaultHttpClient) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 83 with ServiceCall

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

the class OneCollectorIngestionTest method ticketsFailToSerialize.

@Test
public void ticketsFailToSerialize() throws Exception {
    /* Build some payload. */
    final CommonSchemaLog log1 = mock(CommonSchemaLog.class);
    final List<String> ticketKeys = new ArrayList<String>() {

        {
            add("key1");
        }
    };
    TicketCache.putTicket("key1", "value1");
    Extensions ext1 = new Extensions() {

        {
            setProtocol(new ProtocolExtension() {

                {
                    setTicketKeys(ticketKeys);
                }
            });
        }
    };
    when(log1.getExt()).thenReturn(ext1);
    LogContainer container = new LogContainer() {

        {
            setLogs(new ArrayList<Log>() {

                {
                    add(log1);
                }
            });
        }
    };
    JSONObject ticketJson = mock(JSONObject.class);
    whenNew(JSONObject.class).withNoArguments().thenReturn(ticketJson);
    when(ticketJson.put(anyString(), anyString())).thenThrow(new JSONException("mock"));
    /* Configure mock HTTP. */
    ServiceCall call = mock(ServiceCall.class);
    when(mHttpClient.callAsync(anyString(), anyString(), mHeadersCaptor.capture(), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenReturn(call);
    /* Verify call to http client. */
    LogSerializer serializer = mock(LogSerializer.class);
    OneCollectorIngestion ingestion = new OneCollectorIngestion(mHttpClient, serializer);
    ingestion.setLogUrl("http://mock");
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertEquals(call, ingestion.sendAsync(null, null, container, serviceCallback));
    /* Verify call to http client was made without headers as JSON failed. */
    Map<String, String> headers = mHeadersCaptor.getValue();
    assertFalse(headers.containsKey(TICKETS));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) Matchers.anyString(org.mockito.Matchers.anyString) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) JSONObject(org.json.JSONObject) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 84 with ServiceCall

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

the class OneCollectorIngestionTest method passTickets.

private Map<String, String> passTickets() {
    /* Build some payload. */
    final CommonSchemaLog log1 = mock(CommonSchemaLog.class);
    final CommonSchemaLog log2 = mock(CommonSchemaLog.class);
    final List<String> ticketKeys = new ArrayList<String>() {

        {
            add("key1");
            add("key2");
            add(null);
        }
    };
    TicketCache.putTicket("key2", "value2");
    Extensions ext1 = new Extensions() {

        {
            setProtocol(new ProtocolExtension() {

                {
                    setTicketKeys(ticketKeys);
                }
            });
        }
    };
    Extensions ext2 = new Extensions() {

        {
            setProtocol(new ProtocolExtension());
        }
    };
    when(log1.getExt()).thenReturn(ext1);
    when(log2.getExt()).thenReturn(ext2);
    LogContainer container = new LogContainer() {

        {
            setLogs(new ArrayList<Log>() {

                {
                    add(log1);
                    add(log2);
                }
            });
        }
    };
    /* Configure mock HTTP. */
    ServiceCall call = mock(ServiceCall.class);
    when(mHttpClient.callAsync(anyString(), anyString(), mHeadersCaptor.capture(), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenReturn(call);
    /* Verify call to http client. */
    LogSerializer serializer = mock(LogSerializer.class);
    OneCollectorIngestion ingestion = new OneCollectorIngestion(mHttpClient, serializer);
    ingestion.setLogUrl("http://mock");
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertEquals(call, ingestion.sendAsync(null, null, container, serviceCallback));
    /* Verify call to http client. */
    Map<String, String> headers = mHeadersCaptor.getValue();
    assertTrue(headers.containsKey(TICKETS));
    assertEquals("{\"key2\":\"value2\"}", headers.get(TICKETS));
    return headers;
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer)

Example 85 with ServiceCall

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

the class DistributeBeforeDownloadTest method releaseNotes.

@Test
public void releaseNotes() throws Exception {
    /* Mock we already have redirection parameters. */
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
    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"));
            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. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* Verify dialog. */
    verify(mDialogBuilder, never()).setNeutralButton(eq(R.string.appcenter_distribute_update_dialog_view_release_notes), any(DialogInterface.OnClickListener.class));
    verify(mDialog).show();
    reset(mDialog);
    /* 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.appcenter_distribute_update_dialog_view_release_notes), any(DialogInterface.OnClickListener.class));
    verify(mDialog).show();
    reset(mDialog);
    /* 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);
    /* Empty release notes and URL. */
    when(releaseDetails.getReleaseNotes()).thenReturn("");
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mDialogBuilder, never()).setNeutralButton(eq(R.string.appcenter_distribute_update_dialog_view_release_notes), any(DialogInterface.OnClickListener.class));
    verify(mDialog).show();
    reset(mDialog);
    /* Release notes and URL. */
    when(releaseDetails.getReleaseNotes()).thenReturn("Fix a bug");
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mActivity);
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder).setNeutralButton(eq(R.string.appcenter_distribute_update_dialog_view_release_notes), clickListener.capture());
    verify(mDialog).show();
    reset(mDialog);
    /* 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.appcenter_distribute_update_dialog_view_release_notes), clickListener.capture());
    verify(mDialog).show();
    /* Do the same test and simulate failed navigation. */
    mockStatic(AppCenterLog.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();
    AppCenterLog.error(anyString(), anyString(), eq(exception));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) DialogInterface(android.content.DialogInterface) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Activity(android.app.Activity) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) Uri(android.net.Uri) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) ActivityNotFoundException(android.content.ActivityNotFoundException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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