-
Notifications
You must be signed in to change notification settings - Fork 225
Add statestore example with Outbox pattern #1582
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f32a84a
Add statestore example with Outbox pattern
mcruzdev de42a35
Clean events after each test
mcruzdev 77b5db2
Add license header
mcruzdev a46ceb4
Merge branch 'master' into issue-1579
salaboy 5e4ad77
Apply pull request suggestions
mcruzdev b021cbd
Merge branch 'issue-1579' of github.com:mcruzdev/java-sdk into issue-…
mcruzdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
sdk-tests/src/test/java/io/dapr/it/testcontainers/pubsub/outbox/DaprPubSubOutboxIT.java
This file contains hidden or 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,130 @@ | ||
| /* | ||
| * Copyright 2025 The Dapr Authors | ||
| * 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 io.dapr.it.testcontainers.pubsub.outbox; | ||
|
|
||
| import io.dapr.client.DaprClient; | ||
| import io.dapr.client.domain.ExecuteStateTransactionRequest; | ||
| import io.dapr.client.domain.State; | ||
| import io.dapr.client.domain.TransactionalStateOperation; | ||
| import io.dapr.it.testcontainers.DaprClientFactory; | ||
| import io.dapr.testcontainers.Component; | ||
| import io.dapr.testcontainers.DaprContainer; | ||
| import io.dapr.testcontainers.DaprLogLevel; | ||
| import org.assertj.core.api.Assertions; | ||
| import org.awaitility.Awaitility; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.test.context.DynamicPropertyRegistry; | ||
| import org.springframework.test.context.DynamicPropertySource; | ||
| import org.testcontainers.containers.Network; | ||
| import org.testcontainers.containers.wait.strategy.Wait; | ||
| import org.testcontainers.junit.jupiter.Container; | ||
| import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Random; | ||
|
|
||
| import static io.dapr.it.testcontainers.ContainerConstants.DAPR_RUNTIME_IMAGE_TAG; | ||
|
|
||
| @SpringBootTest( | ||
| webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, | ||
| classes = { | ||
| TestPubsubOutboxApplication.class | ||
| } | ||
| ) | ||
| @Testcontainers | ||
| @Tag("testcontainers") | ||
| public class DaprPubSubOutboxIT { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(DaprPubSubOutboxIT.class); | ||
| private static final Network DAPR_NETWORK = Network.newNetwork(); | ||
| private static final Random RANDOM = new Random(); | ||
| private static final int PORT = RANDOM.nextInt(1000) + 8000; | ||
| private static final String APP_FOUND_MESSAGE_PATTERN = ".*application discovered on port.*"; | ||
|
|
||
| private static final String PUBSUB_APP_ID = "pubsub-dapr-app"; | ||
| private static final String PUBSUB_NAME = "pubsub"; | ||
|
|
||
| // topics | ||
| private static final String TOPIC_PRODUCT_CREATED = "product.created"; | ||
| private static final String STATE_STORE_NAME = "kvstore"; | ||
|
|
||
| @Container | ||
| private static final DaprContainer DAPR_CONTAINER = new DaprContainer(DAPR_RUNTIME_IMAGE_TAG) | ||
| .withAppName(PUBSUB_APP_ID) | ||
| .withNetwork(DAPR_NETWORK) | ||
| .withComponent(new Component(STATE_STORE_NAME, "state.in-memory", "v1", Map.of( | ||
| "outboxPublishPubsub", PUBSUB_NAME, | ||
| "outboxPublishTopic", TOPIC_PRODUCT_CREATED | ||
| ))) | ||
| .withComponent(new Component(PUBSUB_NAME, "pubsub.in-memory", "v1", Collections.emptyMap())) | ||
| .withDaprLogLevel(DaprLogLevel.DEBUG) | ||
| .withLogConsumer(outputFrame -> LOG.info(outputFrame.getUtf8String())) | ||
| .withAppChannelAddress("host.testcontainers.internal") | ||
| .withAppPort(PORT); | ||
|
|
||
| /** | ||
| * Expose the Dapr ports to the host. | ||
| * | ||
| * @param registry the dynamic property registry | ||
| */ | ||
| @DynamicPropertySource | ||
| static void daprProperties(DynamicPropertyRegistry registry) { | ||
| registry.add("dapr.http.endpoint", DAPR_CONTAINER::getHttpEndpoint); | ||
| registry.add("dapr.grpc.endpoint", DAPR_CONTAINER::getGrpcEndpoint); | ||
| registry.add("server.port", () -> PORT); | ||
| } | ||
|
|
||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| org.testcontainers.Testcontainers.exposeHostPorts(PORT); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void shouldPublishUsingOutbox() throws Exception { | ||
| Wait.forLogMessage(APP_FOUND_MESSAGE_PATTERN, 1).waitUntilReady(DAPR_CONTAINER); | ||
|
|
||
| try (DaprClient client = DaprClientFactory.createDaprClientBuilder(DAPR_CONTAINER).build()) { | ||
|
|
||
| ExecuteStateTransactionRequest transactionRequest = new ExecuteStateTransactionRequest(STATE_STORE_NAME); | ||
|
|
||
| Product pencil = new Product("Pencil", 1.50); | ||
| State<Product> state = new State<>( | ||
| pencil.getId(), pencil, null | ||
| ); | ||
|
|
||
| TransactionalStateOperation<Product> operation = new TransactionalStateOperation<>( | ||
| TransactionalStateOperation.OperationType.UPSERT, state | ||
| ); | ||
|
|
||
| transactionRequest.setOperations(List.of(operation)); | ||
|
|
||
| client.executeStateTransaction(transactionRequest).block(); | ||
|
|
||
| Awaitility.await().atMost(Duration.ofSeconds(10)) | ||
| .ignoreExceptions() | ||
| .untilAsserted(() -> Assertions.assertThat(ProductWebhookController.EVENT_LIST).isNotEmpty()); | ||
| } | ||
| } | ||
|
|
||
| } |
63 changes: 63 additions & 0 deletions
63
sdk-tests/src/test/java/io/dapr/it/testcontainers/pubsub/outbox/Product.java
This file contains hidden or 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,63 @@ | ||
| /* | ||
| * Copyright 2025 The Dapr Authors | ||
| * 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 io.dapr.it.testcontainers.pubsub.outbox; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public class Product { | ||
| private String id; | ||
| private String name; | ||
| private double price; | ||
|
|
||
| public Product() { | ||
| } | ||
|
|
||
| public Product(String name, double price) { | ||
| this.id = UUID.randomUUID().toString(); | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(String id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public double getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| public void setPrice(double price) { | ||
| this.price = price; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Product{" + | ||
| "id='" + id + '\'' + | ||
| ", name='" + name + '\'' + | ||
| ", price=" + price + | ||
| '}'; | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
...tests/src/test/java/io/dapr/it/testcontainers/pubsub/outbox/ProductWebhookController.java
This file contains hidden or 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,37 @@ | ||
| /* | ||
| * Copyright 2025 The Dapr Authors | ||
| * 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 io.dapr.it.testcontainers.pubsub.outbox; | ||
|
|
||
| import io.dapr.Topic; | ||
| import io.dapr.client.domain.CloudEvent; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/webhooks/products") | ||
| public class ProductWebhookController { | ||
|
|
||
| public static final List<CloudEvent<Product>> EVENT_LIST = new CopyOnWriteArrayList<>(); | ||
|
|
||
| @PostMapping("/created") | ||
| @Topic(name = "product.created", pubsubName = "pubsub") | ||
| public void handleEvent(@RequestBody CloudEvent cloudEvent) { | ||
| System.out.println("Received product.created event: " + cloudEvent.getData()); | ||
| EVENT_LIST.add(cloudEvent); | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
...ts/src/test/java/io/dapr/it/testcontainers/pubsub/outbox/TestPubsubOutboxApplication.java
This file contains hidden or 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,23 @@ | ||
| /* | ||
| * Copyright 2025 The Dapr Authors | ||
| * 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 io.dapr.it.testcontainers.pubsub.outbox; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
| @SpringBootApplication | ||
| public class TestPubsubOutboxApplication { | ||
| public static void main(String[] args) { | ||
| SpringApplication.run(TestPubsubOutboxApplication.class, args); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
...ot-examples/producer-app/src/main/java/io/dapr/springboot/examples/producer/OrderDTO.java
This file contains hidden or 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,31 @@ | ||
| package io.dapr.springboot.examples.producer; | ||
|
|
||
| public class OrderDTO { | ||
|
|
||
| private String id; | ||
| private String item; | ||
| private Integer amount; | ||
|
|
||
| public OrderDTO() { | ||
| } | ||
|
|
||
| public OrderDTO(String id, String item, Integer amount) { | ||
| this.id = id; | ||
| this.item = item; | ||
| this.amount = amount; | ||
| } | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
|
|
||
| public String getItem() { | ||
| return item; | ||
| } | ||
|
|
||
| public Integer getAmount() { | ||
| return amount; | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.