Skip to content

Commit

Permalink
[FSSDK-9493] fix: Added check in AsyncGetQualifiedSegments to check i…
Browse files Browse the repository at this point in the history
…f userID is fsuserid or vuid (#527)

* Added check in AsyncGetQualifiedSegments to check if userID is fsuserid or vuid

* Update unit test and added additional test to verify that proper userKey is getting passed given vuid and userid

---------

Co-authored-by: NomanShoaib <m.nomanshoaib09@gmail.com>
  • Loading branch information
mnoman09 and NomanShoaib authored Jul 28, 2023
1 parent c507649 commit f5528d5
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 4 deletions.
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

0 comments on commit f5528d5

Please sign in to comment.