|
| 1 | +package com.baeldung.jacksonjr; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.time.LocalDate; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.*; |
| 9 | + |
| 10 | +public class JacksonJrFeaturesUnitTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + public void whenSerializingObject_thenReturnJson() throws IOException { |
| 14 | + String json = JacksonJrFeatures.jsonObject(); |
| 15 | + assertTrue(json.contains("name")); |
| 16 | + assertTrue(json.contains("age")); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + public void whenSerializingComposer_thenReturnJson() throws IOException { |
| 21 | + String json = JacksonJrFeatures.jsonComposer(); |
| 22 | + assertTrue(json.contains("objectArray")); |
| 23 | + assertTrue(json.contains("object")); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void whenSerializingSimpleObject_thenAnnotationIsNotConsidered() throws IOException { |
| 28 | + Person person = new Person("John Doe", 30, null); |
| 29 | + String json = JacksonJrFeatures.objectSerialization(person); |
| 30 | + assertTrue(json.contains("name")); |
| 31 | + assertFalse(json.contains("person_name")); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void whenDeserializingJsonObject_thenObjectsAreEqual() throws IOException { |
| 36 | + Person person = new Person("John Doe", 30, null); |
| 37 | + String json = JacksonJrFeatures.objectSerialization(person); |
| 38 | + Person deserializedPerson = JacksonJrFeatures.objectDeserialization(json); |
| 39 | + assertEquals(person, deserializedPerson); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void whenSerializingWithAnnotations_thenAnnotationIsConsidered() throws IOException { |
| 44 | + Person person = new Person("John Doe", 30, null); |
| 45 | + String json = JacksonJrFeatures.objectAnnotationSerialization(person); |
| 46 | + assertTrue(json.contains("person_name")); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void whenSerializingCustomObject_thenLocalDateIsSerializedAsString() throws IOException { |
| 51 | + Person person = new Person("John Doe", 30, LocalDate.now()); |
| 52 | + String json = JacksonJrFeatures.customObjectSerialization(person); |
| 53 | + System.out.println(json); |
| 54 | + assertTrue(json.contains("birthDate")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void whenDeserializingCustomObject_thenLocalDateIsDeserialized() throws IOException { |
| 59 | + Person person = new Person("John Doe", 30, LocalDate.now()); |
| 60 | + String json = JacksonJrFeatures.customObjectSerialization(person); |
| 61 | + Person deserializedPerson = JacksonJrFeatures.customObjectDeserialization(json); |
| 62 | + assertEquals(person, deserializedPerson); |
| 63 | + } |
| 64 | +} |
0 commit comments