Tests ready, starting implementing

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
slinkydeveloper 2020-04-20 19:26:24 +02:00 committed by Francesco Guardiani
parent 30c5fcf1e4
commit 7dcfdba30d
6 changed files with 56 additions and 16 deletions

View File

@ -0,0 +1,7 @@
package io.cloudevents.format;
public class EventDeserializationException extends RuntimeException {
public EventDeserializationException(Throwable e) {
super(e);
}
}

View File

@ -6,9 +6,9 @@ import java.util.Set;
public interface EventFormat {
byte[] serialize(CloudEvent event);
byte[] serialize(CloudEvent event) throws EventSerializationException;
CloudEvent deserialize(byte[] event);
CloudEvent deserialize(byte[] event) throws EventDeserializationException;
Set<String> supportedContentTypes();

View File

@ -0,0 +1,7 @@
package io.cloudevents.format;
public class EventSerializationException extends RuntimeException {
public EventSerializationException(Throwable e) {
super(e);
}
}

View File

@ -13,7 +13,7 @@ public final class CloudEventImpl implements CloudEvent, BinaryMessage {
private final byte[] data;
private final Map<String, Object> extensions;
protected CloudEventImpl(Attributes attributes, byte[] data, Map<String, Object> extensions) {
public CloudEventImpl(Attributes attributes, byte[] data, Map<String, Object> extensions) {
Objects.requireNonNull(attributes);
this.attributes = (AttributesInternal) attributes;
this.data = data;

View File

@ -15,11 +15,16 @@
*/
package io.cloudevents.format.json;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.cloudevents.CloudEvent;
import io.cloudevents.format.EventDeserializationException;
import io.cloudevents.format.EventFormat;
import io.cloudevents.format.EventSerializationException;
import io.cloudevents.impl.CloudEventImpl;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.Set;
@ -36,18 +41,39 @@ public final class JsonFormat implements EventFormat {
MAPPER.registerModule(module);
}
private boolean forceDataBase64Serialization = false;
private boolean forceStringSerialization = false;
@Override
public byte[] serialize(CloudEvent event) {
return new byte[0];
public byte[] serialize(CloudEvent event) throws EventSerializationException {
try {
return MAPPER.writeValueAsBytes(event);
} catch (JsonProcessingException e) {
throw new EventSerializationException(e);
}
}
@Override
public CloudEvent deserialize(byte[] event) {
return null;
public CloudEvent deserialize(byte[] event) throws EventDeserializationException {
try {
return MAPPER.readValue(event, CloudEventImpl.class);
} catch (IOException e) {
throw new EventDeserializationException(e);
}
}
@Override
public Set<String> supportedContentTypes() {
return Collections.singleton("application/cloudevents+json");
}
public JsonFormat forceDataBase64Serialization(boolean forceBase64Serialization) {
this.forceDataBase64Serialization = forceBase64Serialization;
return this;
}
public JsonFormat forceDataStringSerialization(boolean forceStringSerialization) {
this.forceStringSerialization = forceStringSerialization;
return this;
}
}

View File

@ -66,6 +66,14 @@ class JsonFormatTest {
}
private static Stream<Arguments> serializeTestArguments() {
return deserializeTestArguments().map(a -> {
List<Object> vals = new ArrayList<>(Arrays.asList(a.get()));
Collections.reverse(vals);
return Arguments.of(vals.toArray());
});
}
private static Stream<Arguments> deserializeTestArguments() {
return Stream.of(
Arguments.of("v03/min.json", V03_MIN),
Arguments.of("v03/json_data.json", V03_WITH_JSON_DATA),
@ -84,14 +92,6 @@ class JsonFormatTest {
);
}
private static Stream<Arguments> deserializeTestArguments() {
return serializeTestArguments().map(a -> {
List<Object> vals = new ArrayList<>(Arrays.asList(a.get()));
Collections.reverse(vals);
return Arguments.of(vals.toArray());
});
}
private static Stream<String> roundTripTestArguments() {
return Stream.of(
"v03/min.json",
@ -123,7 +123,7 @@ class JsonFormatTest {
}
private EventFormat getFormat() {
return EventFormatProvider.getInstance().resolveFormat("application/json");
return EventFormatProvider.getInstance().resolveFormat("application/cloudevents+json");
}
}