2525import io .cloudevents .CloudEventData ;
2626import io .cloudevents .core .builder .CloudEventBuilder ;
2727import io .cloudevents .core .data .BytesCloudEventData ;
28+ import io .cloudevents .jackson .JsonCloudEventData ;
2829import java .net .URI ;
2930import java .nio .charset .StandardCharsets ;
3031import org .junit .jupiter .api .Test ;
3132
3233public class JacksonCloudEventUtilsTest {
3334
3435 private CloudEvent createSampleEvent () {
36+ return createEventBuilder ()
37+ .withData ("{\" status\" :\" NEEDS_REVISION\" }" .getBytes (StandardCharsets .UTF_8 ))
38+ .build ();
39+ }
40+
41+ private CloudEventBuilder createEventBuilder () {
3542 return CloudEventBuilder .v1 ()
3643 .withId ("5dc4698e-5f98-470e-bb76-04218fe2dd0f" )
3744 .withSource (URI .create ("api:/newsletter" ))
3845 .withType ("org.acme.newsletter.review.done" )
3946 .withDataContentType ("application/json" )
40- .withExtension ("flowinstanceid" , "01KMRBFA19GZYW3XY895Z4SNCK" )
41- .withData ("{\" status\" :\" NEEDS_REVISION\" }" .getBytes (StandardCharsets .UTF_8 ))
42- .build ();
47+ .withExtension ("flowinstanceid" , "01KMRBFA19GZYW3XY895Z4SNCK" );
48+ }
49+
50+ @ Test
51+ public void testCloudEventSerializationNullData () {
52+ CloudEvent event = createEventBuilder ().build ();
53+
54+ JsonNode node = JacksonCloudEventUtils .toJsonNode (event );
55+
56+ assertNotNull (node );
57+ assertTrue (node .has ("specversion" ), "Missing mandatory specversion attribute" );
58+ assertEquals ("1.0" , node .get ("specversion" ).asText ());
59+
60+ assertFalse (node .has ("specVersion" ), "Jackson POJO serializer mangled the envelope!" );
61+
62+ assertEquals ("5dc4698e-5f98-470e-bb76-04218fe2dd0f" , node .get ("id" ).asText ());
63+ assertEquals ("01KMRBFA19GZYW3XY895Z4SNCK" , node .get ("flowinstanceid" ).asText ());
64+
65+ assertFalse (node .has ("data" ));
4366 }
4467
4568 @ Test
@@ -61,6 +84,30 @@ public void testCloudEventSerialization() {
6184 assertEquals ("NEEDS_REVISION" , node .get ("data" ).get ("status" ).asText ());
6285 }
6386
87+ @ Test
88+ public void testCloudEventSerializationJson () {
89+ CloudEvent event =
90+ createEventBuilder ()
91+ .withData (
92+ JsonCloudEventData .wrap (
93+ JsonUtils .mapper ().createObjectNode ().put ("status" , "NEEDS_REVISION" )))
94+ .build ();
95+
96+ JsonNode node = JacksonCloudEventUtils .toJsonNode (event );
97+
98+ assertNotNull (node );
99+ assertTrue (node .has ("specversion" ), "Missing mandatory specversion attribute" );
100+ assertEquals ("1.0" , node .get ("specversion" ).asText ());
101+
102+ assertFalse (node .has ("specVersion" ), "Jackson POJO serializer mangled the envelope!" );
103+
104+ assertEquals ("5dc4698e-5f98-470e-bb76-04218fe2dd0f" , node .get ("id" ).asText ());
105+ assertEquals ("01KMRBFA19GZYW3XY895Z4SNCK" , node .get ("flowinstanceid" ).asText ());
106+
107+ assertTrue (node .has ("data" ));
108+ assertEquals ("NEEDS_REVISION" , node .get ("data" ).get ("status" ).asText ());
109+ }
110+
64111 @ Test
65112 public void testCloudEventDeserialization () {
66113 CloudEvent originalEvent = createSampleEvent ();
0 commit comments