Skip to content

Commit 3af281b

Browse files
committed
test: add test case for v03
Signed-off-by: Ning Sun <sunng@protonmail.com>
1 parent c659e70 commit 3af281b

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

formats/avro/src/main/java/io/cloudevents/avro/AvroCloudEventDataWrapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.io.IOException;
2121
import java.util.Map;
22+
import java.util.Objects;
2223

2324
import io.cloudevents.avro.AvroCloudEventData;
2425
import io.cloudevents.CloudEventData;
@@ -37,7 +38,7 @@ public class AvroCloudEventDataWrapper implements CloudEventData {
3738
*/
3839
public AvroCloudEventDataWrapper(Map<String, Object> data) {
3940
avroCloudEventData = new AvroCloudEventData();
40-
avroCloudEventData.setValue(data);
41+
avroCloudEventData.setValue(Objects.requireNonNull(data));
4142
}
4243

4344
@Override

formats/avro/src/test/java/io/cloudevents/avro/AvroFormatTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.cloudevents.avro;
1818

1919
import java.util.Map;
20+
import java.util.HashMap;
2021
import java.net.URI;
2122

2223
import io.cloudevents.CloudEvent;
@@ -29,7 +30,12 @@
2930

3031
public class AvroFormatTest {
3132

32-
Map<String, Object> testData = Map.of("name", "Ning", "age", 22.0);
33+
public static Map<String, Object> testData = new HashMap<>();
34+
35+
static {
36+
testData.put("name", "Ning");
37+
testData.put("age", 22.0);
38+
}
3339

3440
@Test
3541
public void testSerde() {
@@ -60,4 +66,33 @@ public void testSerde() {
6066
assertThat(cloudEvent2.getType()).isEqualTo("testdata");
6167
}
6268

69+
@Test
70+
public void testV03Serde() {
71+
EventFormat avroFormat = new AvroFormat();
72+
CloudEventData cloudEventData = new AvroCloudEventDataWrapper(testData);
73+
74+
assertThat(cloudEventData).isNotNull();
75+
assertThat(cloudEventData.toBytes()).isNotNull();
76+
77+
CloudEvent cloudEvent = CloudEventBuilder.v03()
78+
.withId("1")
79+
.withType("testdata")
80+
.withSource(URI.create("http://localhost/test"))
81+
.withData("application/avro", cloudEventData)
82+
.build();
83+
assertThat(cloudEvent).isNotNull();
84+
assertThat(cloudEvent.getSpecVersion()).isEqualTo(SpecVersion.V03);
85+
86+
byte[] bytes = avroFormat.serialize(cloudEvent);
87+
88+
assertThat(bytes).isNotNull();
89+
assertThat(bytes).hasSizeGreaterThan(0);
90+
91+
CloudEvent cloudEvent2 = avroFormat.deserialize(bytes);
92+
93+
assertThat(cloudEvent2).isNotNull();
94+
assertThat(cloudEvent2.getId()).isEqualTo("1");
95+
assertThat(cloudEvent2.getType()).isEqualTo("testdata");
96+
}
97+
6398
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy