All tests in cloudevents-api pass!

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
slinkydeveloper 2020-04-20 17:45:40 +02:00 committed by Francesco Guardiani
parent 41f48a5c84
commit 0f94976f7b
11 changed files with 118 additions and 24 deletions

View File

@ -17,9 +17,12 @@ public enum SpecVersion {
public static SpecVersion parse(String sv) {
switch (sv) {
case "0.3": return SpecVersion.V03;
case "1.0": return SpecVersion.V1;
default: throw new IllegalArgumentException("Unrecognized SpecVersion "+ sv);
case "0.3":
return SpecVersion.V03;
case "1.0":
return SpecVersion.V1;
default:
throw new IllegalArgumentException("Unrecognized SpecVersion " + sv);
}
}
}

View File

@ -15,8 +15,6 @@ public final class DistributedTracingExtension implements Extension {
private String traceparent;
private String tracestate;
public DistributedTracingExtension() { }
public String getTraceparent() {
return traceparent;
}

View File

@ -6,4 +6,6 @@ public enum Encoding {
UNKNOWN;
public static IllegalStateException UNKNOWN_ENCODING_EXCEPTION = new IllegalStateException("Unknown encoding");
public static IllegalStateException WRONG_ENCODING_EXCEPTION = new IllegalStateException("Wrong encoding");
}

View File

@ -1,6 +1,7 @@
package io.cloudevents.message;
import io.cloudevents.CloudEvent;
import io.cloudevents.format.EventFormat;
public interface Message extends StructuredMessage, BinaryMessage {
@ -16,9 +17,20 @@ public interface Message extends StructuredMessage, BinaryMessage {
default CloudEvent toEvent() throws MessageVisitException, IllegalStateException {
switch (getEncoding()) {
case BINARY: return ((BinaryMessage)this).toEvent();
case STRUCTURED: return ((StructuredMessage)this).toEvent();
default: throw Encoding.UNKNOWN_ENCODING_EXCEPTION;
case BINARY:
return this.visit(specVersion -> {
switch (specVersion) {
case V1:
return CloudEvent.buildV1();
case V03:
return CloudEvent.buildV03();
}
return null; // This can never happen
});
case STRUCTURED:
return this.visit(EventFormat::deserialize);
default:
throw Encoding.UNKNOWN_ENCODING_EXCEPTION;
}
};

View File

@ -152,12 +152,12 @@ public final class AttributesImpl implements AttributesInternal {
@Override
public String toString() {
return "AttributesImpl [id=" + id + ", source=" + source
+ ", specversion=" + SpecVersion.V03 + ", type=" + type
+ ", time=" + time + ", schemaurl=" + schemaurl
+ ", datacontenttype=" + datacontenttype + ", subject="
+ subject + "]";
}
return "Attributes V0.3 [id=" + id + ", source=" + source
+ ", specversion=" + SpecVersion.V03 + ", type=" + type
+ ", time=" + time + ", schemaurl=" + schemaurl
+ ", datacontenttype=" + datacontenttype + ", subject="
+ subject + "]";
}
@Override
public boolean equals(Object o) {

View File

@ -2,6 +2,8 @@ package io.cloudevents.message;
import io.cloudevents.CloudEvent;
import io.cloudevents.mock.CSVFormat;
import io.cloudevents.mock.MockBinaryMessage;
import io.cloudevents.mock.MockStructuredMessage;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@ -12,10 +14,19 @@ public class EventMessageRoundtripTest {
@ParameterizedTest()
@MethodSource("io.cloudevents.test.Data#allEvents")
void structuredToEvent(CloudEvent input) {
CloudEvent event = input.asStructuredMessage(CSVFormat.INSTANCE).toEvent();
assertThat(input.asStructuredMessage(CSVFormat.INSTANCE).toEvent())
.isEqualTo(input);
}
@ParameterizedTest()
@MethodSource("io.cloudevents.test.Data#allEvents")
void structuredToMockStructuredMessageToEvent(CloudEvent input) {
assertThat(input.asStructuredMessage(CSVFormat.INSTANCE).visit(new MockStructuredMessage()).toEvent())
.isEqualTo(input);
}
@ParameterizedTest()
@MethodSource("io.cloudevents.test.Data#allEvents")
void binaryToEvent(CloudEvent input) {
@ -23,4 +34,11 @@ public class EventMessageRoundtripTest {
.isEqualTo(input);
}
@ParameterizedTest()
@MethodSource("io.cloudevents.test.Data#allEvents")
void binaryToMockBinaryMessageToEvent(CloudEvent input) {
assertThat(input.asBinaryMessage().visit(new MockBinaryMessage()).toEvent())
.isEqualTo(input);
}
}

View File

@ -42,13 +42,13 @@ public class MockBinaryMessage implements Message, BinaryMessageVisitorFactory<M
for (Map.Entry<String, Object> e: this.attributes.entrySet()) {
if (e.getValue() instanceof String) {
visitor.setAttribute(e.getKey(), (String) e.getValue());
} else if (e.getValue() instanceof Number) {
visitor.setExtension(e.getKey(), (Number) e.getValue());
} else if (e.getValue() instanceof Boolean) {
visitor.setExtension(e.getKey(), (Boolean) e.getValue());
} else if (e.getValue() instanceof ZonedDateTime) {
visitor.setAttribute(e.getKey(), (ZonedDateTime) e.getValue());
} else if (e.getValue() instanceof URI) {
visitor.setAttribute(e.getKey(), (URI) e.getValue());
} else {
// This should never happen because we build that map only through our builders
throw new IllegalStateException("Illegal value inside extensions map: " + e);
throw new IllegalStateException("Illegal value inside attributes map: " + e);
}
}
@ -72,7 +72,7 @@ public class MockBinaryMessage implements Message, BinaryMessageVisitorFactory<M
@Override
public <T> T visit(StructuredMessageVisitor<T> visitor) throws MessageVisitException, IllegalStateException {
throw Encoding.UNKNOWN_ENCODING_EXCEPTION;
throw Encoding.WRONG_ENCODING_EXCEPTION;
}
@Override
@ -117,6 +117,8 @@ public class MockBinaryMessage implements Message, BinaryMessageVisitorFactory<M
@Override
public MockBinaryMessage createBinaryMessageVisitor(SpecVersion version) {
this.version = version;
return this;
}
}

View File

@ -0,0 +1,37 @@
package io.cloudevents.mock;
import io.cloudevents.format.EventFormat;
import io.cloudevents.message.*;
public class MockStructuredMessage implements Message, StructuredMessageVisitor<MockStructuredMessage> {
private EventFormat format;
private byte[] payload;
@Override
public Encoding getEncoding() {
return Encoding.STRUCTURED;
}
@Override
public <T extends BinaryMessageVisitor<V>, V> V visit(BinaryMessageVisitorFactory<T, V> visitor) throws MessageVisitException, IllegalStateException {
throw Encoding.WRONG_ENCODING_EXCEPTION;
}
@Override
public <T> T visit(StructuredMessageVisitor<T> visitor) throws MessageVisitException, IllegalStateException {
if (this.format == null) {
throw new IllegalStateException("MockStructuredMessage is empty");
}
return visitor.setEvent(this.format, this.payload);
}
@Override
public MockStructuredMessage setEvent(EventFormat format, byte[] value) throws MessageVisitException {
this.format = format;
this.payload = value;
return this;
}
}

View File

@ -15,7 +15,7 @@ public class Data {
public static final String DATACONTENTTYPE_JSON = "application/json";
public static final URI DATASCHEMA = URI.create("http://localhost/schema");
public static final String SUBJECT = "sub";
public static final ZonedDateTime TIME = ZonedDateTime.now();
public static final ZonedDateTime TIME = ZonedDateTime.now().withFixedOffsetZone().withNano(0);
public static byte[] DATA_JSON_SERIALIZED = "{}".getBytes();

View File

@ -16,22 +16,32 @@
package io.cloudevents.v03;
import io.cloudevents.CloudEvent;
import io.cloudevents.SpecVersion;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.assertj.core.api.Assertions.assertThat;
/**
*
* @author fabiojose
*
*/
public class CloudEventBuilderTest {
@ParameterizedTest()
@MethodSource("io.cloudevents.test.Data#v03Events")
void testCopyWithBuilder(CloudEvent event) {
assertThat(CloudEvent.buildV1(event).build()).isEqualTo(event);
assertThat(CloudEvent.buildV03(event).build()).isEqualTo(event);
}
@ParameterizedTest()
@MethodSource("io.cloudevents.test.Data#v03Events")
void testToV1(CloudEvent event) {
CloudEvent eventV1 = CloudEvent.buildV1(event).build();
assertThat(eventV1.getAttributes().getSpecVersion())
.isEqualTo(SpecVersion.V1);
assertThat(eventV1).isEqualTo(event.toV1());
}
}

View File

@ -16,6 +16,7 @@
package io.cloudevents.v1;
import io.cloudevents.CloudEvent;
import io.cloudevents.SpecVersion;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@ -35,4 +36,15 @@ public class CloudEventBuilderTest {
assertThat(CloudEvent.buildV1(event).build()).isEqualTo(event);
}
@ParameterizedTest()
@MethodSource("io.cloudevents.test.Data#v1Events")
void testToV03(CloudEvent event) {
CloudEvent eventV03 = CloudEvent.buildV03(event).build();
assertThat(eventV03.getAttributes().getSpecVersion())
.isEqualTo(SpecVersion.V03);
assertThat(eventV03).isEqualTo(event.toV03());
}
}