Skip to content

Commit fca0a76

Browse files
committed
add test and fix serde issues
Signed-off-by: Ning Sun <sunng@protonmail.com>
1 parent bb42767 commit fca0a76

12 files changed

+102
-138
lines changed

formats/avro/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
</goals>
5151
<configuration>
5252
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
53-
5453
<outputDirectory>${project.build.directory}/generated-sources/java/</outputDirectory>
54+
<stringType>String</stringType>
5555
</configuration>
5656
</execution>
5757
</executions>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class AvroCloudEventDataWrapper implements CloudEventData {
3535
/**
3636
* Wraps a JSON object-like data structure.
3737
*/
38-
public AvroCloudEventDataWrapper(Map<CharSequence, Object> data) {
38+
public AvroCloudEventDataWrapper(Map<String, Object> data) {
3939
avroCloudEventData = new AvroCloudEventData();
4040
avroCloudEventData.setValue(data);
4141
}

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

+22-17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Map;
2121
import java.net.URI;
22+
import java.nio.ByteBuffer;
2223
import java.time.OffsetDateTime;
2324

2425
import io.cloudevents.CloudEventData;
@@ -43,32 +44,36 @@ public AvroDeserializer(AvroCloudEvent avroCloudEvent) {
4344
@Override
4445
public <W extends CloudEventWriter<R>, R> R read(CloudEventWriterFactory<W, R> writerFactory,
4546
CloudEventDataMapper<? extends CloudEventData> mapper) throws CloudEventRWException {
46-
47-
Map<CharSequence, Object> avroCloudEventAttrs = this.avroCloudEvent.getAttribute();
47+
Map<String, Object> avroCloudEventAttrs = this.avroCloudEvent.getAttribute();
4848
SpecVersion specVersion = SpecVersion.parse((String)avroCloudEventAttrs.get(CloudEventV1.SPECVERSION));
4949
final CloudEventWriter<R> writer = writerFactory.create(specVersion);
5050

51-
for (Map.Entry<CharSequence, Object> entry: avroCloudEventAttrs.entrySet()) {
51+
for (Map.Entry<String, Object> entry: avroCloudEventAttrs.entrySet()) {
5252
String key = entry.getKey().toString();
5353

54-
if (key.equals(CloudEventV1.TIME)) {
55-
// OffsetDateTime
56-
OffsetDateTime value = OffsetDateTime.parse((String) entry.getValue());
57-
writer.withContextAttribute(key, value);
58-
59-
} else if (key.equals(CloudEventV1.DATASCHEMA)) {
60-
// URI
61-
URI value = URI.create((String) entry.getValue());
62-
writer.withContextAttribute(key, value);
63-
} else {
64-
// String
65-
writer.withContextAttribute(key, (String) entry.getValue());
54+
switch(key) {
55+
case CloudEventV1.SPECVERSION:
56+
continue;
57+
case CloudEventV1.TIME: {
58+
// OffsetDateTime
59+
OffsetDateTime value = OffsetDateTime.parse((String) entry.getValue());
60+
writer.withContextAttribute(key, value);
61+
};
62+
case CloudEventV1.DATASCHEMA: {
63+
// URI
64+
URI value = URI.create((String) entry.getValue());
65+
writer.withContextAttribute(key, value);
66+
};
67+
default:
68+
writer.withContextAttribute(key, (String) entry.getValue());
6669
}
6770
}
6871

69-
byte[] data = (byte[]) this.avroCloudEvent.getData();
72+
ByteBuffer buffer = (ByteBuffer) this.avroCloudEvent.getData();
7073

71-
if (data != null) {
74+
if (buffer != null) {
75+
byte[] data = new byte[buffer.remaining()];
76+
buffer.get(data);
7277
return writer.end(mapper.map(BytesCloudEventData.wrap(data)));
7378
} else {
7479
return writer.end();

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

+15-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package io.cloudevents.avro;
1919

20+
import java.nio.ByteBuffer;
2021
import java.util.Map;
2122
import java.util.HashMap;
2223

@@ -30,26 +31,32 @@ class AvroSerializer {
3031
public static final AvroCloudEvent toAvro(CloudEvent e) {
3132
AvroCloudEvent avroCloudEvent = new AvroCloudEvent();
3233

33-
Map<CharSequence, Object> attrs = new HashMap<>();
34+
Map<String, Object> attrs = new HashMap<>();
3435

35-
attrs.put(CloudEventV1.TYPE, e.getType());
3636
attrs.put(CloudEventV1.SPECVERSION, e.getSpecVersion().toString());
37+
attrs.put(CloudEventV1.TYPE, e.getType());
3738
attrs.put(CloudEventV1.ID, e.getId());
3839
attrs.put(CloudEventV1.SOURCE, e.getSource());
39-
// convert to string
40-
attrs.put(CloudEventV1.TIME, e.getTime().toString());
41-
// convert
42-
attrs.put(CloudEventV1.DATASCHEMA, e.getDataSchema().toString());
43-
attrs.put(CloudEventV1.SUBJECT, e.getSubject());
4440

41+
if (e.getTime() != null) {
42+
// convert to string
43+
attrs.put(CloudEventV1.TIME, e.getTime().toString());
44+
}
45+
46+
if (e.getDataSchema() != null) {
47+
// convert
48+
attrs.put(CloudEventV1.DATASCHEMA, e.getDataSchema().toString());
49+
}
50+
51+
attrs.put(CloudEventV1.SUBJECT, e.getSubject());
4552
attrs.put(CloudEventV1.DATACONTENTTYPE, e.getDataContentType());
4653

4754
avroCloudEvent.setAttribute(attrs);
4855

4956
// check datacontenttype
5057
CloudEventData cloudEventData = e.getData();
5158
if (cloudEventData != null) {
52-
avroCloudEvent.setData(cloudEventData.toBytes());
59+
avroCloudEvent.setData(ByteBuffer.wrap(cloudEventData.toBytes()));
5360
}
5461

5562
return avroCloudEvent;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2018-Present The CloudEvents Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package io.cloudevents.avro;
18+
19+
import java.util.Map;
20+
import java.net.URI;
21+
22+
import io.cloudevents.CloudEvent;
23+
import io.cloudevents.CloudEventData;
24+
import io.cloudevents.SpecVersion;
25+
import io.cloudevents.core.builder.CloudEventBuilder;
26+
import io.cloudevents.core.format.EventFormat;
27+
import org.junit.jupiter.api.Test;
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
public class AvroFormatTest {
31+
32+
Map<String, Object> testData = Map.of("name", "Ning", "age", 22.0);
33+
34+
@Test
35+
public void testSerde() {
36+
EventFormat avroFormat = new AvroFormat();
37+
CloudEventData cloudEventData = new AvroCloudEventDataWrapper(testData);
38+
39+
assertThat(cloudEventData).isNotNull();
40+
assertThat(cloudEventData.toBytes()).isNotNull();
41+
42+
CloudEvent cloudEvent = CloudEventBuilder.v1()
43+
.withId("1")
44+
.withType("testdata")
45+
.withSource(URI.create("http://localhost/test"))
46+
.withData("application/avro", cloudEventData)
47+
.build();
48+
assertThat(cloudEvent).isNotNull();
49+
assertThat(cloudEvent.getSpecVersion()).isEqualTo(SpecVersion.V1);
50+
51+
byte[] bytes = avroFormat.serialize(cloudEvent);
52+
53+
assertThat(bytes).isNotNull();
54+
assertThat(bytes).hasSizeGreaterThan(0);
55+
56+
CloudEvent cloudEvent2 = avroFormat.deserialize(bytes);
57+
58+
assertThat(cloudEvent2).isNotNull();
59+
assertThat(cloudEvent2.getId()).isEqualTo("1");
60+
assertThat(cloudEvent2.getType()).isEqualTo("testdata");
61+
}
62+
63+
}

formats/avro/src/test/resources/v03/min.proto.json

-6
This file was deleted.

formats/avro/src/test/resources/v1/binary_ext.proto.json

-12
This file was deleted.

formats/avro/src/test/resources/v1/json_data.proto.json

-21
This file was deleted.

formats/avro/src/test/resources/v1/json_data_with_ext.proto.json

-30
This file was deleted.

formats/avro/src/test/resources/v1/min.proto.json

-6
This file was deleted.

formats/avro/src/test/resources/v1/text_data.proto.json

-18
This file was deleted.

formats/avro/src/test/resources/v1/xml_data.proto.json

-18
This file was deleted.

0 commit comments

Comments
 (0)