|
29 | 29 | * Tests for {@link JsonContentHandler}.
|
30 | 30 | *
|
31 | 31 | * @author Andy Wilkinson
|
32 |
| - * @author Mathias Düsterhöft |
33 | 32 | */
|
34 | 33 | public class JsonContentHandlerTests {
|
35 | 34 |
|
36 | 35 | @Rule
|
37 | 36 | public ExpectedException thrown = ExpectedException.none();
|
38 | 37 |
|
| 38 | + @Test |
| 39 | + public void typeForFieldWithNullValueMustMatch() { |
| 40 | + this.thrown.expect(FieldTypesDoNotMatchException.class); |
| 41 | + new JsonContentHandler("{\"a\": null}".getBytes()) |
| 42 | + .determineFieldType(new FieldDescriptor("a").type(JsonFieldType.STRING)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void typeForFieldWithNotNullAndThenNullValueMustMatch() { |
| 47 | + this.thrown.expect(FieldTypesDoNotMatchException.class); |
| 48 | + new JsonContentHandler("{\"a\":[{\"id\":1},{\"id\":null}]}".getBytes()) |
| 49 | + .determineFieldType( |
| 50 | + new FieldDescriptor("a[].id").type(JsonFieldType.STRING)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void typeForFieldWithNullAndThenNotNullValueMustMatch() { |
| 55 | + this.thrown.expect(FieldTypesDoNotMatchException.class); |
| 56 | + new JsonContentHandler("{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes()) |
| 57 | + .determineFieldType( |
| 58 | + new FieldDescriptor("a.[].id").type(JsonFieldType.STRING)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void typeForOptionalFieldWithNumberAndThenNullValueIsNumber() { |
| 63 | + Object fieldType = new JsonContentHandler( |
| 64 | + "{\"a\":[{\"id\":1},{\"id\":null}]}\"".getBytes()) |
| 65 | + .determineFieldType(new FieldDescriptor("a[].id").optional()); |
| 66 | + assertThat((JsonFieldType) fieldType).isEqualTo(JsonFieldType.NUMBER); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void typeForOptionalFieldWithNullAndThenNumberIsNumber() { |
| 71 | + Object fieldType = new JsonContentHandler( |
| 72 | + "{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes()) |
| 73 | + .determineFieldType(new FieldDescriptor("a[].id").optional()); |
| 74 | + assertThat((JsonFieldType) fieldType).isEqualTo(JsonFieldType.NUMBER); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void typeForFieldWithNumberAndThenNullValueIsVaries() { |
| 79 | + Object fieldType = new JsonContentHandler( |
| 80 | + "{\"a\":[{\"id\":1},{\"id\":null}]}\"".getBytes()) |
| 81 | + .determineFieldType(new FieldDescriptor("a[].id")); |
| 82 | + assertThat((JsonFieldType) fieldType).isEqualTo(JsonFieldType.VARIES); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void typeForFieldWithNullAndThenNumberIsVaries() { |
| 87 | + Object fieldType = new JsonContentHandler( |
| 88 | + "{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes()) |
| 89 | + .determineFieldType(new FieldDescriptor("a[].id")); |
| 90 | + assertThat((JsonFieldType) fieldType).isEqualTo(JsonFieldType.VARIES); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void typeForOptionalFieldWithNullValueCanBeProvidedExplicitly() { |
| 95 | + Object fieldType = new JsonContentHandler("{\"a\": null}".getBytes()) |
| 96 | + .determineFieldType( |
| 97 | + new FieldDescriptor("a").type(JsonFieldType.STRING).optional()); |
| 98 | + assertThat((JsonFieldType) fieldType).isEqualTo(JsonFieldType.STRING); |
| 99 | + } |
| 100 | + |
39 | 101 | @Test
|
40 | 102 | public void failsFastWithNonJsonContent() {
|
41 | 103 | this.thrown.expect(PayloadHandlingException.class);
|
|
0 commit comments