Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import io.cloudevents.SpecVersion;
import io.cloudevents.core.data.BytesCloudEventData;
import io.cloudevents.core.message.impl.BaseGenericBinaryMessageReaderImpl;
import io.cloudevents.types.Time;
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;

import java.time.ZoneOffset;
import java.util.Date;
import java.util.Objects;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -117,14 +120,18 @@ protected void forEachHeader(final BiConsumer<String, Object> fn) {
/**
* Gets the cloud event representation of the value.
* <p>
* This method simply returns the string representation of the type of value passed as argument.
* Native AMQP timestamps are converted to their RFC3339 representation.
* Other values are converted using their string representation.
*
* @param value The value of a CloudEvent attribute or extension.
*
* @return The string representation of the specified value.
*/
@Override
protected String toCloudEventsValue(final Object value) {
if (value instanceof Date timestamp) {
return Time.writeTime(timestamp.toInstant().atOffset(ZoneOffset.UTC));
}
return value.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import org.apache.qpid.proton.amqp.Binary;
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
import org.apache.qpid.proton.amqp.messaging.Section;
import org.apache.qpid.proton.message.Message;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.time.OffsetDateTime;
import java.util.AbstractMap.SimpleEntry;
import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -71,6 +74,41 @@ public void readStructured(final CloudEvent event) {
assertThat(amqpReader.toEvent()).isEqualTo(event);
}

@ParameterizedTest
@MethodSource("nativeTimestampArguments")
public void readBinaryWithNativeTimestamp(final SpecVersion version, final OffsetDateTime expectedTime) {
final Date timestamp = Date.from(expectedTime.toInstant());
final Message message = Message.Factory.create();
message.setApplicationProperties(new ApplicationProperties(properties(
property("specversion", version.toString()),
property("id", Data.ID),
property("type", Data.TYPE),
property("source", Data.SOURCE.toString()),
property("time", timestamp),
property("observedtime", timestamp)
)));

final byte[] encoded = new byte[1024];
final int length = message.encode(encoded, 0, encoded.length);
final Message decoded = Message.Factory.create();
decoded.decode(encoded, 0, length);
assertThat(decoded.getApplicationProperties().getValue().get(AmqpConstants.CE_PREFIX + "time"))
.isInstanceOf(Date.class)
.isEqualTo(timestamp);

final CloudEvent event = ProtonAmqpMessageFactory.createReader(decoded).toEvent();
assertThat(event.getTime()).isEqualTo(expectedTime);
assertThat(event.getExtension("observedtime")).isEqualTo(Time.writeTime(expectedTime));
}

private static Stream<Arguments> nativeTimestampArguments() {
return Stream.of(SpecVersion.V03, SpecVersion.V1).flatMap(version -> Stream.of(
"1969-12-31T23:59:59.999Z",
"1970-01-01T00:00:00Z",
"2026-11-07T20:15:16.123Z"
).map(time -> Arguments.of(version, OffsetDateTime.parse(time))));
}

private static Stream<Arguments> binaryTestArguments() {

return Stream.of(
Expand Down
Loading