use of io.smallrye.reactive.messaging.kafka.KafkaRecord in project automatiko-engine by automatiko-io.
the class VerificationTest method testProcessViaIntermediateEventExecutionCorrelation.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testProcessViaIntermediateEventExecutionCorrelation() throws InterruptedException {
String temp = "{\"timestamp\":1, \"value\" : 25.0, \"location\":\"kitchen\"}";
InMemorySource<KafkaRecord<String, String>> channelAlarms = connector.source("alarm");
InMemorySource<KafkaRecord<String, String>> channelExtraAlarms = connector.source("extraalarms");
InMemorySink<KafkaRecord<String, String>> channelPocessed = connector.sink("processed");
String id = "room";
String id2 = "room2";
execCounter.reset(2);
channelAlarms.send(KafkaRecord.of(id, temp));
Thread.sleep(2000);
channelAlarms.send(KafkaRecord.of(id2, temp));
execCounter.waitTillCompletion(5);
given().accept(ContentType.JSON).when().get("/alarms").then().statusCode(200).body("$.size()", is(2));
Map data = given().accept(ContentType.JSON).when().get("/alarms/" + id).then().statusCode(200).body("id", is(id)).extract().as(Map.class);
Object alarm = data.get("alarm");
assertNotNull(alarm);
List<Map<String, String>> taskInfo = given().accept(ContentType.JSON).when().get("/alarms/" + id + "/tasks").then().statusCode(200).extract().as(List.class);
assertEquals(0, taskInfo.size());
execCounter.reset(1);
channelExtraAlarms.send(KafkaRecord.of(id, temp));
execCounter.waitTillCompletion(5);
given().accept(ContentType.JSON).when().get("/alarms").then().statusCode(200).body("$.size()", is(1));
List<? extends Message<KafkaRecord<String, String>>> received = channelPocessed.received();
assertEquals(0, received.size());
channelPocessed.clear();
given().accept(ContentType.JSON).when().get("/alarms/" + id).then().statusCode(404);
given().accept(ContentType.JSON).when().get("/alarms/" + id2).then().statusCode(200);
given().accept(ContentType.JSON).when().delete("/alarms/" + id2).then().statusCode(200);
given().accept(ContentType.JSON).when().get("/alarms").then().statusCode(200).body("$.size()", is(0));
}
use of io.smallrye.reactive.messaging.kafka.KafkaRecord in project smallrye-reactive-messaging by smallrye.
the class KafkaSourceBatchWithCloudEventsTest method testWithBeanReceivingBinaryAndStructuredCloudEvents.
@SuppressWarnings("unchecked")
@Test
public void testWithBeanReceivingBinaryAndStructuredCloudEvents() {
ConsumptionBean bean = run(getConfig(topic));
List<KafkaRecord<String, String>> list = bean.getKafkaRecords();
assertThat(list).isEmpty();
// Send a binary cloud event
List<Header> headers = new ArrayList<>();
headers.add(new RecordHeader("ce_specversion", CloudEventMetadata.CE_VERSION_1_0.getBytes()));
headers.add(new RecordHeader("ce_type", "type".getBytes()));
headers.add(new RecordHeader("ce_source", "test://test".getBytes()));
headers.add(new RecordHeader("ce_id", "id".getBytes()));
headers.add(new RecordHeader("content-type", "text/plain".getBytes()));
headers.add(new RecordHeader("ce_subject", "foo".getBytes()));
companion.produceStrings().fromRecords(new ProducerRecord<>(topic, null, null, "binary", "Hello Binary 1", headers));
await().atMost(10, TimeUnit.SECONDS).until(() -> list.size() >= 1);
KafkaRecord<String, String> record = list.get(0);
assertThat(record.getTopic()).isEqualTo(topic);
IncomingKafkaCloudEventMetadata<String, String> metadata = record.getMetadata(IncomingKafkaCloudEventMetadata.class).orElse(null);
assertThat(metadata).isNotNull();
assertThat(metadata.getTopic()).isEqualTo(topic);
assertThat(metadata.getKey()).isEqualTo("binary");
assertThat(metadata.getId()).isEqualTo("id");
assertThat(metadata.getSubject()).hasValue("foo");
assertThat(metadata.getData()).isEqualTo("Hello Binary 1");
assertThat(record.getPayload()).isEqualTo("Hello Binary 1");
// send a structured event
companion.produceStrings().fromRecords(new ProducerRecord<>(topic, null, null, "structured", new JsonObject().put("specversion", CloudEventMetadata.CE_VERSION_1_0).put("type", "type").put("id", "id").put("source", "test://test").put("subject", "bar").put("datacontenttype", "application/json").put("dataschema", "http://schema.io").put("time", "2020-07-23T09:12:34Z").put("data", "Hello Structured 1").encode(), Collections.singletonList(new RecordHeader("content-type", "application/cloudevents+json; charset=utf-8".getBytes()))));
await().atMost(10, TimeUnit.SECONDS).until(() -> list.size() >= 2);
record = list.get(1);
assertThat(record.getTopic()).isEqualTo(topic);
metadata = record.getMetadata(IncomingKafkaCloudEventMetadata.class).orElse(null);
assertThat(metadata).isNotNull();
assertThat(metadata.getTopic()).isEqualTo(topic);
assertThat(metadata.getKey()).isEqualTo("structured");
assertThat(metadata.getId()).isEqualTo("id");
assertThat(metadata.getSubject()).hasValue("bar");
assertThat(metadata.getData()).isEqualTo("Hello Structured 1");
assertThat(record.getPayload()).contains("Hello Structured 1");
// Send a last binary cloud event
List<Header> headers2 = new ArrayList<>();
headers2.add(new RecordHeader("ce_specversion", CloudEventMetadata.CE_VERSION_1_0.getBytes()));
headers2.add(new RecordHeader("ce_type", "type".getBytes()));
headers2.add(new RecordHeader("ce_source", "test://test".getBytes()));
headers2.add(new RecordHeader("ce_id", "id".getBytes()));
headers2.add(new RecordHeader("content-type", "text/plain".getBytes()));
headers2.add(new RecordHeader("ce_subject", "foo".getBytes()));
companion.produceStrings().fromRecords(new ProducerRecord<>(topic, null, null, "binary", "Hello Binary 2", headers2));
await().atMost(10, TimeUnit.SECONDS).until(() -> list.size() >= 3);
record = list.get(2);
assertThat(record.getTopic()).isEqualTo(topic);
metadata = record.getMetadata(IncomingKafkaCloudEventMetadata.class).orElse(null);
assertThat(metadata).isNotNull();
assertThat(metadata.getTopic()).isEqualTo(topic);
assertThat(metadata.getKey()).isEqualTo("binary");
assertThat(metadata.getId()).isEqualTo("id");
assertThat(metadata.getSubject()).hasValue("foo");
assertThat(metadata.getData()).isEqualTo("Hello Binary 2");
assertThat(record.getPayload()).isEqualTo("Hello Binary 2");
}
use of io.smallrye.reactive.messaging.kafka.KafkaRecord in project smallrye-reactive-messaging by smallrye.
the class KafkaCommitHandlerTest method testSourceWithAutoCommitEnabled.
@Test
public void testSourceWithAutoCommitEnabled() throws ExecutionException, TimeoutException, InterruptedException {
MapBasedConfig config = newCommonConfigForSource().with("group.id", "test-source-with-auto-commit-enabled").with(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true").with(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 500).with("value.deserializer", IntegerDeserializer.class.getName());
KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
source = new KafkaSource<>(vertx, "test-source-with-auto-commit-enabled", ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
List<Message<?>> messages = Collections.synchronizedList(new ArrayList<>());
source.getStream().subscribe().with(messages::add);
companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 10);
await().atMost(10, TimeUnit.SECONDS).until(() -> messages.size() >= 10);
assertThat(messages.stream().map(m -> ((KafkaRecord<String, Integer>) m).getPayload()).collect(Collectors.toList())).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
Optional<IncomingKafkaRecord<String, Integer>> firstMessage = messages.stream().map(m -> (IncomingKafkaRecord<String, Integer>) m).findFirst();
assertTrue(firstMessage.isPresent());
CompletableFuture<Void> ackFuture = new CompletableFuture<>();
firstMessage.get().ack().whenComplete((a, t) -> ackFuture.complete(null));
ackFuture.get(10, TimeUnit.SECONDS);
await().atMost(2, TimeUnit.MINUTES).ignoreExceptions().untilAsserted(() -> {
TopicPartition topicPartition = new TopicPartition(topic, 0);
OffsetAndMetadata offset = companion.consumerGroups().offsets("test-source-with-auto-commit-enabled", topicPartition);
assertNotNull(offset);
assertEquals(10L, offset.offset());
});
}
use of io.smallrye.reactive.messaging.kafka.KafkaRecord in project automatiko-engine by automatiko-io.
the class BinaryCEVerificationTest method testProcessViaIntermediateEventExecution.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testProcessViaIntermediateEventExecution() throws InterruptedException {
String temp = "{\"timestamp\":1, \"value\" : 25.0, \"location\":\"kitchen\"}";
InMemorySource<KafkaRecord<String, String>> channelAlarms = connector.source("alarm");
InMemorySource<KafkaRecord<String, String>> channelExtraAlarms = connector.source("extraalarms");
InMemorySink<KafkaRecord<String, String>> channelPocessed = connector.sink("processed");
String id = "room";
execCounter.reset(1);
channelAlarms.send(KafkaRecord.of(id, temp));
execCounter.waitTillCompletion(5);
given().accept(ContentType.JSON).when().get("/alarms").then().statusCode(200).body("$.size()", is(1));
Map data = given().accept(ContentType.JSON).when().get("/alarms/" + id).then().statusCode(200).body("id", is(id)).extract().as(Map.class);
Object alarm = data.get("alarm");
assertNotNull(alarm);
List<Map<String, String>> taskInfo = given().accept(ContentType.JSON).when().get("/alarms/" + id + "/tasks").then().statusCode(200).extract().as(List.class);
assertEquals(0, taskInfo.size());
execCounter.reset(1);
channelExtraAlarms.send(KafkaRecord.of(id, temp));
execCounter.waitTillCompletion(5);
given().accept(ContentType.JSON).when().get("/alarms").then().statusCode(200).body("$.size()", is(0));
List<? extends Message<KafkaRecord<String, String>>> received = channelPocessed.received();
assertEquals(0, received.size());
channelPocessed.clear();
}
use of io.smallrye.reactive.messaging.kafka.KafkaRecord in project automatiko-engine by automatiko-io.
the class BinaryCEVerificationTest method testProcessExecution.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testProcessExecution() throws InterruptedException {
String temp = "{\"timestamp\":1, \"value\" : 45.0, \"location\":\"kitchen\"}";
InMemorySource<KafkaRecord<String, String>> channelAlarms = connector.source("alarm");
InMemorySink<KafkaRecord<String, String>> channelPocessed = connector.sink("processed");
String id = "room";
execCounter.reset(1);
channelAlarms.send(KafkaRecord.of(id, temp));
execCounter.waitTillCompletion(5);
given().accept(ContentType.JSON).when().get("/alarms").then().statusCode(200).body("$.size()", is(1));
Map data = given().accept(ContentType.JSON).when().get("/alarms/" + id).then().statusCode(200).body("id", is(id)).extract().as(Map.class);
Object alarm = data.get("alarm");
assertNotNull(alarm);
List<Map<String, String>> taskInfo = given().accept(ContentType.JSON).when().get("/alarms/" + id + "/tasks").then().statusCode(200).extract().as(List.class);
assertEquals(1, taskInfo.size());
String taskId = taskInfo.get(0).get("id");
String taskName = taskInfo.get(0).get("name");
assertEquals("Task_Name", taskName);
String payload = "{}";
given().contentType(ContentType.JSON).accept(ContentType.JSON).body(payload).when().post("/alarms/" + id + "/" + taskName + "/" + taskId).then().statusCode(200).body("id", is(id), "alarm", notNullValue());
given().accept(ContentType.JSON).when().get("/alarms").then().statusCode(200).body("$.size()", is(0));
List<? extends Message<KafkaRecord<String, String>>> received = channelPocessed.received();
assertEquals(1, received.size());
channelPocessed.clear();
}
Aggregations