Skip to content
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

WIP: vuid enabled add to optimizely manager #496

Closed
wants to merge 8 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.optimizely.ab.odp.ODPApiManager;
import com.optimizely.ab.odp.ODPEventManager;
import com.optimizely.ab.odp.ODPManager;
import com.optimizely.ab.android.odp.VuidManager;
import com.optimizely.ab.odp.ODPSegmentManager;

import org.junit.Before;
Expand Down Expand Up @@ -100,13 +101,14 @@ public void setup() throws Exception {

optimizelyManager = OptimizelyManager.builder()
.withSDKKey(testSdkKey)
.withVuid(testVuid)
.withVuidEnabled()
.withODPEventManager(odpEventManager)
.withODPSegmentManager(odpSegmentManager)
.build(context);

optimizelyManager.initialize(context, odpDatafile);
optimizelyClient = optimizelyManager.getOptimizely();

}

@Test
Expand Down Expand Up @@ -134,11 +136,11 @@ public void identifyOdpEventSentWhenUserContextCreated() throws InterruptedExcep

assertEquals(firstEvt.get("action").getAsString(), "client_initialized");
assertEquals(firstIdentifiers.size(), 1);
assertEquals(firstIdentifiers.get("vuid").getAsString(), testVuid);
assertTrue(VuidManager.isVuid(firstIdentifiers.get("vuid").getAsString()));

assertEquals(secondEvt.get("action").getAsString(), "identified");
assertEquals(secondIdentifiers.size(), 2);
assertEquals(secondIdentifiers.get("vuid").getAsString(), testVuid);
assertTrue(VuidManager.isVuid(secondIdentifiers.get("vuid").getAsString()));
assertEquals(secondIdentifiers.get("fs_user_id").getAsString(), testUser);

// validate that ODP event data includes correct values.
Expand Down Expand Up @@ -170,11 +172,11 @@ public void identifyOdpEventSentWhenVuidUserContextCreated() throws InterruptedE

assertEquals(firstEvt.get("action").getAsString(), "client_initialized");
assertEquals(firstIdentifiers.size(), 1);
assertEquals(firstIdentifiers.get("vuid").getAsString(), testVuid);
assertTrue(VuidManager.isVuid(firstIdentifiers.get("vuid").getAsString()));

assertEquals(secondEvt.get("action").getAsString(), "identified");
assertEquals(secondIdentifiers.size(), 1);
assertEquals(secondIdentifiers.get("vuid").getAsString(), testVuid);
assertTrue(VuidManager.isVuid(secondIdentifiers.get("vuid").getAsString()));
}

@Test
Expand Down Expand Up @@ -202,7 +204,7 @@ public void fetchQualifiedSegmentsWithVuidUserContext() throws InterruptedExcept
eq("p-1"),
eq("h-1/v3/graphql"),
eq("vuid"),
eq(testVuid),
eq(optimizelyClient.getVuid()),
eq(new HashSet<>(Arrays.asList("segment-1")))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.optimizely.ab.odp.ODPApiManager;
import com.optimizely.ab.odp.ODPEventManager;
import com.optimizely.ab.odp.ODPManager;
import com.optimizely.ab.android.odp.VuidManager;
import com.optimizely.ab.odp.ODPSegmentManager;

import org.junit.Before;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class ODPIntegrationUpdateConfigTest {

private OptimizelyManager optimizelyManager;
private ODPManager odpManager;
private VuidManager vuidManager;
private DefaultDatafileHandler datafileHandler;
private NotificationCenter notificationCenter;
private Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
Expand Down Expand Up @@ -101,6 +103,7 @@ public void setup() throws Exception {

datafileHandler = new DefaultDatafileHandler();
notificationCenter = new NotificationCenter();
vuidManager = new VuidManager(context, true);

optimizelyManager = new OptimizelyManager(
null,
Expand All @@ -117,7 +120,7 @@ public void setup() throws Exception {
notificationCenter,
null,
odpManager,
"test-vuid",
vuidManager,
null,
null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2222,7 +2222,7 @@ public void testVuidRegister() {
Optimizely mockOptimizely = mock(Optimizely.class);
when(mockOptimizely.isValid()).thenReturn(true);

OptimizelyClient optimizelyClient = new OptimizelyClient(mockOptimizely, logger, "any-vuid");
OptimizelyClient optimizelyClient = new OptimizelyClient(mockOptimizely, logger, "vuid_123");

verify(mockOptimizely).sendODPEvent(
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.optimizely.ab.config.parser.ConfigParseException;
import com.optimizely.ab.event.EventHandler;
import com.optimizely.ab.event.EventProcessor;
import com.optimizely.ab.android.odp.VuidManager;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -166,8 +167,10 @@ public void initializeSyncWithEnvironment() {
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
EventHandler eventHandler = mock(DefaultEventHandler.class);
EventProcessor eventProcessor = mock(EventProcessor.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
VuidManager vuidManager = new VuidManager(context, true);
OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, 3600L, datafileHandler, null, 3600L,
eventHandler, eventProcessor, null, null, null, null, null, null, null);
eventHandler, eventProcessor, null, null, null, null, vuidManager, null, null);
/*
* Scenario#1: when datafile is not Empty
* Scenario#2: when datafile is Empty
Expand Down Expand Up @@ -225,8 +228,10 @@ public void initializeAsyncWithEnvironment() {
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
EventHandler eventHandler = mock(DefaultEventHandler.class);
EventProcessor eventProcessor = mock(EventProcessor.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
VuidManager vuidManager = new VuidManager(context, true);
final OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, 3600L, datafileHandler, null, 3600L,
eventHandler, eventProcessor, null, null, null, null, null, null, null);
eventHandler, eventProcessor, null, null, null, null, vuidManager, null, null);

/*
* Scenario#1: when datafile is not Empty
Expand Down Expand Up @@ -496,9 +501,9 @@ public void initializeSyncWithUpdateOnNewDatafileDisabled() {
DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
Logger logger = mock(Logger.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

VuidManager vuidManager = new VuidManager(context, true);
OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null, null, null);
null, null, null, null, null, null, vuidManager, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -529,9 +534,9 @@ public void initializeSyncWithUpdateOnNewDatafileEnabled() {
DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
Logger logger = mock(Logger.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

VuidManager vuidManager = new VuidManager(context, true);
OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null, null, null);
null, null, null, null, null, null, vuidManager, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -562,9 +567,9 @@ public void initializeSyncWithDownloadToCacheDisabled() {
DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
Logger logger = mock(Logger.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

VuidManager vuidManager = new VuidManager(context, true);
OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null, null, null);
null, null, null, null, null, null, vuidManager, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -595,9 +600,9 @@ public void initializeSyncWithUpdateOnNewDatafileDisabledWithPeriodicPollingEnab
DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
Logger logger = mock(Logger.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

VuidManager vuidManager = new VuidManager(context, true);
OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null, null, null);
null, null, null, null, null, null, vuidManager, null, null);

doAnswer(
(Answer<Object>) invocation -> {
Expand Down Expand Up @@ -627,9 +632,9 @@ public void initializeSyncWithUpdateOnNewDatafileEnabledWithPeriodicPollingEnabl
DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
Logger logger = mock(Logger.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

VuidManager vuidManager = new VuidManager(context, true);
OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null, null, null);
null, null, null, null, null, null, vuidManager, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -660,9 +665,9 @@ public void initializeSyncWithUpdateOnNewDatafileDisabledWithPeriodicPollingDisa
DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
Logger logger = mock(Logger.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

VuidManager vuidManager = new VuidManager(context, true);
OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null, null, null);
null, null, null, null, null, null, vuidManager, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -694,9 +699,9 @@ public void initializeSyncWithUpdateOnNewDatafileEnabledWithPeriodicPollingDisab
DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
Logger logger = mock(Logger.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

VuidManager vuidManager = new VuidManager(context, true);
OptimizelyManager manager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null, null, null);
null, null, null, null, null, null, vuidManager, null, null);

doAnswer(
new Answer<Object>() {
Expand Down Expand Up @@ -727,9 +732,9 @@ public void initializeSyncWithResourceDatafileNoCache() {
DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
Logger logger = mock(Logger.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

VuidManager vuidManager = new VuidManager(context, true);
OptimizelyManager manager = spy(new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null, null, null));
null, null, null, null, null, null, vuidManager, null, null));

datafileHandler.removeSavedDatafile(context, manager.getDatafileConfig());
OptimizelyClient client = manager.initialize(context, R.raw.datafile, downloadToCache, updateConfigOnNewDatafile);
Expand All @@ -744,9 +749,9 @@ public void initializeSyncWithResourceDatafileNoCacheWithDefaultParams() {
DefaultDatafileHandler datafileHandler = spy(new DefaultDatafileHandler());
Logger logger = mock(Logger.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

VuidManager vuidManager = new VuidManager(context, true);
OptimizelyManager manager = spy(new OptimizelyManager(testProjectId, testSdkKey, null, logger, pollingInterval, datafileHandler, null, 0,
null, null, null, null, null, null, null, null, null));
null, null, null, null, null, null, vuidManager, null, null));

datafileHandler.removeSavedDatafile(context, manager.getDatafileConfig());
OptimizelyClient client = manager.initialize(context, R.raw.datafile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class OptimizelyClient {
So, we start with an empty map of default attributes until the manager is initialized.
*/

if (isValid()) {
if (isValid() && vuid != null && VuidManager.isVuid(vuid)) {
// identifiers are empty here since vuid will be inserted by java-sdk core
sendODPEvent(null, "client_initialized", null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public class OptimizelyManager {

@NonNull private UserProfileService userProfileService;
@Nullable private ODPManager odpManager;
@Nullable private final String vuid;

@Nullable private VuidManager vuidManager;
@Nullable private OptimizelyStartListener optimizelyStartListener;
private boolean returnInMainThreadFromAsyncInit = true;

Expand All @@ -117,7 +116,7 @@ public class OptimizelyManager {
@NonNull NotificationCenter notificationCenter,
@Nullable List<OptimizelyDecideOption> defaultDecideOptions,
@Nullable ODPManager odpManager,
@Nullable String vuid,
@Nullable VuidManager vuidManger,
@Nullable String clientEngineName,
@Nullable String clientVersion) {

Expand All @@ -140,7 +139,7 @@ public class OptimizelyManager {
this.eventProcessor = eventProcessor;
this.errorHandler = errorHandler;
this.userProfileService = userProfileService;
this.vuid = vuid;
this.vuidManager = vuidManger;
this.odpManager = odpManager;
this.notificationCenter = notificationCenter;
this.defaultDecideOptions = defaultDecideOptions;
Expand Down Expand Up @@ -652,8 +651,7 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri
builder.withDefaultDecideOptions(defaultDecideOptions);
builder.withODPManager(odpManager);
Optimizely optimizely = builder.build();

return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class), vuid);
return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class), vuidManager.getVuid());
}

@NonNull
Expand Down Expand Up @@ -792,8 +790,7 @@ public static class Builder {
private int timeoutForODPSegmentFetchInSecs = 10;
private int timeoutForODPEventDispatchInSecs = 10;
private boolean odpEnabled = true;
private String vuid = null;

private boolean vuidEnabled = false;
private String customSdkName = null;
private String customSdkVersion = null;

Expand Down Expand Up @@ -823,6 +820,10 @@ public Builder withSDKKey(String sdkKey) {
this.sdkKey = sdkKey;
return this;
}
public Builder withVuidEnabled() {
this.vuidEnabled = true;
return this;
}

/**
* Override the default {@link Logger}.
Expand Down Expand Up @@ -1036,10 +1037,10 @@ public Builder withODPDisabled() {
* @param vuid a user-defined vuid value
* @return this {@link Builder} instance
*/
public Builder withVuid(String vuid) {
this.vuid = vuid;
return this;
}
// public Builder withVuid(String vuid) {
// this.vuid = vuid;
// return this;
// }

/**
* Override the SDK name and version (for client SDKs like flutter-sdk wrapping the core android-sdk) to be included in events.
Expand Down Expand Up @@ -1120,17 +1121,15 @@ public OptimizelyManager build(Context context) {

}

if (vuid == null) {
vuid = VuidManager.Companion.getShared(context).getVuid();
}
VuidManager vuidManager = new VuidManager(context, vuidEnabled);

ODPManager odpManager = null;
if (odpEnabled) {
// Pass common data for android-sdk only to java-core sdk. All ODP events will include these data.
Map<String, Object> commonData = OptimizelyDefaultAttributes.buildODPCommonData(context, logger);

// Pass common identifiers for android-sdk only to java-core sdk. All ODP events will include these identifiers.
Map<String, String> commonIdentifiers = (vuid != null) ? Collections.singletonMap("vuid", vuid) : Collections.emptyMap();
Map<String, String> commonIdentifiers = (vuidEnabled) ? Collections.singletonMap("vuid", vuidManager.getVuid()) : Collections.emptyMap();

ODPApiManager odpApiManager = new DefaultODPApiManager(
context,
Expand Down Expand Up @@ -1165,7 +1164,7 @@ public OptimizelyManager build(Context context) {
notificationCenter,
defaultDecideOptions,
odpManager,
vuid,
vuidManager,
customSdkName,
customSdkVersion
);
Expand Down
Loading
Loading