Skip to content

Commit

Permalink
Fix use of 'allowEmpty' parameter in IdentityProperties.toXDMData() (#86
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kevinlind authored Jan 13, 2023
1 parent 86ae5a2 commit e12492d
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testECID_loadedFromPersistence() throws Exception {
public void testECID_edgePersistenceTakesPreferenceOverDirectExtension() throws Exception {
// setup
setIdentityDirectPersistedECID("legacyECID");
setEdgeIdentityPersistence(createIdentityMap("ECID", "edgeECID").asXDMMap());
setEdgeIdentityPersistence(createIdentityMap("ECID", "edgeECID").asXDMMap(false));
registerExtensions(Arrays.asList(MonitorExtension.EXTENSION, Identity.EXTENSION), null);

// verify
Expand Down Expand Up @@ -217,7 +217,7 @@ public void testECID_AreDifferentAfterResetIdentitiesAndPrivacyChange() throws E
public void testECID_DirectEcidIsRemovedOnPrivacyOptOut() throws Exception {
// setup
setIdentityDirectPersistedECID("legacyECID");
setEdgeIdentityPersistence(createIdentityMap("ECID", "edgeECID").asXDMMap());
setEdgeIdentityPersistence(createIdentityMap("ECID", "edgeECID").asXDMMap(false));

registerExtensions(
Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void handleUpdateIdentities(@NonNull final Event event) {

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

Expand All @@ -271,12 +271,12 @@ 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));
resolver.resolve(state.getIdentityProperties().toXDMData());
return;
}

state.updateCustomerIdentifiers(map);
resolver.resolve(state.getIdentityProperties().toXDMData(false));
resolver.resolve(state.getIdentityProperties().toXDMData());
}

/**
Expand All @@ -292,7 +292,7 @@ void handleRemoveIdentity(@NonNull final Event event) {

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

Expand All @@ -304,12 +304,12 @@ 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));
resolver.resolve(state.getIdentityProperties().toXDMData());
return;
}

state.removeCustomerIdentifiers(map);
resolver.resolve(state.getIdentityProperties().toXDMData(false));
resolver.resolve(state.getIdentityProperties().toXDMData());
}

/**
Expand All @@ -318,7 +318,7 @@ void handleRemoveIdentity(@NonNull final Event event) {
* @param event the identity request {@link Event}
*/
private void handleGetIdentifiersRequest(@NonNull final Event event) {
final Map<String, Object> xdmData = state.getIdentityProperties().toXDMData(false);
final Map<String, Object> xdmData = state.getIdentityProperties().toXDMData(true);
final Event responseEvent = new Event.Builder(
IdentityConstants.EventNames.IDENTITY_RESPONSE_CONTENT_ONE_TIME,
EventType.EDGE_IDENTITY,
Expand All @@ -340,7 +340,7 @@ 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();
resolver.resolve(state.getIdentityProperties().toXDMData(false));
resolver.resolve(state.getIdentityProperties().toXDMData());

// dispatch reset complete event
final Event responseEvent = new Event.Builder(
Expand Down Expand Up @@ -403,6 +403,6 @@ void handleRequestContent(@NonNull final Event event) {
* @param event the {@link Event} that triggered the XDM shared state change
*/
private void shareIdentityXDMSharedState(final Event event) {
sharedStateHandle.createXDMSharedState(state.getIdentityProperties().toXDMData(false), event);
sharedStateHandle.createXDMSharedState(state.getIdentityProperties().toXDMData(), event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,11 @@ boolean clearItemsForNamespace(final String namespace) {
return isRemoved;
}

/**
* Use this method to cast the {@link IdentityMap} as {@code Map<String, Object>} to be passed as EventData for an SDK Event.
* This method returns an empty map if the {@code IdentityMap} contains no data
*
* @return {@code Map} representation of xdm formatted IdentityMap
*/
Map<String, Object> asXDMMap() {
return asXDMMap(true);
}

/**
* Use this method to cast the {@link IdentityMap} as {@code Map<String,Object>} to be passed as EventData for an SDK Event.
*
* @param allowEmpty If false and if this {@code IdentityMap} contains no data, then returns a map with empty xdmFormatted Identity Map.
* If true and if this {@code IdentityMap} contains no data, then returns an empty map
* @param allowEmpty If true and if this {@code IdentityMap} contains no data, then returns a map with empty xdmFormatted Identity Map.
* If false and if this {@code IdentityMap} contains no data, then returns an empty map
* @return {@code Map} representation of xdm formatted IdentityMap
*/
Map<String, Object> asXDMMap(final boolean allowEmpty) {
Expand All @@ -260,7 +250,7 @@ Map<String, Object> asXDMMap(final boolean allowEmpty) {
identityMap.put(namespace, namespaceIds);
}

if (!identityMap.isEmpty() || !allowEmpty) {
if (!identityMap.isEmpty() || allowEmpty) {
xdmMap.put(IdentityConstants.XDMKeys.IDENTITY_MAP, identityMap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,21 @@ void removeCustomerIdentifiers(final IdentityMap map) {
}

/**
* Converts this into an event data representation in XDM format
* Converts this {@code IdentityProperties} into an event data representation in XDM format
* Use this method to cast the {@link IdentityMap} as {@code Map<String, Object>} to be passed as EventData for an SDK Event.
* This method returns an empty map if the {@code IdentityMap} contains no data
*
* @param allowEmpty If this {@link IdentityProperties} contains no data, return a dictionary with a single {@link IdentityMap} key
* @return A {@link Map} representing this in XDM format, or an empty {@link Map} if this contains no data.
*/
Map<String, Object> toXDMData() {
return toXDMData(false);
}

/**
* Converts this {@code IdentityProperties} into an event data representation in XDM format
*
* @param allowEmpty If this {@link IdentityProperties} contains no data, return a dictionary with a single {@link IdentityMap} key,
* otherwise an empty map is returned.
* @return A {@link Map} representing this in XDM format
*/
Map<String, Object> toXDMData(final boolean allowEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ else if (isIdentityDirectRegistered(eventHubStateResult.getValue())) {

hasBooted = true;
Log.debug(LOG_TAG, LOG_SOURCE, "Edge Identity has successfully booted up");
callback.createXDMSharedState(identityProperties.toXDMData(false), null);
callback.createXDMSharedState(identityProperties.toXDMData(), null);

return hasBooted;
}
Expand Down Expand Up @@ -209,7 +209,7 @@ void updateAdvertisingIdentifier(final Event event, final SharedStateCallback ca

// Save to persistence
identityStorageManager.savePropertiesToPersistence(identityProperties);
callback.createXDMSharedState(identityProperties.toXDMData(false), event);
callback.createXDMSharedState(identityProperties.toXDMData(), event);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void savePropertiesToPersistence(final IdentityProperties properties) {
return;
}

final JSONObject jsonObject = new JSONObject(properties.toXDMData(false));
final JSONObject jsonObject = new JSONObject(properties.toXDMData());
final String jsonString = jsonObject.toString();
edgeIdentityStore.setString(IdentityConstants.DataStoreKey.IDENTITY_PROPERTIES, jsonString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ public void test_handleUpdateIdentities_whenValidData_updatesCustomerIdentifiers
// verify identifiers updated
final ArgumentCaptor<IdentityMap> identityMapCaptor = ArgumentCaptor.forClass(IdentityMap.class);
verify(mockIdentityState).updateCustomerIdentifiers(identityMapCaptor.capture());
assertEquals(identityXDM, identityMapCaptor.getValue().asXDMMap());
assertEquals(identityXDM, identityMapCaptor.getValue().asXDMMap(false));

// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(updateIdentityEvent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void test_AddItem() {
map.addItem(new IdentityItem("California"), "location");

// verify
IdentityTestUtil.flattenMap(map.asXDMMap()).get("identityMap.location[0].id");
IdentityTestUtil.flattenMap(map.asXDMMap(false)).get("identityMap.location[0].id");
}

@Test
Expand Down Expand Up @@ -148,7 +148,7 @@ public void test_merge_sameItem_GetsReplaced() {
baseMap.merge(newMap);

// verify the existing identityMap is unchanged
Map<String, String> flattenedMap = IdentityTestUtil.flattenMap(baseMap.asXDMMap());
Map<String, String> flattenedMap = IdentityTestUtil.flattenMap(baseMap.asXDMMap(false));
assertEquals(3, flattenedMap.size());
assertEquals("California", flattenedMap.get("identityMap.location[0].id"));
assertEquals("authenticated", flattenedMap.get("identityMap.location[0].authenticatedState"));
Expand Down Expand Up @@ -219,7 +219,7 @@ public void test_removeAllIdentityItemsForNamespace_onEmptyMap() {

// test
emptyMap.clearItemsForNamespace("location");
assertTrue(emptyMap.asXDMMap().isEmpty());
assertTrue(emptyMap.asXDMMap(false).isEmpty());
}

@Test
Expand Down Expand Up @@ -301,7 +301,7 @@ public void test_FromData() throws Exception {
IdentityMap map = IdentityMap.fromXDMMap(xdmData);

// verify
Map<String, String> flattenedMap = IdentityTestUtil.flattenMap(map.asXDMMap());
Map<String, String> flattenedMap = IdentityTestUtil.flattenMap(map.asXDMMap(false));
assertEquals("randomECID", flattenedMap.get("identityMap.ECID[0].id"));
assertEquals("ambiguous", flattenedMap.get("identityMap.ECID[0].authenticatedState"));
assertEquals("true", flattenedMap.get("identityMap.ECID[0].primary"));
Expand Down Expand Up @@ -387,24 +387,24 @@ public void testFromXDMMap_InvalidIdentityMap() throws Exception {
}

@Test
public void testAsXDMMap_AllowEmptyTrue() {
public void testAsXDMMap_AllowEmptyFalse() {
IdentityMap map = new IdentityMap();
Map xdmMap = map.asXDMMap(true);
Map xdmMap = map.asXDMMap(false);
assertTrue(xdmMap.isEmpty());
}

@Test
public void testAsXDMMap_AllowEmptyFalse() {
public void testAsXDMMap_AllowEmptyTrue() {
IdentityMap map = new IdentityMap();
Map xdmMap = map.asXDMMap(false);
Map xdmMap = map.asXDMMap(true);

// verify that the base xdm key identityMap is present
assertEquals(1, xdmMap.size());
assertEquals(new HashMap<>(), xdmMap.get(IdentityConstants.XDMKeys.IDENTITY_MAP));
}

private Map<String, List<IdentityItem>> getCastedIdentityMap(final IdentityMap map) {
final Map<String, Object> xdmMap = map.asXDMMap();
final Map<String, Object> xdmMap = map.asXDMMap(false);
return (Map<String, List<IdentityItem>>) xdmMap.get(IdentityConstants.XDMKeys.IDENTITY_MAP);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@

import static com.adobe.marketing.mobile.edge.identity.IdentityTestUtil.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.adobe.marketing.mobile.util.DataReader;
import com.adobe.marketing.mobile.util.DataReaderException;
import java.util.Map;
import org.junit.Test;

Expand All @@ -25,13 +29,31 @@ public class IdentityPropertiesTests {
// ======================================================================================================================

@Test
public void test_toXDMData_AllowEmpty() {
public void test_toXDMData_AllowEmpty_True() throws DataReaderException {
// setup
IdentityProperties props = new IdentityProperties();

// test
Map<String, Object> xdmMap = props.toXDMData(true);

// verify
Map<String, Object> identityMap = DataReader.getTypedMap(
Object.class,
xdmMap,
IdentityConstants.XDMKeys.IDENTITY_MAP
);
assertNotNull(identityMap);
assertTrue(identityMap.isEmpty());
}

@Test
public void test_toXDMData_AllowEmpty_False() {
// setup
IdentityProperties props = new IdentityProperties();

// test
Map<String, Object> xdmMap = props.toXDMData(false);

// verify
assertNull(xdmMap.get(IdentityConstants.XDMKeys.IDENTITY_MAP));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ public void testUpdateIdentities() {
assertEquals(IdentityConstants.EventNames.UPDATE_IDENTITIES, dispatchedEvent.getName());
assertEquals(EventType.EDGE_IDENTITY, dispatchedEvent.getType());
assertEquals(EventSource.UPDATE_IDENTITY, dispatchedEvent.getSource());
assertEquals(map.asXDMMap(), dispatchedEvent.getEventData());
assertEquals(map.asXDMMap(false), dispatchedEvent.getEventData());
}

@Test
Expand Down Expand Up @@ -685,7 +685,7 @@ public void testRemoveIdentity() {

final IdentityMap expectedIdentityMap = new IdentityMap();
expectedIdentityMap.addItem(sampleItem, "namespace");
assertEquals(expectedIdentityMap.asXDMMap(), dispatchedEvent.getEventData());
assertEquals(expectedIdentityMap.asXDMMap(false), dispatchedEvent.getEventData());
}

@Test
Expand Down

0 comments on commit e12492d

Please sign in to comment.