Skip to content

Commit

Permalink
[MOB-15718] Updates the parsing of invalid identityMap (#52)
Browse files Browse the repository at this point in the history
* [MOB-15718] Added the cast to map logic inside the try catch

* [MOB-15718] fromXDMMap returns null if invalid xdm data

* [MOB-15718] Parse IdentityMap updates to handle classCastException + unit tests

Co-authored-by: Arjun Bhadra <[email protected]>
  • Loading branch information
emdobrin and addb authored Feb 10, 2022
1 parent 3c9f7c6 commit 9bf6486
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,21 +292,30 @@ static IdentityMap fromXDMMap(final Map<String, Object> map) {
return null;
}

final Map<String, Object> identityMapDict = (HashMap<String, Object>) map.get(
IdentityConstants.XDMKeys.IDENTITY_MAP
);
Map<String, Object> identityMapDict = null;
try {
identityMapDict = (HashMap<String, Object>) map.get(IdentityConstants.XDMKeys.IDENTITY_MAP);
} catch (ClassCastException e) {
MobileCore.log(
LoggingMode.ERROR,
LOG_TAG,
String.format("Failed to create IdentityMap from data. Exception thrown: %s", e.getLocalizedMessage())
);
}

if (identityMapDict == null) {
return null;
}

final IdentityMap identityMap = new IdentityMap();

for (final String namespace : identityMapDict.keySet()) {
try {
final ArrayList<HashMap<String, Object>> idArr = (ArrayList<HashMap<String, Object>>) identityMapDict.get(
namespace
);
if (idArr == null) {
continue;
}

for (Object idMap : idArr) {
final IdentityItem item = IdentityItem.fromData((Map<String, Object>) idMap);
Expand All @@ -316,7 +325,15 @@ static IdentityMap fromXDMMap(final Map<String, Object> map) {
}
}
} catch (ClassCastException e) {
MobileCore.log(LoggingMode.DEBUG, LOG_TAG, "Failed to create IdentityMap from data.");
MobileCore.log(
LoggingMode.ERROR,
LOG_TAG,
String.format(
"Failed to parse data for namespace (%s). Exception thrown: %s",
namespace,
e.getLocalizedMessage()
)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ public void testFromXDMMap_NullAndEmptyData() {
}

@Test
public void testFromXDMMap_InvalidXDMData() throws Exception {
public void testFromXDMMap_InvalidNamespace() throws Exception {
// setup
// ECID is map instead of list
// ECID namespace is map instead of list
final String invalidJsonStr =
"{\n" +
" \"identityMap\": {\n" +
Expand All @@ -340,6 +340,51 @@ public void testFromXDMMap_InvalidXDMData() throws Exception {
assertTrue(map.isEmpty());
}

@Test
public void testFromXDMMap_InvalidItemForNamespace() throws Exception {
// setup
// namespace is an array of arrays instead of an array of identity items
final String invalidJsonStr =
"{\n" +
" \"identityMap\": {\n" +
" \"ECID\": [{\n" +
" \"id\": \"randomECID\",\n" +
" \"authenticatedState\": \"ambiguous\",\n" +
" \"primary\": true\n" +
" }],\n" +
" \"namespace\": [\n" +
" [ \"arrayInsteadOfMap\", \"invalid\"]\n" +
" ]\n" +
" }\n," +
"}";

final JSONObject jsonObject = new JSONObject(invalidJsonStr);
final Map<String, Object> xdmData = Utils.toMap(jsonObject);

// test
IdentityMap map = IdentityMap.fromXDMMap(xdmData);

// verify
// only ECID namespace is correct, namespace should be dropped due to invalid format
assertEquals(1, map.getNamespaces().size());
assertEquals("ECID", map.getNamespaces().get(0));
}

@Test
public void testFromXDMMap_InvalidIdentityMap() throws Exception {
// setup
final String invalidJsonStr = "{\"identityMap\": [\"not a map\"]}";

final JSONObject jsonObject = new JSONObject(invalidJsonStr);
final Map<String, Object> xdmData = Utils.toMap(jsonObject);

// test
IdentityMap map = IdentityMap.fromXDMMap(xdmData);

// verify
assertNull(map);
}

@Test
public void testAsXDMMap_AllowEmptyTrue() {
IdentityMap map = new IdentityMap();
Expand Down

0 comments on commit 9bf6486

Please sign in to comment.