1+ package org .baeldung .jackson .test ;
2+
3+ import static org .hamcrest .Matchers .containsString ;
4+ import static org .hamcrest .Matchers .not ;
5+ import static org .junit .Assert .assertEquals ;
6+ import static org .junit .Assert .assertThat ;
7+
8+ import java .io .IOException ;
9+
10+ import org .baeldung .jackson .jsonview .Item ;
11+ import org .baeldung .jackson .jsonview .MyBeanSerializerModifier ;
12+ import org .baeldung .jackson .jsonview .User ;
13+ import org .baeldung .jackson .jsonview .Views ;
14+ import org .junit .Test ;
15+
16+ import com .fasterxml .jackson .core .JsonProcessingException ;
17+ import com .fasterxml .jackson .databind .ObjectMapper ;
18+ import com .fasterxml .jackson .databind .ser .BeanSerializerFactory ;
19+ import com .fasterxml .jackson .databind .ser .SerializerFactory ;
20+
21+ public class JacksonJsonViewTest {
22+
23+ @ Test
24+ public void whenUseJsonViewToSerialize_thenCorrect () throws JsonProcessingException {
25+ final User user = new User (1 , "John" );
26+
27+ final ObjectMapper mapper = new ObjectMapper ();
28+ final String result = mapper .writerWithView (Views .Public .class ).writeValueAsString (user );
29+
30+ assertThat (result , containsString ("John" ));
31+ assertThat (result , containsString ("1" ));
32+ }
33+
34+ @ Test
35+ public void whenUsePublicView_thenOnlyPublicSerialized () throws JsonProcessingException {
36+ final User owner = new User (1 , "John" );
37+ final Item item = new Item (2 , "book" , owner );
38+
39+ final ObjectMapper mapper = new ObjectMapper ();
40+ final String result = mapper .writerWithView (Views .Public .class ).writeValueAsString (item );
41+
42+ assertThat (result , containsString ("book" ));
43+ assertThat (result , containsString ("2" ));
44+
45+ assertThat (result , not (containsString ("John" )));
46+ assertThat (result , not (containsString ("1" )));
47+ }
48+
49+ @ Test
50+ public void whenUseInternalView_thenAllSerialized () throws JsonProcessingException {
51+ final User owner = new User (1 , "John" );
52+ final Item item = new Item (2 , "book" , owner );
53+
54+ final ObjectMapper mapper = new ObjectMapper ();
55+ final String result = mapper .writerWithView (Views .Internal .class ).writeValueAsString (item );
56+
57+ assertThat (result , containsString ("book" ));
58+ assertThat (result , containsString ("2" ));
59+
60+ assertThat (result , containsString ("John" ));
61+ assertThat (result , containsString ("1" ));
62+ }
63+
64+ @ Test
65+ public void whenUseJsonViewToDeserialize_thenCorrect () throws IOException {
66+ final String json = "{\" id\" :1,\" name\" :\" John\" }" ;
67+
68+ final ObjectMapper mapper = new ObjectMapper ();
69+
70+ final User user = mapper .readerWithView (Views .Public .class ).withType (User .class ).readValue (json );
71+ assertEquals (1 , user .getId ());
72+ assertEquals ("John" , user .getName ());
73+ }
74+
75+ @ Test
76+ public void whenUseCustomJsonViewToSerialize_thenCorrect () throws JsonProcessingException {
77+ final User user = new User (1 , "John" );
78+ final SerializerFactory serializerFactory = BeanSerializerFactory .instance .withSerializerModifier (new MyBeanSerializerModifier ());
79+
80+ final ObjectMapper mapper = new ObjectMapper ();
81+ mapper .setSerializerFactory (serializerFactory );
82+
83+ final String result = mapper .writerWithView (Views .Public .class ).writeValueAsString (user );
84+ assertThat (result , containsString ("JOHN" ));
85+ assertThat (result , containsString ("1" ));
86+ }
87+ }
0 commit comments