|
| 1 | +package com.packtpub.javaee8; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import javax.json.bind.Jsonb; |
| 7 | +import javax.json.bind.JsonbBuilder; |
| 8 | +import javax.json.bind.JsonbConfig; |
| 9 | +import javax.json.bind.annotation.*; |
| 10 | +import javax.json.bind.config.BinaryDataStrategy; |
| 11 | +import javax.json.bind.config.PropertyNamingStrategy; |
| 12 | +import javax.json.bind.config.PropertyOrderStrategy; |
| 13 | +import java.math.BigDecimal; |
| 14 | +import java.time.LocalDate; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.Locale; |
| 17 | +import java.util.Objects; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +public class JsonbTest { |
| 22 | + |
| 23 | + private static final String PLAIN_POJO_JSON = |
| 24 | + "{\"a-double\":23.5,\"a-integer\":47,\"a-string\":\"Hello JSON-B\",\"array-of-ints\":[1,2,3]}"; |
| 25 | + |
| 26 | + private static final String ANNOTATED_POJO_JSON = |
| 27 | + "{\"greeting\":\"Hello JSON-B.\",\"answer-to-everything\":\"42,00\",\"today\":\"02/15/2018\"}"; |
| 28 | + |
| 29 | + private Jsonb jsonb; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setUp() throws Exception { |
| 33 | + JsonbConfig jsonbConfig = new JsonbConfig() |
| 34 | + .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL) |
| 35 | + .withNullValues(true) |
| 36 | + .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES) |
| 37 | + .withFormatting(false) |
| 38 | + .withDateFormat("dd.MM.yyyy", Locale.GERMANY) |
| 39 | + .withBinaryDataStrategy(BinaryDataStrategy.BASE_64) |
| 40 | + .withLocale(Locale.GERMANY); |
| 41 | + |
| 42 | + jsonb = JsonbBuilder.create(jsonbConfig); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testToJsonWithPlainPojo() { |
| 47 | + PlainPojo pojo = PlainPojo.create(); |
| 48 | + String json = jsonb.toJson(pojo); |
| 49 | + assertThat(json).isEqualTo(PLAIN_POJO_JSON); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testPlainPojoFromJson() { |
| 54 | + PlainPojo pojo = jsonb.fromJson(PLAIN_POJO_JSON, PlainPojo.class); |
| 55 | + assertThat(pojo).isEqualTo(PlainPojo.create()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testToJsonWithAnnotatedPojo() { |
| 60 | + AnnotatedPojo pojo = AnnotatedPojo.create(); |
| 61 | + String json = jsonb.toJson(pojo); |
| 62 | + assertThat(json).isEqualTo(ANNOTATED_POJO_JSON); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testAnnotatedPojoFromJson() { |
| 67 | + AnnotatedPojo pojo = jsonb.fromJson(ANNOTATED_POJO_JSON, AnnotatedPojo.class); |
| 68 | + assertThat(pojo).isEqualTo(AnnotatedPojo.create()); |
| 69 | + } |
| 70 | + |
| 71 | + @JsonbPropertyOrder(value = {"message", "answerToEverything", "today"}) |
| 72 | + public static class AnnotatedPojo { |
| 73 | + @JsonbProperty(value = "greeting", nillable = true) |
| 74 | + public String message; |
| 75 | + |
| 76 | + @JsonbNumberFormat("#,##0.00") |
| 77 | + public Integer answerToEverything; |
| 78 | + |
| 79 | + @JsonbDateFormat("MM/dd/yyyy") |
| 80 | + public LocalDate today; |
| 81 | + |
| 82 | + @JsonbTransient |
| 83 | + public BigDecimal invisible = BigDecimal.TEN; |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean equals(Object o) { |
| 87 | + if (this == o) return true; |
| 88 | + if (o == null || getClass() != o.getClass()) return false; |
| 89 | + AnnotatedPojo that = (AnnotatedPojo) o; |
| 90 | + return Objects.equals(message, that.message) && |
| 91 | + Objects.equals(answerToEverything, that.answerToEverything) && |
| 92 | + Objects.equals(today, that.today); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int hashCode() { |
| 97 | + return Objects.hash(message, answerToEverything, today); |
| 98 | + } |
| 99 | + |
| 100 | + public static AnnotatedPojo create() { |
| 101 | + AnnotatedPojo pojo = new AnnotatedPojo(); |
| 102 | + pojo.message = "Hello JSON-B."; |
| 103 | + pojo.answerToEverything = 42; |
| 104 | + pojo.today = LocalDate.of(2018, 2, 15); |
| 105 | + return pojo; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public static class PlainPojo { |
| 110 | + // these need to be public, |
| 111 | + // or public getter / setter are required |
| 112 | + public String aString; |
| 113 | + public Integer aInteger; |
| 114 | + public int[] arrayOfInts; |
| 115 | + public Double aDouble; |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean equals(Object o) { |
| 119 | + if (this == o) return true; |
| 120 | + if (o == null || getClass() != o.getClass()) return false; |
| 121 | + PlainPojo plainPojo = (PlainPojo) o; |
| 122 | + return Objects.equals(aString, plainPojo.aString) && |
| 123 | + Objects.equals(aInteger, plainPojo.aInteger) && |
| 124 | + Arrays.equals(arrayOfInts, plainPojo.arrayOfInts) && |
| 125 | + Objects.equals(aDouble, plainPojo.aDouble); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public int hashCode() { |
| 130 | + int result = Objects.hash(aString, aInteger, aDouble); |
| 131 | + result = 31 * result + Arrays.hashCode(arrayOfInts); |
| 132 | + return result; |
| 133 | + } |
| 134 | + |
| 135 | + public static PlainPojo create() { |
| 136 | + PlainPojo plainPojo = new PlainPojo(); |
| 137 | + plainPojo.aString = "Hello JSON-B"; |
| 138 | + plainPojo.aInteger = 47; |
| 139 | + plainPojo.arrayOfInts = new int[]{1, 2, 3}; |
| 140 | + plainPojo.aDouble = 23.5; |
| 141 | + return plainPojo; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments