-
Notifications
You must be signed in to change notification settings - Fork 145
Unit test to verify multi-level additional data setter #114
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
Changes from all commits
b4ecc09
c75b10d
59fd088
0059f66
fcfdca7
d4f2c0f
d41b575
cd27f49
e006914
0d4d6b2
4831f58
4150c14
d6881da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,59 +118,73 @@ public <T> T deserializeObject(final String inputString, final Class<T> clazz, M | |
| * @param serializedObject the parent object whose children will be iterated to set additional data | ||
| * @param rawJson the raw json | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| private void setChildAdditionalData(IJsonBackedObject serializedObject, JsonObject rawJson) { | ||
| private void setChildAdditionalData(final IJsonBackedObject serializedObject, final JsonObject rawJson) { | ||
| // Use reflection to iterate through fields for eligible Graph children | ||
| for (java.lang.reflect.Field field : serializedObject.getClass().getFields()) { | ||
| try { | ||
| Object fieldObject = field.get(serializedObject); | ||
|
|
||
| // If the object is a HashMap, iterate through its children | ||
| if (fieldObject instanceof HashMap) { | ||
| HashMap<String, Object> serializableChildren = (HashMap<String, Object>) fieldObject; | ||
| Iterator<Entry<String, Object>> it = serializableChildren.entrySet().iterator(); | ||
| if(rawJson != null) { | ||
| for (java.lang.reflect.Field field : serializedObject.getClass().getFields()) { | ||
| try { | ||
| if(field != null) { | ||
| final Object fieldObject = field.get(serializedObject); | ||
| if (fieldObject instanceof HashMap) { | ||
| // If the object is a HashMap, iterate through its children | ||
| @SuppressWarnings("unchecked") | ||
| final HashMap<String, Object> serializableChildren = (HashMap<String, Object>) fieldObject; | ||
| final Iterator<Entry<String, Object>> it = serializableChildren.entrySet().iterator(); | ||
|
|
||
| while (it.hasNext()) { | ||
| Map.Entry<String, Object> pair = (Map.Entry<String, Object>)it.next(); | ||
| Object child = pair.getValue(); | ||
| while (it.hasNext()) { | ||
| final Map.Entry<String, Object> pair = (Map.Entry<String, Object>)it.next(); | ||
| final Object child = pair.getValue(); | ||
|
|
||
| // If the item is a valid Graph object, set its additional data | ||
| if (child instanceof IJsonBackedObject) { | ||
| AdditionalDataManager childAdditionalDataManager = ((IJsonBackedObject) child).additionalDataManager(); | ||
| if(rawJson != null && field != null && rawJson.get(field.getName()) != null && rawJson.get(field.getName()).isJsonObject() | ||
| && rawJson.get(field.getName()).getAsJsonObject().get(pair.getKey()).isJsonObject()) { | ||
| childAdditionalDataManager.setAdditionalData(rawJson.get(field.getName()).getAsJsonObject().get(pair.getKey()).getAsJsonObject()); | ||
| setChildAdditionalData((IJsonBackedObject) child,rawJson.get(field.getName()).getAsJsonObject().get(pair.getKey()).getAsJsonObject()); | ||
| // If the item is a valid Graph object, set its additional data | ||
| if (child instanceof IJsonBackedObject) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to see a test in case where the child is a OData primitive, do we add non-schematized primitive fields to additionalData?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tweaked the last unit test I added so it tests for that as well. |
||
| final AdditionalDataManager childAdditionalDataManager = ((IJsonBackedObject) child).additionalDataManager(); | ||
| final JsonElement fieldElement = rawJson.get(field.getName()); | ||
| if(fieldElement != null && fieldElement.isJsonObject() | ||
| && fieldElement.getAsJsonObject().get(pair.getKey()) != null | ||
| && fieldElement.getAsJsonObject().get(pair.getKey()).isJsonObject()) { | ||
| childAdditionalDataManager.setAdditionalData(fieldElement.getAsJsonObject().get(pair.getKey()).getAsJsonObject()); | ||
| setChildAdditionalData((IJsonBackedObject) child,fieldElement.getAsJsonObject().get(pair.getKey()).getAsJsonObject()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // If the object is a list of Graph objects, iterate through elements | ||
| else if (fieldObject instanceof List && rawJson != null) { | ||
| final JsonElement collectionJson = rawJson.get(field.getName()); | ||
| final List<?> fieldObjectList = (List<?>) fieldObject; | ||
| if (collectionJson.isJsonArray() && ((JsonArray)collectionJson).size() == fieldObjectList.size()) { | ||
| final JsonArray rawJsonArray = (JsonArray) collectionJson; | ||
| for (int i = 0; i < fieldObjectList.size(); i++) { | ||
| final Object element = fieldObjectList.get(i); | ||
| if (element instanceof IJsonBackedObject) { | ||
| final JsonElement elementRawJson = rawJsonArray.get(i); | ||
| setChildAdditionalData((IJsonBackedObject) element, elementRawJson.getAsJsonObject()); | ||
| // If the object is a list of Graph objects, iterate through elements | ||
| else if (fieldObject instanceof List) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test for One test for a homogenous list of objects, and one for a hetergenous list of objects.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a unit test. We currently don't have a case where hashmaps have heterogeneous values and I didn't want to add too much fake code in the test setup. |
||
| final JsonElement collectionJson = rawJson.get(field.getName()); | ||
| final List<?> fieldObjectList = (List<?>) fieldObject; | ||
| if (collectionJson != null && collectionJson.isJsonArray()) { | ||
| final JsonArray rawJsonArray = (JsonArray) collectionJson; | ||
| final Integer fieldObjectListSize = fieldObjectList.size(); | ||
| final Integer rawJsonArraySize = rawJsonArray.size(); | ||
| for (int i = 0; i < fieldObjectListSize && i < rawJsonArraySize; i++) { | ||
| final Object element = fieldObjectList.get(i); | ||
| if (element instanceof IJsonBackedObject) { | ||
| final JsonElement elementRawJson = rawJsonArray.get(i); | ||
| if(elementRawJson != null) { | ||
| setChildAdditionalData((IJsonBackedObject) element, elementRawJson.getAsJsonObject()); | ||
| } | ||
| } | ||
| } | ||
| if (rawJsonArraySize != fieldObjectListSize) | ||
| logger.logDebug("rawJsonArray has a size of " + rawJsonArraySize + " and fieldObjectList of " + fieldObjectListSize); | ||
| } | ||
| } | ||
| // If the object is a valid Graph object, set its additional data | ||
| else if (fieldObject instanceof IJsonBackedObject) { | ||
| final IJsonBackedObject serializedChild = (IJsonBackedObject) fieldObject; | ||
| final AdditionalDataManager childAdditionalDataManager = serializedChild.additionalDataManager(); | ||
| final JsonElement fieldElement = rawJson.get(field.getName()); | ||
| if(fieldElement != null && fieldElement.isJsonObject()) { | ||
| childAdditionalDataManager.setAdditionalData(fieldElement.getAsJsonObject()); | ||
| setChildAdditionalData((IJsonBackedObject) fieldObject,fieldElement.getAsJsonObject()); | ||
| } | ||
| } | ||
| } | ||
| } catch (IllegalArgumentException | IllegalAccessException e) { | ||
| //Not throwing the IllegalArgumentException as the Serialized Object would still be usable even if the additional data is not set. | ||
| logger.logError("Unable to set child fields of " + serializedObject.getClass().getSimpleName(), e); | ||
MIchaelMainer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger.logDebug(rawJson.getAsString()); | ||
| } | ||
| // If the object is a valid Graph object, set its additional data | ||
| else if (fieldObject != null && fieldObject instanceof IJsonBackedObject) { | ||
| IJsonBackedObject serializedChild = (IJsonBackedObject) fieldObject; | ||
| AdditionalDataManager childAdditionalDataManager = serializedChild.additionalDataManager(); | ||
| if(rawJson != null && field != null && rawJson.get(field.getName()) != null && rawJson.get(field.getName()).isJsonObject()) { | ||
| childAdditionalDataManager.setAdditionalData(rawJson.get(field.getName()).getAsJsonObject()); | ||
| setChildAdditionalData((IJsonBackedObject) fieldObject,rawJson.get(field.getName()).getAsJsonObject()); | ||
| } | ||
| } | ||
| } catch (IllegalArgumentException | IllegalAccessException e) { | ||
| logger.logError("Unable to access child fields of " + serializedObject.getClass().getSimpleName(), e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.