8000 fix: add Optimizely builder option for client-engine info by jaeopt · Pull Request #466 · optimizely/java-sdk · GitHub
[go: up one dir, main page]

Skip to content

fix: add Optimizely builder option for client-engine info #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions core-api/src/main/java/com/optimizely/ab/Optimizely.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import com.optimizely.ab.error.ErrorHandler;
import com.optimizely.ab.error.NoOpErrorHandler;
import com.optimizely.ab.event.*;
import com.optimizely.ab.event.internal.ClientEngineInfo;
import com.optimizely.ab.event.internal.EventFactory;
import com.optimizely.ab.event.internal.UserEvent;
import com.optimizely.ab.event.internal.UserEventFactory;
import com.optimizely.ab.event.internal.*;
import com.optimizely.ab.event.internal.payload.EventBatch;
import com.optimizely.ab.notification.*;
import com.optimizely.ab.optimizelyconfig.OptimizelyConfig;
Expand Down Expand Up @@ -1489,7 +1486,7 @@ public Builder withErrorHandler(ErrorHandler errorHandler) {
}

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

/**
* 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 clientVersion the client SDK version.
* @return An Optimizely builder
*/
public Builder withClientInfo(EventBatch.ClientEngine clientEngine, String clientVersion) {
ClientEngineInfo.setClientEngine(clientEngine);
BuildVersionInfo.setClientVersion(clientVersion);
return this;
}

@Deprecated
public Builder withClientEngine(EventBatch.ClientEngine clientEngine) {
logger.info("Deprecated. In the future, set ClientEngine via ClientEngineInfo#setClientEngine.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2016-2017, 2019, Optimizely and contributors
* Copyright 2016-2017, 2019, 2022, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,13 +30,29 @@
/**
* Helper class to retrieve the SDK version information.
*/
@Immutable
public final class BuildVersionInfo {

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

@Deprecated
public final static String VERSION = readVersionNumber();

// can be overridden by other wrapper client (android-sdk, etc)

private static String clientVersion = readVersionNumber();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike the old VERSION, this private variable can be changed with Optimizely.Builder.


public static void setClientVersion(String version) {
if (version == null || version.isEmpty()) {
logger.warn("ClientVersion cannot be empty, defaulting to the core java-sdk version.");
return;
}
clientVersion = version;
}

public static String getClientVersion() {
return clientVersion;
}

private static String readVersionNumber() {
BufferedReader bufferedReader = null;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2016-2020, Optimizely and contributors
* Copyright 2016-2020, 2022, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -73,7 +73,7 @@ public static LogEvent createLogEvent(List<UserEvent> userEvents) {

builder
.setClientName(ClientEngineInfo.getClientEngine().getClientEngineValue())
.setClientVersion(BuildVersionInfo.VERSION)
.setClientVersion(BuildVersionInfo.getClientVersion())
.setAccountId(projectConfig.getAccountId())
.setAnonymizeIp(projectConfig.getAnonymizeIP())
.setProjectId(projectConfig.getProjectId())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2018-2019, Optimizely and contributors
* Copyright 2018-2019, 2022, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -165,7 +165,7 @@ public int hashCode() {
public static class Builder {

private String clientName = ClientEngine.JAVA_SDK.getClientEngineValue();
private String clientVersion = BuildVersionInfo.VERSION;
private String clientVersion = BuildVersionInfo.getClientVersion();
private String accountId;
private List<Visitor> visitors;
private Boolean anonymizeIp;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2016-2017, 2019, Optimizely and contributors
* Copyright 2016-2017, 2019, 2022, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,12 +20,17 @@
import com.optimizely.ab.config.*;
import com.optimizely.ab.error.ErrorHandler;
import com.optimizely.ab.error.NoOpErrorHandler;
import com.optimizely.ab.event.BatchEventProcessor;
import com.optimizely.ab.event.EventHandler;
import com.optimizely.ab.event.LogEvent;
import com.optimizely.ab.event.internal.BuildVersionInfo;
import com.optimizely.ab.event.internal.payload.EventBatch;
import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
Expand All @@ -34,11 +39,14 @@
import java.util.List;

import static com.optimizely.ab.config.DatafileProjectConfigTestUtils.*;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.never;

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

@Test
public void withClientInfo() throws Exception {
Optimizely optimizely;
EventHandler eventHandler;
ArgumentCaptor<LogEvent> argument = ArgumentCaptor.forClass(LogEvent.class);

// default client-engine info (java-sdk)

eventHandler = mock(EventHandler.class);
optimizely = Optimizely.builder(validConfigJsonV4(), eventHandler).build();
optimizely.track("basic_event", "tester");

verify(eventHandler, timeout(5000)).dispatchEvent(argument.capture());
assertEquals(argument.getValue().getEventBatch().getClientName(), "java-sdk");
assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildVersionInfo.getClientVersion());

// invalid override with null inputs

reset(eventHandler);
optimizely = Optimizely.builder(validConfigJsonV4(), eventHandler)
.withClientInfo(null, null)
.build();
optimizely.track("basic_event", "tester");

verify(eventHandler, timeout(5000)).dispatchEvent(argument.capture());
assertEquals(argument.getValue().getEventBatch().getClientName(), "java-sdk");
assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildVersionInfo.getClientVersion());

// override client-engine info

reset(eventHandler);
optimizely = Optimizely.builder(validConfigJsonV4(), eventHandler)
.withClientInfo(EventBatch.ClientEngine.ANDROID_SDK, "1.2.3")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it confusing to check "android-sdk" client engine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify this comment? Not clear to me.

.build();
optimizely.track("basic_event", "tester");

verify(eventHandler, timeout(5000)).dispatchEvent(argument.capture());
assertEquals(argument.getValue().getEventBatch().getClientName(), "android-sdk");
assertEquals(argument.getValue().getEventBatch().getClientVersion(), "1.2.3");
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2016-2020, Optimizely and contributors
* Copyright 2016-2020, 2022, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -157,7 +157,7 @@ public void createImpressionEventPassingUserAgentAttribute() throws Exception {
assertThat(eventBatch.getAccountId(), is(validProjectConfig.getAccountId()));
assertThat(eventBatch.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
assertThat(eventBatch.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.VERSION));
assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.getClientVersion()));
assertNull(eventBatch.getVisitors().get(0).getSessionId());
}

Expand Down Expand Up @@ -224,7 +224,7 @@ public void createImpressionEvent() throws Exception {
assertThat(eventBatch.getAccountId(), is(validProjectConfig.getAccountId()));
assertThat(eventBatch.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
assertThat(eventBatch.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.VERSION));
assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.getClientVersion()));
assertNull(eventBatch.getVisitors().get(0).getSessionId());
}

Expand Down Expand Up @@ -649,7 +649,7 @@ public void createConversionEvent() throws Exception {
assertEquals(conversion.getAnonymizeIp(), validProjectConfig.getAnonymizeIP());
assertTrue(conversion.getEnrichDecisions());
assertEquals(conversion.getClientName(), EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue());
assertEquals(conversion.getClientVersion(), BuildVersionInfo.VERSION);
assertEquals(conversion.getClientVersion(), BuildVersionInfo.getClientVersion());
}

/**
Expand Down Expand Up @@ -717,7 +717,7 @@ public void createConversionEventPassingUserAgentAttribute() throws Exception {
assertEquals(conversion.getAnonymizeIp(), validProjectConfig.getAnonymizeIP());
assertTrue(conversion.getEnrichDecisions());
assertEquals(conversion.getClientName(), EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue());
assertEquals(conversion.getClientVersion(), BuildVersionInfo.VERSION);
assertEquals(conversion.getClientVersion(), BuildVersionInfo.getClientVersion());
}

/**
Expand Down Expand Up @@ -961,7 +961,7 @@ public void createImpressionEventWithBucketingId() throws Exception {

assertThat(impression.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
assertThat(impression.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
assertThat(impression.getClientVersion(), is(BuildVersionInfo.VERSION));
assertThat(impression.getClientVersion(), is(BuildVersionInfo.getClientVersion()));
assertNull(impression.getVisitors().get(0).getSessionId());
}

Expand Down Expand Up @@ -1034,7 +1034,7 @@ public void createConversionEventWithBucketingId() throws Exception {
assertEquals(conversion.getAnonymizeIp(), validProjectConfig.getAnonymizeIP());
assertTrue(conversion.getEnrichDecisions());
assertEquals(conversion.getClientName(), EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue());
assertEquals(conversion.getClientVersion(), BuildVersionInfo.VERSION);
assertEquals(conversion.getClientVersion(), BuildVersionInfo.getClientVersion());
}


Expand Down
0