Search in sources :

Example 6 with ProtocolExtension

use of com.microsoft.appcenter.ingestion.models.one.ProtocolExtension in project mobile-center-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 7 with ProtocolExtension

use of com.microsoft.appcenter.ingestion.models.one.ProtocolExtension in project mobile-center-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)

Aggregations

CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)7 Extensions (com.microsoft.appcenter.ingestion.models.one.Extensions)7 ProtocolExtension (com.microsoft.appcenter.ingestion.models.one.ProtocolExtension)7 Log (com.microsoft.appcenter.ingestion.models.Log)6 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)6 Test (org.junit.Test)6 ServiceCall (com.microsoft.appcenter.http.ServiceCall)4 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)4 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)4 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)4 Matchers.anyString (org.mockito.Matchers.anyString)4 Context (android.content.Context)3 AppCenterHandler (com.microsoft.appcenter.AppCenterHandler)3 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)3 ArrayList (java.util.ArrayList)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)2 DefaultHttpClient (com.microsoft.appcenter.http.DefaultHttpClient)2 HttpClient (com.microsoft.appcenter.http.HttpClient)2 JSONException (org.json.JSONException)2