Skip to content

Commit

Permalink
test: add test case for v03
Browse files Browse the repository at this point in the history
Signed-off-by: Ning Sun <[email protected]>
  • Loading branch information
sunng87 committed Nov 13, 2021
1 parent c659e70 commit 3af281b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;

import io.cloudevents.avro.AvroCloudEventData;
import io.cloudevents.CloudEventData;
Expand All @@ -37,7 +38,7 @@ public class AvroCloudEventDataWrapper implements CloudEventData {
*/
public AvroCloudEventDataWrapper(Map<String, Object> data) {
avroCloudEventData = new AvroCloudEventData();
avroCloudEventData.setValue(data);
avroCloudEventData.setValue(Objects.requireNonNull(data));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.cloudevents.avro;

import java.util.Map;
import java.util.HashMap;
import java.net.URI;

import io.cloudevents.CloudEvent;
Expand All @@ -29,7 +30,12 @@

public class AvroFormatTest {

Map<String, Object> testData = Map.of("name", "Ning", "age", 22.0);
public static Map<String, Object> testData = new HashMap<>();

static {
testData.put("name", "Ning");
testData.put("age", 22.0);
}

@Test
public void testSerde() {
Expand Down Expand Up @@ -60,4 +66,33 @@ public void testSerde() {
assertThat(cloudEvent2.getType()).isEqualTo("testdata");
}

@Test
public void testV03Serde() {
EventFormat avroFormat = new AvroFormat();
CloudEventData cloudEventData = new AvroCloudEventDataWrapper(testData);

assertThat(cloudEventData).isNotNull();
assertThat(cloudEventData.toBytes()).isNotNull();

CloudEvent cloudEvent = CloudEventBuilder.v03()
.withId("1")
.withType("testdata")
.withSource(URI.create("http://localhost/test"))
.withData("application/avro", cloudEventData)
.build();
assertThat(cloudEvent).isNotNull();
assertThat(cloudEvent.getSpecVersion()).isEqualTo(SpecVersion.V03);

byte[] bytes = avroFormat.serialize(cloudEvent);

assertThat(bytes).isNotNull();
assertThat(bytes).hasSizeGreaterThan(0);

CloudEvent cloudEvent2 = avroFormat.deserialize(bytes);

assertThat(cloudEvent2).isNotNull();
assertThat(cloudEvent2.getId()).isEqualTo("1");
assertThat(cloudEvent2.getType()).isEqualTo("testdata");
}

}

0 comments on commit 3af281b

Please sign in to comment.