Skip to content

Commit 4339c49

Browse files
committed
Added JSON-B tests.
1 parent 8aafc4e commit 4339c49

File tree

3 files changed

+220
-5
lines changed

3 files changed

+220
-5
lines changed

src/main/java/com/packtpub/javaee8/JsonbResource.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
import javax.json.bind.annotation.*;
99
import javax.json.bind.config.PropertyNamingStrategy;
1010
import javax.json.bind.config.PropertyOrderStrategy;
11-
import javax.ws.rs.GET;
12-
import javax.ws.rs.POST;
13-
import javax.ws.rs.Path;
14-
import javax.ws.rs.Produces;
11+
import javax.ws.rs.*;
1512
import javax.ws.rs.core.MediaType;
1613
import java.math.BigDecimal;
1714
import java.time.LocalDate;
@@ -32,7 +29,8 @@ public class JsonbResource {
3229
private CustomJsonbPojo customJsonbPojo;
3330

3431
@PostConstruct
35-
void initialize() {
32+
@HEAD
33+
public void initialize() {
3634
jsonbPojo = new JsonbPojo("Hello World.", 42, LocalDate.now());
3735

3836
customJsonbPojo = new CustomJsonbPojo();
@@ -75,6 +73,7 @@ public void unmarshallCustom(String jsonBody) {
7573
LOGGER.log(Level.INFO, "Custom Unmarshalled {0}", customJsonbPojo);
7674
}
7775

76+
@JsonbNillable
7877
@JsonbPropertyOrder(value = {"message", "answerToEverything", "today"})
7978
public static class JsonbPojo {
8079

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.packtpub.javaee8;
2+
3+
import org.glassfish.jersey.jsonb.JsonBindingFeature;
4+
import org.junit.After;
5+
import org.junit.Before;
6+
import org.junit.FixMethodOrder;
7+
import org.junit.Test;
8+
import org.junit.runners.MethodSorters;
9+
10+
import javax.ws.rs.client.Client;
11+
import javax.ws.rs.client.ClientBuilder;
12+
import javax.ws.rs.client.Entity;
13+
import javax.ws.rs.client.WebTarget;
14+
import javax.ws.rs.core.MediaType;
15+
import javax.ws.rs.core.Response;
16+
import java.time.LocalDate;
17+
import java.util.concurrent.TimeUnit;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
22+
public class JsonbResourceIntegrationTest {
23+
24+
private Client client;
25+
private WebTarget webTarget;
26+
27+
@Before
28+
public void setUp() throws Exception {
29+
client = ClientBuilder.newBuilder()
30+
.connectTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS)
31+
.register(JsonBindingFeature.class)
32+
.build();
33+
34+
webTarget = client.target("http://localhost:8080").path("/content-service/api").path("/json-b");
35+
}
36+
37+
@After
38+
public void tearDown() throws Exception {
39+
client.close();
40+
}
41+
42+
@Test
43+
public void _1_GetJsonb() {
44+
JsonbResource.JsonbPojo pojo = webTarget.request().accept(MediaType.APPLICATION_JSON).get(JsonbResource.JsonbPojo.class);
45+
assertThat(pojo).isNotNull();
46+
assertThat(pojo.getMessage()).isEqualTo("Hello World.");
47+
assertThat(pojo.getAnswerToEverything()).isEqualTo(42);
48+
assertThat(pojo.getToday()).isEqualTo(LocalDate.now());
49+
}
50+
51+
@Test
52+
public void _2_PostUpdatedJsonb() {
53+
JsonbResource.JsonbPojo pojo = new JsonbResource.JsonbPojo("Updated JSON-B.", null, null);
54+
Response response = webTarget.request().post(Entity.json(pojo));
55+
assertThat(response.getStatus()).isEqualTo(204);
56+
}
57+
58+
@Test
59+
public void _3_GetUpdatedJsonb() {
60+
JsonbResource.JsonbPojo pojo = webTarget.request().accept(MediaType.APPLICATION_JSON).get(JsonbResource.JsonbPojo.class);
61+
assertThat(pojo).isNotNull();
62+
assertThat(pojo.getMessage()).isEqualTo("Updated JSON-B.");
63+
assertThat(pojo.getAnswerToEverything()).isNull();
64+
assertThat(pojo.getToday()).isNull();
65+
}
66+
67+
@Test
68+
public void _99_Reset() {
69+
Response response = webTarget.request().head();
70+
assertThat(response.getStatus()).isEqualTo(204);
71+
}
72+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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

Comments
 (0)