diff --git a/SensorsAnalyticsSDK/pom.xml b/SensorsAnalyticsSDK/pom.xml index a8dc8de..2e052b0 100644 --- a/SensorsAnalyticsSDK/pom.xml +++ b/SensorsAnalyticsSDK/pom.xml @@ -15,7 +15,7 @@ com.sensorsdata.analytics.javasdk SensorsAnalyticsSDK SensorsAnalyticsSDK - 3.4.3 + 3.4.4 The official Java SDK of Sensors Analytics http://sensorsdata.cn @@ -37,7 +37,7 @@ utf-8 1.7 4.5.13 - 2.9.10.7 + 2.9.10.8 1.18.20 4.11 1.7.25 diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalytics.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalytics.java index 546eee4..6550cf2 100755 --- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalytics.java +++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalytics.java @@ -45,8 +45,6 @@ public class SensorsAnalytics implements ISensorsAnalytics { private static final String DISTINCT_ID = "distinct_id"; private static final String EVENT_NAME = "event name"; private static final String ORIGINAL_DISTINCT_ID = "Original Distinct Id"; - private static final String ITEM_TYPE = "Item Type"; - private static final String ITEM_ID = "Item Id"; public SensorsAnalytics(final Consumer consumer) { worker = new SensorsAnalyticsWorker(consumer); @@ -100,6 +98,20 @@ public void profileIncrement(@NonNull UserRecord userRecord) throws InvalidArgum @Override public void profileUnset(@NonNull UserRecord userRecord) throws InvalidArgumentException { + if (userRecord.getPropertyMap() == null) { + return; + } + for (Map.Entry property : userRecord.getPropertyMap().entrySet()) { + if (!PROJECT_SYSTEM_ATTR.equals(property.getKey())) { + if (property.getValue() instanceof Boolean) { + boolean value = (Boolean) property.getValue(); + if (value) { + continue; + } + } + throw new InvalidArgumentException("The property value of " + property.getKey() + " should be true."); + } + } dealProfile(userRecord, PROFILE_UNSET_ACTION_TYPE); } @@ -147,7 +159,7 @@ public void trackSignUp(@NonNull String loginId, @NonNull String anonymousId, Ma @Override public void profileSet(@NonNull String distinctId, @NonNull boolean isLoginId, Map properties) throws InvalidArgumentException { - addEvent(distinctId, isLoginId, null, PROFILE_SET_ACTION_TYPE, null, properties); + dealProfile(distinctId, isLoginId, properties, PROFILE_SET_ACTION_TYPE); } @Override @@ -155,13 +167,13 @@ public void profileSet(@NonNull String distinctId, @NonNull boolean isLoginId, @ @NonNull Object value) throws InvalidArgumentException { Map properties = new HashMap<>(); properties.put(property, value); - addEvent(distinctId, isLoginId, null, PROFILE_SET_ACTION_TYPE, null, properties); + dealProfile(distinctId, isLoginId, properties, PROFILE_SET_ACTION_TYPE); } @Override public void profileSetOnce(@NonNull String distinctId, @NonNull boolean isLoginId, Map properties) throws InvalidArgumentException { - addEvent(distinctId, isLoginId, null, PROFILE_SET_ONCE_ACTION_TYPE, null, properties); + dealProfile(distinctId, isLoginId, properties, PROFILE_SET_ONCE_ACTION_TYPE); } @Override @@ -169,13 +181,13 @@ public void profileSetOnce(@NonNull String distinctId, @NonNull boolean isLoginI @NonNull Object value) throws InvalidArgumentException { Map properties = new HashMap<>(); properties.put(property, value); - addEvent(distinctId, isLoginId, null, PROFILE_SET_ONCE_ACTION_TYPE, null, properties); + dealProfile(distinctId, isLoginId, properties, PROFILE_SET_ONCE_ACTION_TYPE); } @Override public void profileIncrement(@NonNull String distinctId, @NonNull boolean isLoginId, Map properties) throws InvalidArgumentException { - addEvent(distinctId, isLoginId, null, PROFILE_INCREMENT_ACTION_TYPE, null, properties); + dealProfile(distinctId, isLoginId, properties, PROFILE_INCREMENT_ACTION_TYPE); } @Override @@ -183,13 +195,13 @@ public void profileIncrement(@NonNull String distinctId, @NonNull boolean isLogi @NonNull long value) throws InvalidArgumentException { Map properties = new HashMap<>(); properties.put(property, value); - addEvent(distinctId, isLoginId, null, PROFILE_INCREMENT_ACTION_TYPE, null, properties); + dealProfile(distinctId, isLoginId, properties, PROFILE_INCREMENT_ACTION_TYPE); } @Override public void profileAppend(@NonNull String distinctId, @NonNull boolean isLoginId, Map properties) throws InvalidArgumentException { - addEvent(distinctId, isLoginId, null, PROFILE_APPEND_ACTION_TYPE, null, properties); + dealProfile(distinctId, isLoginId, properties, PROFILE_APPEND_ACTION_TYPE); } @Override @@ -199,7 +211,7 @@ public void profileAppend(@NonNull String distinctId, @NonNull boolean isLoginId values.add(value); Map properties = new HashMap<>(); properties.put(property, values); - addEvent(distinctId, isLoginId, null, PROFILE_APPEND_ACTION_TYPE, null, properties); + dealProfile(distinctId, isLoginId, properties, PROFILE_APPEND_ACTION_TYPE); } @Override @@ -207,7 +219,7 @@ public void profileUnset(@NonNull String distinctId, @NonNull boolean isLoginId, throws InvalidArgumentException { Map properties = new HashMap<>(); properties.put(property, true); - addEvent(distinctId, isLoginId, null, PROFILE_UNSET_ACTION_TYPE, null, properties); + dealProfile(distinctId, isLoginId, properties, PROFILE_UNSET_ACTION_TYPE); } @Override @@ -227,12 +239,12 @@ public void profileUnset(@NonNull String distinctId, @NonNull boolean isLoginId, throw new InvalidArgumentException("The property value of " + property.getKey() + " should be true."); } } - addEvent(distinctId, isLoginId, null, PROFILE_UNSET_ACTION_TYPE, null, properties); + dealProfile(distinctId, isLoginId, properties, PROFILE_UNSET_ACTION_TYPE); } @Override public void profileDelete(@NonNull String distinctId, @NonNull boolean isLoginId) throws InvalidArgumentException { - addEvent(distinctId, isLoginId, null, PROFILE_DELETE_ACTION_TYPE, null, null); + dealProfile(distinctId, isLoginId, null, PROFILE_DELETE_ACTION_TYPE); } @Override @@ -256,13 +268,12 @@ public void bind(@NonNull SensorsAnalyticsIdentity... identities) throws Invalid if (identityMap.size() < 2) { throw new InvalidArgumentException("The identities is invalid,you should have at least two identities."); } - assertIdentityMap(BIND_ID_ACTION_TYPE, identityMap); - worker.doAddEventIdentity(identityMap, BIND_ID_ACTION_TYPE, BIND_ID, null); + addEventIdentity(identityMap, BIND_ID_ACTION_TYPE, BIND_ID, null); } @Override public void unbind(@NonNull SensorsAnalyticsIdentity analyticsIdentity) throws InvalidArgumentException { - addEventIdentity(analyticsIdentity, UNBIND_ID_ACTION_TYPE, UNBIND_ID, null); + addEventIdentity(analyticsIdentity.getIdentityMap(), UNBIND_ID_ACTION_TYPE, UNBIND_ID, null); } @Override @@ -279,7 +290,7 @@ public void trackById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, @NonN @Override public void profileSetById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, Map properties) throws InvalidArgumentException { - addEventIdentity(analyticsIdentity, PROFILE_SET_ACTION_TYPE, null, properties); + addProfileIdentity(analyticsIdentity, PROFILE_SET_ACTION_TYPE, properties); } @Override @@ -287,13 +298,13 @@ public void profileSetById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, @NonNull Object value) throws InvalidArgumentException { Map properties = new HashMap<>(); properties.put(property, value); - addEventIdentity(analyticsIdentity, PROFILE_SET_ACTION_TYPE, null, properties); + addProfileIdentity(analyticsIdentity, PROFILE_SET_ACTION_TYPE, properties); } @Override public void profileSetOnceById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, Map properties) throws InvalidArgumentException { - addEventIdentity(analyticsIdentity, PROFILE_SET_ONCE_ACTION_TYPE, null, properties); + addProfileIdentity(analyticsIdentity, PROFILE_SET_ONCE_ACTION_TYPE, properties); } @Override @@ -301,13 +312,13 @@ public void profileSetOnceById(@NonNull SensorsAnalyticsIdentity analyticsIdenti @NonNull Object value) throws InvalidArgumentException { Map properties = new HashMap<>(); properties.put(property, value); - addEventIdentity(analyticsIdentity, PROFILE_SET_ONCE_ACTION_TYPE, null, properties); + addProfileIdentity(analyticsIdentity, PROFILE_SET_ONCE_ACTION_TYPE, properties); } @Override public void profileIncrementById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, Map properties) throws InvalidArgumentException { - addEventIdentity(analyticsIdentity, PROFILE_INCREMENT_ACTION_TYPE, null, properties); + addProfileIdentity(analyticsIdentity, PROFILE_INCREMENT_ACTION_TYPE, properties); } @Override @@ -315,23 +326,23 @@ public void profileIncrementById(@NonNull SensorsAnalyticsIdentity analyticsIden throws InvalidArgumentException { Map properties = new HashMap<>(); properties.put(property, value); - addEventIdentity(analyticsIdentity, PROFILE_INCREMENT_ACTION_TYPE, null, properties); + addProfileIdentity(analyticsIdentity, PROFILE_INCREMENT_ACTION_TYPE, properties); } @Override public void profileAppendById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, Map properties) throws InvalidArgumentException { - addEventIdentity(analyticsIdentity, PROFILE_APPEND_ACTION_TYPE, null, properties); + addProfileIdentity(analyticsIdentity, PROFILE_APPEND_ACTION_TYPE, properties); } @Override public void profileAppendById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, @NonNull String property, @NonNull String value) throws InvalidArgumentException { - List values = new ArrayList(); + List values = new ArrayList<>(); values.add(value); Map properties = new HashMap<>(); properties.put(property, values); - addEventIdentity(analyticsIdentity, PROFILE_APPEND_ACTION_TYPE, null, properties); + addProfileIdentity(analyticsIdentity, PROFILE_APPEND_ACTION_TYPE, properties); } @Override @@ -351,7 +362,7 @@ public void profileUnsetById(@NonNull SensorsAnalyticsIdentity analyticsIdentity throw new InvalidArgumentException("The property value of [" + property.getKey() + "] should be true."); } } - addEventIdentity(analyticsIdentity, PROFILE_UNSET_ACTION_TYPE, null, properties); + addProfileIdentity(analyticsIdentity, PROFILE_UNSET_ACTION_TYPE, properties); } @Override @@ -359,12 +370,12 @@ public void profileUnsetById(@NonNull SensorsAnalyticsIdentity analyticsIdentity throws InvalidArgumentException { Map properties = new HashMap<>(); properties.put(property, true); - addEventIdentity(analyticsIdentity, PROFILE_UNSET_ACTION_TYPE, null, properties); + addProfileIdentity(analyticsIdentity, PROFILE_UNSET_ACTION_TYPE, properties); } @Override public void profileDeleteById(@NonNull SensorsAnalyticsIdentity analyticsIdentity) throws InvalidArgumentException { - addEventIdentity(analyticsIdentity, PROFILE_DELETE_ACTION_TYPE, null, null); + addProfileIdentity(analyticsIdentity, PROFILE_DELETE_ACTION_TYPE, null); } @Override @@ -399,6 +410,20 @@ public void profileAppendById(@NonNull IDMUserRecord idmUserRecord) throws Inval @Override public void profileUnsetById(@NonNull IDMUserRecord idmUserRecord) throws InvalidArgumentException { SensorsAnalyticsUtil.assertProperties(PROFILE_UNSET_ACTION_TYPE, idmUserRecord.getPropertyMap()); + if (idmUserRecord.getPropertyMap() == null) { + return; + } + for (Map.Entry property : idmUserRecord.getPropertyMap().entrySet()) { + if (!PROJECT_SYSTEM_ATTR.equals(property.getKey())) { + if (property.getValue() instanceof Boolean) { + boolean value = (Boolean) property.getValue(); + if (value) { + continue; + } + } + throw new InvalidArgumentException("The property value of " + property.getKey() + " should be true."); + } + } worker.doAddData(new SensorsData(idmUserRecord, PROFILE_UNSET_ACTION_TYPE)); } @@ -413,34 +438,62 @@ public void shutdown() { worker.shutdown(); } + private void dealProfile(String distinctId, Boolean isLoginId, Map properties, String actionType) + throws InvalidArgumentException { + SensorsAnalyticsUtil.assertProperties(actionType, properties); + UserRecord userRecord = UserRecord.builder() + .setDistinctId(distinctId) + .isLoginId(isLoginId) + .addProperties(properties) + .build(); + worker.doAddData(new SensorsData(userRecord, actionType)); + } + private void dealProfile(UserRecord userRecord, String actionType) throws InvalidArgumentException { - addEvent(userRecord.getDistinctId(), userRecord.getIsLoginId(), null, actionType, null, - userRecord.getPropertyMap()); + SensorsAnalyticsUtil.assertProperties(actionType, userRecord.getPropertyMap()); + worker.doAddData(new SensorsData(userRecord, actionType)); } private void addItem(String itemType, String itemId, String actionType, Map properties) throws InvalidArgumentException { - SensorsAnalyticsUtil.assertKey(ITEM_TYPE, itemType); - SensorsAnalyticsUtil.assertValue(ITEM_ID, itemId); SensorsAnalyticsUtil.assertProperties(actionType, properties); - worker.doAddItem(itemType, itemId, actionType, properties); + ItemRecord itemRecord = ItemRecord.builder() + .setItemId(itemId) + .setItemType(itemType) + .addProperties(properties) + .build(); + worker.doAddData(new SensorsData(itemRecord, actionType)); } private void addEvent(String distinctId, boolean isLoginId, String originDistinctId, String actionType, String eventName, Map properties) throws InvalidArgumentException { - SensorsAnalyticsUtil.assertValue(DISTINCT_ID, distinctId); SensorsAnalyticsUtil.assertProperties(actionType, properties); - if (actionType.equals(TRACK_ACTION_TYPE)) { - SensorsAnalyticsUtil.assertKey(EVENT_NAME, eventName); - } else if (actionType.equals(TRACK_SIGN_UP_ACTION_TYPE)) { + if (actionType.equals(TRACK_SIGN_UP_ACTION_TYPE)) { SensorsAnalyticsUtil.assertValue(ORIGINAL_DISTINCT_ID, originDistinctId); } - worker.doAddEvent(distinctId, isLoginId, originDistinctId, actionType, eventName, properties); + EventRecord eventRecord = EventRecord.builder() + .setEventName(eventName) + .setDistinctId(distinctId) + .isLoginId(isLoginId) + .addProperties(properties) + .build(); + SensorsData sensorsData = new SensorsData(eventRecord, actionType); + sensorsData.setOriginalId(originDistinctId); + worker.doAddData(sensorsData); } - private void addEventIdentity(SensorsAnalyticsIdentity analyticsIdentity, String actionType, String eventName, + private void addProfileIdentity(SensorsAnalyticsIdentity analyticsIdentity, String actionType, Map properties) throws InvalidArgumentException { - addEventIdentity(analyticsIdentity.getIdentityMap(), actionType, eventName, properties); + if (analyticsIdentity.getIdentityMap().isEmpty()) { + throw new InvalidArgumentException("The identity is empty."); + } + assertIdentityMap(actionType, analyticsIdentity.getIdentityMap()); + SensorsAnalyticsUtil.assertProperties(actionType, properties); + IDMUserRecord idmUserRecord = IDMUserRecord.starter() + .identityMap(analyticsIdentity.getIdentityMap()) + .addProperties(properties) + .build(); + worker.doAddData(new SensorsData(idmUserRecord, actionType)); } private void addEventIdentity(Map identityMap, String actionType, String eventName, @@ -450,11 +503,12 @@ private void addEventIdentity(Map identityMap, String actionType } assertIdentityMap(actionType, identityMap); SensorsAnalyticsUtil.assertProperties(actionType, properties); - if (actionType.equals(TRACK_ACTION_TYPE)) { - SensorsAnalyticsUtil.assertKey(EVENT_NAME, eventName); - } - - worker.doAddEventIdentity(identityMap, actionType, eventName, properties); + IDMEventRecord idmEventRecord = IDMEventRecord.starter() + .setEventName(eventName) + .identityMap(identityMap) + .addProperties(properties) + .build(); + worker.doAddData(new SensorsData(idmEventRecord, actionType)); } diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalyticsWorker.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalyticsWorker.java index 7848e8f..ec25d73 100644 --- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalyticsWorker.java +++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalyticsWorker.java @@ -32,7 +32,7 @@ class SensorsAnalyticsWorker { private final Consumer consumer; - private final Map superProperties = new ConcurrentHashMap(); + private final Map superProperties = new ConcurrentHashMap<>(); private boolean enableTimeFree = false; @@ -157,7 +157,7 @@ void shutdown() { private Map generateEventMap(String distinctId, Boolean isLoginId, String originDistinctId, Map identity, String actionType, String eventName, Map properties) { Map eventMap = new HashMap<>(); - eventMap.put("_track_id", new Random().nextInt()); + eventMap.put("_track_id", SensorsAnalyticsUtil.getTrackId(properties, distinctId)); eventMap.put("type", actionType); eventMap.put("lib", getLibProperties()); //开启历史数据导入 diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsConst.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsConst.java index e0680b9..e5e93a6 100644 --- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsConst.java +++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsConst.java @@ -15,7 +15,7 @@ private SensorsConst() { /** * 当前JDK版本号,注意要和pom文件里面的version保持一致 */ - public static final String SDK_VERSION = "3.4.3"; + public static final String SDK_VERSION = "3.4.4"; /** * 当前语言类型 */ @@ -38,6 +38,9 @@ private SensorsConst() { */ public static final String BIND_ID_ACTION_TYPE = "track_id_bind"; public static final String UNBIND_ID_ACTION_TYPE = "track_id_unbind"; + + public static final String ITEM_TYPE = "Item Type"; + public static final String ITEM_ID = "Item Id"; /** * 绑定事件名称 */ @@ -46,9 +49,12 @@ private SensorsConst() { * 解绑事件名称 */ public static final String UNBIND_ID = "$UnbindID"; + + public static final String PROPERTIES = "properties"; /** * 系统预置属性 */ + public static final String TRACK_ID = "$track_id"; public static final String PROJECT_SYSTEM_ATTR = "$project"; public static final String TIME_SYSTEM_ATTR = "$time"; public static final String TOKEN_SYSTEM_ATTR = "$token"; diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsData.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsData.java index 995b96c..50d62f7 100644 --- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsData.java +++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsData.java @@ -2,8 +2,11 @@ import static com.sensorsdata.analytics.javasdk.SensorsConst.TRACK_ACTION_TYPE; +import com.sensorsdata.analytics.javasdk.bean.EventRecord; import com.sensorsdata.analytics.javasdk.bean.IDMEventRecord; import com.sensorsdata.analytics.javasdk.bean.IDMUserRecord; +import com.sensorsdata.analytics.javasdk.bean.ItemRecord; +import com.sensorsdata.analytics.javasdk.bean.UserRecord; import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil; import lombok.Getter; @@ -12,7 +15,6 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; -import java.util.Random; /** * 神策数据格式 @@ -77,18 +79,38 @@ class SensorsData { */ private String itemId; + protected SensorsData(EventRecord eventRecord, String actionType) { + this(eventRecord.getDistinctId(), eventRecord.getOriginalId(), null, actionType, eventRecord.getEventName(), + eventRecord.getPropertyMap(), null, null, eventRecord.getTrackId()); + } + + protected SensorsData(ItemRecord itemRecord, String actionType ) { + this(null,null,null,actionType,null,itemRecord.getPropertyMap(), + itemRecord.getItemType(),itemRecord.getItemId(), itemRecord.getTrackId()); + } + + protected SensorsData (UserRecord userRecord, String actionType) { + this(userRecord.getDistinctId(), actionType, null, userRecord.getPropertyMap(), userRecord.getTrackId()); + } + protected SensorsData(IDMUserRecord userRecord, String actionType) { - this(userRecord.getDistinctId(), actionType, userRecord.getIdentityMap(), userRecord.getPropertyMap()); + this(userRecord.getDistinctId(), actionType, userRecord.getIdentityMap(), userRecord.getPropertyMap(), + userRecord.getTrackId()); } protected SensorsData(IDMEventRecord eventRecord) { this(eventRecord.getDistinctId(), eventRecord.getIdentityMap(), eventRecord.getEventName(), - eventRecord.getPropertyMap()); + eventRecord.getPropertyMap(), eventRecord.getTrackId()); + } + + protected SensorsData(IDMEventRecord eventRecord, String actionType) { + this(eventRecord.getDistinctId(), null, eventRecord.getIdentityMap(), actionType, + eventRecord.getEventName(), eventRecord.getPropertyMap(), null, null, eventRecord.getTrackId()); } protected SensorsData(String distinctId, String type, Map identities, - Map properties) { - this(distinctId, null, identities, type, null, properties, null, null); + Map properties, Integer trackId) { + this(distinctId, null, identities, type, null, properties, null, null, trackId); } /** @@ -99,13 +121,13 @@ protected SensorsData(String distinctId, String type, Map identi * @param properties 事件属性集合 */ protected SensorsData(String distinctId, Map identities, String event, - Map properties) { - this(distinctId, null, identities, TRACK_ACTION_TYPE, event, properties, null, null); + Map properties, Integer trackId) { + this(distinctId, null, identities, TRACK_ACTION_TYPE, event, properties, null, null, trackId); } private SensorsData(String distinctId, String originalId, Map identities, String type, String event, - Map properties, String itemType, String itemId) { - this.trackId = new Random().nextInt(); + Map properties, String itemType, String itemId, Integer trackId) { + this.trackId = trackId; this.distinctId = distinctId; this.originalId = originalId; this.identities = identities; @@ -118,6 +140,7 @@ private SensorsData(String distinctId, String originalId, Map id this.itemId = itemId; } + protected static Map generateData(SensorsData sensorsData) { Map eventMap = new HashMap<>(); if (sensorsData.getTrackId() != null) { @@ -156,6 +179,9 @@ protected static Map generateData(SensorsData sensorsData) { eventMap.put("item_type", sensorsData.getItemType()); } if (sensorsData.getProperties() != null) { + if (sensorsData.getTrackId() != null) { + sensorsData.getProperties().remove(SensorsConst.TRACK_ID); + } eventMap.put("properties", sensorsData.getProperties()); } return eventMap; diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/EventRecord.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/EventRecord.java index 0670b2f..081f364 100644 --- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/EventRecord.java +++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/EventRecord.java @@ -1,6 +1,8 @@ package com.sensorsdata.analytics.javasdk.bean; +import com.sensorsdata.analytics.javasdk.SensorsConst; import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil; import java.io.Serializable; import java.util.Date; @@ -22,11 +24,21 @@ public class EventRecord implements Serializable { private final Boolean isLoginId; - private EventRecord(String eventName, String distinctId, Boolean isLoginId, Map propertyMap) { + private final Integer trackId; + + private final String originalId; + + private EventRecord(String eventName, String distinctId, Boolean isLoginId, Map propertyMap, + Integer trackId, String originalId) { this.eventName = eventName; this.distinctId = distinctId; this.isLoginId = isLoginId; + if (isLoginId) { + propertyMap.put(SensorsConst.LOGIN_SYSTEM_ATTR, true); + } this.propertyMap = propertyMap; + this.trackId = trackId; + this.originalId = originalId; } @Override @@ -59,16 +71,24 @@ public Boolean getIsLoginId() { return isLoginId; } + public String getOriginalId() { + return originalId; + } + + public Integer getTrackId() {return trackId; } + public static class Builder { - private final Map propertyMap = new HashMap(); + private final Map propertyMap = new HashMap<>(); private String eventName; private String distinctId; private Boolean isLoginId; + private Integer trackId; + private String originalId; private Builder() { } - public EventRecord build() throws InvalidArgumentException { + if (eventName == null) { throw new InvalidArgumentException("The eventName is empty."); } @@ -78,7 +98,16 @@ public EventRecord build() throws InvalidArgumentException { if (isLoginId == null) { throw new InvalidArgumentException("The isLoginId is empty."); } - return new EventRecord(eventName, distinctId, isLoginId, propertyMap); + SensorsAnalyticsUtil.assertKey("event_name",eventName); + SensorsAnalyticsUtil.assertValue("distinct_id", distinctId); + String message = String.format("[distinct_id=%s,event_name=%s,is_login_id=%s]",distinctId,eventName,isLoginId); + trackId = SensorsAnalyticsUtil.getTrackId(propertyMap, message); + return new EventRecord(eventName, distinctId, isLoginId, propertyMap,trackId, originalId); + } + + public EventRecord.Builder setOriginalId(String originalId) { + this.originalId = originalId; + return this; } public EventRecord.Builder setEventName(String eventName) { diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMEventRecord.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMEventRecord.java index 89aa973..adce1c4 100644 --- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMEventRecord.java +++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMEventRecord.java @@ -1,8 +1,8 @@ package com.sensorsdata.analytics.javasdk.bean; -import static com.sensorsdata.analytics.javasdk.SensorsConst.LOGIN_SYSTEM_ATTR; import static com.sensorsdata.analytics.javasdk.SensorsConst.TRACK_ACTION_TYPE; +import com.sensorsdata.analytics.javasdk.SensorsConst; import com.sensorsdata.analytics.javasdk.common.Pair; import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil; @@ -44,12 +44,16 @@ public class IDMEventRecord extends SensorsAnalyticsIdentity { */ private final Map propertyMap; + private final Integer trackId; + + private IDMEventRecord(Map identityMap, String eventName, String distinctId, - Map propertyMap) { + Map propertyMap, Integer trackId) { super(identityMap); this.eventName = eventName; this.distinctId = distinctId; this.propertyMap = propertyMap; + this.trackId = trackId; } public static IDMBuilder starter() { @@ -62,6 +66,7 @@ public static class IDMBuilder { private final Map propertyMap = new HashMap<>(); private String eventName; private String distinctId; + private Integer trackId; public IDMEventRecord build() throws InvalidArgumentException { SensorsAnalyticsUtil.assertKey("event_name", eventName); @@ -73,8 +78,12 @@ public IDMEventRecord build() throws InvalidArgumentException { } Pair resPair = SensorsAnalyticsUtil.checkIdentitiesAndGenerateDistinctId(distinctId, idMap); - propertyMap.put(LOGIN_SYSTEM_ATTR, resPair.getValue()); - return new IDMEventRecord(idMap, eventName, resPair.getKey(), propertyMap); + if (resPair.getValue()) { + propertyMap.put(SensorsConst.LOGIN_SYSTEM_ATTR, true); + } + String message = String.format("[distinct_id=%s,event_name=%s]",distinctId,eventName); + trackId = SensorsAnalyticsUtil.getTrackId(propertyMap, message); + return new IDMEventRecord(idMap, eventName, resPair.getKey(), propertyMap,trackId); } public IDMEventRecord.IDMBuilder identityMap(Map identityMap) { @@ -97,7 +106,6 @@ public IDMEventRecord.IDMBuilder setEventName(@NonNull String eventName) { public IDMEventRecord.IDMBuilder setDistinctId(@NonNull String distinctId) { this.distinctId = distinctId; // IDM3.0 设置 distinctId,设置 $is_login_id = false,其实也可不设置 - propertyMap.put(LOGIN_SYSTEM_ATTR, false); return this; } diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMUserRecord.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMUserRecord.java index 9e9ca6c..82e003d 100644 --- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMUserRecord.java +++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMUserRecord.java @@ -1,7 +1,7 @@ package com.sensorsdata.analytics.javasdk.bean; -import static com.sensorsdata.analytics.javasdk.SensorsConst.LOGIN_SYSTEM_ATTR; +import com.sensorsdata.analytics.javasdk.SensorsConst; import com.sensorsdata.analytics.javasdk.common.Pair; import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil; @@ -35,10 +35,14 @@ public class IDMUserRecord extends SensorsAnalyticsIdentity { private final String distinctId; - private IDMUserRecord(Map identityMap, Map propertyMap, String distinctId) { + private final Integer trackId; + + private IDMUserRecord(Map identityMap, Map propertyMap, String distinctId, + Integer trackId) { super(identityMap); this.propertyMap = propertyMap; this.distinctId = distinctId; + this.trackId = trackId; } public static IDMBuilder starter() { @@ -50,12 +54,18 @@ public static class IDMBuilder { private final Map idMap = new LinkedHashMap<>(); private String distinctId; private final Map propertyMap = new HashMap<>(); + private Integer trackId; public IDMUserRecord build() throws InvalidArgumentException { Pair resPair = SensorsAnalyticsUtil.checkIdentitiesAndGenerateDistinctId(distinctId, idMap); - propertyMap.put(LOGIN_SYSTEM_ATTR, resPair.getValue()); - return new IDMUserRecord(idMap, propertyMap, resPair.getKey()); + if (resPair.getValue()) { + propertyMap.put(SensorsConst.LOGIN_SYSTEM_ATTR, true); + } + // 填充 distinct_id 和 目标表 + String message = String.format("[distinct_id=%s,target_table=user]",distinctId); + trackId = SensorsAnalyticsUtil.getTrackId(propertyMap, message); + return new IDMUserRecord(idMap, propertyMap, resPair.getKey(), trackId); } public IDMUserRecord.IDMBuilder identityMap(Map identityMap) { @@ -73,7 +83,6 @@ public IDMUserRecord.IDMBuilder addIdentityProperty(String key, String value) { public IDMUserRecord.IDMBuilder setDistinctId(@NonNull String distinctId) { this.distinctId = distinctId; // IDM3.0 设置 distinctId,设置 $is_login_id = false,其实也可不设置 - propertyMap.put(LOGIN_SYSTEM_ATTR, false); return this; } diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/ItemRecord.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/ItemRecord.java index bd86d43..8ca2130 100644 --- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/ItemRecord.java +++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/ItemRecord.java @@ -1,6 +1,10 @@ package com.sensorsdata.analytics.javasdk.bean; +import static com.sensorsdata.analytics.javasdk.SensorsConst.ITEM_ID; +import static com.sensorsdata.analytics.javasdk.SensorsConst.ITEM_TYPE; + import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil; import java.io.Serializable; import java.util.Date; @@ -25,10 +29,13 @@ public class ItemRecord implements Serializable { private final String itemType; - private ItemRecord(Map propertyMap, String itemId, String itemType) { + private final Integer trackId; + + private ItemRecord(Map propertyMap, String itemId, String itemType, Integer trackId) { this.propertyMap = propertyMap; this.itemId = itemId; this.itemType = itemType; + this.trackId = trackId; } public Map getPropertyMap() { @@ -43,18 +50,22 @@ public String getItemType() { return itemType; } + public Integer getTrackId() { return trackId; } + public static Builder builder() { return new Builder(); } public static class Builder { - private Map propertyMap = new HashMap(); + private Map propertyMap = new HashMap<>(); private String itemId; private String itemType; + private Integer trackId; + private Builder() { } @@ -65,7 +76,10 @@ public ItemRecord build() throws InvalidArgumentException { if (null == itemType) { throw new InvalidArgumentException("The itemType is empty."); } - return new ItemRecord(propertyMap, itemId, itemType); + SensorsAnalyticsUtil.assertKey(ITEM_TYPE, itemType); + SensorsAnalyticsUtil.assertValue(ITEM_ID, itemId); + this.trackId = null; + return new ItemRecord(propertyMap, itemId, itemType, trackId); } public ItemRecord.Builder setItemId(String itemId) { diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/UserRecord.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/UserRecord.java index 4408094..07afde6 100644 --- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/UserRecord.java +++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/UserRecord.java @@ -1,6 +1,9 @@ package com.sensorsdata.analytics.javasdk.bean; + +import com.sensorsdata.analytics.javasdk.SensorsConst; import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil; import java.io.Serializable; import java.util.Date; @@ -25,10 +28,16 @@ public class UserRecord implements Serializable { private final Boolean isLoginId; - private UserRecord(Map propertyMap, String distinctId, Boolean isLoginId) { + private final Integer trackId; + + private UserRecord(Map propertyMap, String distinctId, Boolean isLoginId, Integer trackId) { this.propertyMap = propertyMap; + if (isLoginId) { + propertyMap.put(SensorsConst.LOGIN_SYSTEM_ATTR, true); + } this.distinctId = distinctId; this.isLoginId = isLoginId; + this.trackId = trackId; } public static Builder builder() { @@ -47,10 +56,13 @@ public Map getPropertyMap() { return propertyMap; } + public Integer getTrackId() {return trackId; } + public static class Builder { - private final Map propertyMap = new HashMap(); + private final Map propertyMap = new HashMap<>(); private String distinctId; private Boolean isLoginId; + private Integer trackId; private Builder() { } @@ -62,7 +74,10 @@ public UserRecord build() throws InvalidArgumentException { if (isLoginId == null) { throw new InvalidArgumentException("The isLoginId is empty."); } - return new UserRecord(propertyMap, distinctId, isLoginId); + SensorsAnalyticsUtil.assertValue("distinct_id", distinctId); + String message = String.format("[distinct_id=%s,target_table=user]",distinctId); + trackId = SensorsAnalyticsUtil.getTrackId(propertyMap, message); + return new UserRecord(propertyMap, distinctId, isLoginId, trackId); } public UserRecord.Builder setDistinctId(String distinctId) { diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/util/SensorsAnalyticsUtil.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/util/SensorsAnalyticsUtil.java index e284665..eeb5e4c 100644 --- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/util/SensorsAnalyticsUtil.java +++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/util/SensorsAnalyticsUtil.java @@ -28,6 +28,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import lombok.extern.slf4j.Slf4j; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -36,9 +37,11 @@ import java.util.List; import java.util.ListIterator; import java.util.Map; +import java.util.Random; import java.util.TimeZone; import java.util.regex.Pattern; +@Slf4j public class SensorsAnalyticsUtil { private static final Pattern KEY_PATTERN = Pattern.compile( "^((?!^distinct_id$|^original_id$|^time$|^properties$|^id$|^first_id$|^second_id$|^users$|^events$|^event$|^user_id$|^date$|^datetime$|^user_group|^user_tag)[a-zA-Z_$][a-zA-Z\\d_$]{0,99})$", @@ -177,6 +180,19 @@ public static void assertKey(String type, String key) throws InvalidArgumentExce } } + public static Integer getTrackId(Map propertyMap, String message) { + try { + if (propertyMap.containsKey(SensorsConst.TRACK_ID)) { + return Integer.parseInt((String.valueOf(propertyMap.get(SensorsConst.TRACK_ID)))); + } + return new Random().nextInt(); + } catch (Exception e) { + log.error("Failed Get Track ID. {} ", message ,e); + return new Random().nextInt(); + } + + } + /** * IDM3.0 校验 distinctId 和 identities 集合,并生成最终 distinctId * diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/BatchConsumerTestConstruct.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/BatchConsumerTestConstruct.java index f631644..d527001 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/BatchConsumerTestConstruct.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/BatchConsumerTestConstruct.java @@ -3,6 +3,7 @@ import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer; import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import java.lang.reflect.Field; @@ -24,7 +25,9 @@ public class BatchConsumerTestConstruct { private BatchConsumer batchConsumer; private SensorsAnalytics sa; private List> messageList; - private String serverUrl = "http://10.120.73.51:8106/sa?project=default&token="; +// private String serverUrl = "http://10.120.73.51:8106/sa?project=default&token="; + String serverUrl = "http://10.120.111.143:8106/sa?project=default"; + @Before public void init() throws NoSuchFieldException, IllegalAccessException { @@ -249,6 +252,7 @@ public void testBatchConsumerTimeoutSec06() throws NoSuchFieldException, Illegal * public BatchConsumer(final String serverUrl, final int bulkSize, final int timeoutSec) */ @Test + @Ignore public void testBatchConsumerTimeoutSec06AssertTimeout() throws NoSuchFieldException, IllegalAccessException, InterruptedException, InvalidArgumentException { String serverUrl = "http://localhost:8887/test"; int bulkSize = 100; @@ -286,6 +290,7 @@ public void testBatchConsumerTimeoutSec07() throws NoSuchFieldException, Illegal * public BatchConsumer(final String serverUrl, final int bulkSize, final int timeoutSec) */ @Test + @Ignore public void testBatchConsumerTimeoutSec07AssertTimeout() throws NoSuchFieldException, IllegalAccessException, InterruptedException, InvalidArgumentException { String serverUrl = "http://localhost:8887/test"; int bulkSize = 100; diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/FastBatchConsumerResendTest.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/FastBatchConsumerResendTest.java index c749991..64d3076 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/FastBatchConsumerResendTest.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/FastBatchConsumerResendTest.java @@ -618,7 +618,6 @@ public void TestProfileSetById() throws InvalidArgumentException{ assertNotNull(messageList.get(0).get("_track_id")); assertEquals("profile_set", messageList.get(0).get("type")); assertEquals("123", messageList.get(0).get("distinct_id")); - assertNotNull(messageList.get(0).get("project")); sa.flush(); } @@ -647,12 +646,12 @@ public void TestProfileSetOnceById() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); assertEquals("profile_set_once", messageList.get(0).get("type")); - assertEquals("123", messageList.get(0).get("distinct_id")); + assertEquals("$identity_email+123", messageList.get(0).get("distinct_id")); sa.flush(); } @@ -681,12 +680,13 @@ public void TestProfileIncrementById() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); assertEquals("profile_increment", messageList.get(0).get("type")); - assertEquals("123", messageList.get(0).get("distinct_id")); + assertEquals("$identity_email+123", messageList.get(0).get("distinct_id")); sa.flush(); } @@ -718,12 +718,12 @@ public void TestProfileAppendById() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(list, props.get("favorite")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); assertEquals("profile_append", messageList.get(0).get("type")); - assertEquals("123", messageList.get(0).get("distinct_id")); + assertEquals("$identity_email+123", messageList.get(0).get("distinct_id")); sa.flush(); } @@ -749,19 +749,20 @@ public void TestProfileUnsetById() throws InvalidArgumentException{ fail(); } - List> messageList = dataList.get(0).getFailedData(); assertEquals(1, messageList.size()); + List> messageList = dataList.get(0).getFailedData(); + assertEquals(1, messageList.size()); assertNotNull(messageList.get(0).get("identities")); Map identities = (Map) messageList.get(0).get("identities"); assertEquals("123", identities.get("$identity_email")); Map props = (Map)messageList.get(0).get("properties"); assertEquals(true, props.get("favorite")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); assertEquals("profile_unset", messageList.get(0).get("type")); - assertEquals("123", messageList.get(0).get("distinct_id")); + assertEquals("$identity_email+123", messageList.get(0).get("distinct_id")); sa.flush(); } @@ -794,12 +795,12 @@ public void TestProfileDeleteById() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(list, props.get("favorite")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); assertEquals("profile_append", messageList.get(0).get("type")); - assertEquals("123", messageList.get(0).get("distinct_id")); + assertEquals("$identity_email+123", messageList.get(0).get("distinct_id")); sa.flush(); } diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/FastBatchConsumerTest.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/FastBatchConsumerTest.java index 5b13cf4..8e32210 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/FastBatchConsumerTest.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/FastBatchConsumerTest.java @@ -7,6 +7,7 @@ import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; import lombok.SneakyThrows; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import java.lang.reflect.Field; @@ -280,7 +281,8 @@ public void onFailed(FailedData failedData) { @Test public void testBulkSizeInvalid() throws InvalidArgumentException, NoSuchFieldException, IllegalAccessException, InterruptedException { - String url = "http://10.120.73.51:8106/sa?project=default&token="; +// String url = "http://10.120.73.51:8106/sa?project=default&token="; + String url = "http://10.120.111.143:8106/sa?project=default"; FastBatchConsumer consumer = new FastBatchConsumer(url,false,0, new Callback() { @Override @@ -387,13 +389,13 @@ public void onFailed(FailedData failedData) { } @Test + @Ignore public void testFlushSec0() throws InvalidArgumentException, NoSuchFieldException, IllegalAccessException, InterruptedException { String url = "http://localhost:8887/test"; FastBatchConsumer consumer = new FastBatchConsumer(url,0, true,new Callback() { @Override public void onFailed(FailedData failedData) { - System.out.println(failedData); } }); @@ -437,7 +439,7 @@ public void onFailed(FailedData failedData) { assertEquals(50,bulkSize); sa.track("123", true, "test", null); - Thread.sleep(1000); + Thread.sleep(1300); assertEquals(0,buffer.size()); } @@ -566,6 +568,7 @@ public void onFailed(FailedData failedData) { */ long endTime; @Test + @Ignore public void testTimeoutSec() throws InvalidArgumentException, InterruptedException, JsonProcessingException { String url = "http://localhost:8887/test"; FastBatchConsumer consumer = new FastBatchConsumer(url, new Callback() { @@ -606,6 +609,7 @@ public void onFailed(FailedData failedData) { * 需要使用 Charles 加断点 */ @Test + @Ignore public void testTimeoutSec05() throws InvalidArgumentException, InterruptedException, JsonProcessingException { String url = "http://localhost:8887/test"; FastBatchConsumer consumer = new FastBatchConsumer(url, false, 50, 1000,1, 5,new Callback() { @@ -646,6 +650,7 @@ public void onFailed(FailedData failedData) { * 需要使用 Charles 加断点 */ @Test + @Ignore public void testTimeoutSec01() throws InvalidArgumentException, InterruptedException, JsonProcessingException { String url = "http://localhost:8887/test"; FastBatchConsumer consumer = new FastBatchConsumer(url, false, 50, 1000,1, 0,new Callback() { diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackId1.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackId1.java new file mode 100644 index 0000000..c359df7 --- /dev/null +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackId1.java @@ -0,0 +1,467 @@ +package com.sensorsdata.analytics.javasdk; + +import com.sensorsdata.analytics.javasdk.bean.EventRecord; +import com.sensorsdata.analytics.javasdk.bean.ItemRecord; +import com.sensorsdata.analytics.javasdk.bean.UserRecord; +import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer; +import com.sensorsdata.analytics.javasdk.consumer.ConcurrentLoggingConsumer; +import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.util.*; + +import static org.junit.Assert.*; + +/** + * 适用于 v3.4.4+ 版本 + * 测试点:验证事件的 _track_id 正常由 $track_id 生成。 + * 无特殊情况,不在下面的 testcase 上一一说明 + */ +public class IDMappingModel1TrackId1 extends SensorsBaseTest { + + private BatchConsumer batchConsumer; + + private List> messageList; + private ConcurrentLoggingConsumer consumer; + + private StringBuilder messageBuffer; + SensorsAnalytics sa; + + @Before + public void init() throws NoSuchFieldException, IllegalAccessException { + String url = "http://10.120.111.143:8106/sa?project=default"; + // 注意要设置 bulkSize 稍微大一点,这里设置为 100,否则超过 1 条就上报,messageList 里面拿不到事件数据 + batchConsumer = new BatchConsumer(url, 100, true, 3); + // 通过反射机制获取 BatchConsumer 的 messageList + Field field = batchConsumer.getClass().getDeclaredField("messageList"); // messageList 是 BatchConsumer 用来暂存事件数据的成员变量 + field.setAccessible(true); + messageList = (List>) field.get(batchConsumer); + sa = new SensorsAnalytics(batchConsumer); + } + + private void assertNotNullProp(){ + assertNotNull(messageList.get(0).get("time")); + assertNotNull(messageList.get(0).get("_track_id")); + assertNotNull(messageList.get(0).get("properties")); + } + + /** + * 校验调用 track 方法生成事件节点数是否完整 + */ + @Test + public void checkTrackEventLoginTrue() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + + sa.track("123", true, "test", properties); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + + } + + + /** + * 校验 trackSignup 记录节点 + */ + @Test + public void checkTrackSignUpProp() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("$track_id", 111); + sa.trackSignUp("123", "345", properties); + + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataType() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("date1", date); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("list1", list); + properties.put("$track_id", 111); + sa.profileSet("123", true, properties); + + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("number1")); // properties 包含其他自定义属性 + + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataType01() throws InvalidArgumentException { + + sa.profileSet("123", true, "$track_id", 111); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + + } + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileSetOnceDataType() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("date1", date); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("list1", list); + properties.put("$track_id", 111); + sa.profileSetOnce("123", true, properties); + + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("number1")); // properties 包含其他自定义属性 + + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileIncrement() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("$track_id", 111); + sa.profileIncrement("123", true, properties); + + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("number1")); // properties 包含其他自定义属性 + + + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileIncrement01() throws InvalidArgumentException { + sa.profileIncrement("123", true, "$track_id", 111); + + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + + @Test + public void testProfileAppend() throws InvalidArgumentException{ + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("list1", list); + + List listInt = new ArrayList<>(); + listInt.add(111); + properties.put("$track_id", listInt); + + try { + sa.profileAppend("123", true, properties); + fail("[ERROR] profileAppend should throw InvalidArgumentException."); + }catch (Exception e){ + e.printStackTrace(); + assertEquals("The property '$track_id' should be a list of String.", e.getMessage()); + } + + } + + @Test + public void testProfileAppend01() throws InvalidArgumentException{ + sa.profileAppend("123", true, "$track_id", "111"); + + assertNotNullProp(); + + assertNotEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + + } + + // profileUnset + @Test + public void testProfileUnset() { + Map properties = new HashMap<>(); + properties.put("list1", true); + properties.put("$track_id", 111); + + try { + sa.profileUnset("123", true, properties); + fail("[ERROR] profileUnset should throw InvalidArgumentException."); + }catch (InvalidArgumentException e){ + assertEquals("The property value of $track_id should be true.", e.getMessage()); + } + + } + + // profileUnset + @Test + public void testProfileUnset01() throws InvalidArgumentException{ + sa.profileUnset("123", true, "$track_id"); + + assertNotNullProp(); + + assertNotEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + // profileDelete + @Test + public void testProfileDelete() throws InvalidArgumentException{ + sa.profileDelete("123", true); + } + + @Test + public void testItemSet_Delete() throws Exception { + //物品纬度表上报 + String itemId = "product001", itemType = "mobile"; + ItemRecord addRecord = ItemRecord.builder().setItemId(itemId).setItemType(itemType) + .addProperty("color", "white") + .build(); + sa.itemSet(addRecord); + + //删除物品纬度信息 + ItemRecord deleteRecord = ItemRecord.builder().setItemId(itemId).setItemType(itemType) + .build(); + sa.itemDelete(deleteRecord); + sa.flush(); + } + + + + /** + * 校验 event Builder 模式生成数据用户属性是否正常 + */ + @Test + public void checkTrackEventBuilder() throws InvalidArgumentException { + EventRecord eventRecord = EventRecord.builder() + .setDistinctId("abc") + .isLoginId(false) + .setEventName("test") + .addProperty("$track_id", 111) + .build(); + sa.track(eventRecord); + + assertNotNullProp(); + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 校验 is_login_id 为 true 的事件属性 + */ + @Test + public void checkTrackEventBuilderLoginIdIsTrue() throws InvalidArgumentException { + EventRecord eventRecord = EventRecord.builder() + .setDistinctId("abc") + .isLoginId(true) + .setEventName("test") + .addProperty("$track_id", 111) + .build(); + sa.track(eventRecord); + + assertNotNullProp(); + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataTypeEventBuilder() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("number1", 1234) + .addProperty("date1", date) + .addProperty("String1", "str") + .addProperty("boolean1", false) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .build(); + sa.profileSet(userRecord); + + assertNotNullProp(); + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileSetOnceDataTypeEventBuilder() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("number1", 1234) + .addProperty("date1", date) + .addProperty("String1", "str") + .addProperty("boolean1", false) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .build(); + sa.profileSetOnce(userRecord); + + assertNotNullProp(); + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileIncrementEventBuilder() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("number1", 1234) + .addProperty("$track_id", 111) + .build(); + sa.profileIncrement(userRecord); + + assertNotNullProp(); + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + @Test + public void testProfileAppendByIdEventBuilder() throws InvalidArgumentException{ + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .build(); + try { + sa.profileAppend(userRecord); + fail("profileAppend should throw InvalidArgumentException"); + }catch (InvalidArgumentException e){ + assertEquals("The property value of PROFILE_APPEND should be a List.", e.getMessage()); + } + + } + + // profileUnsetById + @Test + public void testProfileUnsetByIdEventBuilder() { + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = null; + try { + userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("$track_id", 111) + .build(); + sa.profileUnset(userRecord); + fail("[ERROR] profileUnset should throw InvalidArgumentException."); + }catch (InvalidArgumentException e){ + assertEquals("The property value of $track_id should be true.", e.getMessage()); + } + + } + + // profileDeleteById + @Test + public void testProfileDeleteByIdEventBuilder() throws InvalidArgumentException{ + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .build(); + sa.profileDelete(userRecord); + + assertNotNullProp(); + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } +} diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackIdAndTime.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackIdAndTime.java new file mode 100644 index 0000000..5b506d1 --- /dev/null +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackIdAndTime.java @@ -0,0 +1,493 @@ +package com.sensorsdata.analytics.javasdk; + +import com.sensorsdata.analytics.javasdk.bean.EventRecord; +import com.sensorsdata.analytics.javasdk.bean.ItemRecord; +import com.sensorsdata.analytics.javasdk.bean.UserRecord; +import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer; +import com.sensorsdata.analytics.javasdk.consumer.ConcurrentLoggingConsumer; +import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.util.*; + +import static org.junit.Assert.*; + +/** + * 适用于 v3.4.4+ 版本 + * 测试点:验证事件的 _track_id 正常由 $track_id 生成,time 由 $time 生成。 + * 无特殊情况,不在下面的 testcase 上一一说明 + */ +public class IDMappingModel1TrackIdAndTime extends SensorsBaseTest { + + private BatchConsumer batchConsumer; + + private List> messageList; + SensorsAnalytics sa; + + @Before + public void init() throws NoSuchFieldException, IllegalAccessException { + + String url = "http://10.120.111.143:8106/sa?project=default"; + // 注意要设置 bulkSize 稍微大一点,这里设置为 100,否则超过 1 条就上报,messageList 里面拿不到事件数据 + batchConsumer = new BatchConsumer(url, 100, true, 3); + // 通过反射机制获取 BatchConsumer 的 messageList + Field field = batchConsumer.getClass().getDeclaredField("messageList"); // messageList 是 BatchConsumer 用来暂存事件数据的成员变量 + field.setAccessible(true); + messageList = (List>) field.get(batchConsumer); + sa = new SensorsAnalytics(batchConsumer); + } + + private void assertNotNullProp(){ + assertNotNull(messageList.get(0).get("time")); + assertNotNull(messageList.get(0).get("_track_id")); + assertNotNull(messageList.get(0).get("properties")); + } + + /** + * 校验调用 track 方法生成事件节点数是否完整 + */ + @Test + public void checkTrackEventLoginTrue() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + sa.track("123", true, "test", properties); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + + } + + + /** + * 校验 trackSignup 记录节点 + */ + @Test + public void checkTrackSignUpProp() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + sa.trackSignUp("123", "345", properties); + + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataType() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("date1", date); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("list1", list); + properties.put("$track_id", 111); + properties.put("$time", date); + sa.profileSet("123", true, properties); + + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("number1")); // properties 包含其他自定义属性 + + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataType01() throws InvalidArgumentException { + + sa.profileSet("123", true, "$track_id", 111); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); +// assertEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + + } + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileSetOnceDataType() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("date1", date); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("list1", list); + properties.put("$track_id", 111); + properties.put("$time", date); + sa.profileSetOnce("123", true, properties); + + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("number1")); // properties 包含其他自定义属性 + + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileIncrement() { + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + try { + sa.profileIncrement("123", true, properties); + fail("[ERROR] profileIncrement should throw InvalidArgumentException."); + }catch (Exception e){ + e.printStackTrace(); + assertEquals("The property value of PROFILE_INCREMENT should be a Number.The current type is class java.util.Date.", e.getMessage()); + } + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileIncrement01(){ + Date date = new Date(); + try { + sa.profileIncrement("123", true, "$time", date.getTime()); + fail("[ERROR] profileIncrement should throw InvalidArgumentException."); + }catch (Exception e){ + e.printStackTrace(); + assertEquals("The property '$time' should be a java.util.Date.", e.getMessage()); + } + } + + + @Test + public void testProfileAppend() { + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("list1", list); + + List listInt = new ArrayList<>(); + listInt.add(111); + properties.put("$track_id", listInt); + + try { + sa.profileAppend("123", true, properties); + fail("[ERROR] profileAppend should throw InvalidArgumentException."); + }catch (Exception e){ + e.printStackTrace(); + assertEquals("The property '$track_id' should be a list of String.", e.getMessage()); + } + + } + + @Test + public void testProfileAppend01() throws InvalidArgumentException{ + sa.profileAppend("123", true, "$track_id", "111"); + + assertNotNullProp(); + + assertNotEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + + } + + // profileUnset + @Test + public void testProfileUnset() { + Map properties = new HashMap<>(); + properties.put("list1", true); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + try { + sa.profileUnset("123", true, properties); + fail("[ERROR] profileUnset should throw InvalidArgumentException."); + }catch (InvalidArgumentException e){ + assertEquals("The property value of $time should be true.", e.getMessage()); + } + + } + + // profileUnset + @Test + public void testProfileUnset01() throws InvalidArgumentException{ + sa.profileUnset("123", true, "$track_id"); + + assertNotNullProp(); + + assertNotEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + + } + + // profileDelete + @Test + public void testProfileDelete() throws InvalidArgumentException{ + sa.profileDelete("123", true); + } + + @Test + public void testItemSet_Delete() throws Exception { + Date date = new Date(); + //物品纬度表上报 + String itemId = "product001", itemType = "mobile"; + ItemRecord addRecord = ItemRecord.builder().setItemId(itemId).setItemType(itemType) + .addProperty("color", "white") + .addProperty("$time", date) + .build(); + sa.itemSet(addRecord); + System.out.println(date.getTime()); + + //删除物品纬度信息 + ItemRecord deleteRecord = ItemRecord.builder().setItemId(itemId).setItemType(itemType) + .addProperty("$time", date) + .build(); + sa.itemDelete(deleteRecord); + sa.flush(); + } + + + + /** + * 校验 event Builder 模式生成数据用户属性是否正常 + */ + @Test + public void checkTrackEventBuilder() throws InvalidArgumentException { + Date date = new Date(); + EventRecord eventRecord = EventRecord.builder() + .setDistinctId("abc") + .isLoginId(false) + .setEventName("test") + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + sa.track(eventRecord); + + assertNotNullProp(); + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 校验 is_login_id 为 true 的事件属性 + */ + @Test + public void checkTrackEventBuilderLoginIdIsTrue() throws InvalidArgumentException { + Date date = new Date(); + EventRecord eventRecord = EventRecord.builder() + .setDistinctId("abc") + .isLoginId(true) + .setEventName("test") + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + sa.track(eventRecord); + + assertNotNullProp(); + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataTypeEventBuilder() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("number1", 1234) + .addProperty("date1", date) + .addProperty("String1", "str") + .addProperty("boolean1", false) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + sa.profileSet(userRecord); + + assertNotNullProp(); + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileSetOnceDataTypeEventBuilder() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("number1", 1234) + .addProperty("date1", date) + .addProperty("String1", "str") + .addProperty("boolean1", false) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + sa.profileSetOnce(userRecord); + + assertNotNullProp(); + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileIncrementEventBuilder() { + Date date = new Date(); + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + try { + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("number1", 1234) + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + + sa.profileIncrement(userRecord); + fail("profileIncrement should throw InvalidArgumentException"); + }catch (InvalidArgumentException e){ + assertEquals("The property value of PROFILE_INCREMENT should be a Number.The current type is class java.util.Date.", e.getMessage()); + } + } + + @Test + public void testProfileAppendByIdEventBuilder() throws InvalidArgumentException{ + Date date = new Date(); + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + try { + sa.profileAppend(userRecord); + fail("profileAppend should throw InvalidArgumentException"); + }catch (InvalidArgumentException e){ + assertEquals("The property value of PROFILE_APPEND should be a List.", e.getMessage()); + } + + } + + // profileUnsetById + @Test + public void testProfileUnsetByIdEventBuilder(){ + Date date = new Date(); + UserRecord userRecord = null; + try { + userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + sa.profileUnset(userRecord); + fail("profileUnset should throw InvalidArgumentException"); + }catch (InvalidArgumentException e){ + assertEquals("The property value of $time should be true.", e.getMessage()); + } + + } + + // profileDeleteById + + @Test + public void testProfileDeleteByIdEventBuilder() throws InvalidArgumentException{ + Date date = new Date(); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("$track_id", 111) + .addProperty("$time", date) + .addProperty("abc", "acb") + .build(); + sa.profileDelete(userRecord); + + assertNotNullProp(); + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } +} diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackIdAndTimeDebugConsumer.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackIdAndTimeDebugConsumer.java new file mode 100644 index 0000000..fda864b --- /dev/null +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackIdAndTimeDebugConsumer.java @@ -0,0 +1,309 @@ +package com.sensorsdata.analytics.javasdk; + +import com.sensorsdata.analytics.javasdk.bean.EventRecord; +import com.sensorsdata.analytics.javasdk.bean.ItemRecord; +import com.sensorsdata.analytics.javasdk.bean.UserRecord; +import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer; +import com.sensorsdata.analytics.javasdk.consumer.ConcurrentLoggingConsumer; +import com.sensorsdata.analytics.javasdk.consumer.DebugConsumer; +import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import org.junit.Before; +import org.junit.Test; + +import java.util.*; + +import static org.junit.Assert.fail; + +/** + * 适用于 v3.4.4+ 版本 + * 测试点:验证事件的 _track_id 正常由 $track_id 生成,time 由 $time 生成。 + * 无特殊情况,不在下面的 testcase 上一一说明 + */ +public class IDMappingModel1TrackIdAndTimeDebugConsumer extends SensorsBaseTest { + + private BatchConsumer batchConsumer; + + private List> messageList; + private ConcurrentLoggingConsumer consumer; + + private StringBuilder messageBuffer; + SensorsAnalytics sa; + + @Before + public void init() throws NoSuchFieldException, IllegalAccessException { + String url = "http://10.120.111.143:8106/sa?project=default"; + DebugConsumer consumer = new DebugConsumer(url, true); + sa = new SensorsAnalytics(consumer); + + } + + + /** + * 校验调用 track 方法生成事件节点数是否完整 + */ + @Test + public void checkTrackEventLoginTrue() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + sa.track("123", true, "test", properties);} + + + /** + * 校验 trackSignup 记录节点 + */ + @Test + public void checkTrackSignUpProp() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + sa.trackSignUp("123", "345", properties);} + + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataType() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("date1", date); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("list1", list); + properties.put("$track_id", 111); + properties.put("$time", date); + sa.profileSet("123", true, properties);} + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataType01() throws InvalidArgumentException { + + sa.profileSet("123", true, "$track_id", 111); + + } + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileSetOnceDataType() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("date1", date); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("list1", list); + properties.put("$track_id", 111); + properties.put("$time", date); + sa.profileSetOnce("123", true, properties); + } + + + @Test + public void testProfileAppend() { + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("list1", list); + + List listInt = new ArrayList<>(); + listInt.add(111); + properties.put("$track_id", listInt); + + try { + sa.profileAppend("123", true, properties); + fail("[ERROR] profileAppend should throw InvalidArgumentException."); + }catch (Exception e){ + e.printStackTrace(); + + } + + } + + @Test + public void testProfileAppend01() throws InvalidArgumentException{ + sa.profileAppend("123", true, "$track_id", "111");} + + // profileUnset + @Test + public void testProfileUnset() { + Map properties = new HashMap<>(); + properties.put("list1", true); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + try { + sa.profileUnset("123", true, properties); + fail("[ERROR] profileUnset should throw InvalidArgumentException."); + }catch (InvalidArgumentException e){ + + } + + } + + // profileUnset + @Test + public void testProfileUnset01() throws InvalidArgumentException{ + sa.profileUnset("123", true, "$track_id");} + + // profileDelete + @Test + public void testProfileDelete() throws InvalidArgumentException{ + sa.profileDelete("123", true); + } + + @Test + public void testItemSet_Delete() throws Exception { + Date date = new Date(); + //物品纬度表上报 + String itemId = "product001", itemType = "mobile"; + ItemRecord addRecord = ItemRecord.builder().setItemId(itemId).setItemType(itemType) + .addProperty("color", "white") + .addProperty("$time", date) + .build(); + sa.itemSet(addRecord); + System.out.println(date.getTime()); + + //删除物品纬度信息 + ItemRecord deleteRecord = ItemRecord.builder().setItemId(itemId).setItemType(itemType) + .addProperty("$time", date) + .build(); + sa.itemDelete(deleteRecord); + sa.flush(); + } + + + + /** + * 校验 event Builder 模式生成数据用户属性是否正常 + */ + @Test + public void checkTrackEventBuilder() throws InvalidArgumentException { + Date date = new Date(); + EventRecord eventRecord = EventRecord.builder() + .setDistinctId("abc") + .isLoginId(false) + .setEventName("test") + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + sa.track(eventRecord);} + + /** + * 校验 is_login_id 为 true 的事件属性 + */ + @Test + public void checkTrackEventBuilderLoginIdIsTrue() throws InvalidArgumentException { + Date date = new Date(); + EventRecord eventRecord = EventRecord.builder() + .setDistinctId("abc") + .isLoginId(true) + .setEventName("test") + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + sa.track(eventRecord);} + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataTypeEventBuilder() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("number1", 1234) + .addProperty("date1", date) + .addProperty("String1", "str") + .addProperty("boolean1", false) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + sa.profileSet(userRecord);} + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileSetOnceDataTypeEventBuilder() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("number1", 1234) + .addProperty("date1", date) + .addProperty("String1", "str") + .addProperty("boolean1", false) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + sa.profileSetOnce(userRecord); + } + + + @Test + public void testProfileAppendByIdEventBuilder() throws InvalidArgumentException{ + Date date = new Date(); + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + try { + sa.profileAppend(userRecord); + fail("profileAppend should throw InvalidArgumentException"); + }catch (InvalidArgumentException e){ + + } + + } + + + // profileDeleteById + @Test + public void testProfileDeleteByIdEventBuilder() throws InvalidArgumentException{ + Date date = new Date(); + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .addProperty("$time", date) + .build(); + sa.profileDelete(userRecord); + } +} diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackIdDebugComsumer.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackIdDebugComsumer.java new file mode 100644 index 0000000..21c24d9 --- /dev/null +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel1TrackIdDebugComsumer.java @@ -0,0 +1,307 @@ +package com.sensorsdata.analytics.javasdk; + +import com.sensorsdata.analytics.javasdk.bean.EventRecord; +import com.sensorsdata.analytics.javasdk.bean.ItemRecord; +import com.sensorsdata.analytics.javasdk.bean.UserRecord; +import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer; +import com.sensorsdata.analytics.javasdk.consumer.ConcurrentLoggingConsumer; +import com.sensorsdata.analytics.javasdk.consumer.DebugConsumer; +import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import org.junit.Before; +import org.junit.Test; + +import java.util.*; + +import static org.junit.Assert.fail; + +/** + * 适用于 v3.4.4+ 版本 + * 测试点:验证事件的 _track_id 正常由 $track_id 生成。 + * 无特殊情况,不在下面的 testcase 上一一说明 + */ +public class IDMappingModel1TrackIdDebugComsumer extends SensorsBaseTest { + + private BatchConsumer batchConsumer; + + private List> messageList; + private ConcurrentLoggingConsumer consumer; + + private StringBuilder messageBuffer; + SensorsAnalytics sa; + + @Before + public void init() { + String url = "http://10.120.111.143:8106/sa?project=default"; + DebugConsumer consumer = new DebugConsumer(url, true); + sa = new SensorsAnalytics(consumer); + } + + /** + * 校验调用 track 方法生成事件节点数是否完整 + */ + @Test + public void checkTrackEventLoginTrue() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + + sa.track("123", true, "test", properties); + } + + + /** + * 校验 trackSignup 记录节点 + */ + @Test + public void checkTrackSignUpProp() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("$track_id", 111); + sa.trackSignUp("123", "345", properties); + } + + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataType() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("date1", date); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("list1", list); + properties.put("$track_id", 111); + sa.profileSet("123", true, properties); + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataType01() throws InvalidArgumentException { + + sa.profileSet("123", true, "$track_id", 111); + } + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileSetOnceDataType() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("date1", date); + properties.put("String1", "str"); + properties.put("boolean1", false); + properties.put("list1", list); + properties.put("$track_id", 111); + sa.profileSetOnce("123", true, properties); + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileIncrement() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("number1", 1234); + properties.put("$track_id", 111); + sa.profileIncrement("123", true, properties); + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileIncrement01() throws InvalidArgumentException { + sa.profileIncrement("123", true, "$track_id", 111); + } + + + @Test + public void testProfileAppend() throws InvalidArgumentException{ + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + Map properties = new HashMap<>(); + properties.put("list1", list); + + List listInt = new ArrayList<>(); + listInt.add(111); + properties.put("$track_id", listInt); + + try { + sa.profileAppend("123", true, properties); + fail("[ERROR] profileAppend should throw InvalidArgumentException."); + }catch (Exception e){ + e.printStackTrace(); + } + } + + @Test + public void testProfileAppend01() throws InvalidArgumentException{ + sa.profileAppend("123", true, "$track_id", "111"); + } + + // profileUnset + @Test + public void testProfileUnset() { + Map properties = new HashMap<>(); + properties.put("list1", true); + properties.put("$track_id", 111); + + try { + sa.profileUnset("123", true, properties); + fail("[ERROR] profileUnset should throw InvalidArgumentException."); + }catch (InvalidArgumentException e){ + } + } + + // profileUnset + @Test + public void testProfileUnset01() throws InvalidArgumentException{ + sa.profileUnset("123", true, "$track_id"); + } + + // profileDelete + @Test + public void testProfileDelete() throws InvalidArgumentException{ + sa.profileDelete("123", true); + } + + @Test + public void testItemSet_Delete() throws Exception { + //物品纬度表上报 + String itemId = "product001", itemType = "mobile"; + ItemRecord addRecord = ItemRecord.builder().setItemId(itemId).setItemType(itemType) + .addProperty("color", "white") + .build(); + sa.itemSet(addRecord); + + //删除物品纬度信息 + ItemRecord deleteRecord = ItemRecord.builder().setItemId(itemId).setItemType(itemType) + .build(); + sa.itemDelete(deleteRecord); + sa.flush(); + } + + + + /** + * 校验 event Builder 模式生成数据用户属性是否正常 + */ + @Test + public void checkTrackEventBuilder() throws InvalidArgumentException { + EventRecord eventRecord = EventRecord.builder() + .setDistinctId("abc") + .isLoginId(false) + .setEventName("test") + .addProperty("$track_id", 111) + .build(); + sa.track(eventRecord); + } + + /** + * 校验 is_login_id 为 true 的事件属性 + */ + @Test + public void checkTrackEventBuilderLoginIdIsTrue() throws InvalidArgumentException { + EventRecord eventRecord = EventRecord.builder() + .setDistinctId("abc") + .isLoginId(true) + .setEventName("test") + .addProperty("$track_id", 111) + .build(); + sa.track(eventRecord); + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void checkProfileSetDataTypeEventBuilder() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("number1", 1234) + .addProperty("date1", date) + .addProperty("String1", "str") + .addProperty("boolean1", false) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .build(); + sa.profileSet(userRecord); + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileSetOnceDataTypeEventBuilder() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("number1", 1234) + .addProperty("date1", date) + .addProperty("String1", "str") + .addProperty("boolean1", false) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .build(); + sa.profileSetOnce(userRecord); + } + + /** + * 校验自定义属性格式是否正常 + */ + @Test + public void testProfileIncrementEventBuilder() throws InvalidArgumentException { + List list = new ArrayList<>(); + Date date = new Date(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("number1", 1234) + .addProperty("$track_id", 111) + .build(); + sa.profileIncrement(userRecord); + } + + @Test + public void testProfileAppendByIdEventBuilder() throws InvalidArgumentException{ + List list = new ArrayList<>(); + list.add("aaa"); + list.add("bbb"); + UserRecord userRecord = UserRecord.builder() + .setDistinctId("123") + .isLoginId(true) + .addProperty("list1", list) + .addProperty("$track_id", 111) + .build(); + try { + sa.profileAppend(userRecord); + fail("profileAppend should throw InvalidArgumentException"); + }catch (InvalidArgumentException e){ + } + } + } diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2BuilderTest.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2BuilderTest.java index b8b49a6..b49c307 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2BuilderTest.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2BuilderTest.java @@ -186,11 +186,11 @@ public void testProfileUnsetById() throws InvalidArgumentException{ UserRecord userRecord = UserRecord.builder() .setDistinctId("123") .isLoginId(true) - .addProperty("list1", list) + .addProperty("list1", true) .build(); sa.profileUnset(userRecord); Map messageListult = (Map) messageList.get(0).get("properties"); - assertEquals(list, messageListult.get("list1")); + assertEquals(true, messageListult.get("list1")); assertEquals("123", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2Test.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2Test.java index 864a64c..0dd71bc 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2Test.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2Test.java @@ -57,7 +57,7 @@ public void init() throws NoSuchFieldException, IllegalAccessException { public void checkTrackEventLoginTrue() throws InvalidArgumentException { Map properties = new HashMap<>(); properties.put("test", "test"); -// properties.put("$project", "abc"); + properties.put("$project", "default"); properties.put("$token", "123"); sa.track("123", true, "test", properties); @@ -219,7 +219,7 @@ public void testProfileIncrement01() throws InvalidArgumentException { sa.profileIncrement("123", true, "number1", 1234); // 属性检查 Map messageListult = (Map) messageList.get(0).get("properties"); - assertEquals(1234, messageListult.get("number1")); + assertEquals(1234L, messageListult.get("number1")); // 其他字段检查 assertTrue((Boolean) messageListult.get("$is_login_id")); assertEquals("123", messageList.get(0).get("distinct_id")); diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2TestDebugConsumer.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2TestDebugConsumer.java index 52cfea3..c441020 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2TestDebugConsumer.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel2TestDebugConsumer.java @@ -42,8 +42,8 @@ public void init() throws NoSuchFieldException, IllegalAccessException { @Test public void checkTrackEventLoginTrue() throws InvalidArgumentException { Map properties = new HashMap<>(); + properties.put("$time", new Date()); sa.track("123", true, "test", properties); - } /** @@ -312,7 +312,6 @@ public void testProfileUnsetByIdEventBuilder() throws InvalidArgumentException{ UserRecord userRecord = UserRecord.builder() .setDistinctId("123") .isLoginId(true) - .addProperty("list1", list) .build(); sa.profileUnset(userRecord); } diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestBind.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestBind.java index 6789ab2..093bf58 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestBind.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestBind.java @@ -36,7 +36,8 @@ private void initConcurrentLoggingConsumer() throws NoSuchFieldException, Illega } private void initBatchConsumer() throws NoSuchFieldException, IllegalAccessException { - String url = "http://10.120.73.51:8106/sa?project=default&token="; +// String url = "http://10.120.73.51:8106/sa?project=default&token="; + String url = "http://10.120.111.143:8106/sa?project=default"; batchConsumer = new BatchConsumer(url, 100, true, 3); Field field = batchConsumer.getClass().getDeclaredField("messageList"); field.setAccessible(true); @@ -96,7 +97,7 @@ public void testIdMappingBindIdentity() throws InvalidArgumentException { Map lib = (Map) messageList.get(0).get("properties"); Boolean isLogin = (Boolean) lib.get("$is_login_id"); - assertFalse(isLogin); + assertNull(isLogin); // v3.4.2 新增逻辑:distinct_id 取第一个维度标识作为 distinct_id,且取值格式为 key+value; assertEquals("$identity_mobile+123", messageList.get(0).get("distinct_id")); @@ -322,7 +323,7 @@ public void testUnbindUserId02() throws InvalidArgumentException { saTmp.unbind(identity); Map lib = (Map) messageList.get(0).get("properties"); Boolean isLogin = (Boolean) lib.get("$is_login_id"); - assertFalse(isLogin); + assertNull(isLogin); assertEquals("id_test1+id_value1", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("identities")); diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestProfile.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestProfile.java index aedddb8..19d2249 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestProfile.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestProfile.java @@ -120,7 +120,7 @@ public void testProfileSetById02() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_mobile+13800000001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -205,7 +205,7 @@ public void testProfileSetOnceById02() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_mobile+13800000001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -232,7 +232,7 @@ public void testProfileSetOnceById() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); @@ -317,7 +317,7 @@ public void testProfileIncrementById02() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_mobile+13800000001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -340,7 +340,7 @@ public void testProfileIncrementById() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); @@ -368,7 +368,7 @@ public void testProfileAppendById() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(list, props.get("favorite")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); @@ -460,7 +460,7 @@ public void testProfileAppendById02() throws InvalidArgumentException{ assertEquals("13800000001", identities.get("$identity_mobile")); Map props = (Map)messageList.get(0).get("properties"); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_mobile+13800000001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -487,7 +487,8 @@ public void testProfileUnsetById() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(true, props.get("favorite")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); @@ -573,7 +574,7 @@ public void testProfileUnsetById02() throws InvalidArgumentException{ assertEquals("13800000001", identities.get("$identity_mobile")); Map props = (Map)messageList.get(0).get("properties"); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_mobile+13800000001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -595,7 +596,7 @@ public void testProfileDeleteById() throws InvalidArgumentException{ assertEquals("123", identities.get("$identity_email")); Map props = (Map)messageList.get(0).get("properties"); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); @@ -679,7 +680,7 @@ public void testProfileDeleteById02() throws InvalidArgumentException{ assertEquals("13800000001", identities.get("$identity_mobile")); Map props = (Map)messageList.get(0).get("properties"); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_mobile+13800000001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -716,7 +717,7 @@ public void testProfileSetByIdNew() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("xc001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -748,7 +749,7 @@ public void testProfileSetByIdNew01() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("xc001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -810,7 +811,7 @@ public void testProfileSetByIdNew03() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_email+123@qq.com", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -857,7 +858,7 @@ public void testProfileSetOnceByIdNew() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("xc001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -889,7 +890,7 @@ public void testProfileSetOnceByIdNew01() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("xc001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -951,7 +952,7 @@ public void testProfileSetOnceByIdNew03() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_email+123@qq.com", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -998,7 +999,7 @@ public void testProfileIncrementByIdNew() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("xc001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -1030,7 +1031,7 @@ public void testProfileIncrementByIdNew01() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("xc001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -1092,7 +1093,7 @@ public void testProfileIncrementByIdNew03() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(1, props.get("age")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_email+123@qq.com", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -1143,7 +1144,7 @@ public void testProfileAppendByIdNew() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(list, props.get("favorite")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("xc001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -1179,7 +1180,7 @@ public void testProfileAppendByIdNew01() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(list, props.get("favorite")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("xc001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -1249,7 +1250,7 @@ public void testProfileAppendByIdNew03() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(list, props.get("favorite")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_email+123@qq.com", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -1301,7 +1302,7 @@ public void testProfileUnsetByIdNew() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(true, props.get("favorite")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("xc001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -1337,7 +1338,7 @@ public void testProfileUnsetByIdNew01() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(true, props.get("favorite")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("xc001", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); @@ -1407,7 +1408,7 @@ public void testProfileUnsetByIdNew03() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); assertEquals(true, props.get("favorite")); - assertEquals(false, props.get("$is_login_id")); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_email+123@qq.com", messageList.get(0).get("distinct_id")); assertNotNull(messageList.get(0).get("time")); diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackById.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackById.java index 7839749..a728622 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackById.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackById.java @@ -23,7 +23,8 @@ public class IDMappingModel3TestTrackById extends SensorsBaseTest { @Before public void init() throws NoSuchFieldException, IllegalAccessException { - String url = "http://10.120.73.51:8106/sa?project=default&token="; +// String url = "http://10.120.73.51:8106/sa?project=default&token="; + String url = "http://10.120.111.143:8106/sa?project=default"; // 注意要设置 bulkSize 稍微大一点,这里设置为 100,否则超过 1 条就上报,messageList 里面拿不到事件数据 batchConsumer = new BatchConsumer(url, 100, true, 3); // 通过反射机制获取 BatchConsumer 的 messageList @@ -38,8 +39,8 @@ private void assertNotNullProp(){ assertNotNull(messageList.get(0).get("time")); assertNotNull(messageList.get(0).get("_track_id")); assertNotNull(messageList.get(0).get("properties")); - assertNotNull(messageList.get(0).get("project")); - assertNotNull(messageList.get(0).get("token")); +// assertNotNull(messageList.get(0).get("project")); +// assertNotNull(messageList.get(0).get("token")); } @Test @@ -115,7 +116,7 @@ public void testTrackByIdMultiIdentity02() throws InvalidArgumentException{ Map props = (Map)messageList.get(0).get("properties"); Boolean isLoginID = (Boolean)props.get("$is_login_id"); - assertFalse(isLoginID); + assertFalse(props.containsKey("$is_login_id")); saTmp.flush(); } @@ -521,7 +522,7 @@ public void testTrackByIdIDMEventRecord() throws InvalidArgumentException { Map props = (Map)messageList.get(0).get("properties"); Boolean isLoginID = (Boolean)props.get("$is_login_id"); - assertFalse(isLoginID); + assertFalse(props.containsKey("$is_login_id")); assertEquals("disId123", messageList.get(0).get("distinct_id")); assertEquals("test", messageList.get(0).get("event")); @@ -548,7 +549,7 @@ public void testTrackByIdIDMEventRecord01() throws InvalidArgumentException { Map props = (Map)messageList.get(0).get("properties"); Boolean isLoginID = (Boolean)props.get("$is_login_id"); - assertFalse(isLoginID); + assertFalse(props.containsKey("$is_login_id")); assertEquals("disId123", messageList.get(0).get("distinct_id")); assertEquals("test", messageList.get(0).get("event")); @@ -604,7 +605,7 @@ public void testTrackByIdIDMEventRecord03() throws InvalidArgumentException { Map props = (Map)messageList.get(0).get("properties"); Boolean isLoginID = (Boolean)props.get("$is_login_id"); - assertFalse(isLoginID); + assertFalse(props.containsKey("$is_login_id")); assertEquals("$identity_mobile+1300000055", messageList.get(0).get("distinct_id")); assertEquals("test", messageList.get(0).get("event")); @@ -744,5 +745,28 @@ public void testTrackByIdIDMEventRecordInvalid05() { } } + /** + * v3.4.4 新增功能: properties 可传入的 $track_id + */ + @Test + public void testTrackId() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .addIdentityProperty(SensorsAnalyticsIdentity.MOBILE, "13800000001") + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "email1@qq.com") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + + saTmp.trackById(identity, "testMultiIdentity", properties); + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } } diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackID.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackID.java new file mode 100644 index 0000000..cd7a4f5 --- /dev/null +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackID.java @@ -0,0 +1,654 @@ +package com.sensorsdata.analytics.javasdk; + +import com.sensorsdata.analytics.javasdk.bean.IDMEventRecord; +import com.sensorsdata.analytics.javasdk.bean.IDMUserRecord; +import com.sensorsdata.analytics.javasdk.bean.SensorsAnalyticsIdentity; +import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer; +import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.util.*; + +import static org.junit.Assert.*; + +/** + * 适用于 v3.4.4+ 版本 + * 测试点:验证事件的 _track_id 正常由 $track_id 生成,不在下面的 testcase 上一一说明 + */ +public class IDMappingModel3TestTrackID extends SensorsBaseTest { + private BatchConsumer batchConsumer; + + private List> messageList; + SensorsAnalytics saTmp; + + @Before + public void init() throws NoSuchFieldException, IllegalAccessException { +// String url = "http://10.120.111.143:8106/sa?project=default"; + String url = "http://10.120.101.188:8106/sa?project=default"; + // 注意要设置 bulkSize 稍微大一点,这里设置为 100,否则超过 1 条就上报,messageList 里面拿不到事件数据 + batchConsumer = new BatchConsumer(url, 100, true, 3); + // 通过反射机制获取 BatchConsumer 的 messageList + Field field = batchConsumer.getClass().getDeclaredField("messageList"); // messageList 是 BatchConsumer 用来暂存事件数据的成员变量 + field.setAccessible(true); + messageList = (List>) field.get(batchConsumer); + saTmp = new SensorsAnalytics(batchConsumer); + } + + private void assertNotNullProp(){ + assertNotNull(messageList.get(0).get("identities")); + assertNotNull(messageList.get(0).get("time")); + assertNotNull(messageList.get(0).get("_track_id")); + assertNotNull(messageList.get(0).get("properties")); + } + + @Test + public void testSingleIdentity() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + + saTmp.trackById(identity, "test", properties); + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 测试点: properties 传入错误的 $track_id:非 int 类型- float 类型 + * @throws InvalidArgumentException + */ + @Test + public void testInvalidTrackId() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", 111.123); + + saTmp.trackById(identity, "test", properties); + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertNotEquals(111.123, messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 测试点: properties 传入错误的 $track_id:非 int 类型- String 类型 + * @throws InvalidArgumentException + */ + @Test + public void testInvalidTrackId01() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", "abc"); + + saTmp.trackById(identity, "test", properties); + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertNotEquals("abc", messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 测试点: properties 传入错误的 $track_id:null + * 【已知问题】 https://jira.sensorsdata.cn/browse/SDK-4841 + * @throws InvalidArgumentException + */ + @Test + @Ignore + public void testInvalidTrackId02() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", null); + + saTmp.track("xccc", true, "test1", properties); +// saTmp.trackById(identity, "test", properties); + saTmp.trackById(identity, "test",null); + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertNotEquals("abc", messageList.get(0).get("_track_id")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + /** + * 测试点: properties 传入错误的 $time:非 Date 类型- number 类型 + * [已知问题] 直接抛出异常 InvalidArgumentException: The property '$time' should be a java.util.Date. + * @throws InvalidArgumentException + */ + @Test + @Ignore + public void testInvalidTime() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + Date date = new Date(); + properties.put("$time", date.getTime()); // Long 型时间戳 + + saTmp.trackById(identity, "test", properties); + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertNotEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$time")); // properties 不包含 $track_id + } + + /** + * 测试点: properties 传入错误的 $time:非 Date 类型- String 类型 + * [已知问题] 直接抛出异常 InvalidArgumentException: The property '$time' should be a java.util.Date. + * @throws InvalidArgumentException + */ + @Test + @Ignore + public void testInvalidTime01() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$time", "abc"); + + saTmp.trackById(identity, "test", properties); + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertNotEquals("abc", messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$time")); // properties 不包含 $track_id + } + + /** + * 测试点: properties 传入错误的 $time:非 Date 类型- null + * [已知问题] 直接抛出异常 InvalidArgumentException: The property '$time' should be a java.util.Date. + * @throws InvalidArgumentException + */ + @Test + @Ignore + public void testInvalidTime02() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$time", null); + + saTmp.trackById(identity, "test", properties); + saTmp.trackById(identity, "test",null); + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertNotEquals("abc", messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$time")); // properties 不包含 $track_id + } + + /** + * 测试点:$time 超过服务端时间窗口 + * @throws InvalidArgumentException + */ + @Test + public void testInvalidTime03() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$time", new Date(1671828405241L)); + + saTmp.trackById(identity, "test", properties); + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertNotEquals("abc", messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$time")); // properties 不包含 $track_id + } + + + /** + * 【已知问题】https://jira.sensorsdata.cn/browse/SDK-4848 + */ + @Test + public void testIdMappingBind() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("$identity_mobile", "123") + .addIdentityProperty("$identity_email", "fz@163.com") + .build(); + saTmp.bind(identity); + } + + + /** + * 【已知问题】https://jira.sensorsdata.cn/browse/SDK-4848 + */ + @Test + public void checkUnbindUserId() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.unbind(identity); + + } + + /** + * 测试点:公共属性传入 $track_id + */ + @Test + public void checkTrackByIdSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.trackById(identity, "eee", null); + + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertNotEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("abc")); // properties 包含其他自定义属性 + } + + /** + * 测试点:公共属性传入 $track_id :$track_id 传入 String 类型 + */ + @Test + public void checkTrackByIdSuperProperties01() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", "aaa"); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.trackById(identity, "eee", null); + + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertNotEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("abc")); // properties 包含其他自定义属性 + } + + + /** + * 测试点:公共属性传入 $track_id + */ + @Test + public void checkBindSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .addIdentityProperty("eee", "123") + .build(); + saTmp.bind(identity); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertNotEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("abc")); // properties 包含其他自定义属性 + + } + + /** + * 测试点:公共属性传入 $track_id + */ + @Test + public void checkUnbindSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.unbind(identity); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertNotEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("abc")); // properties 包含其他自定义属性 + } + + /** + * v3.4.2 后新增接口 trackById(@NonNull IDMEventRecord idmEventRecord) 测试 + * 测试点:IDMEventRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + */ + @Test + public void testTrackByIdIDMEventRecord() throws InvalidArgumentException { + IDMEventRecord record = IDMEventRecord.starter() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .setDistinctId("disId123") + .setEventName("test") + .addProperty("$track_id", 111) + .addProperty("abc", 111) + .build(); + + saTmp.trackById(record); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("abc")); // properties 包含其他自定义属性 + } + + + /** + * SensorsAnalyticsIdentity 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetById() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("age", 1); + properties.put("$track_id", 111); + + saTmp.profileSetById(identity, properties); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("age")); // properties 包含其他自定义属性 + } + + /** + * SensorsAnalyticsIdentity 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetOnesById00() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("age", 1); + properties.put("$track_id", 111); + saTmp.profileSetOnceById(identity, properties); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("age")); // properties 包含其他自定义属性 + } + + /** + * SensorsAnalyticsIdentity 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileIncrementById00() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("age", 1); + properties.put("$track_id", 111); + + saTmp.profileIncrementById(identity, properties); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("age")); // properties 包含其他自定义属性 + } + + @Test + public void testProfileAppendById() { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123") + .build(); + List list = new ArrayList<>(); + list.add("apple"); + list.add("orange"); + Map properties = new HashMap<>(); + properties.put("favorite", list); + properties.put("$track_id", 111); + try { + saTmp.profileAppendById(identity, properties); + fail("profileAppendById should throw InvalidArgumentException."); + }catch (InvalidArgumentException e){ + assertEquals("The property value of PROFILE_APPEND should be a List.", e.getMessage()); + e.printStackTrace(); + } + } + + @Test + public void testProfileUnsetById() { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123") + .build(); + List list = new ArrayList<>(); + list.add("apple"); + list.add("orange"); + Map properties = new HashMap<>(); + properties.put("favorite", true); + properties.put("$track_id", 111); + + try { + saTmp.profileUnsetById(identity, properties); + fail("profileUnsetById should throw InvalidArgumentException."); + }catch (InvalidArgumentException e){ + assertEquals("The property value of [$track_id] should be true.", e.getMessage()); + e.printStackTrace(); + } + } + + // profileDeleteById + // TODO + @Test + public void testProfileDeleteById() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123") + .build(); + + saTmp.profileDeleteById(identity); + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetByIdNew() throws InvalidArgumentException { + // 新版本接口 + IDMUserRecord userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("age", 1) // 设置埋点事件属性 + .addProperty("$track_id", 111) // 设置 $track_id + .build(); + saTmp.profileSetById(userRecord); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("age")); // properties 包含其他自定义属性 + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetOnceByIdNew() throws InvalidArgumentException { + // 新版本接口 + IDMUserRecord userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("age", 1) // 设置埋点事件属性 + .addProperty("$track_id", 111) // 设置 $track_id + .build(); + saTmp.profileSetOnceById(userRecord); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("age")); // properties 包含其他自定义属性 + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileIncrementByIdNew() throws InvalidArgumentException { + // 新版本接口 + IDMUserRecord userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("age", 1) // 设置埋点事件属性 + .addProperty("$track_id", 111) // 设置 $track_id + .build(); + saTmp.profileIncrementById(userRecord); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("age")); // properties 包含其他自定义属性 + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileAppendByIdNew() { + List list = new ArrayList<>(); + list.add("apple"); + list.add("orange"); + + try { + // 新版本接口 + IDMUserRecord userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("favorite", list) // 设置埋点事件属性 + .addProperty("$track_id", 111) // 设置 $track_id + .build(); + + saTmp.profileAppendById(userRecord); + fail("profileAppendById should throw InvalidArgumentException."); + }catch (InvalidArgumentException e){ + assertEquals("The property value of PROFILE_APPEND should be a List.", e.getMessage()); + e.printStackTrace(); + } + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileUnsetByIdNew() { + List list = new ArrayList<>(); + list.add("apple"); + list.add("orange"); + + // 新版本接口 + IDMUserRecord userRecord = null; + try { + userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("favorite", true) // 设置埋点事件属性 + .addProperty("$track_id", 111) // 设置 $track_id + .build(); + + saTmp.profileUnsetById(userRecord); + fail("profileUnsetById should throw InvalidArgumentException."); + }catch (InvalidArgumentException e){ + assertEquals("The property value of $track_id should be true.", e.getMessage()); + e.printStackTrace(); + } + } + +} diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackIDDebugConsumer.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackIDDebugConsumer.java new file mode 100644 index 0000000..af51bb5 --- /dev/null +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackIDDebugConsumer.java @@ -0,0 +1,284 @@ +package com.sensorsdata.analytics.javasdk; + +import com.sensorsdata.analytics.javasdk.bean.IDMEventRecord; +import com.sensorsdata.analytics.javasdk.bean.IDMUserRecord; +import com.sensorsdata.analytics.javasdk.bean.SensorsAnalyticsIdentity; +import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer; +import com.sensorsdata.analytics.javasdk.consumer.DebugConsumer; +import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertNotNull; + +/** + * 适用于 v3.4.4+ 版本 + * 测试数据是否能正常上传 + */ +public class IDMappingModel3TestTrackIDDebugConsumer extends SensorsBaseTest { + private BatchConsumer batchConsumer; + + private List> messageList; + SensorsAnalytics saTmp; + + @Before + public void init() { +// String url = "http://10.120.73.51:8106/sa?project=default&token="; + String url = "http://10.120.111.143:8106/sa?project=default"; +// String url = "http://10.120.101.188:8106/sa?project=default"; + DebugConsumer debugConsumer = new DebugConsumer(url, true); + saTmp = new SensorsAnalytics(debugConsumer); + } + + private void assertNotNullProp(){ + assertNotNull(messageList.get(0).get("identities")); + assertNotNull(messageList.get(0).get("time")); + assertNotNull(messageList.get(0).get("_track_id")); + assertNotNull(messageList.get(0).get("properties")); + } + + @Test + public void testSingleIdentity() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + + saTmp.trackById(identity, "test", properties); + + } + + + /** + * 校验 ID-Mapping bind 接口 + */ + @Test + public void testIdMappingBind() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("$identity_mobile", "123") + .addIdentityProperty("$identity_email", "fz@163.com") + .build(); + saTmp.bind(identity); + } + + + /** + * 校验 ID-Mapping unbind 接口用户格式 + */ + @Test + public void checkUnbindUserId() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.unbind(identity); + + } + + /** + * 校验 ID-Mapping 公共属性 + */ + @Test + public void checkTrackByIdSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.trackById(identity, "eee", null); + + + } + + /** + * 校验 ID-Mapping 公共属性 + */ + @Test + public void checkTrackByIdSuperProperties01() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", "aaa"); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.trackById(identity, "eee", null); + + + } + + + /** + * 校验 ID-Mapping bind 接口公共属性 + */ + @Test + public void checkBindSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .addIdentityProperty("eee", "123") + .build(); + saTmp.bind(identity); + } + + /** + * 校验 ID_Mapping unbind 接口公共属性 + */ + @Test + public void checkUnbindSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.unbind(identity); + } + + /** + * v3.4.2 后新增接口 trackById(@NonNull IDMEventRecord idmEventRecord) 测试 + * 测试点:IDMEventRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + */ + @Test + public void testTrackByIdIDMEventRecord() throws InvalidArgumentException { + IDMEventRecord record = IDMEventRecord.starter() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .setDistinctId("disId123") + .setEventName("test") + .addProperty("$track_id", 111) + .addProperty("abc", 111) + .build(); + + saTmp.trackById(record); + } + + + /** + * SensorsAnalyticsIdentity 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetById() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("age", 1); + properties.put("$track_id", 111); + + saTmp.profileSetById(identity, properties); + } + + /** + * SensorsAnalyticsIdentity 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetOnesById00() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("age", 1); + properties.put("$track_id", 111); + saTmp.profileSetOnceById(identity, properties); + + + } + + /** + * SensorsAnalyticsIdentity 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileIncrementById00() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("age", 1); + properties.put("$track_id", 111); + + saTmp.profileIncrementById(identity, properties); + + + } + + // profileDeleteById + // TODO + @Test + public void testProfileDeleteById() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123") + .build(); + + saTmp.profileDeleteById(identity); + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetByIdNew() throws InvalidArgumentException { + // 新版本接口 + IDMUserRecord userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("age", 1) // 设置埋点事件属性 + .addProperty("$track_id", 111) // 设置 $track_id + .build(); + saTmp.profileSetById(userRecord); + + + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetOnceByIdNew() throws InvalidArgumentException { + // 新版本接口 + IDMUserRecord userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("age", 1) // 设置埋点事件属性 + .addProperty("$track_id", 111) // 设置 $track_id + .build(); + saTmp.profileSetOnceById(userRecord); + + + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileIncrementByIdNew() throws InvalidArgumentException { + // 新版本接口 + IDMUserRecord userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("age", 1) // 设置埋点事件属性 + .addProperty("$track_id", 111) // 设置 $track_id + .build(); + saTmp.profileIncrementById(userRecord); + } + + +} diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackIdAndTime.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackIdAndTime.java new file mode 100644 index 0000000..16ea2b3 --- /dev/null +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackIdAndTime.java @@ -0,0 +1,528 @@ +package com.sensorsdata.analytics.javasdk; + +import com.sensorsdata.analytics.javasdk.bean.IDMEventRecord; +import com.sensorsdata.analytics.javasdk.bean.IDMUserRecord; +import com.sensorsdata.analytics.javasdk.bean.SensorsAnalyticsIdentity; +import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer; +import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.util.*; + +import static org.junit.Assert.*; + +/** + * 适用于 v3.4.4+ 版本 + * 测试点:验证事件的 _track_id 正常由 $track_id 生成,time 由 $time 生成。 + * 无特殊情况,不在下面的 testcase 上一一说明 + */ +public class IDMappingModel3TestTrackIdAndTime extends SensorsBaseTest { + private BatchConsumer batchConsumer; + + private List> messageList; + SensorsAnalytics saTmp; + + @Before + public void init() throws NoSuchFieldException, IllegalAccessException { + String url = "http://10.120.111.143:8106/sa?project=default"; + // 注意要设置 bulkSize 稍微大一点,这里设置为 100,否则超过 1 条就上报,messageList 里面拿不到事件数据 + batchConsumer = new BatchConsumer(url, 100, true, 3); + // 通过反射机制获取 BatchConsumer 的 messageList + Field field = batchConsumer.getClass().getDeclaredField("messageList"); // messageList 是 BatchConsumer 用来暂存事件数据的成员变量 + field.setAccessible(true); + messageList = (List>) field.get(batchConsumer); + saTmp = new SensorsAnalytics(batchConsumer); + } + + private void assertNotNullProp(){ + assertNotNull(messageList.get(0).get("identities")); + assertNotNull(messageList.get(0).get("time")); + assertNotNull(messageList.get(0).get("_track_id")); + assertNotNull(messageList.get(0).get("properties")); + } + + @Test + public void testSingleIdentity() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.trackById(identity, "test", properties); + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + } + + + /** + * 校验 ID-Mapping bind 接口 + */ + @Test + public void testIdMappingBind() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("$identity_mobile", "123") + .addIdentityProperty("$identity_email", "fz@163.com") + .build(); + saTmp.bind(identity); + } + + + /** + * 校验 ID-Mapping unbind 接口用户格式 + */ + @Test + public void checkUnbindUserId() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.unbind(identity); + + } + + /** + * 【已知问题】https://jira.sensorsdata.cn/browse/SDK-4863 + * 【Java SDK】【_track_id】公共属性设置 $time, 属性中也带 $time,同时这条事件的 time 也会由 $time 生成 + */ + @Test + @Ignore + public void checkTrackByIdSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.trackById(identity, "eee", null); + + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertNotEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + assertNotEquals(date.getTime(), messageList.get(0).get("time")); // 公共属性设置 $time 不生效的 + + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("abc")); // properties 包含其他自定义属性 + } + + /** + * 【已知问题】https://jira.sensorsdata.cn/browse/SDK-4863 + * 【Java SDK】【_track_id】公共属性设置 $time, 属性中也带 $time,同时这条事件的 time 也会由 $time 生成 + */ + @Test + @Ignore + public void checkTrackByIdSuperProperties01() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", "aaa"); + Date date = new Date(); + properties.put("$time", date); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.trackById(identity, "eee", null); + + assertEquals(1, messageList.size()); + + assertNotNullProp(); + + assertNotEquals("aaa", messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + assertNotEquals(date.getTime(), messageList.get(0).get("time")); // 公共属性设置 $time 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("abc")); // properties 包含其他自定义属性 + } + + + /** + * 【已知问题】https://jira.sensorsdata.cn/browse/SDK-4863 + * 【Java SDK】【_track_id】公共属性设置 $time, 属性中也带 $time,同时这条事件的 time 也会由 $time 生成 + */ + @Test + public void checkBindSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .addIdentityProperty("eee", "123") + .build(); + saTmp.bind(identity); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertNotEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("abc")); // properties 包含其他自定义属性 + + } + + /** + * 【已知问题】https://jira.sensorsdata.cn/browse/SDK-4863 + * 【Java SDK】【_track_id】公共属性设置 $time, 属性中也带 $time,同时这条事件的 time 也会由 $time 生成 + */ + @Test + public void checkUnbindSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.unbind(identity); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertNotEquals(111, messageList.get(0).get("_track_id")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("abc")); // properties 包含其他自定义属性 + } + + /** + * v3.4.2 后新增接口 trackById(@NonNull IDMEventRecord idmEventRecord) 测试 + * 测试点:IDMEventRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + */ + @Test + public void testTrackByIdIDMEventRecord() throws InvalidArgumentException { + Date date = new Date(); + IDMEventRecord record = IDMEventRecord.starter() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .setDistinctId("disId123") + .setEventName("test") + .addProperty("$track_id", 111) + .addProperty("$time", date) + .addProperty("abc", 111) + .build(); + + saTmp.trackById(record); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("abc")); // properties 包含其他自定义属性 + } + + + /** + * SensorsAnalyticsIdentity 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetById() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("age", 1); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.profileSetById(identity, properties); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("age")); // properties 包含其他自定义属性 + } + + /** + * SensorsAnalyticsIdentity 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetOnesById00() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("age", 1); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + saTmp.profileSetOnceById(identity, properties); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("age")); // properties 包含其他自定义属性 + } + + /** + * SensorsAnalyticsIdentity 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileIncrementById00(){ + try { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("age", 1); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.profileIncrementById(identity, properties); + fail("profileIncrementById should throw InvalidArgumentException."); + }catch (InvalidArgumentException e){ + assertEquals("The property value of PROFILE_INCREMENT should be a Number.The current type is class java.util.Date.", e.getMessage()); + e.printStackTrace(); + } + } + + @Test + public void testProfileAppendById() { + try{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123") + .build(); + List list = new ArrayList<>(); + list.add("apple"); + list.add("orange"); + Map properties = new HashMap<>(); + properties.put("favorite", list); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + saTmp.profileAppendById(identity, properties); + + fail("profileAppendById should throw InvalidArgumentException."); + }catch(InvalidArgumentException e){ + String msg = "The property value of PROFILE_APPEND should be a List."; + assertEquals(msg, e.getMessage()); + e.printStackTrace(); + } + } + + @Test + public void testProfileUnsetById() { + try{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123") + .build(); + List list = new ArrayList<>(); + list.add("apple"); + list.add("orange"); + Map properties = new HashMap<>(); + properties.put("favorite", true); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.profileUnsetById(identity, properties); + + fail("profileUnsetById should throw InvalidArgumentException."); + }catch(InvalidArgumentException e){ + String msg = "The property value of [$time] should be true."; + assertEquals(msg, e.getMessage()); + e.printStackTrace(); + } + } + + // profileDeleteById + // TODO + @Test + public void testProfileDeleteById() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123") + .build(); + + saTmp.profileDeleteById(identity); + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetByIdNew() throws InvalidArgumentException { + // 新版本接口 + Date date = new Date(); + IDMUserRecord userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("age", 1) // 设置埋点事件属性 + .addProperty("$track_id", 111) + .addProperty("$time", date) // 设置 $track_id + .build(); + saTmp.profileSetById(userRecord); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("age")); // properties 包含其他自定义属性 + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetOnceByIdNew() throws InvalidArgumentException { + // 新版本接口 + Date date = new Date(); + IDMUserRecord userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("age", 1) // 设置埋点事件属性 + .addProperty("$track_id", 111) + .addProperty("$time", date) // 设置 $track_id + .build(); + saTmp.profileSetOnceById(userRecord); + + assertEquals(1, messageList.size()); + assertNotNullProp(); + + assertEquals(111, messageList.get(0).get("_track_id")); + assertEquals(date.getTime(), messageList.get(0).get("time")); // 公共属性设置 $track_id 不生效的 + + Map props = (Map)messageList.get(0).get("properties"); + assertFalse(props.containsKey("$track_id")); // properties 不包含 $track_id + assertTrue(props.containsKey("age")); // properties 包含其他自定义属性 + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileIncrementByIdNew() { + // 新版本接口 + Date date = new Date(); + IDMUserRecord userRecord = null; + try { + userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("age", 1) // 设置埋点事件属性 + .addProperty("$track_id", 111) + .addProperty("$time", date) // 设置 $track_id + .build(); + saTmp.profileIncrementById(userRecord); + fail("profileIncrementById should throw InvalidArgumentException."); + } catch (InvalidArgumentException e) { + assertEquals("The property value of PROFILE_INCREMENT should be a Number.The current type is class java.util.Date.", e.getMessage()); + e.printStackTrace(); + } + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileAppendByIdNew() { + List list = new ArrayList<>(); + list.add("apple"); + list.add("orange"); + + // 新版本接口 + Date date = new Date(); + IDMUserRecord userRecord = null; + try { + userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("favorite", list) // 设置埋点事件属性 + .addProperty("$track_id", 111) + .addProperty("$time", date) // 设置 $track_id + .build(); + saTmp.profileAppendById(userRecord); + fail("profileAppendById should throw InvalidArgumentException."); + }catch(InvalidArgumentException e){ + String msg = "The property value of PROFILE_APPEND should be a List."; + assertEquals(msg, e.getMessage()); + e.printStackTrace(); + } + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileUnsetByIdNew() { + List list = new ArrayList<>(); + list.add("apple"); + list.add("orange"); + + // 新版本接口 + Date date = new Date(); + IDMUserRecord userRecord = null; + try { + userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("favorite", true) // 设置埋点事件属性 + .addProperty("$track_id", 111) + .addProperty("$time", date) // 设置 $track_id + .build(); + saTmp.profileUnsetById(userRecord); + fail("profileUnsetById should throw InvalidArgumentException."); + }catch(InvalidArgumentException e){ + String msg = "The property value of $time should be true."; + assertEquals(msg, e.getMessage()); + e.printStackTrace(); + } + } + +} diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackIdAndTimeDebugConsumer.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackIdAndTimeDebugConsumer.java new file mode 100644 index 0000000..7c9a4d5 --- /dev/null +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingModel3TestTrackIdAndTimeDebugConsumer.java @@ -0,0 +1,373 @@ +package com.sensorsdata.analytics.javasdk; + +import com.sensorsdata.analytics.javasdk.bean.IDMEventRecord; +import com.sensorsdata.analytics.javasdk.bean.IDMUserRecord; +import com.sensorsdata.analytics.javasdk.bean.SensorsAnalyticsIdentity; +import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer; +import com.sensorsdata.analytics.javasdk.consumer.DebugConsumer; +import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import org.junit.Before; +import org.junit.Test; + +import java.util.*; + +import static org.junit.Assert.*; + +/** + * 适用于 v3.4.4+ 版本 + * 测试数据是否能正常上传 + */ +public class IDMappingModel3TestTrackIdAndTimeDebugConsumer extends SensorsBaseTest { + private BatchConsumer batchConsumer; + + private List> messageList; + SensorsAnalytics saTmp; + + @Before + public void init() { +// String url = "http://10.120.73.51:8106/sa?project=default&token="; + String url = "http://10.120.111.143:8106/sa?project=default"; +// String url = "http://10.120.101.188:8106/sa?project=default"; + DebugConsumer debugConsumer = new DebugConsumer(url, true); + saTmp = new SensorsAnalytics(debugConsumer); + } + + private void assertNotNullProp(){ + assertNotNull(messageList.get(0).get("identities")); + assertNotNull(messageList.get(0).get("time")); + assertNotNull(messageList.get(0).get("_track_id")); + assertNotNull(messageList.get(0).get("properties")); + } + + @Test + public void testTrackSame() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.trackById(identity, "test", properties); + saTmp.trackById(identity, "test", properties); + } + + @Test + public void testTrackDiffTrackId() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.trackById(identity, "test", properties); + + Map properties2 = new HashMap<>(); + properties2.put("$track_id", 222); + properties2.put("$time", date); + saTmp.trackById(identity, "test", properties2); + } + + @Test + public void testTrackDiffTime() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.trackById(identity, "test", properties); + + Map properties2 = new HashMap<>(); + properties2.put("$track_id", 111); + properties2.put("$time", new Date()); + saTmp.trackById(identity, "test", properties2); + } + + @Test + public void testTrackDiffEventName() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.trackById(identity, "test111", properties); + saTmp.trackById(identity, "test222", properties); + } + + @Test + public void testTrackDiffUserID() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + SensorsAnalyticsIdentity identity2 = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.MOBILE, "122255555") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.trackById(identity, "test", properties); + saTmp.trackById(identity2, "test", properties); + } + + + @Test + public void testSingleIdentity() throws InvalidArgumentException{ + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.trackById(identity, "test", properties); + } + + + /** + * 校验 ID-Mapping bind 接口 + */ + @Test + public void testIdMappingBind() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("$identity_mobile", "123") + .addIdentityProperty("$identity_email", "fz@163.com") + .build(); + saTmp.bind(identity); + } + + + /** + * 校验 ID-Mapping unbind 接口用户格式 + */ + @Test + public void checkUnbindUserId() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.unbind(identity); + + } + + /** + * 校验 ID-Mapping 公共属性 + */ + @Test + public void checkTrackByIdSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.trackById(identity, "eee", null); + } + + /** + * 校验 ID-Mapping 公共属性 + */ + @Test + public void checkTrackByIdSuperProperties01() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", "aaa"); + Date date = new Date(); + properties.put("$time", date); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.trackById(identity, "eee", null); + } + + + /** + * 校验 ID-Mapping bind 接口公共属性 + */ + @Test + public void checkBindSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .addIdentityProperty("eee", "123") + .build(); + saTmp.bind(identity); +} + + /** + * 校验 ID_Mapping unbind 接口公共属性 + */ + @Test + public void checkUnbindSuperProperties() throws InvalidArgumentException { + Map properties = new HashMap<>(); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + properties.put("abc", 111); + saTmp.registerSuperProperties(properties); + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty("id_test1", "id_value1") + .build(); + saTmp.unbind(identity); + + + +} + + /** + * v3.4.2 后新增接口 trackById(@NonNull IDMEventRecord idmEventRecord) 测试 + * 测试点:IDMEventRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + */ + @Test + public void testTrackByIdIDMEventRecord() throws InvalidArgumentException { + Date date = new Date(); + IDMEventRecord record = IDMEventRecord.starter() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .setDistinctId("disId123") + .setEventName("test") + .addProperty("$track_id", 111) + .addProperty("$time", date) + .addProperty("abc", 111) + .build(); + + saTmp.trackById(record); +} + + + /** + * SensorsAnalyticsIdentity 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetById() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("age", 1); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.profileSetById(identity, properties); + + +} + + /** + * SensorsAnalyticsIdentity 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetOnesById00() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "123") + .build(); + Map properties = new HashMap<>(); + properties.put("age", 1); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + saTmp.profileSetOnceById(identity, properties); + + +} + + @Test + public void testProfileUnsetById() { + try { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123") + .build(); + List list = new ArrayList<>(); + list.add("apple"); + list.add("orange"); + Map properties = new HashMap<>(); + properties.put("favorite", true); + properties.put("$track_id", 111); + Date date = new Date(); + properties.put("$time", date); + + saTmp.profileUnsetById(identity, properties); + fail("profileUnsetById should throw InvalidArgumentException."); + }catch(InvalidArgumentException e){ + String msg = "The property value of [$time] should be true."; + assertEquals(msg, e.getMessage()); + e.printStackTrace(); + } +} + + // profileDeleteById + // TODO + @Test + public void testProfileDeleteById() throws InvalidArgumentException { + SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123") + .build(); + + saTmp.profileDeleteById(identity); + } + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetByIdNew() throws InvalidArgumentException { + // 新版本接口 + Date date = new Date(); + IDMUserRecord userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("age", 1) // 设置埋点事件属性 + .addProperty("$track_id", 111) + .addProperty("$time", date) // 设置 $track_id + .build(); + saTmp.profileSetById(userRecord); + + +} + + /** + * IDMUserRecord 传入正常 distinctId,identityMap 传入 $identity_login_id + * @throws InvalidArgumentException + */ + @Test + public void testProfileSetOnceByIdNew() throws InvalidArgumentException { + // 新版本接口 + Date date = new Date(); + IDMUserRecord userRecord = IDMUserRecord.starter() + .setDistinctId("xc001") //手动指定外层 distinct_id + .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "dis123") //用户维度标识 + .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, "123@qq.com") //用户维度标识 + .addProperty("age", 1) // 设置埋点事件属性 + .addProperty("$track_id", 111) + .addProperty("$time", date) // 设置 $track_id + .build(); + saTmp.profileSetOnceById(userRecord); + + +} + + + +} diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingProfileTest.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingProfileTest.java index d74e498..5cc981f 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingProfileTest.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingProfileTest.java @@ -1,17 +1,15 @@ package com.sensorsdata.analytics.javasdk; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - import com.sensorsdata.analytics.javasdk.bean.IDMUserRecord; import com.sensorsdata.analytics.javasdk.bean.SensorsAnalyticsIdentity; import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; - import org.junit.Test; import java.util.ArrayList; import java.util.Map; +import static org.junit.Assert.*; + /** * id-mapping profile 相关接口单元测试 * 版本支持 3.3.0+ @@ -76,8 +74,8 @@ public void checkProfileSuperProperties01() throws InvalidArgumentException { sa.profileSetById(userRecord); assertIDM3UserData(data); Map properties = (Map) data.get("properties"); - assertEquals(2, properties.size()); - assertTrue(properties.containsKey("$is_login_id")); + assertEquals(1, properties.size()); + assertFalse(properties.containsKey("$is_login_id")); assertTrue(properties.containsKey("test")); } diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingTrackTest.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingTrackTest.java index fcaa48c..69b9405 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingTrackTest.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/IDMappingTrackTest.java @@ -46,7 +46,7 @@ public void checkIdMappingEvent() throws InvalidArgumentException { assertEquals("login_id+123", data.get("distinct_id")); Map prop = (Map)data.get("properties"); - assertFalse((Boolean) prop.get("$is_login_id")); + assertFalse(data.containsKey("$is_login_id")); assertNotNull(data.get("properties")); assertNotNull(data.get("project")); assertNotNull(data.get("token")); @@ -379,7 +379,7 @@ public void checkEventRecordWithPresetProperty() throws InvalidArgumentException .build(); sa.trackById(eventRecord); assertIDM3EventData(data); - assertEquals(time, data.get("time")); + assertEquals(time.getTime(), data.get("time")); assertTrue(data.containsKey("project")); assertEquals("test", data.get("project")); assertTrue(data.containsKey("token")); diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/SensorsBaseTest.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/SensorsBaseTest.java index 2276bd9..025bceb 100644 --- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/SensorsBaseTest.java +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/SensorsBaseTest.java @@ -1,15 +1,7 @@ package com.sensorsdata.analytics.javasdk; -import static com.sensorsdata.analytics.javasdk.SensorsConst.LIB_METHOD_SYSTEM_ATTR; -import static com.sensorsdata.analytics.javasdk.SensorsConst.LIB_SYSTEM_ATTR; -import static com.sensorsdata.analytics.javasdk.SensorsConst.LIB_VERSION_SYSTEM_ATTR; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import com.sensorsdata.analytics.javasdk.consumer.Consumer; - import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; @@ -22,6 +14,9 @@ import java.util.List; import java.util.Map; +import static com.sensorsdata.analytics.javasdk.SensorsConst.*; +import static org.junit.Assert.*; + public class SensorsBaseTest { /** * 单元测试中若生成多条数据,则存放到该集合中 @@ -113,11 +108,21 @@ protected void assertUserData(Map data) { assertTrue("数据中没有 lib 节点!", data.containsKey("lib")); assertTrue("数据中没有 distinct_id 节点!", data.containsKey("distinct_id")); assertTrue("数据中没有 time 节点!", data.containsKey("time")); - //增加 time 类型校验 - assertTrue("数据中 time 节点类型不正确!", data.get("time") instanceof Long); assertTrue("数据中没有 type 节点!", data.containsKey("type")); + + //增加各字段类型校验 + assertTrue("数据中 time 节点类型不正确!", data.get("time") instanceof Long); + assertTrue("数据中 _track_id 节点类型不正确!", data.get("_track_id") instanceof Integer); + assertTrue("数据中 distinct_id 节点类型不正确!", data.get("distinct_id") instanceof String); + assertTrue("数据中 type 节点类型不正确!", data.get("type") instanceof String); + assertTrue("数据中 lib 节点类型不正确!", data.get("lib") instanceof Map); + + assertFalse("数据中包含 item_id 节点!", data.containsKey("item_id")); assertFalse("数据中包含 item_type 节点!", data.containsKey("item_type")); + if (data.containsKey(PROPERTIES)) { + assertProperties(data.get(PROPERTIES)); + } } /** @@ -168,7 +173,19 @@ protected void assertDataLib(Object lib) { assertTrue("lib 节点信息不存在 $lib 信息!", libMap.containsKey(LIB_SYSTEM_ATTR)); assertTrue("lib 节点信息不存在 $lib_version 信息!", libMap.containsKey(LIB_VERSION_SYSTEM_ATTR)); assertTrue("lib 节点信息不存在 $lib_method 信息!", libMap.containsKey(LIB_METHOD_SYSTEM_ATTR)); + assertTrue("lib 节点信息不存在 $lib_detail 信息!", libMap.containsKey(LIB_DETAIL_SYSTEM_ATTR)); + } + /** + * 校验 properties + * 校验原则:如果 properties 存在,判断 $time 和 $track_id 是否存在 + */ + protected void assertProperties(Object properties) { + assertTrue(properties instanceof Map); + Map propertiesMap = (Map) properties; + assertFalse("properties 中的 $time 属性并未删除", propertiesMap.containsKey(TIME_SYSTEM_ATTR)); + assertFalse("properties 中的 $track_id 属性并未删除", propertiesMap.containsKey(TRACK_ID)); + } } diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/TrackIdModeTest.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/TrackIdModeTest.java new file mode 100644 index 0000000..f4e9e7d --- /dev/null +++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/TrackIdModeTest.java @@ -0,0 +1,328 @@ +package com.sensorsdata.analytics.javasdk; + +import com.sensorsdata.analytics.javasdk.bean.*; +import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.*; + +/** + * 迁移 idMapping 1.0 逻辑和支持自定义 _track_id 单元测试 + * @author luowenjie + * @version 3.4.3 + * @since 2022/04/20 15:50 + */ +public class TrackIdModeTest extends SensorsBaseTest{ + private final static Map mp = new HashMap<>(); + private final String cookieId = "ABCDEF123456789"; + private final SensorsAnalyticsIdentity sensorsAnalyticsIdentity = + SensorsAnalyticsIdentity.builder().addIdentityProperty("cookeId",cookieId).build(); + + + /** + * mp :作为 properties + * 功能: 填充 map + */ + @BeforeClass + public static void buildMap() { + mp.put("Channel", "baidu"); + mp.put("$time",new Date(1650785225)); + mp.put("$project", "default"); + mp.put("$token", "123"); + mp.put("$track_id",123); + } + + /** + * 测试功能:上报事件到 event 表 + * 测试预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void track() throws InvalidArgumentException { + EventRecord eventRecord = EventRecord.builder() + .setEventName("test1") + .isLoginId(false) + .addProperties(mp) + .setDistinctId(cookieId) + .build(); + sa.track(cookieId,false,"test1",mp); + assertEventData(data); + sa.track(eventRecord); + assertEventData(data); + sa.track(cookieId,false, "test"); + assertEventData(data); + } + + /** + * 功能:检验 IDM 下上报事件属性 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void trackById() throws InvalidArgumentException { + IDMEventRecord idmEventRecord = IDMEventRecord.starter() + .setEventName("trackById") + .addProperties(mp) + .addIdentityProperty("abc",cookieId) + .setDistinctId(cookieId) + .build(); + sa.trackById(idmEventRecord); + assertEventData(data); + sa.trackById(sensorsAnalyticsIdentity, "track", mp); + assertEventData(data); + } + + /** + * 功能:校验上报 $SignUp 事件 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void trackSignup() throws InvalidArgumentException { + sa.trackSignUp("123","dada"); + assertEventData(data); + sa.trackSignUp("564","daxfad", mp); + assertEventData(data); + } + + /** + * 功能:校验记录单个用户的属性信息 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileSet() throws InvalidArgumentException { + UserRecord userRecord = UserRecord.builder().setDistinctId(cookieId) + .addProperties(mp) + .isLoginId(Boolean.FALSE) + .build(); + sa.profileSet(userRecord); + assertUserData(data); + sa.profileSet(cookieId,true,mp); + assertUserData(data); + sa.profileSet(cookieId,true, "$track_id",3131); + assertProperties(data); + } + + /** + * 功能:IDM 下校验记录单个用户的属性信息 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileSetById() throws InvalidArgumentException { + IDMUserRecord userRecord = IDMUserRecord.starter().setDistinctId(cookieId) + .addProperties(mp) + .build(); + sa.profileSetById(userRecord); + assertUserData(data); + sa.profileSetById(sensorsAnalyticsIdentity,mp); + assertUserData(data); + sa.profileSetById(sensorsAnalyticsIdentity, "$track_id",123); + assertUserData(data); + } + + /** + * 功能:校验首次设置用户属性 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileSetOnce() throws InvalidArgumentException { + UserRecord userRecord = UserRecord.builder().setDistinctId(cookieId) + .addProperties(mp) + .isLoginId(Boolean.FALSE) + .build(); + sa.profileSetOnce(userRecord); + assertUserData(data); + sa.profileSetOnce("ABCDEF123456789",false, mp); + assertUserData(data); + sa.profileSetOnce(cookieId, false, "$track_id",123); + assertUserData(data); + } + + /** + * 功能:IDM 下校验首次设置用户属性 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileSetOnceById() throws InvalidArgumentException { + IDMUserRecord userRecord = IDMUserRecord.starter().setDistinctId(cookieId) + .addProperties(mp) + .build(); + sa.profileSetOnceById(userRecord); + assertUserData(data); + sa.profileSetOnceById(sensorsAnalyticsIdentity, mp); + assertUserData(data); + sa.profileSetOnceById(sensorsAnalyticsIdentity, "$track_id", 123); + assertUserData(data); + } + + /** + * 功能:校验删除指定用户已存在的一条或者多条属性 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileUnset() throws InvalidArgumentException { + Map mp = new HashMap<>(); + mp.put("Channel", true); + UserRecord userRecord = UserRecord.builder().setDistinctId(cookieId) + .addProperties(mp) + .isLoginId(Boolean.FALSE) + .build(); + sa.profileUnset(userRecord); + assertUserData(data); + sa.profileUnset(cookieId,true,mp); + assertUserData(data); + sa.profileUnset(cookieId, true, "Channel"); + assertUserData(data); + } + + /** + * 功能:IDM 下校验删除指定用户已存在的一条或者多条属性 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileUnsetById() throws InvalidArgumentException { + Map ma = new HashMap<>(); + ma.put("Channel", true); + IDMUserRecord idmUserRecord = IDMUserRecord.starter().setDistinctId(cookieId) + .addProperties(ma) + .build(); + sa.profileUnsetById(idmUserRecord); + assertUserData(data); + sa.profileUnsetById(sensorsAnalyticsIdentity, ma); + assertUserData(data); + sa.profileUnsetById(sensorsAnalyticsIdentity, "Channel"); + assertUserData(data); + } + + /** + * 功能:校验删除指定用户所有属性 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileDelete() throws InvalidArgumentException { + UserRecord userRecord = UserRecord.builder() + .setDistinctId(cookieId) + .isLoginId(true) + .build(); + sa.profileDelete(cookieId, true); + assertUserData(data); + sa.profileDelete(userRecord); + assertUserData(data); + } + + /** + * 功能:IDM 下校验删除指定用户所有属性 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileDeleteById() throws InvalidArgumentException { + sa.profileDeleteById(sensorsAnalyticsIdentity); + assertUserData(data); + } + + /** + * 功能:校验为指定用户的一个或多个数组类型的属性追加字符串 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileAppend() throws InvalidArgumentException { + List newInterest = new ArrayList<>(); + newInterest.add("ball"); + Map mp = new HashMap<>(); + mp.put("interest", newInterest); + UserRecord appendRecord = UserRecord.builder().setDistinctId(cookieId).isLoginId(Boolean.TRUE) + .addProperty("interest", newInterest) + .build(); + sa.profileAppend(appendRecord); + assertUserData(data); + sa.profileAppend(cookieId,false,mp); + assertUserData(data); + sa.profileAppend(cookieId,false, "interest", "ball"); + assertUserData(data); + } + + /** + * 功能:IDM 下校验为指定用户的一个或多个数组类型的属性追加字符串 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileAppendById() throws InvalidArgumentException { + List newInterest = new ArrayList<>(); + newInterest.add("ball"); + Map mp = new HashMap<>(); + mp.put("interest", newInterest); + IDMUserRecord idmUserRecord = IDMUserRecord.starter().setDistinctId(cookieId).addProperty("interest", newInterest).build(); + sa.profileAppendById(idmUserRecord); + assertUserData(data); + sa.profileAppendById(sensorsAnalyticsIdentity,mp); + assertUserData(data); + sa.profileAppendById(sensorsAnalyticsIdentity,"interest","ball"); + assertUserData(data); + } + + /** + * 功能:校验为指定用户的一个或多个数值类型的属性累加一个数值 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileIncrement() throws InvalidArgumentException { + Map mp = new HashMap<>(); + mp.put("age",2); + UserRecord incrementRecord = UserRecord.builder().setDistinctId(cookieId).isLoginId(Boolean.TRUE) + .addProperty("age", 2) + .build(); + sa.profileIncrement(incrementRecord); + assertUserData(data); + sa.profileIncrement(cookieId,false,mp); + assertUserData(data); + sa.profileIncrement(cookieId,false,"age",2); + assertUserData(data); + } + + /** + * 功能:IDM 下校验为指定用户的一个或多个数值类型的属性累加一个数值 + * 预期:生成的数据符合神策格式,且 _track_id 存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void profileIncrementById() throws InvalidArgumentException { + IDMUserRecord idmUserRecord = IDMUserRecord.starter().setDistinctId(cookieId).addProperty("age",2).build(); + Map mp = new HashMap<>(); + mp.put("age",2); + sa.profileIncrementById(idmUserRecord); + assertUserData(data); + sa.profileIncrementById(sensorsAnalyticsIdentity,mp); + assertUserData(data); + sa.profileIncrementById(sensorsAnalyticsIdentity,"age",2); + assertUserData(data); + } + + /** + * 功能:校验增加 item 记录 + * 预期:生成的数据符合神策格式,且 _track_id 不存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void itemSet() throws InvalidArgumentException { + Map mp = new HashMap<>(); + mp.put("$track_id",123); + mp.put("$time", new Date(1650785226)); + ItemRecord itemRecord = ItemRecord.builder().setItemType("test").setItemId(cookieId).addProperties(mp).build(); + sa.itemSet(itemRecord); + assertItemData(data); + sa.itemSet("test",cookieId,mp); + assertItemData(data); + } + + /** + * 功能:校验删除 item 记录 + * 预期:生成的数据符合神策格式,且 _track_id 不存在,properties 中不包含 $time 和 $track_id + */ + @Test + public void itemDelete() throws InvalidArgumentException { + Map mp = new HashMap<>(); + mp.put("$track_id",123); + mp.put("$time", new Date(1650785226)); + ItemRecord itemRecord = ItemRecord.builder().setItemType("test").setItemId(cookieId).addProperties(mp).build(); + sa.itemDelete(itemRecord); + assertItemData(data); + sa.itemDelete("test",cookieId,mp); + assertItemData(data); + } +}