Skip to content

Commit 336b610

Browse files
authored
fix: add Optimizely builder option for client-engine info (#466)
1 parent 81fdfb2 commit 336b610

File tree

6 files changed

+96
-21
lines changed

6 files changed

+96
-21
lines changed

core-api/src/main/java/com/optimizely/ab/Optimizely.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
import com.optimizely.ab.error.ErrorHandler;
2626
import com.optimizely.ab.error.NoOpErrorHandler;
2727
import com.optimizely.ab.event.*;
28-
import com.optimizely.ab.event.internal.ClientEngineInfo;
29-
import com.optimizely.ab.event.internal.EventFactory;
30-
import com.optimizely.ab.event.internal.UserEvent;
31-
import com.optimizely.ab.event.internal.UserEventFactory;
28+
import com.optimizely.ab.event.internal.*;
3229
import com.optimizely.ab.event.internal.payload.EventBatch;
3330
import com.optimizely.ab.notification.*;
3431
import com.optimizely.ab.optimizelyconfig.OptimizelyConfig;
@@ -1489,7 +1486,7 @@ public Builder withErrorHandler(ErrorHandler errorHandler) {
14891486
}
14901487

14911488
/**
1492-
* The withEventHandler has has been moved to the EventProcessor which takes a EventHandler in it's builder
1489+
* The withEventHandler has been moved to the EventProcessor which takes a EventHandler in it's builder
14931490
* method.
14941491
* {@link com.optimizely.ab.event.BatchEventProcessor.Builder#withEventHandler(com.optimizely.ab.event.EventHandler)} label}
14951492
* Please use that builder method instead.
@@ -1519,6 +1516,19 @@ public Builder withUserProfileService(UserProfileService userProfileService) {
15191516
return this;
15201517
}
15211518

1519+
/**
1520+
* Override the SDK name and version (for client SDKs like android-sdk wrapping the core java-sdk) to be included in events.
1521+
*
1522+
* @param clientEngine the client engine type.
1523+
* @param clientVersion the client SDK version.
1524+
* @return An Optimizely builder
1525+
*/
1526+
public Builder withClientInfo(EventBatch.ClientEngine clientEngine, String clientVersion) {
1527+
ClientEngineInfo.setClientEngine(clientEngine);
1528+
BuildVersionInfo.setClientVersion(clientVersion);
1529+
return this;
1530+
}
1531+
15221532
@Deprecated
15231533
public Builder withClientEngine(EventBatch.ClientEngine clientEngine) {
15241534
logger.info("Deprecated. In the future, set ClientEngine via ClientEngineInfo#setClientEngine.");

core-api/src/main/java/com/optimizely/ab/event/internal/BuildVersionInfo.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2016-2017, 2019, Optimizely and contributors
3+
* Copyright 2016-2017, 2019, 2022, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -30,13 +30,29 @@
3030
/**
3131
* Helper class to retrieve the SDK version information.
3232
*/
33-
@Immutable
3433
public final class BuildVersionInfo {
3534

3635
private static final Logger logger = LoggerFactory.getLogger(BuildVersionInfo.class);
3736

37+
@Deprecated
3838
public final static String VERSION = readVersionNumber();
3939

40+
// can be overridden by other wrapper client (android-sdk, etc)
41+
42+
private static String clientVersion = readVersionNumber();
43+
44+
public static void setClientVersion(String version) {
45+
if (version == null || version.isEmpty()) {
46+
logger.warn("ClientVersion cannot be empty, defaulting to the core java-sdk version.");
47+
return;
48+
}
49+
clientVersion = version;
50+
}
51+
52+
public static String getClientVersion() {
53+
return clientVersion;
54+
}
55+
4056
private static String readVersionNumber() {
4157
BufferedReader bufferedReader = null;
4258
try {

core-api/src/main/java/com/optimizely/ab/event/internal/EventFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2016-2020, Optimizely and contributors
3+
* Copyright 2016-2020, 2022, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -73,7 +73,7 @@ public static LogEvent createLogEvent(List<UserEvent> userEvents) {
7373

7474
builder
7575
.setClientName(ClientEngineInfo.getClientEngine().getClientEngineValue())
76-
.setClientVersion(BuildVersionInfo.VERSION)
76+
.setClientVersion(BuildVersionInfo.getClientVersion())
7777
.setAccountId(projectConfig.getAccountId())
7878
.setAnonymizeIp(projectConfig.getAnonymizeIP())
7979
.setProjectId(projectConfig.getProjectId())

core-api/src/main/java/com/optimizely/ab/event/internal/payload/EventBatch.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2018-2019, Optimizely and contributors
3+
* Copyright 2018-2019, 2022, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -165,7 +165,7 @@ public int hashCode() {
165165
public static class Builder {
166166

167167
private String clientName = ClientEngine.JAVA_SDK.getClientEngineValue();
168-
private String clientVersion = BuildVersionInfo.VERSION;
168+
private String clientVersion = BuildVersionInfo.getClientVersion();
169169
private String accountId;
170170
private List<Visitor> visitors;
171171
private Boolean anonymizeIp;

core-api/src/test/java/com/optimizely/ab/OptimizelyBuilderTest.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2016-2017, 2019, Optimizely and contributors
3+
* Copyright 2016-2017, 2019, 2022, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -20,12 +20,17 @@
2020
import com.optimizely.ab.config.*;
2121
import com.optimizely.ab.error.ErrorHandler;
2222
import com.optimizely.ab.error.NoOpErrorHandler;
23+
import com.optimizely.ab.event.BatchEventProcessor;
2324
import com.optimizely.ab.event.EventHandler;
25+
import com.optimizely.ab.event.LogEvent;
26+
import com.optimizely.ab.event.internal.BuildVersionInfo;
27+
import com.optimizely.ab.event.internal.payload.EventBatch;
2428
import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption;
2529
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2630
import org.junit.Rule;
2731
import org.junit.Test;
2832
import org.junit.rules.ExpectedException;
33+
import org.mockito.ArgumentCaptor;
2934
import org.mockito.Mock;
3035
import org.mockito.junit.MockitoJUnit;
3136
import org.mockito.junit.MockitoRule;
@@ -34,11 +39,14 @@
3439
import java.util.List;
3540

3641
import static com.optimizely.ab.config.DatafileProjectConfigTestUtils.*;
42+
import static junit.framework.Assert.assertEquals;
3743
import static org.hamcrest.CoreMatchers.instanceOf;
3844
import static org.hamcrest.CoreMatchers.is;
3945
import static org.junit.Assert.*;
40-
import static org.mockito.Mockito.mock;
41-
import static org.mockito.Mockito.when;
46+
import static org.mockito.Matchers.any;
47+
import static org.mockito.Matchers.anyString;
48+
import static org.mockito.Mockito.*;
49+
import static org.mockito.Mockito.never;
4250

4351
/**
4452
* Tests for {@link Optimizely#builder(String, EventHandler)}.
@@ -195,4 +203,45 @@ public void withDefaultDecideOptions() throws Exception {
195203
assertEquals(optimizelyClient.defaultDecideOptions.get(2), OptimizelyDecideOption.EXCLUDE_VARIABLES);
196204
}
197205

206+
@Test
207+
public void withClientInfo() throws Exception {
208+
Optimizely optimizely;
209+
EventHandler eventHandler;
210+
ArgumentCaptor<LogEvent> argument = ArgumentCaptor.forClass(LogEvent.class);
211+
212+
// default client-engine info (java-sdk)
213+
214+
eventHandler = mock(EventHandler.class);
215+
optimizely = Optimizely.builder(validConfigJsonV4(), eventHandler).build();
216+
optimizely.track("basic_event", "tester");
217+
218+
verify(eventHandler, timeout(5000)).dispatchEvent(argument.capture());
219+
assertEquals(argument.getValue().getEventBatch().getClientName(), "java-sdk");
220+
assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildVersionInfo.getClientVersion());
221+
222+
// invalid override with null inputs
223+
224+
reset(eventHandler);
225+
optimizely = Optimizely.builder(validConfigJsonV4(), eventHandler)
226+
.withClientInfo(null, null)
227+
.build();
228+
optimizely.track("basic_event", "tester");
229+
230+
verify(eventHandler, timeout(5000)).dispatchEvent(argument.capture());
231+
assertEquals(argument.getValue().getEventBatch().getClientName(), "java-sdk");
232+
assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildVersionInfo.getClientVersion());
233+
234+
// override client-engine info
235+
236+
reset(eventHandler);
237+
optimizely = Optimizely.builder(validConfigJsonV4(), eventHandler)
238+
.withClientInfo(EventBatch.ClientEngine.ANDROID_SDK, "1.2.3")
239+
.build();
240+
optimizely.track("basic_event", "tester");
241+
242+
verify(eventHandler, timeout(5000)).dispatchEvent(argument.capture());
243+
assertEquals(argument.getValue().getEventBatch().getClientName(), "android-sdk");
244+
assertEquals(argument.getValue().getEventBatch().getClientVersion(), "1.2.3");
245+
}
246+
198247
}

core-api/src/test/java/com/optimizely/ab/event/internal/EventFactoryTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2016-2020, Optimizely and contributors
3+
* Copyright 2016-2020, 2022, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -157,7 +157,7 @@ public void createImpressionEventPassingUserAgentAttribute() throws Exception {
157157
assertThat(eventBatch.getAccountId(), is(validProjectConfig.getAccountId()));
158158
assertThat(eventBatch.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
159159
assertThat(eventBatch.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
160-
assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.VERSION));
160+
assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.getClientVersion()));
161161
assertNull(eventBatch.getVisitors().get(0).getSessionId());
162162
}
163163

@@ -224,7 +224,7 @@ public void createImpressionEvent() throws Exception {
224224
assertThat(eventBatch.getAccountId(), is(validProjectConfig.getAccountId()));
225225
assertThat(eventBatch.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
226226
assertThat(eventBatch.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
227-
assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.VERSION));
227+
assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.getClientVersion()));
228228
assertNull(eventBatch.getVisitors().get(0).getSessionId());
229229
}
230230

@@ -649,7 +649,7 @@ public void createConversionEvent() throws Exception {
649649
assertEquals(conversion.getAnonymizeIp(), validProjectConfig.getAnonymizeIP());
650650
assertTrue(conversion.getEnrichDecisions());
651651
assertEquals(conversion.getClientName(), EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue());
652-
assertEquals(conversion.getClientVersion(), BuildVersionInfo.VERSION);
652+
assertEquals(conversion.getClientVersion(), BuildVersionInfo.getClientVersion());
653653
}
654654

655655
/**
@@ -717,7 +717,7 @@ public void createConversionEventPassingUserAgentAttribute() throws Exception {
717717
assertEquals(conversion.getAnonymizeIp(), validProjectConfig.getAnonymizeIP());
718718
assertTrue(conversion.getEnrichDecisions());
719719
assertEquals(conversion.getClientName(), EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue());
720-
assertEquals(conversion.getClientVersion(), BuildVersionInfo.VERSION);
720+
assertEquals(conversion.getClientVersion(), BuildVersionInfo.getClientVersion());
721721
}
722722

723723
/**
@@ -961,7 +961,7 @@ public void createImpressionEventWithBucketingId() throws Exception {
961961

962962
assertThat(impression.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
963963
assertThat(impression.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
964-
assertThat(impression.getClientVersion(), is(BuildVersionInfo.VERSION));
964+
assertThat(impression.getClientVersion(), is(BuildVersionInfo.getClientVersion()));
965965
assertNull(impression.getVisitors().get(0).getSessionId());
966966
}
967967

@@ -1034,7 +1034,7 @@ public void createConversionEventWithBucketingId() throws Exception {
10341034
assertEquals(conversion.getAnonymizeIp(), validProjectConfig.getAnonymizeIP());
10351035
assertTrue(conversion.getEnrichDecisions());
10361036
assertEquals(conversion.getClientName(), EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue());
1037-
assertEquals(conversion.getClientVersion(), BuildVersionInfo.VERSION);
1037+
assertEquals(conversion.getClientVersion(), BuildVersionInfo.getClientVersion());
10381038
}
10391039

10401040

0 commit comments

Comments
 (0)