|
1 | 1 | package com.fasterxml.jackson.datatype.jsr353;
|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 4 | +import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
4 | 5 |
|
5 | 6 | import javax.json.*;
|
6 | 7 |
|
7 | 8 | import java.util.Objects;
|
8 | 9 |
|
| 10 | +import static org.hamcrest.CoreMatchers.containsString; |
9 | 11 | import static org.hamcrest.CoreMatchers.instanceOf;
|
10 | 12 | import static org.hamcrest.CoreMatchers.is;
|
11 | 13 | import static org.hamcrest.MatcherAssert.assertThat;
|
| 14 | +import static org.junit.Assert.assertThrows; |
12 | 15 |
|
13 | 16 | public class JsonPatchDeserializationTest extends TestBase {
|
14 | 17 |
|
| 18 | + private static final String EXPECTED_MESSAGE = "JSON patch has to be an array of objects"; |
| 19 | + |
15 | 20 | private static final ObjectMapper MAPPER = newMapper();
|
16 | 21 |
|
17 |
| - public void testDeserializationAndPatching() throws Exception { |
| 22 | + public void testArrayOfObjectsDeserializationAndPatching() throws Exception { |
18 | 23 | final String json = "[" +
|
19 | 24 | "{" +
|
20 | 25 | "\"op\":\"replace\"," +
|
@@ -49,6 +54,41 @@ public void testDeserializationAndPatching() throws Exception {
|
49 | 54 | assertThat(patchedPerson, is(new Person("Json", "Smith")));
|
50 | 55 | }
|
51 | 56 |
|
| 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 | + |
52 | 92 | static class Person {
|
53 | 93 | private String name;
|
54 | 94 | private String lastName;
|
|
0 commit comments