Skip to content

Avro format implementation #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test and fix serde issues
Signed-off-by: Ning Sun <sunng@protonmail.com>
  • Loading branch information
sunng87 committed Jul 17, 2021
commit 8505d783d5495288b3fb2ebfbc294fc382d7dd46
2 changes: 1 addition & 1 deletion formats/avro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>

<outputDirectory>${project.build.directory}/generated-sources/java/</outputDirectory>
<stringType>String</stringType>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class AvroCloudEventDataWrapper implements CloudEventData {
/**
* Wraps a JSON object-like data structure.
*/
public AvroCloudEventDataWrapper(Map<CharSequence, Object> data) {
public AvroCloudEventDataWrapper(Map<String, Object> data) {
avroCloudEventData = new AvroCloudEventData();
avroCloudEventData.setValue(data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.Map;
import java.net.URI;
import java.nio.ByteBuffer;
import java.time.OffsetDateTime;

import io.cloudevents.CloudEventData;
Expand All @@ -43,32 +44,36 @@ public AvroDeserializer(AvroCloudEvent avroCloudEvent) {
@Override
public <W extends CloudEventWriter<R>, R> R read(CloudEventWriterFactory<W, R> writerFactory,
CloudEventDataMapper<? extends CloudEventData> mapper) throws CloudEventRWException {

Map<CharSequence, Object> avroCloudEventAttrs = this.avroCloudEvent.getAttribute();
Map<String, Object> avroCloudEventAttrs = this.avroCloudEvent.getAttribute();
SpecVersion specVersion = SpecVersion.parse((String)avroCloudEventAttrs.get(CloudEventV1.SPECVERSION));
final CloudEventWriter<R> writer = writerFactory.create(specVersion);

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

if (key.equals(CloudEventV1.TIME)) {
// OffsetDateTime
OffsetDateTime value = OffsetDateTime.parse((String) entry.getValue());
writer.withContextAttribute(key, value);

} else if (key.equals(CloudEventV1.DATASCHEMA)) {
// URI
URI value = URI.create((String) entry.getValue());
writer.withContextAttribute(key, value);
} else {
// String
writer.withContextAttribute(key, (String) entry.getValue());
switch(key) {
case CloudEventV1.SPECVERSION:
continue;
case CloudEventV1.TIME: {
// OffsetDateTime
OffsetDateTime value = OffsetDateTime.parse((String) entry.getValue());
writer.withContextAttribute(key, value);
};
case CloudEventV1.DATASCHEMA: {
// URI
URI value = URI.create((String) entry.getValue());
writer.withContextAttribute(key, value);
};
default:
writer.withContextAttribute(key, (String) entry.getValue());
}
}

byte[] data = (byte[]) this.avroCloudEvent.getData();
ByteBuffer buffer = (ByteBuffer) this.avroCloudEvent.getData();

if (data != null) {
if (buffer != null) {
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
return writer.end(mapper.map(BytesCloudEventData.wrap(data)));
} else {
return writer.end();
Expand Down
23 changes: 15 additions & 8 deletions formats/avro/src/main/java/io/cloudevents/avro/AvroSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package io.cloudevents.avro;

import java.nio.ByteBuffer;
import java.util.Map;
import java.util.HashMap;

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

Map<CharSequence, Object> attrs = new HashMap<>();
Map<String, Object> attrs = new HashMap<>();

attrs.put(CloudEventV1.TYPE, e.getType());
attrs.put(CloudEventV1.SPECVERSION, e.getSpecVersion().toString());
attrs.put(CloudEventV1.TYPE, e.getType());
attrs.put(CloudEventV1.ID, e.getId());
attrs.put(CloudEventV1.SOURCE, e.getSource());
// convert to string
attrs.put(CloudEventV1.TIME, e.getTime().toString());
// convert
attrs.put(CloudEventV1.DATASCHEMA, e.getDataSchema().toString());
attrs.put(CloudEventV1.SUBJECT, e.getSubject());

if (e.getTime() != null) {
// convert to string
attrs.put(CloudEventV1.TIME, e.getTime().toString());
}

if (e.getDataSchema() != null) {
// convert
attrs.put(CloudEventV1.DATASCHEMA, e.getDataSchema().toString());
}

attrs.put(CloudEventV1.SUBJECT, e.getSubject());
attrs.put(CloudEventV1.DATACONTENTTYPE, e.getDataContentType());

avroCloudEvent.setAttribute(attrs);

// check datacontenttype
CloudEventData cloudEventData = e.getData();
if (cloudEventData != null) {
avroCloudEvent.setData(cloudEventData.toBytes());
avroCloudEvent.setData(ByteBuffer.wrap(cloudEventData.toBytes()));
}

return avroCloudEvent;
Expand Down
63 changes: 63 additions & 0 deletions formats/avro/src/test/java/io/cloudevents/avro/AvroFormatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2018-Present The CloudEvents Authors
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.cloudevents.avro;

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

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.SpecVersion;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.format.EventFormat;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;

public class AvroFormatTest {

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

@Test
public void testSerde() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add more test cases?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I will do it in next few commits.

EventFormat avroFormat = new AvroFormat();
CloudEventData cloudEventData = new AvroCloudEventDataWrapper(testData);

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

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

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");
}

}
6 changes: 0 additions & 6 deletions formats/avro/src/test/resources/v03/min.proto.json

This file was deleted.

12 changes: 0 additions & 12 deletions formats/avro/src/test/resources/v1/binary_ext.proto.json

This file was deleted.

21 changes: 0 additions & 21 deletions formats/avro/src/test/resources/v1/json_data.proto.json

This file was deleted.

30 changes: 0 additions & 30 deletions formats/avro/src/test/resources/v1/json_data_with_ext.proto.json

This file was deleted.

6 changes: 0 additions & 6 deletions formats/avro/src/test/resources/v1/min.proto.json

This file was deleted.

18 changes: 0 additions & 18 deletions formats/avro/src/test/resources/v1/text_data.proto.json

This file was deleted.

18 changes: 0 additions & 18 deletions formats/avro/src/test/resources/v1/xml_data.proto.json

This file was deleted.

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