Skip to content

Commit

Permalink
Create pending state when resetIdentities is called.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlind committed Jan 12, 2023
1 parent 113677e commit 6952e75
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,14 @@ private void handleUrlVariableResponse(
* @param event the edge update identity {@link Event}
*/
void handleUpdateIdentities(@NonNull final Event event) {
// Add pending shared state to avoid race condition between updating and reading identity map
final SharedStateResolver resolver = getApi().createPendingXDMSharedState(event);

final Map<String, Object> eventData = event.getEventData();

if (eventData == null) {
Log.trace(LOG_TAG, LOG_SOURCE, "Cannot update identifiers, event data is null.");
resolver.resolve(state.getIdentityProperties().toXDMData(false));
return;
}

Expand All @@ -266,11 +270,10 @@ void handleUpdateIdentities(@NonNull final Event event) {
LOG_SOURCE,
"Failed to update identifiers as no identifiers were found in the event data."
);
resolver.resolve(state.getIdentityProperties().toXDMData(false));
return;
}

// Add pending shared state to avoid race condition between updating and reading identity map
final SharedStateResolver resolver = getApi().createPendingXDMSharedState(event);
state.updateCustomerIdentifiers(map);
resolver.resolve(state.getIdentityProperties().toXDMData(false));
}
Expand All @@ -281,10 +284,14 @@ void handleUpdateIdentities(@NonNull final Event event) {
* @param event the edge remove identity request {@link Event}
*/
void handleRemoveIdentity(@NonNull final Event event) {
// Add pending shared state to avoid race condition between updating and reading identity map
final SharedStateResolver resolver = getApi().createPendingXDMSharedState(event);

final Map<String, Object> eventData = event.getEventData();

if (eventData == null) {
Log.trace(LOG_TAG, LOG_SOURCE, "Cannot remove identifiers, event data is null.");
resolver.resolve(state.getIdentityProperties().toXDMData(false));
return;
}

Expand All @@ -296,11 +303,10 @@ void handleRemoveIdentity(@NonNull final Event event) {
LOG_SOURCE,
"Failed to remove identifiers as no identifiers were found in the event data."
);
resolver.resolve(state.getIdentityProperties().toXDMData(false));
return;
}

// Add pending shared state to avoid race condition between updating and reading identity map
final SharedStateResolver resolver = getApi().createPendingXDMSharedState(event);
state.removeCustomerIdentifiers(map);
resolver.resolve(state.getIdentityProperties().toXDMData(false));
}
Expand Down Expand Up @@ -330,8 +336,10 @@ private void handleGetIdentifiersRequest(@NonNull final Event event) {
* @param event the identity request reset {@link Event}
*/
void handleRequestReset(@NonNull final Event event) {
// Add pending shared state to avoid race condition between updating and reading identity map
final SharedStateResolver resolver = getApi().createPendingXDMSharedState(event);
state.resetIdentifiers();
shareIdentityXDMSharedState(event);
resolver.resolve(state.getIdentityProperties().toXDMData(false));

// dispatch reset complete event
final Event responseEvent = new Event.Builder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,10 @@ public void test_handleUpdateIdentities_whenValidData_updatesCustomerIdentifiers
verify(mockIdentityState).updateCustomerIdentifiers(identityMapCaptor.capture());
assertEquals(identityXDM, identityMapCaptor.getValue().asXDMMap());

final ArgumentCaptor<Map<String, Object>> stateCaptor = ArgumentCaptor.forClass(Map.class);
// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(updateIdentityEvent));
verify(mockSharedStateResolver).resolve(stateCaptor.capture());
assertEquals(properties.toXDMData(false), stateCaptor.getValue());
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));

verify(mockExtensionApi, never()).dispatch(any());
}

Expand All @@ -699,6 +699,7 @@ public void test_handleUpdateIdentities_nullEventData_returns() {
// setup
final IdentityProperties properties = new IdentityProperties();
when(mockIdentityState.getIdentityProperties()).thenReturn(properties);
when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);

extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

Expand All @@ -713,17 +714,20 @@ public void test_handleUpdateIdentities_nullEventData_returns() {

// verify that identifiers are not updated
verify(mockIdentityState, never()).updateCustomerIdentifiers(any());
// verify that no shared state is created
verify(mockExtensionApi, never()).createXDMSharedState(any(), any());
// verify that no event is dispatched
verify(mockExtensionApi, never()).dispatch(any());

// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(updateIdentityEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}

@Test
public void test_handleUpdateIdentities_EmptyEventData_returns() {
// setup
final IdentityProperties properties = new IdentityProperties();
when(mockIdentityState.getIdentityProperties()).thenReturn(properties);
when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);
extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

// test
Expand All @@ -738,10 +742,12 @@ public void test_handleUpdateIdentities_EmptyEventData_returns() {

// verify that identifiers are not updated
verify(mockIdentityState, never()).updateCustomerIdentifiers(any());
// verify that no shared state is created
verify(mockExtensionApi, never()).createXDMSharedState(any(), any());
// verify that no event is dispatched
verify(mockExtensionApi, never()).dispatch(any());

// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(updateIdentityEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}

// ========================================================================================
Expand Down Expand Up @@ -786,12 +792,9 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
removedIdentityMapCaptor.getValue().toString()
);

// verify shared state
final Map<String, Object> expectedState = properties.toXDMData(false);
final ArgumentCaptor<Map<String, Object>> stateCaptor = ArgumentCaptor.forClass(Map.class);
// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(removeIdentityEvent));
verify(mockSharedStateResolver).resolve(stateCaptor.capture());
assertEquals(expectedState, stateCaptor.getValue());
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}

@Test
Expand All @@ -803,6 +806,7 @@ public void test_handleRemoveIdentity_whenNullData_returns() {
);
final IdentityProperties properties = new IdentityProperties(identityXDM);
when(mockIdentityState.getIdentityProperties()).thenReturn(properties);
when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);
extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

// test
Expand All @@ -812,13 +816,17 @@ public void test_handleRemoveIdentity_whenNullData_returns() {
// verify identifiers not removed
verify(mockIdentityState, never()).removeCustomerIdentifiers(any());

// verify shared state is never created
verify(mockExtensionApi, never()).createXDMSharedState(any(), any());
// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(removeIdentityEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}

@Test
public void test_handleRemoveIdentity_eventWithEmptyData_returns() {
// setup
final IdentityProperties properties = new IdentityProperties();
when(mockIdentityState.getIdentityProperties()).thenReturn(properties);
when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);
extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

// test
Expand All @@ -828,8 +836,9 @@ public void test_handleRemoveIdentity_eventWithEmptyData_returns() {
// verify identifiers not removed
verify(mockIdentityState, never()).removeCustomerIdentifiers(any());

// verify shared state is never created
verify(mockExtensionApi, never()).createXDMSharedState(any(), any());
// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(notARemoveIdentityEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}

// ========================================================================================
Expand Down Expand Up @@ -894,6 +903,7 @@ public void test_handleRequestContent_EventIsNotAdIdEvent() {
public void test_handleRequestReset() {
final IdentityProperties properties = new IdentityProperties();
when(mockIdentityState.getIdentityProperties()).thenReturn(properties);
when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);

extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

Expand All @@ -903,6 +913,9 @@ public void test_handleRequestReset() {
extension.handleRequestReset(resetEvent);

verify(mockIdentityState).resetIdentifiers();
verify(mockExtensionApi).createXDMSharedState(properties.toXDMData(false), resetEvent); // will fail because of new ecid

// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(resetEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}
}

0 comments on commit 6952e75

Please sign in to comment.