@Override public void read(JSONObject object) throws JSONException { /* Override abstract log JSON since it's Common Schema and not App Center schema. */ /* Read top level PART A simple fields. */ setVer(object.getString(VER)); setName(object.getString(NAME)); setTimestamp(JSONDateUtils.toDate(object.getString(TIME))); if (object.has(POP_SAMPLE)) { setPopSample(object.getDouble(POP_SAMPLE)); } setIKey(object.optString(IKEY, null)); setFlags(JSONUtils.readLong(object, FLAGS)); setCV(object.optString(CV, null)); /* Read extensions. */ if (object.has(EXT)) { Extensions extensions = new Extensions(); extensions.read(object.getJSONObject(EXT)); setExt(extensions); } /* Read Parts B&C. */ if (object.has(DATA)) { Data data = new Data(); data.read(object.getJSONObject(DATA)); setData(data); } }
@Override public Object answer(InvocationOnMock invocation) { ProtocolExtension protocol = new ProtocolExtension(); Extensions ext = new Extensions(); ext.setProtocol(protocol); CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(ext); sentLogs.add(log); /* Call the listener after conversion of common schema for authentication decoration. */ AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test"); return null; } }).when(mChannel).enqueue(any(Log.class), anyString(), anyInt());
dest.setExt(new Extensions());
ext.setProtocol(protocol); CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(ext); AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
log.setExt(new Extensions() {{ setProtocol(protocol); }});
@Test public void collectDeviceId() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setDevice(new DeviceExtension()); /* Mock context. */ mockStatic(Secure.class); when(Secure.getString(any(ContentResolver.class), anyString())).thenReturn("mockDeviceId"); /* Get property configurator and collect device ID. */ PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator(); pc.collectDeviceId(); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log.addTransmissionTarget("test"); log.setTag(Analytics.getTransmissionTarget("test")); pc.onPreparingLog(log, "groupName"); /* Assert device ID is collected. */ assertEquals("a:mockDeviceId", log.getExt().getDevice().getLocalId()); }
@Test public void collectDeviceIdSavedWhenDisabled() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setDevice(new DeviceExtension()); /* Mock context. */ mockStatic(Secure.class); when(Secure.getString(any(ContentResolver.class), anyString())).thenReturn("mockDeviceId"); /* Disable Analytics. */ Analytics.setEnabled(false).get(); /* Get property configurator and collect device ID. */ PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator(); pc.collectDeviceId(); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log.addTransmissionTarget("test"); log.setTag(Analytics.getTransmissionTarget("test")); /* Enable and simulate log preparing. */ Analytics.setEnabled(true).get(); pc.onPreparingLog(log, "groupName"); /* Assert device ID is collected. */ assertEquals("a:mockDeviceId", log.getExt().getDevice().getLocalId()); }
Extensions ext1 = new Extensions(); ext1.setSdk(new SdkExtension()); log1.setExt(ext1); Extensions ext2 = new Extensions(); ext2.setSdk(new SdkExtension()); log2.setExt(ext2); Extensions ext3 = new Extensions(); ext3.setSdk(new SdkExtension()); log3.setExt(ext3); when(logSerializer.toCommonSchemaLog(any(Log.class))).thenReturn(Collections.singletonList(log3)); listener.onPreparedLog(originalLog, TEST_GROUP, PERSISTENCE_CRITICAL); Extensions ext4 = new Extensions(); ext4.setSdk(new SdkExtension()); log4.setExt(ext4); when(logSerializer.toCommonSchemaLog(any(Log.class))).thenReturn(Collections.singletonList(log4)); listener.onPreparedLog(originalLog, TEST_GROUP, PERSISTENCE_NORMAL);
@Test public void setNonPrefixUserId() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setUser(new UserExtension()); /* Get property configurator and set properties. */ PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator(); pc.setUserId("bob"); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log.addTransmissionTarget("test"); log.setTag(Analytics.getTransmissionTarget("test")); pc.onPreparingLog(log, "groupName"); /* Check prefix was added. */ assertEquals("c:bob", log.getExt().getUser().getLocalId()); }
logBeforeSetProperty.setExt(new Extensions()); logBeforeSetProperty.getExt().setApp(new AppExtension()); logBeforeSetProperty.getExt().setUser(new UserExtension()); logAfterSetProperty.setExt(new Extensions()); logAfterSetProperty.getExt().setApp(new AppExtension()); logAfterSetProperty.getExt().setUser(new UserExtension());
dest.setExt(new Extensions());
@Test public void unsetUserId() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); /* Get property configurator and set properties. */ PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator(); pc.setUserId("c:bob"); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log.addTransmissionTarget("test"); log.setTag(Analytics.getTransmissionTarget("test")); pc.onPreparingLog(log, "groupName"); /* Assert properties set on common schema. */ assertEquals("c:bob", log.getExt().getUser().getLocalId()); /* Create second log. */ CommonSchemaLog log2 = new CommonSchemaEventLog(); log2.setExt(new Extensions()); log2.getExt().setApp(new AppExtension()); log2.getExt().setUser(new UserExtension()); /* Un-set user ID. */ pc.setUserId(null); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log2.addTransmissionTarget("test"); log2.setTag(Analytics.getTransmissionTarget("test")); pc.onPreparingLog(log2, "groupName"); /* Assert properties set on common schema. */ assertNull(log2.getExt().getUser().getLocalId()); }
@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()); }
@Test public void setInvalidUserId() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); /* Get property configurator and set properties. */ PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator(); pc.setUserId("x:bob"); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log.addTransmissionTarget("test"); log.setTag(Analytics.getTransmissionTarget("test")); pc.onPreparingLog(log, "groupName"); /* Assert property not set on common schema. */ assertNull(log.getExt().getUser().getLocalId()); /* Set user id with just the prefix. */ pc.setUserId("c:"); /* Assert property not set on common schema. */ assertNull(log.getExt().getUser().getLocalId()); /* Set empty user id. */ pc.setUserId(""); /* Assert property not set on common schema. */ assertNull(log.getExt().getUser().getLocalId()); }
@Test public void setCommonSchemaProperties() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); /* Get property configurator and set properties. */ PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator(); pc.setAppVersion("appVersion"); pc.setAppName("appName"); pc.setAppLocale("appLocale"); pc.setUserId("c:bob"); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log.addTransmissionTarget("test"); log.setTag(Analytics.getTransmissionTarget("test")); pc.onPreparingLog(log, "groupName"); /* Assert properties set on common schema. */ assertEquals("appVersion", log.getExt().getApp().getVer()); assertEquals("appName", log.getExt().getApp().getName()); assertEquals("appLocale", log.getExt().getApp().getLocale()); assertEquals("c:bob", log.getExt().getUser().getLocalId()); }
@Test public void grandparentsHaveNoPropertiesSet() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); /* Set up empty chain of parents. */ AnalyticsTransmissionTarget grandparent = Analytics.getTransmissionTarget("grandparent"); AnalyticsTransmissionTarget parent = grandparent.getTransmissionTarget("parent"); AnalyticsTransmissionTarget child = parent.getTransmissionTarget("child"); /* Simulate channel callbacks. */ log.addTransmissionTarget("child"); log.setTag(child); grandparent.getPropertyConfigurator().onPreparingLog(log, "groupName"); parent.getPropertyConfigurator().onPreparingLog(log, "groupName"); child.getPropertyConfigurator().onPreparingLog(log, "groupName"); /* Assert properties not set on common schema for child. */ assertNull(log.getExt().getApp().getVer()); assertNull(log.getExt().getApp().getName()); assertNull(log.getExt().getApp().getLocale()); assertNull(log.getExt().getUser().getLocalId()); }
@Test public void inheritCommonSchemaPropertiesFromGrandparent() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); /* Set properties on parent to override unset properties on child */ AnalyticsTransmissionTarget grandparent = Analytics.getTransmissionTarget("grandparent"); grandparent.getPropertyConfigurator().setAppVersion("appVersion"); grandparent.getPropertyConfigurator().setAppName("appName"); grandparent.getPropertyConfigurator().setAppLocale("appLocale"); grandparent.getPropertyConfigurator().setUserId("c:alice"); /* Set up hierarchy. */ AnalyticsTransmissionTarget parent = grandparent.getTransmissionTarget("parent"); AnalyticsTransmissionTarget child = parent.getTransmissionTarget("child"); /* Simulate channel callbacks. */ log.addTransmissionTarget("child"); log.setTag(child); grandparent.getPropertyConfigurator().onPreparingLog(log, "groupName"); parent.getPropertyConfigurator().onPreparingLog(log, "groupName"); child.getPropertyConfigurator().onPreparingLog(log, "groupName"); /* Assert properties set on common schema. */ assertEquals("appVersion", log.getExt().getApp().getVer()); assertEquals("appName", log.getExt().getApp().getName()); assertEquals("appLocale", log.getExt().getApp().getLocale()); assertEquals("c:alice", log.getExt().getUser().getLocalId()); }
@Test public void checkGrandParentNotOverriddenByDescendants() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); /* Set up hierarchy. */ AnalyticsTransmissionTarget grandparent = Analytics.getTransmissionTarget("grandparent"); AnalyticsTransmissionTarget parent = grandparent.getTransmissionTarget("parent"); AnalyticsTransmissionTarget child = parent.getTransmissionTarget("child"); /* Set properties on parent to override unset properties on child (but not grandparent). */ parent.getPropertyConfigurator().setAppVersion("appVersion"); parent.getPropertyConfigurator().setAppName("appName"); parent.getPropertyConfigurator().setAppLocale("appLocale"); parent.getPropertyConfigurator().setUserId("c:bob"); /* Also set 1 on child. */ child.getPropertyConfigurator().setAppName("childName"); /* Simulate channel callbacks. */ log.addTransmissionTarget("grandParent"); log.setTag(grandparent); grandparent.getPropertyConfigurator().onPreparingLog(log, "groupName"); parent.getPropertyConfigurator().onPreparingLog(log, "groupName"); child.getPropertyConfigurator().onPreparingLog(log, "groupName"); /* Assert properties not set on common schema for grandparent. */ assertNull(log.getExt().getApp().getVer()); assertNull(log.getExt().getApp().getName()); assertNull(log.getExt().getApp().getLocale()); assertNull(log.getExt().getUser().getLocalId()); }
@Test public void commonSchemaPropertiesNotSetWhenDisabled() { CommonSchemaLog log = new CommonSchemaEventLog(); log.setExt(new Extensions()); log.getExt().setApp(new AppExtension()); log.getExt().setUser(new UserExtension()); /* Get target, disable it, and set properties. */ AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("test"); target.setEnabledAsync(false).get(); target.getPropertyConfigurator().setAppVersion("appVersion"); target.getPropertyConfigurator().setAppName("appName"); target.getPropertyConfigurator().setAppLocale("appLocale"); target.getPropertyConfigurator().setUserId("c:alice"); /* Simulate what the pipeline does to convert from App Center to Common Schema. */ log.addTransmissionTarget("test"); log.setTag(target); target.getPropertyConfigurator().onPreparingLog(log, "groupName"); /* Assert properties are null. */ assertNull(log.getExt().getApp().getVer()); assertNull(log.getExt().getApp().getName()); assertNull(log.getExt().getApp().getLocale()); assertNull(log.getExt().getUser().getLocalId()); /* The properties are not applied but are saved, if we enable now we can see the values. */ target.setEnabledAsync(true).get(); target.getPropertyConfigurator().onPreparingLog(log, "groupName"); assertEquals("appVersion", log.getExt().getApp().getVer()); assertEquals("appName", log.getExt().getApp().getName()); assertEquals("appLocale", log.getExt().getApp().getLocale()); assertEquals("c:alice", log.getExt().getUser().getLocalId()); }