Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ private void deleteIncomingFieldByPath(ListValue listValue, JsonNode foundNode)
if (foundNode != null && !foundNode.isEmpty()) {
ArrayNode arrayNode = (ArrayNode) foundNode;
int indexForDelete = 0;
for (int i = 0; i < arrayNode.size() + 1; i++) {
int initialSize = arrayNode.size();
for (int i = 0; i < initialSize + 1; i++) {
if (arrayNode.get(i - indexForDelete) != null && listValue.getValue()
.contains(arrayNode.get(i - indexForDelete).textValue())) {
arrayNode.remove(i - indexForDelete);
Expand All @@ -403,11 +404,7 @@ private void removeIncomingArrayValues(JsonNode currentObject, String currentPat
}

private void removeMatchingObjectValues(JsonNode currentObject, ArrayNode arrayNode) {
for (int i = 0; i < arrayNode.size(); i++) {
if (arrayNode.get(i).equals(currentObject) || ifDeepEquals(currentObject, arrayNode.get(i))) {
arrayNode.remove(i);
}
}
arrayNode.removeIf(node -> ifDeepEquals(currentObject, node));
}

/**
Expand All @@ -424,7 +421,8 @@ private void removeScalarIncomingValues(JsonNode currentObject, String currentPa
incomingArray.forEach(node -> incomingValues.add(node.textValue()));

int indexForDelete = 0;
for (int i = 0; i < arrayNode.size() + 1; i++) {
int initialSize = arrayNode.size();
for (int i = 0; i < initialSize + 1; i++) {
JsonNode node = arrayNode.get(i - indexForDelete);
if (node != null && incomingValues.contains(node.textValue())) {
arrayNode.remove(i - indexForDelete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,82 @@ void shouldWrite_RepeatableDeleteIncomingValuesInStringArray() throws IOExceptio
assertEquals("{\"instance\":{\"administrativeNotes\":[\"Test2\",\"Test3\"]}}", resultInstance);
}

@Test
void shouldDeleteAllMatchingEntries_whenDeleteIncomingWithInterspersedDuplicates() throws IOException {
// given
DataImportEventPayload eventContext = new DataImportEventPayload();
HashMap<String, String> context = new HashMap<>();
context.put(EntityType.INSTANCE.value(),
"""
{
"instance": {
"administrativeNotes": ["Keep1", "Delete", "Keep2", "Delete", "Delete", "Keep3", "Delete"]
}
}
""");
eventContext.setContext(context);
// when
WRITER.initialize(eventContext);
WRITER.write("instance.administrativeNotes[]", ListValue.of(List.of("Delete"), DELETE_INCOMING));
WRITER.getResult(eventContext);
// then
String resultInstance = eventContext.getContext().get(EntityType.INSTANCE.value());
assertEquals("{\"instance\":{\"administrativeNotes\":[\"Keep1\",\"Keep2\",\"Keep3\"]}}", resultInstance);
}

@Test
void shouldDeleteAllConsecutiveDuplicates_whenDeleteIncomingObjectsAreConsecutive() throws IOException {
// given
DataImportEventPayload eventContext = new DataImportEventPayload();
HashMap<String, String> context = new HashMap<>();
context.put(EntityType.INSTANCE.value(),
"""
{
"instance": {
"contributor": [{"id":"1"},{"id":"2"},{"id":"2"},{"id":"3"}]
}
}
""");
eventContext.setContext(context);
// when
WRITER.initialize(eventContext);
List<Map<String, Value>> objects = List.of(Map.of("instance.contributor[].id", StringValue.of("2")));
RepeatableFieldValue field =
RepeatableFieldValue.of(objects, MappingRule.RepeatableFieldAction.DELETE_INCOMING, "contributor");
WRITER.write("instance.contributor[]", field);
WRITER.getResult(eventContext);
// then
String resultInstance = eventContext.getContext().get(EntityType.INSTANCE.value());
assertEquals("{\"instance\":{\"contributor\":[{\"id\":\"1\"},{\"id\":\"3\"}]}}", resultInstance);
}

@Test
void shouldDeleteAllMatchingScalars_whenDeleteIncomingViaRepeatableFieldWithInterspersed() throws IOException {
// given
DataImportEventPayload eventContext = new DataImportEventPayload();
HashMap<String, String> context = new HashMap<>();
context.put(EntityType.INSTANCE.value(),
"""
{
"instance": {
"administrativeNotes": ["Delete","Keep","Delete","Delete","Keep","Delete"]
}
}
""");
eventContext.setContext(context);
// when
WRITER.initialize(eventContext);
List<Map<String, Value>> values =
List.of(Map.of("instance.administrativeNotes[]", ListValue.of(List.of("Delete"))));
RepeatableFieldValue field =
RepeatableFieldValue.of(values, MappingRule.RepeatableFieldAction.DELETE_INCOMING, "administrativeNotes");
WRITER.write("instance.administrativeNotes[]", field);
WRITER.getResult(eventContext);
// then
String resultInstance = eventContext.getContext().get(EntityType.INSTANCE.value());
assertEquals("{\"instance\":{\"administrativeNotes\":[\"Keep\",\"Keep\"]}}", resultInstance);
}

@Test
void shouldWrite_RepeatableDeleteIncomingValuesIfThereAreSomeAdditionalFieldsExistsInEntity() throws IOException {
// given
Expand Down
Loading