Skip to content

Commit 175085b

Browse files
patveckcowtowncoder
authored andcommitted
Add tests for JsonPatch
1 parent 9851081 commit 175085b

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

jsr-353/src/test/java/com/fasterxml/jackson/datatype/jsr353/JsonPatchDeserializationTest.java

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
package com.fasterxml.jackson.datatype.jsr353;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
45

56
import javax.json.*;
67

78
import java.util.Objects;
89

10+
import static org.hamcrest.CoreMatchers.containsString;
911
import static org.hamcrest.CoreMatchers.instanceOf;
1012
import static org.hamcrest.CoreMatchers.is;
1113
import static org.hamcrest.MatcherAssert.assertThat;
14+
import static org.junit.Assert.assertThrows;
1215

1316
public class JsonPatchDeserializationTest extends TestBase {
1417

18+
private static final String EXPECTED_MESSAGE = "JSON patch has to be an array of objects";
19+
1520
private static final ObjectMapper MAPPER = newMapper();
1621

17-
public void testDeserializationAndPatching() throws Exception {
22+
public void testArrayOfObjectsDeserializationAndPatching() throws Exception {
1823
final String json = "[" +
1924
"{" +
2025
"\"op\":\"replace\"," +
@@ -49,6 +54,41 @@ public void testDeserializationAndPatching() throws Exception {
4954
assertThat(patchedPerson, is(new Person("Json", "Smith")));
5055
}
5156

57+
public void testArrayWithOScalarDeserializationAndPatching() {
58+
final String json = "[" +
59+
"{" +
60+
"\"op\":\"replace\"," +
61+
"\"path\":\"/name\"," +
62+
"\"value\":\"Json\"" +
63+
"}," +
64+
"\"Jackson\"" +
65+
"]";
66+
67+
final InvalidFormatException ex = assertThrows(InvalidFormatException.class,
68+
() -> MAPPER.readValue(json, JsonPatch.class));
69+
assertThat(ex.getMessage(), containsString(EXPECTED_MESSAGE));
70+
}
71+
72+
public void testObjectDeserializationAndPatching() {
73+
final String json = "{" +
74+
"\"op\":\"replace\"," +
75+
"\"path\":\"/name\"," +
76+
"\"value\":\"Json\"" +
77+
"}";
78+
79+
final InvalidFormatException ex = assertThrows(InvalidFormatException.class,
80+
() -> MAPPER.readValue(json, JsonPatch.class));
81+
assertThat(ex.getMessage(), containsString(EXPECTED_MESSAGE));
82+
}
83+
84+
public void testScalarDeserializationAndPatching() {
85+
final String json = "\"op\"";
86+
87+
final InvalidFormatException ex = assertThrows(InvalidFormatException.class,
88+
() -> MAPPER.readValue(json, JsonPatch.class));
89+
assertThat(ex.getMessage(), containsString(EXPECTED_MESSAGE));
90+
}
91+
5292
static class Person {
5393
private String name;
5494
private String lastName;

0 commit comments

Comments
 (0)