-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explicit Split of Replace Op | Null Json Node handling (#120)
* Comment release plugin * Support: Replace instruction into add remove and add * Add tests and ensure full coverage * Introduce null safety for null target/src nodes * Self review: Undo basic formatting * Add missing test: No replace split in absence of flag * RC: Update JavaDoc * RC: Optimize early returns and enrich existing contract for asJson
- Loading branch information
1 parent
6eadbe6
commit 88793f4
Showing
5 changed files
with
196 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/test/java/com/flipkart/zjsonpatch/JsonSplitReplaceOpTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.flipkart.zjsonpatch; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Test; | ||
|
||
import java.util.EnumSet; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author isopropylcyanide | ||
*/ | ||
public class JsonSplitReplaceOpTest { | ||
|
||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
||
@Test | ||
public void testJsonDiffSplitsReplaceIntoAddAndRemoveOperationWhenFlagIsAdded() throws JsonProcessingException { | ||
String source = "{ \"ids\": [ \"F1\", \"F3\" ] }"; | ||
String target = "{ \"ids\": [ \"F1\", \"F6\", \"F4\" ] }"; | ||
JsonNode sourceNode = OBJECT_MAPPER.reader().readTree(source); | ||
JsonNode targetNode = OBJECT_MAPPER.reader().readTree(target); | ||
|
||
JsonNode diff = JsonDiff.asJson(sourceNode, targetNode, EnumSet.of( | ||
DiffFlags.ADD_EXPLICIT_REMOVE_ADD_ON_REPLACE | ||
)); | ||
assertEquals(3, diff.size()); | ||
assertEquals(Operation.REMOVE.rfcName(), diff.get(0).get("op").textValue()); | ||
assertEquals("/ids/1", diff.get(0).get("path").textValue()); | ||
assertEquals("F3", diff.get(0).get("value").textValue()); | ||
|
||
assertEquals(Operation.ADD.rfcName(), diff.get(1).get("op").textValue()); | ||
assertEquals("/ids/1", diff.get(1).get("path").textValue()); | ||
assertEquals("F6", diff.get(1).get("value").textValue()); | ||
|
||
assertEquals(Operation.ADD.rfcName(), diff.get(2).get("op").textValue()); | ||
assertEquals("/ids/2", diff.get(2).get("path").textValue()); | ||
assertEquals("F4", diff.get(2).get("value").textValue()); | ||
} | ||
|
||
@Test | ||
public void testJsonDiffDoesNotSplitReplaceIntoAddAndRemoveOperationWhenFlagIsNotAdded() throws JsonProcessingException { | ||
String source = "{ \"ids\": [ \"F1\", \"F3\" ] }"; | ||
String target = "{ \"ids\": [ \"F1\", \"F6\", \"F4\" ] }"; | ||
JsonNode sourceNode = OBJECT_MAPPER.reader().readTree(source); | ||
JsonNode targetNode = OBJECT_MAPPER.reader().readTree(target); | ||
|
||
JsonNode diff = JsonDiff.asJson(sourceNode, targetNode); | ||
System.out.println(diff); | ||
assertEquals(2, diff.size()); | ||
assertEquals(Operation.REPLACE.rfcName(), diff.get(0).get("op").textValue()); | ||
assertEquals("/ids/1", diff.get(0).get("path").textValue()); | ||
assertEquals("F6", diff.get(0).get("value").textValue()); | ||
|
||
assertEquals(Operation.ADD.rfcName(), diff.get(1).get("op").textValue()); | ||
assertEquals("/ids/2", diff.get(1).get("path").textValue()); | ||
assertEquals("F4", diff.get(1).get("value").textValue()); | ||
} | ||
|
||
@Test | ||
public void testJsonDiffDoesNotSplitsWhenThereIsNoReplaceOperationButOnlyRemove() throws JsonProcessingException { | ||
String source = "{ \"ids\": [ \"F1\", \"F3\" ] }"; | ||
String target = "{ \"ids\": [ \"F3\"] }"; | ||
|
||
JsonNode sourceNode = OBJECT_MAPPER.reader().readTree(source); | ||
JsonNode targetNode = OBJECT_MAPPER.reader().readTree(target); | ||
|
||
JsonNode diff = JsonDiff.asJson(sourceNode, targetNode, EnumSet.of( | ||
DiffFlags.ADD_EXPLICIT_REMOVE_ADD_ON_REPLACE | ||
)); | ||
assertEquals(1, diff.size()); | ||
assertEquals(Operation.REMOVE.rfcName(), diff.get(0).get("op").textValue()); | ||
assertEquals("/ids/0", diff.get(0).get("path").textValue()); | ||
assertEquals("F1", diff.get(0).get("value").textValue()); | ||
} | ||
|
||
@Test | ||
public void testJsonDiffDoesNotSplitsWhenThereIsNoReplaceOperationButOnlyAdd() throws JsonProcessingException { | ||
String source = "{ \"ids\": [ \"F1\" ] }"; | ||
String target = "{ \"ids\": [ \"F1\", \"F6\"] }"; | ||
|
||
JsonNode sourceNode = OBJECT_MAPPER.reader().readTree(source); | ||
JsonNode targetNode = OBJECT_MAPPER.reader().readTree(target); | ||
|
||
JsonNode diff = JsonDiff.asJson(sourceNode, targetNode, EnumSet.of( | ||
DiffFlags.ADD_EXPLICIT_REMOVE_ADD_ON_REPLACE | ||
)); | ||
assertEquals(1, diff.size()); | ||
assertEquals(Operation.ADD.rfcName(), diff.get(0).get("op").textValue()); | ||
assertEquals("/ids/1", diff.get(0).get("path").textValue()); | ||
assertEquals("F6", diff.get(0).get("value").textValue()); | ||
} | ||
} |