use of io.smallrye.reactive.messaging.ce.IncomingCloudEventMetadata in project smallrye-reactive-messaging by smallrye.
the class CloudEventConsumptionTest method testReceivingStructuredCloudEventsWithStringPayload.
@SuppressWarnings("unchecked")
@Test
public void testReceivingStructuredCloudEventsWithStringPayload() {
String address = UUID.randomUUID().toString();
new MapBasedConfig().with("mp.messaging.incoming.source.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.incoming.source.address", address).with("mp.messaging.incoming.source.host", host).with("mp.messaging.incoming.source.port", port).with("mp.messaging.incoming.source.tracing-enabled", false).with("amqp-username", username).with("amqp-password", password).write();
weld.addBeanClass(MyConsumptionBean.class);
container = weld.initialize();
await().until(() -> isAmqpConnectorReady(container));
MyConsumptionBean bean = container.getBeanManager().createInstance().select(MyConsumptionBean.class).get();
usage.produce(address, 1, () -> {
JsonObject json = new JsonObject().put("specversion", CloudEventMetadata.CE_VERSION_1_0).put("type", "type").put("id", "id").put("source", "test://test").put("subject", "foo").put("datacontenttype", "application/json").put("dataschema", "http://schema.io").put("time", "2020-07-23T09:12:34Z").put("data", new JsonObject().put("name", "neo"));
return AmqpMessage.create().withBody(json.encode()).contentType(AmqpCloudEventHelper.STRUCTURED_CONTENT_TYPE).build();
});
await().atMost(2, TimeUnit.MINUTES).until(() -> bean.getList().size() >= 1);
Message<JsonObject> message = bean.getList().get(0);
IncomingCloudEventMetadata<JsonObject> metadata = message.getMetadata(IncomingCloudEventMetadata.class).orElse(null);
assertThat(metadata).isNotNull();
assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
assertThat(metadata.getType()).isEqualTo("type");
assertThat(metadata.getId()).isEqualTo("id");
assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
assertThat(metadata.getSubject()).hasValue("foo");
assertThat(metadata.getDataContentType()).hasValue("application/json");
assertThat(metadata.getDataSchema()).hasValue(URI.create("http://schema.io"));
assertThat(metadata.getTimeStamp()).isNotEmpty();
assertThat(metadata.getData().getString("name")).isEqualTo("neo");
assertThat(message.getPayload()).isInstanceOf(JsonObject.class);
assertThat(message.getPayload().getString("name")).isEqualTo("neo");
}
use of io.smallrye.reactive.messaging.ce.IncomingCloudEventMetadata in project smallrye-reactive-messaging by smallrye.
the class CloudEventConsumptionTest method testReceivingStructuredCloudEventsWithBinaryPayload.
@SuppressWarnings("unchecked")
@Test
public void testReceivingStructuredCloudEventsWithBinaryPayload() {
String address = UUID.randomUUID().toString();
new MapBasedConfig().with("mp.messaging.incoming.source.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.incoming.source.address", address).with("mp.messaging.incoming.source.host", host).with("mp.messaging.incoming.source.port", port).with("mp.messaging.incoming.source.tracing-enabled", false).with("amqp-username", username).with("amqp-password", password).write();
weld.addBeanClass(MyConsumptionBean.class);
container = weld.initialize();
await().until(() -> isAmqpConnectorReady(container));
MyConsumptionBean bean = container.getBeanManager().createInstance().select(MyConsumptionBean.class).get();
usage.produce(address, 1, () -> {
JsonObject json = new JsonObject().put("specversion", CloudEventMetadata.CE_VERSION_1_0).put("type", "type").put("id", "id").put("source", "test://test").put("subject", "foo").put("datacontenttype", "application/json").put("dataschema", "http://schema.io").put("time", "2020-07-23T09:12:34Z").put("data", new JsonObject().put("name", "neo"));
return AmqpMessage.create().withBufferAsBody(Buffer.buffer(json.toBuffer().getBytes())).contentType(AmqpCloudEventHelper.STRUCTURED_CONTENT_TYPE).build();
});
await().atMost(2, TimeUnit.MINUTES).until(() -> bean.getList().size() >= 1);
Message<JsonObject> message = bean.getList().get(0);
IncomingCloudEventMetadata<JsonObject> metadata = message.getMetadata(IncomingCloudEventMetadata.class).orElse(null);
assertThat(metadata).isNotNull();
assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
assertThat(metadata.getType()).isEqualTo("type");
assertThat(metadata.getId()).isEqualTo("id");
assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
assertThat(metadata.getSubject()).hasValue("foo");
assertThat(metadata.getDataContentType()).hasValue("application/json");
assertThat(metadata.getDataSchema()).hasValue(URI.create("http://schema.io"));
assertThat(metadata.getTimeStamp()).isNotEmpty();
assertThat(metadata.getData().getString("name")).isEqualTo("neo");
assertThat(message.getPayload()).isInstanceOf(JsonObject.class);
assertThat(message.getPayload().getString("name")).isEqualTo("neo");
}
use of io.smallrye.reactive.messaging.ce.IncomingCloudEventMetadata in project smallrye-reactive-messaging by smallrye.
the class CloudEventConsumptionTest method testReceivingStructuredCloudEventsWithoutMatchingContentTypeIsNotReadACloudEvent.
@SuppressWarnings("unchecked")
@Test
public void testReceivingStructuredCloudEventsWithoutMatchingContentTypeIsNotReadACloudEvent() {
String address = UUID.randomUUID().toString();
new MapBasedConfig().with("mp.messaging.incoming.source.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.incoming.source.address", address).with("mp.messaging.incoming.source.host", host).with("mp.messaging.incoming.source.port", port).with("mp.messaging.incoming.source.tracing-enabled", false).with("amqp-username", username).with("amqp-password", password).write();
weld.addBeanClass(MyConsumptionBean.class);
container = weld.initialize();
await().until(() -> isAmqpConnectorReady(container));
MyConsumptionBean bean = container.getBeanManager().createInstance().select(MyConsumptionBean.class).get();
usage.produce(address, 1, () -> {
JsonObject json = new JsonObject().put("specversion", CloudEventMetadata.CE_VERSION_1_0).put("type", "type").put("id", "id").put("source", "test://test").put("data", new JsonObject().put("name", "neo"));
return AmqpMessage.create().withJsonObjectAsBody(// Set the content type to application/json
json).build();
});
await().atMost(2, TimeUnit.MINUTES).until(() -> bean.getList().size() >= 1);
Message<JsonObject> message = bean.getList().get(0);
IncomingCloudEventMetadata<JsonObject> metadata = message.getMetadata(IncomingCloudEventMetadata.class).orElse(null);
assertThat(metadata).isNull();
assertThat(message.getPayload().getString("id")).isEqualTo("id");
}
use of io.smallrye.reactive.messaging.ce.IncomingCloudEventMetadata in project smallrye-reactive-messaging by smallrye.
the class CloudEventConsumptionTest method testWithBeanReceivingBinaryAndStructuredCloudEvents.
@Test
public void testWithBeanReceivingBinaryAndStructuredCloudEvents() {
String address = UUID.randomUUID().toString();
new MapBasedConfig().with("mp.messaging.incoming.source.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.incoming.source.address", address).with("mp.messaging.incoming.source.host", host).with("mp.messaging.incoming.source.port", port).with("mp.messaging.incoming.source.tracing-enabled", false).with("amqp-username", username).with("amqp-password", password).write();
weld.addBeanClass(MyConsumptionBean.class);
container = weld.initialize();
await().until(() -> isAmqpConnectorReady(container));
MyConsumptionBean bean = container.getBeanManager().createInstance().select(MyConsumptionBean.class).get();
usage.produce(address, 1, () -> {
JsonObject json = new JsonObject().put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SPEC_VERSION, CloudEventMetadata.CE_VERSION_1_0).put(AmqpCloudEventHelper.AMQP_HEADER_FOR_TYPE, "type").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_ID, "id").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SOURCE, "test://test").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SUBJECT, "foo").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SCHEMA, "http://schema.io").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_TIME, "2020-07-23T09:12:34Z");
JsonObject payload = new JsonObject();
payload.put("name", "neo");
return AmqpMessage.create().applicationProperties(json).withJsonObjectAsBody(payload).build();
});
await().atMost(2, TimeUnit.MINUTES).until(() -> bean.getList().size() == 1);
Message<JsonObject> message = bean.getList().get(0);
IncomingCloudEventMetadata<JsonObject> metadata = message.getMetadata(IncomingCloudEventMetadata.class).orElse(null);
assertThat(metadata).isNotNull();
assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
assertThat(metadata.getType()).isEqualTo("type");
assertThat(metadata.getId()).isEqualTo("id");
assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
assertThat(metadata.getSubject()).hasValue("foo");
assertThat(metadata.getDataContentType()).hasValue("application/json");
assertThat(metadata.getDataSchema()).hasValue(URI.create("http://schema.io"));
assertThat(metadata.getTimeStamp()).isNotEmpty();
assertThat(metadata.getData().getString("name")).isEqualTo("neo");
assertThat(message.getPayload()).isInstanceOf(JsonObject.class);
assertThat(message.getPayload().getString("name")).isEqualTo("neo");
usage.produce(address, 1, () -> {
JsonObject json = new JsonObject().put("specversion", CloudEventMetadata.CE_VERSION_1_0).put("type", "type").put("id", "id").put("source", "test://test").put("subject", "foo").put("datacontenttype", "application/json").put("dataschema", "http://schema.io").put("time", "2020-07-23T09:12:34Z").put("data", new JsonObject().put("name", "neo-2"));
return AmqpMessage.create().withBufferAsBody(Buffer.buffer(json.toBuffer().getBytes())).contentType(AmqpCloudEventHelper.STRUCTURED_CONTENT_TYPE).build();
});
await().atMost(2, TimeUnit.MINUTES).until(() -> bean.getList().size() == 2);
message = bean.getList().get(1);
metadata = message.getMetadata(IncomingCloudEventMetadata.class).orElse(null);
assertThat(metadata).isNotNull();
assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
assertThat(metadata.getType()).isEqualTo("type");
assertThat(metadata.getId()).isEqualTo("id");
assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
assertThat(metadata.getSubject()).hasValue("foo");
assertThat(metadata.getDataContentType()).hasValue("application/json");
assertThat(metadata.getDataSchema()).hasValue(URI.create("http://schema.io"));
assertThat(metadata.getTimeStamp()).isNotEmpty();
assertThat(metadata.getData().getString("name")).isEqualTo("neo-2");
assertThat(message.getPayload()).isInstanceOf(JsonObject.class);
assertThat(message.getPayload().getString("name")).isEqualTo("neo-2");
}
use of io.smallrye.reactive.messaging.ce.IncomingCloudEventMetadata in project smallrye-reactive-messaging by smallrye.
the class CloudEventConsumptionTest method testReceivingBinaryCloudEventsUsingStringPayload.
@SuppressWarnings("unchecked")
@Test
public void testReceivingBinaryCloudEventsUsingStringPayload() {
String address = UUID.randomUUID().toString();
new MapBasedConfig().with("mp.messaging.incoming.source.connector", AmqpConnector.CONNECTOR_NAME).with("mp.messaging.incoming.source.address", address).with("mp.messaging.incoming.source.host", host).with("mp.messaging.incoming.source.port", port).with("mp.messaging.incoming.source.tracing-enabled", false).with("amqp-username", username).with("amqp-password", password).write();
weld.addBeanClass(MyStringConsumptionBean.class);
container = weld.initialize();
await().until(() -> isAmqpConnectorReady(container));
MyStringConsumptionBean bean = container.getBeanManager().createInstance().select(MyStringConsumptionBean.class).get();
usage.produce(address, 1, () -> {
JsonObject json = new JsonObject().put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SPEC_VERSION, CloudEventMetadata.CE_VERSION_1_0).put(AmqpCloudEventHelper.AMQP_HEADER_FOR_TYPE, "type").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_ID, "id").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SOURCE, "test://test").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SUBJECT, "foo").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_SCHEMA, "http://schema.io").put(AmqpCloudEventHelper.AMQP_HEADER_FOR_TIME, "2020-07-23T09:12:34Z").put(AmqpCloudEventHelper.CE_HEADER_PREFIX + "ext", "hello");
return AmqpMessage.create().applicationProperties(json).contentType("text/plain").withBody("Hello World").build();
});
await().atMost(2, TimeUnit.MINUTES).until(() -> bean.getList().size() >= 1);
Message<String> message = bean.getList().get(0);
IncomingCloudEventMetadata<String> metadata = message.getMetadata(IncomingCloudEventMetadata.class).orElse(null);
assertThat(metadata).isNotNull();
assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
assertThat(metadata.getType()).isEqualTo("type");
assertThat(metadata.getId()).isEqualTo("id");
assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
assertThat(metadata.getSubject()).hasValue("foo");
assertThat(metadata.getDataContentType()).hasValue("text/plain");
assertThat(metadata.getDataSchema()).hasValue(URI.create("http://schema.io"));
assertThat(metadata.getTimeStamp()).isNotEmpty();
assertThat(metadata.getExtensions()).containsEntry("ext", "hello");
assertThat(metadata.getData()).isEqualTo("Hello World");
assertThat(message.getPayload()).isInstanceOf(String.class);
assertThat(message.getPayload()).isEqualTo("Hello World");
}
Aggregations