-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: salaboy <Salaboy@gmail.com>
- Loading branch information
Showing
11 changed files
with
378 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.dapr</groupId> | ||
<artifactId>spring-boot-examples</artifactId> | ||
<version>0.14.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>consumer-app</artifactId> | ||
<name>consumer-app</name> | ||
<description>Spring Boot, Testcontainers and Dapr Integration Examples :: Consumer App</description> | ||
|
||
<properties> | ||
<springboot.version>3.2.6</springboot.version> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>${springboot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.dapr.spring</groupId> | ||
<artifactId>dapr-spring-boot-starter</artifactId> | ||
<version>${dapr-java-sdk.alpha-version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.dapr.spring</groupId> | ||
<artifactId>dapr-spring-boot-starter</artifactId> | ||
<version>${dapr.sdk.alpha.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.dapr.spring</groupId> | ||
<artifactId>dapr-spring-boot-starter-test</artifactId> | ||
<version>${dapr.sdk.alpha.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<version>1.20.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>rabbitmq</artifactId> | ||
<version>1.20.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>kafka</artifactId> | ||
<version>1.20.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.rest-assured</groupId> | ||
<artifactId>rest-assured</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
.../consumer-app/src/main/java/io/dapr/springboot/examples/consumer/ConsumerApplication.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,13 @@ | ||
package io.dapr.springboot.examples.consumer; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class ConsumerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ConsumerApplication.class, args); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...-boot-examples/consumer-app/src/main/java/io/dapr/springboot/examples/consumer/Order.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,46 @@ | ||
package io.dapr.springboot.examples.consumer; | ||
|
||
public class Order { | ||
private String id; | ||
private String item; | ||
private Integer amount; | ||
|
||
public Order() { | ||
} | ||
|
||
/** | ||
* Creates a new Order. | ||
* @param id order id | ||
* @param item item reference | ||
* @param amount of items in the order | ||
*/ | ||
public Order(String id, String item, Integer amount) { | ||
this.id = id; | ||
this.item = item; | ||
this.amount = amount; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getItem() { | ||
return item; | ||
} | ||
|
||
public void setItem(String item) { | ||
this.item = item; | ||
} | ||
|
||
public Integer getAmount() { | ||
return amount; | ||
} | ||
|
||
public void setAmount(Integer amount) { | ||
this.amount = amount; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...umer-app/src/main/java/io/dapr/springboot/examples/consumer/SubscriberRestController.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,36 @@ | ||
package io.dapr.springboot.examples.consumer; | ||
|
||
import io.dapr.Topic; | ||
import io.dapr.client.domain.CloudEvent; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@RestController | ||
public class SubscriberRestController { | ||
|
||
private List<CloudEvent> events = new ArrayList<>(); | ||
|
||
/** | ||
* Subscribe to cloud events. | ||
* @param cloudEvent payload | ||
*/ | ||
@PostMapping("subscribe") | ||
@Topic(pubsubName = "pubsub", name = "topic") | ||
public void subscribe(@RequestBody CloudEvent<Order> cloudEvent) { | ||
System.out.println("CONSUME +++++ " + cloudEvent); | ||
System.out.println("ORDER +++++ " + cloudEvent.getData()); | ||
events.add(cloudEvent); | ||
} | ||
|
||
@GetMapping("events") | ||
public List<CloudEvent> getAllEvents() { | ||
return events; | ||
} | ||
|
||
} | ||
|
2 changes: 2 additions & 0 deletions
2
spring-boot-examples/consumer-app/src/main/resources/application.properties
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,2 @@ | ||
spring.application.name=consumer-app | ||
server.port=8081 |
18 changes: 18 additions & 0 deletions
18
...-app/src/test/java/io/dapr/springboot/examples/consumer/ConsumerAppTestConfiguration.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,18 @@ | ||
package io.dapr.springboot.examples.consumer; | ||
|
||
import io.dapr.client.DaprClient; | ||
import io.dapr.spring.boot.autoconfigure.pubsub.DaprPubSubProperties; | ||
import io.dapr.spring.messaging.DaprMessagingTemplate; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties({DaprPubSubProperties.class}) | ||
public class ConsumerAppTestConfiguration { | ||
@Bean | ||
public DaprMessagingTemplate<Order> messagingTemplate(DaprClient daprClient, | ||
DaprPubSubProperties daprPubSubProperties) { | ||
return new DaprMessagingTemplate<>(daprClient, daprPubSubProperties.getName(), false); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...les/consumer-app/src/test/java/io/dapr/springboot/examples/consumer/ConsumerAppTests.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,64 @@ | ||
package io.dapr.springboot.examples.consumer; | ||
|
||
import io.dapr.spring.messaging.DaprMessagingTemplate; | ||
import io.dapr.springboot.DaprAutoConfiguration; | ||
import io.restassured.RestAssured; | ||
import io.restassured.http.ContentType; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
||
|
||
@SpringBootTest(classes = {TestConsumerApplication.class, DaprTestContainersConfig.class, | ||
ConsumerAppTestConfiguration.class, DaprAutoConfiguration.class}, | ||
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) | ||
class ConsumerAppTests { | ||
|
||
@Autowired | ||
private DaprMessagingTemplate<Order> messagingTemplate; | ||
|
||
@Autowired | ||
private SubscriberRestController subscriberRestController; | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
org.testcontainers.Testcontainers.exposeHostPorts(8081); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
RestAssured.baseURI = "http://localhost:" + 8081; | ||
} | ||
|
||
|
||
@Test | ||
void testMessageConsumer() throws InterruptedException, IOException { | ||
|
||
messagingTemplate.send("topic", new Order("abc-123", "the mars volta LP", 1)); | ||
|
||
|
||
given() | ||
.contentType(ContentType.JSON) | ||
.when() | ||
.get("/events") | ||
.then() | ||
.statusCode(200); | ||
|
||
|
||
await() | ||
.atMost(Duration.ofSeconds(5)) | ||
.until(subscriberRestController.getAllEvents()::size, equalTo(1)); | ||
|
||
|
||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
...umer-app/src/test/java/io/dapr/springboot/examples/consumer/DaprTestContainersConfig.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,78 @@ | ||
package io.dapr.springboot.examples.consumer; | ||
|
||
import io.dapr.testcontainers.Component; | ||
import io.dapr.testcontainers.DaprContainer; | ||
import io.dapr.testcontainers.DaprLogLevel; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.context.annotation.Bean; | ||
import org.testcontainers.DockerClientFactory; | ||
import org.testcontainers.containers.Network; | ||
import org.testcontainers.containers.RabbitMQContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@TestConfiguration(proxyBeanMethods = false) | ||
public class DaprTestContainersConfig { | ||
|
||
@Bean | ||
public Network getDaprNetwork() { | ||
Network defaultDaprNetwork = new Network() { | ||
@Override | ||
public String getId() { | ||
return "dapr-network"; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
|
||
} | ||
|
||
@Override | ||
public Statement apply(Statement base, Description description) { | ||
return null; | ||
} | ||
}; | ||
|
||
List<com.github.dockerjava.api.model.Network> networks = DockerClientFactory.instance().client().listNetworksCmd() | ||
.withNameFilter("dapr-network").exec(); | ||
if (networks.isEmpty()) { | ||
Network.builder().createNetworkCmdModifier(cmd -> cmd.withName("dapr-network")).build().getId(); | ||
return defaultDaprNetwork; | ||
} else { | ||
return defaultDaprNetwork; | ||
} | ||
} | ||
|
||
@Bean | ||
public RabbitMQContainer rabbitMQContainer(Network daprNetwork) { | ||
return new RabbitMQContainer(DockerImageName.parse("rabbitmq:3.7.25-management-alpine")) | ||
.withExposedPorts(5672).withNetworkAliases("rabbitmq").withReuse(true).withNetwork(daprNetwork); | ||
|
||
} | ||
|
||
@Bean | ||
@ServiceConnection | ||
public DaprContainer daprContainer(Network daprNetwork, RabbitMQContainer rabbitMQContainer) { | ||
|
||
Map<String, String> rabbitMqProperties = new HashMap<>(); | ||
rabbitMqProperties.put("connectionString", "amqp://guest:guest@rabbitmq:5672"); | ||
rabbitMqProperties.put("user", "guest"); | ||
rabbitMqProperties.put("password", "guest"); | ||
|
||
return new DaprContainer("daprio/daprd:1.14.4").withAppName("consumer-app") | ||
.withNetwork(daprNetwork).withComponent(new Component("pubsub", | ||
"pubsub.rabbitmq", "v1", rabbitMqProperties)) | ||
.withDaprLogLevel(DaprLogLevel.DEBUG) | ||
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String())) | ||
.withAppPort(8081).withAppChannelAddress("host.testcontainers.internal") | ||
.withReusablePlacement(true).dependsOn(rabbitMQContainer); | ||
} | ||
|
||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...sumer-app/src/test/java/io/dapr/springboot/examples/consumer/TestConsumerApplication.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,19 @@ | ||
package io.dapr.springboot.examples.consumer; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
|
||
@SpringBootApplication | ||
public class TestConsumerApplication { | ||
|
||
public static void main(String[] args) { | ||
org.testcontainers.Testcontainers.exposeHostPorts(8081); | ||
SpringApplication | ||
.from(ConsumerApplication::main) | ||
.with(DaprTestContainersConfig.class) | ||
.run(args); | ||
} | ||
|
||
|
||
} |
2 changes: 2 additions & 0 deletions
2
spring-boot-examples/consumer-app/src/test/resources/application.properties
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,2 @@ | ||
dapr.pubsub.name=pubsub | ||
server.port=8081 |
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
<modules> | ||
<module>producer-app</module> | ||
<module>consumer-app</module> | ||
</modules> | ||
|
||
</project> |