Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improving eventing testkit configuration documentation #1846

Merged
merged 5 commits into from
Nov 8, 2023
Merged
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 @@ -548,9 +548,9 @@ Java::
----
include::example$java-protobuf-eventsourced-counter/src/it/java/com/example/CounterTopicIntegrationTest.java[tag=test-topic]
----
<1> Start the TestKit, booting up both the service and its proxy.
<2> Get a `IncomingMessages` for the topic named `counter-commands` from the TestKit.
<3> Get a `OutgoingMessages` for the topic named `counter-events` from the TestKit.
<1> Start the TestKit. Set the configuration to mock incoming messages from the `counter-commands` topic and mock outgoing messages from the `counter-events` topic.
<2> Get `IncomingMessages` for the topic named `counter-commands` from the TestKit.
<3> Get `OutgoingMessages` for the topic named `counter-events` from the TestKit.
<4> Build 2 commands and publish both to the topic. The `counterId` is passed as the subject id of the message.
<5> Read 2 messages, one at a time. We pass in the expected class type for the next message.
<6> Assert the received messages have the same value as the commands sent.
Expand All @@ -562,9 +562,9 @@ Scala::
----
include::example$scala-protobuf-eventsourced-counter/src/test/scala/com/example/CounterServiceIntegrationSpec.scala[tag=test-topic]
----
<1> Start the TestKit, booting up both the service and its proxy.
<2> Get a mocked topic named `counter-commands` from the TestKit.
<3> Get a mocked topic named `counter-events` from the TestKit.
<1> Start the TestKit. Set the configuration to mock incoming messages from the `counter-commands` topic and mock outgoing messages from the `counter-events` topic.
<2> Get `IncomingMessages` for topic named `counter-commands` from the TestKit.
<3> Get `OutgoingMessages` for topic named `counter-events` from the TestKit.
<4> Build 2 commands and publish both to the topic. Note the `counterId` is passed as the subject id of the message.
<5> Read 2 messages, one at a time and assert the received messages values. Note we pass in the expected class type for the next message.

Expand Down
2 changes: 1 addition & 1 deletion docs/src/modules/java/pages/access-control.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ When running integration tests, ACLs are disabled by default but can be explicit

[source, java]
----
include::example$java-spring-eventsourced-counter/src/it/java/com/example/TestkitConfig.java[tags=class;acls]
include::example$java-spring-eventsourced-counter/src/it/java/com/example/TestKitConfig.java[tags=class;acls]
----
<1> Using the DEFAULT `Settings` and enabling ACL.

Expand Down
13 changes: 12 additions & 1 deletion docs/src/modules/java/pages/actions-publishing-subscribing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ include::example$java-spring-eventsourced-counter/src/it/java/com/example/Counte

TIP: In the example above we take advantage of the TestKit to serialize / deserialize the messages and pass all the required metadata automatically for us. However, the API also offers the possibility to read and write raw bytes, construct your metadata or read multiple messages at once.

==== Configuration

Before running your test, make sure to configure the TestKit correctly.

[source, java]
----
include::example$java-spring-eventsourced-counter/src/it/java/com/example/TestKitConfig.java[tags=class;eventing-config]
----
<1> Mock incoming messages from the `counter-commands` topic.
<2> Mock outgoing messages from the `counter-events` topic.

==== Metadata

Typically, messages are published with associated metadata. If you want to construct your own `Metadata` to be consumed by a service or make sure the messages published out of your service have specific metadata attached, you can do so using the TestKit, as shown below.
Expand Down Expand Up @@ -258,5 +269,5 @@ To run an integration test against a real instance of Google PubSub (or its Emul
[source,java]
.src/it/java/com/example/TestkitConfig.java
----
include::example$java-spring-eventsourced-counter/src/it/java/com/example/TestkitConfig.java[tags=class;pubsub]
include::example$java-spring-eventsourced-counter/src/it/java/com/example/TestKitConfig.java[tags=class;pubsub]
----
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public CustomerActionWithKafkaIntegrationTest() {

@Test
public void createAndPublish() throws Exception {
System.out.println("Settings - >>>>>>> " + settings.mockedEventing.toString());
var id = UUID.randomUUID().toString();
var customer = buildCustomer(id, "Johanna", "[email protected]", "Porto", "Long Road");
createCustomer(customer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ public class CounterTopicIntegrationTest {
@ClassRule
public static final KalixTestKitResource testKit =
new KalixTestKitResource(Main.createKalix(), KalixTestKit.Settings.DEFAULT
// end::test-topic[]
.withTopicOutgoingMessages("counter-events-with-meta")
// tag::test-topic[]
.withTopicIncomingMessages("counter-commands")
.withTopicOutgoingMessages("counter-events")
.withTopicOutgoingMessages("counter-events-with-meta")); // <1>
.withTopicOutgoingMessages("counter-events")); // <1>

private EventingTestKit.IncomingMessages commandsTopic;
private EventingTestKit.OutgoingMessages eventsTopic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@ActiveProfiles("with-mocked-eventing")
// tag::class[]
@SpringBootTest(classes = Main.class)
@Import(TestkitConfig.class)
@Import(TestKitConfig.class)
public class CounterIntegrationTest extends KalixIntegrationTestKitSupport { // <1>

// end::class[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

// tag::class[]
@SpringBootTest(classes = Main.class)
@Import(TestkitConfig.class)
@Import(TestKitConfig.class)
@ActiveProfiles("with-pubsub")
public class CounterIntegrationWithRealPubSubTest extends KalixIntegrationTestKitSupport { // <1>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,27 @@

// tag::class[]
@Configuration
public class TestkitConfig {
public class TestKitConfig {
// end::class[]

@Profile("with-mocked-eventing")
// tag::acls[]
// tag::eventing-config[]
@Bean
public KalixTestKit.Settings settings() {
return KalixTestKit.Settings.DEFAULT.withAclEnabled()
.withTopicIncomingMessages("counter-commands")
.withTopicOutgoingMessages("counter-events") // <1>
return KalixTestKit.Settings.DEFAULT
// end::eventing-config[]
.withAclEnabled() // <1>
// end::acls[]
// tag::eventing-config[]
.withTopicIncomingMessages("counter-commands") // <1>
.withTopicOutgoingMessages("counter-events") // <2>
// end::eventing-config[]
.withTopicOutgoingMessages("counter-events-with-meta");
// tag::eventing-config[]
// tag::acls[]
}
// end::eventing-config[]
// end::acls[]

@Profile("with-pubsub")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ class CounterServiceIntegrationSpec
private val testKit = KalixTestKit(
Main.createKalix(),
KalixTestKit.DefaultSettings
// end::test-topic[]
.withTopicOutgoingMessages("counter-events-with-meta")
// tag::test-topic[]
.withTopicIncomingMessages("counter-commands")
.withTopicOutgoingMessages("counter-events")
.withTopicOutgoingMessages("counter-events-with-meta")).start() // <1>
.withTopicOutgoingMessages("counter-events")).start() // <1>
// end::test-topic[]

private val client = testKit.getGrpcClient(classOf[CounterService])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ public Integer emptyState() {

@PostMapping("/{id_part_1}/{id_part_2}/set/{value}")
public Effect<String> set(@PathVariable Integer value) {
System.out.println(commandContext().entityId());
return effects().updateState(value).thenReply("OK");
}

@GetMapping("/{id_part_1}/{id_part_2}")
public Effect<Integer> get() {
System.out.println(commandContext().entityId());
return effects().reply(currentState());
}
}