From be4675b41dea8715e13b12098fcb711afbcd451e Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Thu, 15 Jun 2023 15:29:11 -0700 Subject: [PATCH 1/5] fix to support arbitrary client names (flutter, ...) --- .../java/com/optimizely/ab/Optimizely.java | 10 ++++++ .../ab/event/internal/BuildVersionInfo.java | 4 +-- .../ab/event/internal/ClientEngineInfo.java | 35 +++++++++++++++++-- .../ab/event/internal/EventFactory.java | 2 +- .../ab/event/internal/payload/EventBatch.java | 2 ++ .../optimizely/ab/odp/ODPEventManager.java | 2 +- .../optimizely/ab/OptimizelyBuilderTest.java | 5 ++- .../event/internal/ClientEngineInfoTest.java | 20 ++++++----- .../ab/event/internal/EventFactoryTest.java | 18 +++++----- 9 files changed, 71 insertions(+), 27 deletions(-) diff --git a/core-api/src/main/java/com/optimizely/ab/Optimizely.java b/core-api/src/main/java/com/optimizely/ab/Optimizely.java index acd9d05fd..8f2c6ad13 100644 --- a/core-api/src/main/java/com/optimizely/ab/Optimizely.java +++ b/core-api/src/main/java/com/optimizely/ab/Optimizely.java @@ -1637,6 +1637,16 @@ public Builder withUserProfileService(UserProfileService userProfileService) { * @param clientVersion the client SDK version. * @return An Optimizely builder */ + public Builder withClientInfo(String clientEngineName, String clientVersion) { + ClientEngineInfo.setClientEngineName(clientEngineName); + BuildVersionInfo.setClientVersion(clientVersion); + return this; + } + + /** + * @deprecated in favor of {@link withClientInfo(String, String)} which can set with arbitrary client names. + */ + @Deprecated public Builder withClientInfo(EventBatch.ClientEngine clientEngine, String clientVersion) { ClientEngineInfo.setClientEngine(clientEngine); BuildVersionInfo.setClientVersion(clientVersion); diff --git a/core-api/src/main/java/com/optimizely/ab/event/internal/BuildVersionInfo.java b/core-api/src/main/java/com/optimizely/ab/event/internal/BuildVersionInfo.java index d5620b4e9..f69be7cb5 100644 --- a/core-api/src/main/java/com/optimizely/ab/event/internal/BuildVersionInfo.java +++ b/core-api/src/main/java/com/optimizely/ab/event/internal/BuildVersionInfo.java @@ -37,9 +37,9 @@ public final class BuildVersionInfo { @Deprecated public final static String VERSION = readVersionNumber(); + public final static String DEFAULT_VERSION = readVersionNumber(); // can be overridden by other wrapper client (android-sdk, etc) - - private static String clientVersion = readVersionNumber(); + private static String clientVersion = DEFAULT_VERSION; public static void setClientVersion(String version) { if (version == null || version.isEmpty()) { diff --git a/core-api/src/main/java/com/optimizely/ab/event/internal/ClientEngineInfo.java b/core-api/src/main/java/com/optimizely/ab/event/internal/ClientEngineInfo.java index beb64be3d..3e6584929 100644 --- a/core-api/src/main/java/com/optimizely/ab/event/internal/ClientEngineInfo.java +++ b/core-api/src/main/java/com/optimizely/ab/event/internal/ClientEngineInfo.java @@ -17,9 +17,13 @@ package com.optimizely.ab.event.internal; import com.optimizely.ab.event.internal.payload.EventBatch; +import com.optimizely.ab.notification.DecisionNotification; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + /** * ClientEngineInfo is a utility to globally get and set the ClientEngine used in * event tracking. The ClientEngine defaults to JAVA_SDK but can be overridden at @@ -28,9 +32,34 @@ public class ClientEngineInfo { private static final Logger logger = LoggerFactory.getLogger(ClientEngineInfo.class); + public static final String DEFAULT_NAME = "java-sdk"; + private static String clientEngineName = DEFAULT_NAME; + + public static void setClientEngineName(@Nullable String name) { + if (name == null || name.isEmpty()) { + logger.warn("ClientEngineName cannot be empty, defaulting to {}", ClientEngineInfo.clientEngineName); + return; + } + ClientEngineInfo.clientEngineName = name; + } + + @Nonnull + public static String getClientEngineName() { + return clientEngineName; + } + + private ClientEngineInfo() { + } + + @Deprecated public static final EventBatch.ClientEngine DEFAULT = EventBatch.ClientEngine.JAVA_SDK; + @Deprecated private static EventBatch.ClientEngine clientEngine = DEFAULT; + /** + * @deprecated in favor of {@link #setClientEngineName(String)} which can set with arbitrary client names. + */ + @Deprecated public static void setClientEngine(EventBatch.ClientEngine clientEngine) { if (clientEngine == null) { logger.warn("ClientEngine cannot be null, defaulting to {}", ClientEngineInfo.clientEngine.getClientEngineValue()); @@ -41,10 +70,12 @@ public static void setClientEngine(EventBatch.ClientEngine clientEngine) { ClientEngineInfo.clientEngine = clientEngine; } + /** + * @deprecated in favor of {@link #getClientEngineName()}. + */ + @Deprecated public static EventBatch.ClientEngine getClientEngine() { return clientEngine; } - private ClientEngineInfo() { - } } diff --git a/core-api/src/main/java/com/optimizely/ab/event/internal/EventFactory.java b/core-api/src/main/java/com/optimizely/ab/event/internal/EventFactory.java index f651be851..47839810d 100644 --- a/core-api/src/main/java/com/optimizely/ab/event/internal/EventFactory.java +++ b/core-api/src/main/java/com/optimizely/ab/event/internal/EventFactory.java @@ -72,7 +72,7 @@ public static LogEvent createLogEvent(List userEvents) { ProjectConfig projectConfig = userContext.getProjectConfig(); builder - .setClientName(ClientEngineInfo.getClientEngine().getClientEngineValue()) + .setClientName(ClientEngineInfo.getClientEngineName()) .setClientVersion(BuildVersionInfo.getClientVersion()) .setAccountId(projectConfig.getAccountId()) .setAnonymizeIp(projectConfig.getAnonymizeIP()) diff --git a/core-api/src/main/java/com/optimizely/ab/event/internal/payload/EventBatch.java b/core-api/src/main/java/com/optimizely/ab/event/internal/payload/EventBatch.java index c50ee6288..43965dafa 100644 --- a/core-api/src/main/java/com/optimizely/ab/event/internal/payload/EventBatch.java +++ b/core-api/src/main/java/com/optimizely/ab/event/internal/payload/EventBatch.java @@ -24,6 +24,8 @@ import java.util.List; public class EventBatch { + + @Deprecated public enum ClientEngine { JAVA_SDK("java-sdk"), ANDROID_SDK("android-sdk"), diff --git a/core-api/src/main/java/com/optimizely/ab/odp/ODPEventManager.java b/core-api/src/main/java/com/optimizely/ab/odp/ODPEventManager.java index ba668a138..efcdd6cda 100644 --- a/core-api/src/main/java/com/optimizely/ab/odp/ODPEventManager.java +++ b/core-api/src/main/java/com/optimizely/ab/odp/ODPEventManager.java @@ -148,7 +148,7 @@ protected Map augmentCommonData(Map sourceData) Map data = new HashMap<>(); data.put("idempotence_id", UUID.randomUUID().toString()); data.put("data_source_type", "sdk"); - data.put("data_source", ClientEngineInfo.getClientEngine().getClientEngineValue()); + data.put("data_source", ClientEngineInfo.getClientEngineName()); data.put("data_source_version", BuildVersionInfo.getClientVersion()); data.putAll(userCommonData); diff --git a/core-api/src/test/java/com/optimizely/ab/OptimizelyBuilderTest.java b/core-api/src/test/java/com/optimizely/ab/OptimizelyBuilderTest.java index 566eec635..6f091fdf8 100644 --- a/core-api/src/test/java/com/optimizely/ab/OptimizelyBuilderTest.java +++ b/core-api/src/test/java/com/optimizely/ab/OptimizelyBuilderTest.java @@ -224,7 +224,6 @@ public void withClientInfo() throws Exception { reset(eventHandler); optimizely = Optimizely.builder(validConfigJsonV4(), eventHandler) - .withClientInfo(null, null) .build(); optimizely.track("basic_event", "tester"); @@ -245,8 +244,8 @@ public void withClientInfo() throws Exception { assertEquals(argument.getValue().getEventBatch().getClientVersion(), "1.2.3"); // restore the default values for other tests - ClientEngineInfo.setClientEngine(ClientEngineInfo.DEFAULT); - BuildVersionInfo.setClientVersion(BuildVersionInfo.VERSION); + ClientEngineInfo.setClientEngineName(ClientEngineInfo.DEFAULT_NAME); + BuildVersionInfo.setClientVersion(BuildVersionInfo.DEFAULT_VERSION); } @Test diff --git a/core-api/src/test/java/com/optimizely/ab/event/internal/ClientEngineInfoTest.java b/core-api/src/test/java/com/optimizely/ab/event/internal/ClientEngineInfoTest.java index 33be48517..55b04296a 100644 --- a/core-api/src/test/java/com/optimizely/ab/event/internal/ClientEngineInfoTest.java +++ b/core-api/src/test/java/com/optimizely/ab/event/internal/ClientEngineInfoTest.java @@ -26,19 +26,21 @@ public class ClientEngineInfoTest { @After public void tearDown() throws Exception { - ClientEngineInfo.setClientEngine(ClientEngineInfo.DEFAULT); + ClientEngineInfo.setClientEngineName(ClientEngineInfo.DEFAULT_NAME); } @Test public void testSetAndGetClientEngine() { - for (EventBatch.ClientEngine expected: EventBatch.ClientEngine.values()) { - ClientEngineInfo.setClientEngine(expected); - assertEquals(expected, ClientEngineInfo.getClientEngine()); - } - } + // default "java-sdk" name + assertEquals("java-sdk", ClientEngineInfo.getClientEngineName()); - @Test - public void testDefaultValue() { - assertEquals(ClientEngineInfo.DEFAULT, ClientEngineInfo.getClientEngine()); + ClientEngineInfo.setClientEngineName(null); + assertEquals("java-sdk", ClientEngineInfo.getClientEngineName()); + + ClientEngineInfo.setClientEngineName(""); + assertEquals("java-sdk", ClientEngineInfo.getClientEngineName()); + + ClientEngineInfo.setClientEngineName("test-name"); + assertEquals("test-name", ClientEngineInfo.getClientEngineName()); } } diff --git a/core-api/src/test/java/com/optimizely/ab/event/internal/EventFactoryTest.java b/core-api/src/test/java/com/optimizely/ab/event/internal/EventFactoryTest.java index 657bc4fbf..e347074a8 100644 --- a/core-api/src/test/java/com/optimizely/ab/event/internal/EventFactoryTest.java +++ b/core-api/src/test/java/com/optimizely/ab/event/internal/EventFactoryTest.java @@ -87,7 +87,7 @@ public EventFactoryTest(int datafileVersion, @After public void tearDown() { - ClientEngineInfo.setClientEngine(EventBatch.ClientEngine.JAVA_SDK); + ClientEngineInfo.setClientEngineName(ClientEngineInfo.DEFAULT_NAME); } /** @@ -554,7 +554,7 @@ public void createImpressionEventIgnoresNullAttributes() { */ @Test public void createImpressionEventAndroidClientEngineClientVersion() throws Exception { - ClientEngineInfo.setClientEngine(EventBatch.ClientEngine.ANDROID_SDK); + ClientEngineInfo.setClientEngineName("android-sdk"); ProjectConfig projectConfig = validProjectConfigV2(); Experiment activatedExperiment = projectConfig.getExperiments().get(0); Variation bucketedVariation = activatedExperiment.getVariations().get(0); @@ -566,7 +566,7 @@ public void createImpressionEventAndroidClientEngineClientVersion() throws Excep userId, attributeMap); EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class); - assertThat(impression.getClientName(), is(EventBatch.ClientEngine.ANDROID_SDK.getClientEngineValue())); + assertThat(impression.getClientName(), is("android-sdk")); // assertThat(impression.getClientVersion(), is("0.0.0")); } @@ -577,7 +577,7 @@ public void createImpressionEventAndroidClientEngineClientVersion() throws Excep @Test public void createImpressionEventAndroidTVClientEngineClientVersion() throws Exception { String clientVersion = "0.0.0"; - ClientEngineInfo.setClientEngine(EventBatch.ClientEngine.ANDROID_TV_SDK); + ClientEngineInfo.setClientEngineName("android-tv-sdk"); ProjectConfig projectConfig = validProjectConfigV2(); Experiment activatedExperiment = projectConfig.getExperiments().get(0); Variation bucketedVariation = activatedExperiment.getVariations().get(0); @@ -589,7 +589,7 @@ public void createImpressionEventAndroidTVClientEngineClientVersion() throws Exc userId, attributeMap); EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class); - assertThat(impression.getClientName(), is(EventBatch.ClientEngine.ANDROID_TV_SDK.getClientEngineValue())); + assertThat(impression.getClientName(), is("android-tv-sdk")); // assertThat(impression.getClientVersion(), is(clientVersion)); } @@ -816,7 +816,7 @@ public void createConversionEventExperimentStatusPrecedesForcedVariation() { */ @Test public void createConversionEventAndroidClientEngineClientVersion() throws Exception { - ClientEngineInfo.setClientEngine(EventBatch.ClientEngine.ANDROID_SDK); + ClientEngineInfo.setClientEngineName("android-sdk"); Attribute attribute = validProjectConfig.getAttributes().get(0); EventType eventType = validProjectConfig.getEventTypes().get(0); @@ -838,7 +838,7 @@ public void createConversionEventAndroidClientEngineClientVersion() throws Excep EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class); - assertThat(conversion.getClientName(), is(EventBatch.ClientEngine.ANDROID_SDK.getClientEngineValue())); + assertThat(conversion.getClientName(), is("android-sdk")); // assertThat(conversion.getClientVersion(), is("0.0.0")); } @@ -849,7 +849,7 @@ public void createConversionEventAndroidClientEngineClientVersion() throws Excep @Test public void createConversionEventAndroidTVClientEngineClientVersion() throws Exception { String clientVersion = "0.0.0"; - ClientEngineInfo.setClientEngine(EventBatch.ClientEngine.ANDROID_TV_SDK); + ClientEngineInfo.setClientEngineName("android-tv-sdk"); ProjectConfig projectConfig = validProjectConfigV2(); Attribute attribute = projectConfig.getAttributes().get(0); EventType eventType = projectConfig.getEventTypes().get(0); @@ -873,7 +873,7 @@ public void createConversionEventAndroidTVClientEngineClientVersion() throws Exc EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class); - assertThat(conversion.getClientName(), is(EventBatch.ClientEngine.ANDROID_TV_SDK.getClientEngineValue())); + assertThat(conversion.getClientName(), is("android-tv-sdk")); // assertThat(conversion.getClientVersion(), is(clientVersion)); } From 06295158a9787828de9ee73c2d69f6b8a7b71cd0 Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Thu, 15 Jun 2023 17:12:09 -0700 Subject: [PATCH 2/5] fix for depricate clientengine methods --- .../java/com/optimizely/ab/event/internal/ClientEngineInfo.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core-api/src/main/java/com/optimizely/ab/event/internal/ClientEngineInfo.java b/core-api/src/main/java/com/optimizely/ab/event/internal/ClientEngineInfo.java index 3e6584929..85573d7fc 100644 --- a/core-api/src/main/java/com/optimizely/ab/event/internal/ClientEngineInfo.java +++ b/core-api/src/main/java/com/optimizely/ab/event/internal/ClientEngineInfo.java @@ -68,6 +68,7 @@ public static void setClientEngine(EventBatch.ClientEngine clientEngine) { logger.info("Setting Optimizely client engine to {}", clientEngine.getClientEngineValue()); ClientEngineInfo.clientEngine = clientEngine; + ClientEngineInfo.clientEngineName = clientEngine.getClientEngineValue(); } /** From 631c224b421c1c4b7429a78928b7a152f3f045b9 Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Thu, 15 Jun 2023 17:24:47 -0700 Subject: [PATCH 3/5] fix comments --- core-api/src/main/java/com/optimizely/ab/Optimizely.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-api/src/main/java/com/optimizely/ab/Optimizely.java b/core-api/src/main/java/com/optimizely/ab/Optimizely.java index 8f2c6ad13..3524cb24a 100644 --- a/core-api/src/main/java/com/optimizely/ab/Optimizely.java +++ b/core-api/src/main/java/com/optimizely/ab/Optimizely.java @@ -1633,7 +1633,7 @@ public Builder withUserProfileService(UserProfileService userProfileService) { /** * Override the SDK name and version (for client SDKs like android-sdk wrapping the core java-sdk) to be included in events. * - * @param clientEngine the client engine type. + * @param clientEngineName the client engine name ("java-sdk", "android-sdk", "flutter-sdk", etc.). * @param clientVersion the client SDK version. * @return An Optimizely builder */ From 7fc9c4ceb0ac33cfc29e51139be93ceac49f6974 Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Fri, 16 Jun 2023 15:06:09 -0700 Subject: [PATCH 4/5] revert jackson version down to 2.13 for android support --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 71d3f3310..cba81abe8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,7 +13,7 @@ org.gradle.parallel = true gsonVersion = 2.10.1 guavaVersion = 22.0 hamcrestVersion = 1.3 -jacksonVersion = 2.15.2 +jacksonVersion = 2.13.5 jsonVersion = 20190722 jsonSimpleVersion = 1.1.1 logbackVersion = 1.2.3 From 1be7a53e0f5feda8cf4b79b76a2e08738ee87ac9 Mon Sep 17 00:00:00 2001 From: Jae Kim Date: Fri, 16 Jun 2023 15:35:24 -0700 Subject: [PATCH 5/5] note on jackson version --- gradle.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/gradle.properties b/gradle.properties index cba81abe8..f8816af24 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,6 +13,7 @@ org.gradle.parallel = true gsonVersion = 2.10.1 guavaVersion = 22.0 hamcrestVersion = 1.3 +# NOTE: jackson 2.14+ uses Java8 stream apis not supported in android jacksonVersion = 2.13.5 jsonVersion = 20190722 jsonSimpleVersion = 1.1.1