11package com .baeldung .jackson .annotation .extra ;
22
3- import static org .junit .Assert .*;
43import static org .hamcrest .CoreMatchers .containsString ;
54import static org .hamcrest .CoreMatchers .instanceOf ;
5+ import static org .hamcrest .CoreMatchers .not ;
6+ import static org .junit .Assert .assertEquals ;
7+ import static org .junit .Assert .assertThat ;
8+
9+ import org .junit .Test ;
610
711import java .io .IOException ;
812import java .util .ArrayList ;
913import java .util .List ;
1014
11- import org .junit .Test ;
12-
15+ import com .baeldung .jackson .annotation .extra .AppendBeans .BeanWithAppend ;
16+ import com .baeldung .jackson .annotation .extra .AppendBeans .BeanWithoutAppend ;
17+ import com .baeldung .jackson .annotation .extra .IdentityReferenceBeans .BeanWithIdentityReference ;
18+ import com .baeldung .jackson .annotation .extra .IdentityReferenceBeans .BeanWithoutIdentityReference ;
1319import com .fasterxml .jackson .core .JsonProcessingException ;
1420import com .fasterxml .jackson .databind .ObjectMapper ;
1521import com .fasterxml .jackson .databind .ObjectMapper .DefaultTyping ;
22+ import com .fasterxml .jackson .databind .ObjectWriter ;
1623import com .fasterxml .jackson .module .jsonSchema .JsonSchema ;
1724import com .fasterxml .jackson .module .jsonSchema .factories .SchemaFactoryWrapper ;
1825
1926public class ExtraAnnotationTest {
27+ @ Test
28+ public void whenNotUsingJsonIdentityReferenceAnnotation_thenCorrect () throws JsonProcessingException {
29+ ObjectMapper mapper = new ObjectMapper ();
30+ BeanWithoutIdentityReference bean = new BeanWithoutIdentityReference (1 , "Bean Without Identity Reference Annotation" );
31+ String jsonString = mapper .writeValueAsString (bean );
32+
33+ assertThat (jsonString , containsString ("Bean Without Identity Reference Annotation" ));
34+ }
35+
2036 @ Test
2137 public void whenUsingJsonIdentityReferenceAnnotation_thenCorrect () throws JsonProcessingException {
2238 ObjectMapper mapper = new ObjectMapper ();
23- IdentityReferenceBean bean = new IdentityReferenceBean (1 , "Identity Reference Bean " );
39+ BeanWithIdentityReference bean = new BeanWithIdentityReference (1 , "Bean With Identity Reference Annotation " );
2440 String jsonString = mapper .writeValueAsString (bean );
2541
2642 assertEquals ("1" , jsonString );
2743 }
2844
45+ @ Test
46+ public void whenNotUsingJsonAppendAnnotation_thenCorrect () throws JsonProcessingException {
47+ ObjectMapper mapper = new ObjectMapper ();
48+
49+ BeanWithoutAppend bean = new BeanWithoutAppend (2 , "Bean Without Append Annotation" );
50+ ObjectWriter writer = mapper .writerFor (BeanWithoutAppend .class ).withAttribute ("version" , "1.0" );
51+ String jsonString = writer .writeValueAsString (bean );
52+
53+ assertThat (jsonString , not (containsString ("version" )));
54+ assertThat (jsonString , not (containsString ("1.0" )));
55+ }
56+
2957 @ Test
3058 public void whenUsingJsonAppendAnnotation_thenCorrect () throws JsonProcessingException {
3159 ObjectMapper mapper = new ObjectMapper ();
32- AppendBean bean = new AppendBean (2 , "Append Bean" );
33- String jsonString = mapper .writeValueAsString (bean );
3460
35- assertThat (jsonString , containsString ("appendedProperty" ));
61+ BeanWithAppend bean = new BeanWithAppend (2 , "Bean With Append Annotation" );
62+ ObjectWriter writer = mapper .writerFor (BeanWithAppend .class ).withAttribute ("version" , "1.0" );
63+ String jsonString = writer .writeValueAsString (bean );
64+
65+ assertThat (jsonString , containsString ("version" ));
66+ assertThat (jsonString , containsString ("1.0" ));
3667 }
3768
3869 @ Test
3970 public void whenUsingJsonNamingAnnotation_thenCorrect () throws JsonProcessingException {
4071 ObjectMapper mapper = new ObjectMapper ();
4172 NamingBean bean = new NamingBean (3 , "Naming Bean" );
4273 String jsonString = mapper .writeValueAsString (bean );
43-
74+
4475 assertThat (jsonString , containsString ("bean_name" ));
4576 }
4677
@@ -51,7 +82,7 @@ public void whenUsingJsonPropertyDescriptionAnnotation_thenCorrect() throws Json
5182 mapper .acceptJsonFormatVisitor (PropertyDescriptionBean .class , wrapper );
5283 JsonSchema jsonSchema = wrapper .finalSchema ();
5384 String jsonString = mapper .writeValueAsString (jsonSchema );
54-
85+ System . out . println ( jsonString );
5586 assertThat (jsonString , containsString ("This is a description of the name property" ));
5687 }
5788
@@ -86,7 +117,7 @@ public void whenUsingJsonTypeIdResolverAnnotation_thenCorrect() throws IOExcepti
86117
87118 TypeIdResolverStructure .BeanContainer serializedContainer = new TypeIdResolverStructure .BeanContainer ();
88119 serializedContainer .setBeans (beans );
89-
120+
90121 ObjectMapper mapper = new ObjectMapper ();
91122 String jsonString = mapper .writeValueAsString (serializedContainer );
92123 assertThat (jsonString , containsString ("bean1" ));
0 commit comments