-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving persistence test to test project to avoid product dependencies
- Loading branch information
Showing
8 changed files
with
209 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...c/test/java/org/kie/kogito/serverless/workflow/executor/MockKafkaEventEmitterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.kie.kogito.serverless.workflow.executor; | ||
|
||
import org.apache.kafka.clients.producer.MockProducer; | ||
import org.apache.kafka.clients.producer.Producer; | ||
import org.apache.kafka.common.header.internals.RecordHeaders; | ||
import org.apache.kafka.common.serialization.ByteArraySerializer; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import io.cloudevents.kafka.CloudEventSerializer; | ||
|
||
public class MockKafkaEventEmitterFactory extends KafkaEventEmitterFactory { | ||
|
||
public static MockProducer<byte[], CloudEvent> producer = new MockProducer<>(true, new ByteArraySerializer(), new CloudEventSerializer() { | ||
@Override | ||
public byte[] serialize(String topic, CloudEvent data) { | ||
return super.serialize(topic, new RecordHeaders(), data); | ||
} | ||
}); | ||
|
||
@Override | ||
public int ordinal() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
protected Producer<byte[], CloudEvent> createKafkaProducer() { | ||
return producer; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
.../test/java/org/kie/kogito/serverless/workflow/executor/MockKafkaEventReceiverFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.kie.kogito.serverless.workflow.executor; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.kafka.clients.consumer.Consumer; | ||
import org.apache.kafka.clients.consumer.MockConsumer; | ||
import org.apache.kafka.clients.consumer.OffsetResetStrategy; | ||
import org.apache.kafka.common.TopicPartition; | ||
|
||
import io.cloudevents.CloudEvent; | ||
|
||
public class MockKafkaEventReceiverFactory extends KafkaEventReceiverFactory { | ||
|
||
public static MockConsumer<byte[], CloudEvent> consumer; | ||
|
||
@Override | ||
public int ordinal() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
protected Consumer<byte[], CloudEvent> createKafkaConsumer() { | ||
return consumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST) { | ||
@Override | ||
public void subscribe(Collection<String> topics) { | ||
super.subscribe(topics); | ||
List<TopicPartition> partitions = topics.stream().map(topic -> new TopicPartition(topic, 0)).collect(Collectors.toList()); | ||
Map<TopicPartition, Long> partitionsBeginningMap = new HashMap<>(); | ||
Map<TopicPartition, Long> partitionsEndMap = new HashMap<>(); | ||
for (TopicPartition partition : partitions) { | ||
partitionsBeginningMap.put(partition, 0L); | ||
partitionsEndMap.put(partition, 10L); | ||
} | ||
rebalance(partitions); | ||
updateBeginningOffsets(partitionsBeginningMap); | ||
updateEndOffsets(partitionsEndMap); | ||
} | ||
}; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
.../src/test/java/org/kie/kogito/serverless/workflow/executor/PersistentApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.kie.kogito.serverless.workflow.executor; | ||
|
||
import java.net.URI; | ||
import java.nio.file.Path; | ||
import java.time.Duration; | ||
import java.time.OffsetDateTime; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.clients.consumer.MockConsumer; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.kie.kogito.persistence.rocksdb.RocksDBProcessInstancesFactory; | ||
import org.rocksdb.Options; | ||
import org.rocksdb.RocksDBException; | ||
|
||
import com.fasterxml.jackson.databind.node.TextNode; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import io.cloudevents.core.builder.CloudEventBuilder; | ||
import io.cloudevents.jackson.JsonCloudEventData; | ||
import io.serverlessworkflow.api.Workflow; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.kie.kogito.serverless.workflow.fluent.ActionBuilder.call; | ||
import static org.kie.kogito.serverless.workflow.fluent.EventDefBuilder.eventDef; | ||
import static org.kie.kogito.serverless.workflow.fluent.FunctionBuilder.expr; | ||
import static org.kie.kogito.serverless.workflow.fluent.StateBuilder.callback; | ||
import static org.kie.kogito.serverless.workflow.fluent.WorkflowBuilder.jsonObject; | ||
import static org.kie.kogito.serverless.workflow.fluent.WorkflowBuilder.workflow; | ||
|
||
public class PersistentApplicationTest { | ||
@Test | ||
void testCallbackSubscriberWithPersistence(@TempDir Path tempDir) throws InterruptedException, TimeoutException, RocksDBException { | ||
final String eventType = "testSubscribe"; | ||
final String additionalData = "This has been injected by the event"; | ||
Workflow workflow = workflow("testCallback").start(callback(call(expr("concat", "{slogan:.slogan+\"er Beti\"}")), eventDef(eventType))).end().build(); | ||
try (StaticWorkflowApplication application = | ||
StaticWorkflowApplication.create().processInstancesFactory(new RocksDBProcessInstancesFactory(new Options().setCreateIfMissing(true), tempDir.toString()))) { | ||
String id = application.execute(workflow, jsonObject().put("slogan", "Viva ")).getId(); | ||
assertThat(application.variables(id).orElseThrow().getWorkflowdata()).doesNotContain(new TextNode(additionalData)); | ||
publish(eventType, buildCloudEvent(eventType, id) | ||
.withData(JsonCloudEventData.wrap(jsonObject().put("additionalData", additionalData))) | ||
.build()); | ||
assertThat(application.waitForFinish(id, Duration.ofSeconds(20)).orElseThrow().getWorkflowdata()) | ||
.isEqualTo(jsonObject().put("additionalData", additionalData).put("slogan", "Viva er Beti")); | ||
await().atMost(Duration.ofSeconds(1)).pollInterval(Duration.ofMillis(50)).until(() -> application.variables(id).isEmpty()); | ||
} | ||
} | ||
|
||
private CloudEventBuilder buildCloudEvent(String eventType, String id) { | ||
return CloudEventBuilder.v1() | ||
.withId(UUID.randomUUID().toString()) | ||
.withSource(URI.create("")) | ||
.withType(eventType) | ||
.withTime(OffsetDateTime.now()) | ||
.withExtension("kogitoprocrefid", id); | ||
} | ||
|
||
private void publish(String topic, CloudEvent event) { | ||
MockConsumer<byte[], CloudEvent> mockConsumer = MockKafkaEventReceiverFactory.consumer; | ||
Set<String> topics = mockConsumer.subscription(); | ||
assertThat(topics).contains(topic); | ||
mockConsumer.addRecord(new ConsumerRecord<>(topic, 0, 0, new byte[0], event)); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...cutor-tests/src/test/resources/META-INF/services/org.kie.kogito.event.EventEmitterFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.kie.kogito.serverless.workflow.executor.MockKafkaEventEmitterFactory |
1 change: 1 addition & 0 deletions
1
...utor-tests/src/test/resources/META-INF/services/org.kie.kogito.event.EventReceiverFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.kie.kogito.serverless.workflow.executor.MockKafkaEventReceiverFactory |