Search in sources :

Example 21 with CommonSchemaLog

use of com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog in project mobile-center-sdk-android by Microsoft.

the class PropertyConfiguratorTest method defaultTargetIsNotReturnedFromGetTransmissionTarget.

@Test
public void defaultTargetIsNotReturnedFromGetTransmissionTarget() {
    /* Start the application with a token. */
    Analytics analytics = Analytics.getInstance();
    analytics.onStarting(mAppCenterHandler);
    String defaultToken = "test";
    analytics.onStarted(mock(Context.class), mChannel, null, defaultToken, true);
    /* Create the test target with the same default token. */
    AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget(defaultToken);
    /* Verify it's not the same instance. */
    assertNotSame(target, analytics.mDefaultTransmissionTarget);
    /* Set a Part A property on the target. */
    target.getPropertyConfigurator().setAppName("someName");
    /* Simulate what the pipeline does to convert from App Center to Common Schema. */
    CommonSchemaLog log = new CommonSchemaEventLog();
    log.setExt(new Extensions());
    log.getExt().setApp(new AppExtension());
    log.getExt().setUser(new UserExtension());
    log.addTransmissionTarget("test");
    log.setTag(target);
    /* When the callback is called on the default target. */
    analytics.mDefaultTransmissionTarget.getPropertyConfigurator().onPreparingLog(log, "groupName");
    /* Then the log is not modified. */
    assertNull(log.getExt().getApp().getName());
    /* When the callback is called on the returned target. */
    target.getPropertyConfigurator().onPreparingLog(log, "groupName");
    /* Check the property is added to the log. */
    assertEquals("someName", log.getExt().getApp().getName());
}
Also used : Context(android.content.Context) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) AppExtension(com.microsoft.appcenter.ingestion.models.one.AppExtension) UserExtension(com.microsoft.appcenter.ingestion.models.one.UserExtension) Matchers.anyString(org.mockito.Matchers.anyString) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with CommonSchemaLog

use of com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog in project mobile-center-sdk-android by Microsoft.

the class EventLogFactoryTest method convertEventWithoutProperties.

@Test
@PrepareForTest({ PartAUtils.class, CommonSchemaDataUtils.class })
public void convertEventWithoutProperties() {
    /* Mock utilities. */
    mockStatic(PartAUtils.class);
    mockStatic(CommonSchemaDataUtils.class);
    /* Create event log. */
    EventLog log = new EventLog();
    log.setName("test");
    /* Old properties are ignored. */
    Map<String, String> oldProperties = new HashMap<>();
    oldProperties.put("ignored", "ignored");
    log.setProperties(oldProperties);
    /* Set typed properties. */
    List<TypedProperty> properties = new ArrayList<>();
    StringTypedProperty stringTypedProperty = new StringTypedProperty();
    stringTypedProperty.setName("a");
    stringTypedProperty.setValue("b");
    properties.add(stringTypedProperty);
    log.setTypedProperties(properties);
    /* With 2 targets. */
    log.addTransmissionTarget("t1");
    log.addTransmissionTarget("t2");
    /* And with a tag. */
    Object tag = new Object();
    log.setTag(tag);
    /* When we convert logs. */
    Collection<CommonSchemaLog> convertedLogs = new EventLogFactory().toCommonSchemaLogs(log);
    /* Check number of logs: 1 per target. */
    assertNotNull(convertedLogs);
    assertEquals(2, convertedLogs.size());
    /* For each target. */
    for (CommonSchemaLog commonSchemaLog : convertedLogs) {
        /* Check name was added. */
        verifyStatic();
        PartAUtils.setName(same(commonSchemaLog), eq("test"));
        /* Check tag was added. */
        assertSame(tag, commonSchemaLog.getTag());
    }
    /* Check Part A was added with target tokens. */
    verifyStatic();
    PartAUtils.addPartAFromLog(eq(log), notNull(CommonSchemaLog.class), eq("t1"));
    verifyStatic();
    PartAUtils.addPartAFromLog(eq(log), notNull(CommonSchemaLog.class), eq("t2"));
    /* Check data was added with typed properties (and thus not old ones). */
    verifyStatic(times(2));
    CommonSchemaDataUtils.addCommonSchemaData(eq(properties), notNull(CommonSchemaLog.class));
}
Also used : HashMap(java.util.HashMap) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) ArrayList(java.util.ArrayList) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) CommonSchemaEventLogFactory(com.microsoft.appcenter.analytics.ingestion.models.one.json.CommonSchemaEventLogFactory) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 23 with CommonSchemaLog

use of com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog in project mobile-center-sdk-android by Microsoft.

the class EventLogFactoryTest method dontConvertEventWithoutTargetTokens.

@Test
public void dontConvertEventWithoutTargetTokens() {
    /* Create event log with just a name and no target. */
    EventLog log = new EventLog();
    log.setName("test");
    Collection<CommonSchemaLog> convertedLogs = new CommonSchemaEventLogFactory().toCommonSchemaLogs(log);
    assertNotNull(convertedLogs);
    assertEquals(0, convertedLogs.size());
}
Also used : EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) CommonSchemaEventLogFactory(com.microsoft.appcenter.analytics.ingestion.models.one.json.CommonSchemaEventLogFactory) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with CommonSchemaLog

use of com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTransmissionTargetTest method registerCallbackWhenDisabledWorks.

@Test
public void registerCallbackWhenDisabledWorks() {
    /* Simulate disabling and background thread. */
    Analytics analytics = Analytics.getInstance();
    AppCenterHandler handler = mock(AppCenterHandler.class);
    ArgumentCaptor<Runnable> backgroundRunnable = ArgumentCaptor.forClass(Runnable.class);
    ArgumentCaptor<Runnable> disabledRunnable = ArgumentCaptor.forClass(Runnable.class);
    doNothing().when(handler).post(backgroundRunnable.capture(), disabledRunnable.capture());
    analytics.onStarting(handler);
    analytics.onStarted(mock(Context.class), mChannel, null, "test", true);
    /* Disable. */
    Analytics.setEnabled(false);
    backgroundRunnable.getValue().run();
    /* Add authentication provider while disabled. */
    AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class);
    AuthenticationProvider authenticationProvider = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider));
    AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider);
    /* Unlock command. */
    disabledRunnable.getValue().run();
    /* Verify update while disabled. */
    assertEquals(authenticationProvider, AnalyticsTransmissionTarget.sAuthenticationProvider);
    verify(authenticationProvider).acquireTokenAsync();
    /* Enable. */
    Analytics.setEnabled(true);
    disabledRunnable.getValue().run();
    /* Call prepare log. */
    ProtocolExtension protocol = new ProtocolExtension();
    Extensions ext = new Extensions();
    ext.setProtocol(protocol);
    CommonSchemaLog log = new CommonSchemaEventLog();
    log.setExt(ext);
    AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
    /* Verify log. */
    assertEquals(Collections.singletonList(authenticationProvider.getTicketKeyHash()), protocol.getTicketKeys());
    /* And that we check expiry. */
    verify(authenticationProvider).checkTokenExpiry();
}
Also used : Context(android.content.Context) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) Test(org.junit.Test)

Example 25 with CommonSchemaLog

use of com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog in project mobile-center-sdk-android by Microsoft.

the class DatabasePersistenceAndroidTest method getLogsFilteringOutPausedTargetKeys.

@Test
public void getLogsFilteringOutPausedTargetKeys() throws PersistenceException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence(sContext);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MockCommonSchemaLog.TYPE, new MockCommonSchemaLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Test constants. */
        int numberOfLogsPerKey = 10;
        /* Generate and persist some logs with a first iKey. */
        String pausedKey1 = "1";
        generateCsLogsWithIKey(persistence, pausedKey1, numberOfLogsPerKey);
        /* Generate more logs with another iKey to exclude. */
        String pausedKey2 = "2";
        generateCsLogsWithIKey(persistence, pausedKey2, numberOfLogsPerKey);
        /* Generate logs from a third key. */
        String resumedKey = "3";
        generateCsLogsWithIKey(persistence, resumedKey, numberOfLogsPerKey);
        /* Get logs without disabled keys. */
        List<Log> outLogs = new ArrayList<>();
        int limit = numberOfLogsPerKey * 3;
        String batchId = persistence.getLogs("test", Arrays.asList(pausedKey1, pausedKey2), limit, outLogs);
        assertNotNull(batchId);
        /* Verify we get a subset of logs without the disabled keys. */
        assertEquals(numberOfLogsPerKey, outLogs.size());
        assertEquals(limit, persistence.countLogs("test"));
        for (Log log : outLogs) {
            assertTrue(log instanceof CommonSchemaLog);
            assertEquals(resumedKey, ((CommonSchemaLog) log).getIKey());
        }
        /* Calling a second time should return nothing since the batch is in progress. */
        outLogs.clear();
        batchId = persistence.getLogs("test", Arrays.asList(pausedKey1, pausedKey2), limit, outLogs);
        assertNull(batchId);
        assertEquals(0, outLogs.size());
        /* If we try to get a second batch without filtering, we should get all disabled logs. */
        outLogs.clear();
        batchId = persistence.getLogs("test", Collections.<String>emptyList(), limit, outLogs);
        assertNotNull(batchId);
        assertEquals(numberOfLogsPerKey * 2, outLogs.size());
        for (Log log : outLogs) {
            assertTrue(log instanceof CommonSchemaLog);
            assertNotEquals(resumedKey, ((CommonSchemaLog) log).getIKey());
        }
    } finally {
        persistence.close();
    }
}
Also used : CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) MockLog(com.microsoft.appcenter.ingestion.models.json.MockLog) MockCommonSchemaLogFactory(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLogFactory) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Mockito.anyString(org.mockito.Mockito.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) SuppressLint(android.annotation.SuppressLint) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Aggregations

CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)32 Test (org.junit.Test)24 Extensions (com.microsoft.appcenter.ingestion.models.one.Extensions)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)16 Log (com.microsoft.appcenter.ingestion.models.Log)10 UserExtension (com.microsoft.appcenter.ingestion.models.one.UserExtension)10 AppExtension (com.microsoft.appcenter.ingestion.models.one.AppExtension)9 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)8 ProtocolExtension (com.microsoft.appcenter.ingestion.models.one.ProtocolExtension)7 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)7 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)6 ArrayList (java.util.ArrayList)6 Context (android.content.Context)5 MockCommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog)5 Matchers.anyString (org.mockito.Matchers.anyString)5 AppCenterHandler (com.microsoft.appcenter.AppCenterHandler)4 ServiceCall (com.microsoft.appcenter.http.ServiceCall)4 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)4 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)4