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

[FSSDK-9493] fix: Added check in AsyncGetQualifiedSegments to check if userID is fsuserid or vuid #527

Merged
merged 2 commits into from
Jul 28, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ public void getQualifiedSegments(ODPUserKey userKey, String userValue, ODPSegmen
getQualifiedSegments(userKey, userValue, callback, Collections.emptyList());
}

public void getQualifiedSegments(String fsUserId, ODPSegmentFetchCallback callback, List<ODPSegmentOption> segmentOptions) {
getQualifiedSegments(ODPUserKey.FS_USER_ID, fsUserId, callback, segmentOptions);
public void getQualifiedSegments(String userId, ODPSegmentFetchCallback callback, List<ODPSegmentOption> options) {
if (ODPManager.isVuid(userId)) {
getQualifiedSegments(ODPUserKey.VUID, userId, callback, options);
} else {
getQualifiedSegments(ODPUserKey.FS_USER_ID, userId, callback, options);
}
}

public void getQualifiedSegments(String fsUserId, ODPSegmentFetchCallback callback) {
getQualifiedSegments(ODPUserKey.FS_USER_ID, fsUserId, callback, Collections.emptyList());
public void getQualifiedSegments(String userId, ODPSegmentFetchCallback callback) {
getQualifiedSegments(userId, callback, Collections.emptyList());
}

private String getCacheKey(String userKey, String userValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,103 @@ public void fetchQualifiedSegmentsAsync() throws InterruptedException {
assertEquals(Arrays.asList("segment1", "segment2"), userContext.getQualifiedSegments());
}

@Test
public void fetchQualifiedSegmentsAsyncWithVUID() throws InterruptedException {
ODPEventManager mockODPEventManager = mock(ODPEventManager.class);
ODPApiManager mockAPIManager = mock(ODPApiManager.class);
ODPSegmentManager mockODPSegmentManager = spy(new ODPSegmentManager(mockAPIManager));
ODPManager mockODPManager = mock(ODPManager.class);

doAnswer(
invocation -> {
ODPSegmentManager.ODPSegmentFetchCallback callback = invocation.getArgumentAt(2, ODPSegmentManager.ODPSegmentFetchCallback.class);
callback.onCompleted(Arrays.asList("segment1", "segment2"));
return null;
}
).when(mockODPSegmentManager).getQualifiedSegments(any(), eq("vuid_f6db3d60ba3a493d8e41bc995bb"), (ODPSegmentManager.ODPSegmentFetchCallback) any(), any());
Mockito.when(mockODPManager.getEventManager()).thenReturn(mockODPEventManager);
Mockito.when(mockODPManager.getSegmentManager()).thenReturn(mockODPSegmentManager);

Optimizely optimizely = Optimizely.builder()
.withDatafile(datafile)
.withEventProcessor(new ForwardingEventProcessor(eventHandler, null))
.withODPManager(mockODPManager)
.build();

OptimizelyUserContext userContext = optimizely.createUserContext("vuid_f6db3d60ba3a493d8e41bc995bb");

CountDownLatch countDownLatch = new CountDownLatch(1);
userContext.fetchQualifiedSegments((Boolean isFetchSuccessful) -> {
assertTrue(isFetchSuccessful);
countDownLatch.countDown();
});

countDownLatch.await();
verify(mockODPSegmentManager).getQualifiedSegments(eq(ODPUserKey.VUID), eq("vuid_f6db3d60ba3a493d8e41bc995bb"), any(ODPSegmentManager.ODPSegmentFetchCallback.class), eq(Collections.emptyList()));
assertEquals(Arrays.asList("segment1", "segment2"), userContext.getQualifiedSegments());

// reset qualified segments
userContext.setQualifiedSegments(Collections.emptyList());
CountDownLatch countDownLatch2 = new CountDownLatch(1);
userContext.fetchQualifiedSegments((Boolean isFetchSuccessful) -> {
assertTrue(isFetchSuccessful);
countDownLatch2.countDown();
}, Collections.singletonList(ODPSegmentOption.RESET_CACHE));

countDownLatch2.await();
verify(mockODPSegmentManager).getQualifiedSegments(eq(ODPUserKey.VUID) ,eq("vuid_f6db3d60ba3a493d8e41bc995bb"), any(ODPSegmentManager.ODPSegmentFetchCallback.class), eq(Collections.singletonList(ODPSegmentOption.RESET_CACHE)));
assertEquals(Arrays.asList("segment1", "segment2"), userContext.getQualifiedSegments());
}


@Test
public void fetchQualifiedSegmentsAsyncWithUserID() throws InterruptedException {
ODPEventManager mockODPEventManager = mock(ODPEventManager.class);
ODPApiManager mockAPIManager = mock(ODPApiManager.class);
ODPSegmentManager mockODPSegmentManager = spy(new ODPSegmentManager(mockAPIManager));
ODPManager mockODPManager = mock(ODPManager.class);

doAnswer(
invocation -> {
ODPSegmentManager.ODPSegmentFetchCallback callback = invocation.getArgumentAt(2, ODPSegmentManager.ODPSegmentFetchCallback.class);
callback.onCompleted(Arrays.asList("segment1", "segment2"));
return null;
}
).when(mockODPSegmentManager).getQualifiedSegments(any(), eq("f6db3d60ba3a493d8e41bc995bb"), (ODPSegmentManager.ODPSegmentFetchCallback) any(), any());
Mockito.when(mockODPManager.getEventManager()).thenReturn(mockODPEventManager);
Mockito.when(mockODPManager.getSegmentManager()).thenReturn(mockODPSegmentManager);

Optimizely optimizely = Optimizely.builder()
.withDatafile(datafile)
.withEventProcessor(new ForwardingEventProcessor(eventHandler, null))
.withODPManager(mockODPManager)
.build();

OptimizelyUserContext userContext = optimizely.createUserContext("f6db3d60ba3a493d8e41bc995bb");

CountDownLatch countDownLatch = new CountDownLatch(1);
userContext.fetchQualifiedSegments((Boolean isFetchSuccessful) -> {
assertTrue(isFetchSuccessful);
countDownLatch.countDown();
});

countDownLatch.await();
verify(mockODPSegmentManager).getQualifiedSegments(eq(ODPUserKey.FS_USER_ID), eq("f6db3d60ba3a493d8e41bc995bb"), any(ODPSegmentManager.ODPSegmentFetchCallback.class), eq(Collections.emptyList()));
assertEquals(Arrays.asList("segment1", "segment2"), userContext.getQualifiedSegments());

// reset qualified segments
userContext.setQualifiedSegments(Collections.emptyList());
CountDownLatch countDownLatch2 = new CountDownLatch(1);
userContext.fetchQualifiedSegments((Boolean isFetchSuccessful) -> {
assertTrue(isFetchSuccessful);
countDownLatch2.countDown();
}, Collections.singletonList(ODPSegmentOption.RESET_CACHE));

countDownLatch2.await();
verify(mockODPSegmentManager).getQualifiedSegments(eq(ODPUserKey.FS_USER_ID) ,eq("f6db3d60ba3a493d8e41bc995bb"), any(ODPSegmentManager.ODPSegmentFetchCallback.class), eq(Collections.singletonList(ODPSegmentOption.RESET_CACHE)));
assertEquals(Arrays.asList("segment1", "segment2"), userContext.getQualifiedSegments());
}

@Test
public void fetchQualifiedSegmentsAsyncError() throws InterruptedException {
Optimizely optimizely = Optimizely.builder()
Expand Down