From 9fba5455ed4b4312afeb08522e958b0cac53d98c Mon Sep 17 00:00:00 2001 From: Andrzej Ludwikowski Date: Wed, 8 Nov 2023 10:06:05 +0100 Subject: [PATCH] docs: improving eventing testkit configuration documentation (#1846) * docs: improving eventing testkit configuration documentation * small improvements * Update docs/src/modules/java/pages/actions-publishing-subscribing.adoc Co-authored-by: Eduardo Pinto * fixing docs * fixing compilation --------- Co-authored-by: Eduardo Pinto --- .../pages/actions-publishing-subscribing.adoc | 12 ++++++------ docs/src/modules/java/pages/access-control.adoc | 2 +- .../pages/actions-publishing-subscribing.adoc | 13 ++++++++++++- .../CustomerActionWithKafkaIntegrationTest.java | 1 - .../com/example/CounterTopicIntegrationTest.java | 6 ++++-- .../java/com/example/CounterIntegrationTest.java | 2 +- .../CounterIntegrationWithRealPubSubTest.java | 2 +- .../{TestkitConfig.java => TestKitConfig.java} | 15 +++++++++++---- .../example/CounterServiceIntegrationSpec.scala | 6 ++++-- .../user/CompoundIdCounterEntity.java | 2 -- 10 files changed, 40 insertions(+), 21 deletions(-) rename samples/java-spring-eventsourced-counter/src/it/java/com/example/{TestkitConfig.java => TestKitConfig.java} (67%) diff --git a/docs/src/modules/java-protobuf/pages/actions-publishing-subscribing.adoc b/docs/src/modules/java-protobuf/pages/actions-publishing-subscribing.adoc index 1e8e159e46..55dd13671f 100644 --- a/docs/src/modules/java-protobuf/pages/actions-publishing-subscribing.adoc +++ b/docs/src/modules/java-protobuf/pages/actions-publishing-subscribing.adoc @@ -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. @@ -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. diff --git a/docs/src/modules/java/pages/access-control.adoc b/docs/src/modules/java/pages/access-control.adoc index f8d03e9cee..141d905947 100644 --- a/docs/src/modules/java/pages/access-control.adoc +++ b/docs/src/modules/java/pages/access-control.adoc @@ -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. diff --git a/docs/src/modules/java/pages/actions-publishing-subscribing.adoc b/docs/src/modules/java/pages/actions-publishing-subscribing.adoc index 3ae073242d..68765a85cc 100644 --- a/docs/src/modules/java/pages/actions-publishing-subscribing.adoc +++ b/docs/src/modules/java/pages/actions-publishing-subscribing.adoc @@ -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. @@ -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] ---- diff --git a/samples/java-protobuf-customer-registry-kafka-quickstart/src/it/java/customer/api/CustomerActionWithKafkaIntegrationTest.java b/samples/java-protobuf-customer-registry-kafka-quickstart/src/it/java/customer/api/CustomerActionWithKafkaIntegrationTest.java index 7047760276..a8484d418a 100644 --- a/samples/java-protobuf-customer-registry-kafka-quickstart/src/it/java/customer/api/CustomerActionWithKafkaIntegrationTest.java +++ b/samples/java-protobuf-customer-registry-kafka-quickstart/src/it/java/customer/api/CustomerActionWithKafkaIntegrationTest.java @@ -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", "foo@example.com", "Porto", "Long Road"); createCustomer(customer); diff --git a/samples/java-protobuf-eventsourced-counter/src/it/java/com/example/CounterTopicIntegrationTest.java b/samples/java-protobuf-eventsourced-counter/src/it/java/com/example/CounterTopicIntegrationTest.java index cf82eeb8fe..bbaaa11013 100644 --- a/samples/java-protobuf-eventsourced-counter/src/it/java/com/example/CounterTopicIntegrationTest.java +++ b/samples/java-protobuf-eventsourced-counter/src/it/java/com/example/CounterTopicIntegrationTest.java @@ -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; diff --git a/samples/java-spring-eventsourced-counter/src/it/java/com/example/CounterIntegrationTest.java b/samples/java-spring-eventsourced-counter/src/it/java/com/example/CounterIntegrationTest.java index fb9c627006..30d9cd1a32 100644 --- a/samples/java-spring-eventsourced-counter/src/it/java/com/example/CounterIntegrationTest.java +++ b/samples/java-spring-eventsourced-counter/src/it/java/com/example/CounterIntegrationTest.java @@ -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[] diff --git a/samples/java-spring-eventsourced-counter/src/it/java/com/example/CounterIntegrationWithRealPubSubTest.java b/samples/java-spring-eventsourced-counter/src/it/java/com/example/CounterIntegrationWithRealPubSubTest.java index 9156c06236..284306f8d0 100644 --- a/samples/java-spring-eventsourced-counter/src/it/java/com/example/CounterIntegrationWithRealPubSubTest.java +++ b/samples/java-spring-eventsourced-counter/src/it/java/com/example/CounterIntegrationWithRealPubSubTest.java @@ -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> diff --git a/samples/java-spring-eventsourced-counter/src/it/java/com/example/TestkitConfig.java b/samples/java-spring-eventsourced-counter/src/it/java/com/example/TestKitConfig.java similarity index 67% rename from samples/java-spring-eventsourced-counter/src/it/java/com/example/TestkitConfig.java rename to samples/java-spring-eventsourced-counter/src/it/java/com/example/TestKitConfig.java index 58321939d0..2d052cdd2a 100644 --- a/samples/java-spring-eventsourced-counter/src/it/java/com/example/TestkitConfig.java +++ b/samples/java-spring-eventsourced-counter/src/it/java/com/example/TestKitConfig.java @@ -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") diff --git a/samples/scala-protobuf-eventsourced-counter/src/test/scala/com/example/CounterServiceIntegrationSpec.scala b/samples/scala-protobuf-eventsourced-counter/src/test/scala/com/example/CounterServiceIntegrationSpec.scala index db02f4eff8..636c7fbafa 100644 --- a/samples/scala-protobuf-eventsourced-counter/src/test/scala/com/example/CounterServiceIntegrationSpec.scala +++ b/samples/scala-protobuf-eventsourced-counter/src/test/scala/com/example/CounterServiceIntegrationSpec.scala @@ -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]) diff --git a/sdk/java-sdk-spring/src/it/java/com/example/wiring/valueentities/user/CompoundIdCounterEntity.java b/sdk/java-sdk-spring/src/it/java/com/example/wiring/valueentities/user/CompoundIdCounterEntity.java index 4059981795..bb8eadcefd 100644 --- a/sdk/java-sdk-spring/src/it/java/com/example/wiring/valueentities/user/CompoundIdCounterEntity.java +++ b/sdk/java-sdk-spring/src/it/java/com/example/wiring/valueentities/user/CompoundIdCounterEntity.java @@ -37,13 +37,11 @@ public Integer emptyState() { @PostMapping("/{id_part_1}/{id_part_2}/set/{value}") public Effect set(@PathVariable Integer value) { - System.out.println(commandContext().entityId()); return effects().updateState(value).thenReply("OK"); } @GetMapping("/{id_part_1}/{id_part_2}") public Effect get() { - System.out.println(commandContext().entityId()); return effects().reply(currentState()); } }