From 42ccbc51c0276b9c5a2ebc797eca6cef9031b215 Mon Sep 17 00:00:00 2001 From: Muyao Date: Tue, 20 Aug 2024 14:11:11 +0800 Subject: [PATCH] add testcontainers sample --- README.md | 10 ++-- .../README.md | 5 -- .../spring-cloud-azure-cosmos-sample/pom.xml | 30 ---------- .../src/test/resources/application.yml | 0 pom.xml | 3 + .../storage-blob-sample/README.md | 5 -- .../storage-blob-sample/pom.xml | 20 ------- .../src/test/resources/application.yml | 0 .../storage-queue-client/README.md | 5 -- .../storage-queue-client/pom.xml | 20 ------- .../src/test/resources/application.yaml | 0 testcontainers/README.md | 43 ++++++++++++++ testcontainers/cosmos/pom.xml | 57 +++++++++++++++++++ .../test/java/CosmosTestcontainersTest.java | 0 testcontainers/storage-blob/pom.xml | 47 +++++++++++++++ .../java/StorageBlobTestcontainersTest.java | 0 testcontainers/storage-queue/pom.xml | 46 +++++++++++++++ .../java/StorageQueueTestcontainersTest.java | 0 18 files changed, 201 insertions(+), 90 deletions(-) delete mode 100644 cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/src/test/resources/application.yml delete mode 100644 storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/test/resources/application.yml delete mode 100644 storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/src/test/resources/application.yaml create mode 100644 testcontainers/README.md create mode 100644 testcontainers/cosmos/pom.xml rename {cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample => testcontainers/cosmos}/src/test/java/CosmosTestcontainersTest.java (100%) create mode 100644 testcontainers/storage-blob/pom.xml rename {storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample => testcontainers/storage-blob}/src/test/java/StorageBlobTestcontainersTest.java (100%) create mode 100644 testcontainers/storage-queue/pom.xml rename {storage/spring-cloud-azure-starter-storage-queue/storage-queue-client => testcontainers/storage-queue}/src/test/java/StorageQueueTestcontainersTest.java (100%) diff --git a/README.md b/README.md index 4c816c449..de1e5fc06 100644 --- a/README.md +++ b/README.md @@ -110,11 +110,11 @@ ### TestContainers Support -| Sample Project | Support Spring Cloud Azure 4.x | Support Spring Cloud Azure 5.x | -|------------------------------------------------------------------------------------------------------------------------|--------------------------------|--------------------------------| -| [testContainers for Cosmos](cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/src/test/java) | ❌ | ✅ | -| [testContainers for Storage Queue](storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/test/java) | ❌ | ✅ | -| [testContainers for Storage Blob](storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/src/test/java) | ❌ | ✅ | +| Sample Project | Support Spring Cloud Azure 4.x | Support Spring Cloud Azure 5.x | +|------------------------------------------------------------------|--------------------------------|--------------------------------| +| [testContainers for Cosmos](testcontainers/cosmos) | ❌ | ✅ | +| [testContainers for Storage Blob](testcontainers/storage-blob) | ❌ | ✅ | +| [testContainers for Storage Queue](testcontainers/storage-queue) | ❌ | ✅ | ## Getting Help - If you have any question about using these samples, please [create an new issue](https://github.com/Azure-Samples/azure-spring-boot-samples/issues/new/choose). diff --git a/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/README.md b/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/README.md index b2af12604..b3fa62044 100644 --- a/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/README.md +++ b/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/README.md @@ -192,11 +192,6 @@ terraform -chdir=./terraform destroy -auto-approve terraform -chdir=terraform destroy -auto-approve ``` -## Testcontainers Support -Testcontainers is an open source framework for providing throwaway, lightweight instances of databases, message brokers, web browsers, or just about anything that can run in a Docker container. -We also provide `spring-cloud-azure-testcontainers` library to support Testcontainers in Spring Cloud Azure. It allows you to write a test class that can start up a container before any of the tests run. Testcontainers is especially useful for writing integration tests that talk to a real backend service. -Before running the test, you need to prepare a [Docker environment](https://java.testcontainers.org/supported_docker_environment/) supported by Testcontainers. Then you can run the `CosmosTestcontainersTest`. - ## Deploy to Azure Spring Apps Now that you have the Spring Boot application running locally, it's time to move it to production. [Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/overview) makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see [Deploy your first application to Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/quickstart?tabs=Azure-CLI). diff --git a/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/pom.xml b/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/pom.xml index aec10ee51..d9668e243 100644 --- a/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/pom.xml +++ b/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/pom.xml @@ -26,36 +26,6 @@ org.springframework.boot spring-boot-starter-web - - com.azure.spring - spring-cloud-azure-testcontainers - test - - - org.testcontainers - junit-jupiter - test - - - org.testcontainers - azure - test - - - org.springframework.boot - spring-boot-test - test - - - org.junit.jupiter - junit-jupiter-api - test - - - org.assertj - assertj-core - test - diff --git a/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/src/test/resources/application.yml b/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/src/test/resources/application.yml deleted file mode 100644 index e69de29bb..000000000 diff --git a/pom.xml b/pom.xml index b8966969b..180eb8e9f 100644 --- a/pom.xml +++ b/pom.xml @@ -195,6 +195,9 @@ storage/spring-cloud-azure-starter-integration-storage-queue/storage-queue-integration storage/spring-cloud-azure-starter-storage-queue/storage-queue-client storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging + testcontainers/cosmos + testcontainers/storage-blob + testcontainers/storage-queue true diff --git a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/README.md b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/README.md index f8ea7d4f1..fbeee3e14 100644 --- a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/README.md +++ b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/README.md @@ -199,11 +199,6 @@ terraform -chdir=./terraform destroy -auto-approve terraform -chdir=terraform destroy -auto-approve ``` -## Testcontainers Support -Testcontainers is an open source framework for providing throwaway, lightweight instances of databases, message brokers, web browsers, or just about anything that can run in a Docker container. -We also provide `spring-cloud-azure-testcontainers` library to support Testcontainers in Spring Cloud Azure. It allows you to write a test class that can start up a container before any of the tests run. Testcontainers is especially useful for writing integration tests that talk to a real backend service. -Before running the test, you need to prepare a [Docker environment](https://java.testcontainers.org/supported_docker_environment/) supported by Testcontainers. Then you can run the `StorageBlobTestcontainersTest`. - ## Deploy to Azure Spring Apps Now that you have the Spring Boot application running locally, it's time to move it to production. [Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/overview) makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see [Deploy your first application to Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/quickstart?tabs=Azure-CLI). diff --git a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/pom.xml b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/pom.xml index 944f585bd..6a57378c7 100644 --- a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/pom.xml +++ b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/pom.xml @@ -26,25 +26,5 @@ org.springframework.boot spring-boot-starter-web - - com.azure.spring - spring-cloud-azure-testcontainers - test - - - org.springframework.boot - spring-boot-test - test - - - org.assertj - assertj-core - test - - - org.testcontainers - junit-jupiter - test - diff --git a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/test/resources/application.yml b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/test/resources/application.yml deleted file mode 100644 index e69de29bb..000000000 diff --git a/storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/README.md b/storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/README.md index 9c80a731d..138a16657 100644 --- a/storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/README.md +++ b/storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/README.md @@ -186,11 +186,6 @@ terraform -chdir=./terraform destroy -auto-approve terraform -chdir=terraform destroy -auto-approve ``` -## Testcontainers Support -Testcontainers is an open source framework for providing throwaway, lightweight instances of databases, message brokers, web browsers, or just about anything that can run in a Docker container. -We also provide `spring-cloud-azure-testcontainers` library to support Testcontainers in Spring Cloud Azure. It allows you to write a test class that can start up a container before any of the tests run. Testcontainers is especially useful for writing integration tests that talk to a real backend service. -Before running the test, you need to prepare a [Docker environment](https://java.testcontainers.org/supported_docker_environment/) supported by Testcontainers. Then you can run the `StorageQueueTestcontainersTest`. - ## Deploy to Azure Spring Apps Now that you have the Spring Boot application running locally, it's time to move it to production. [Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/overview) makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see [Deploy your first application to Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/quickstart?tabs=Azure-CLI). diff --git a/storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/pom.xml b/storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/pom.xml index 68ad69730..6a9579716 100644 --- a/storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/pom.xml +++ b/storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/pom.xml @@ -21,25 +21,5 @@ com.azure.spring spring-cloud-azure-starter-storage-queue - - com.azure.spring - spring-cloud-azure-testcontainers - test - - - org.testcontainers - junit-jupiter - test - - - org.springframework.boot - spring-boot-test - test - - - org.assertj - assertj-core - test - diff --git a/storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/src/test/resources/application.yaml b/storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/src/test/resources/application.yaml deleted file mode 100644 index e69de29bb..000000000 diff --git a/testcontainers/README.md b/testcontainers/README.md new file mode 100644 index 000000000..e756b544c --- /dev/null +++ b/testcontainers/README.md @@ -0,0 +1,43 @@ +--- +page_type: sample +languages: +- java +products: +- spring-cloud-azure-testcontainers +name: Spring Cloud Azure Testcontainers samples +description: These samples demonstrates how to use Spring Cloud Azure Testcontainers in test cases. +--- + +# Testcontainers Service Connection Samples for Spring Cloud Azure +Testcontainers is an open source framework for providing throwaway, lightweight instances of databases, message brokers, web browsers, or just about anything that can run in a Docker container. + +We provide `spring-cloud-azure-testcontainers` library to support Testcontainers in Spring Cloud Azure. It allows you to write a test class that can start up a container before any of the tests run. Testcontainers is especially useful for writing integration tests that talk to a real backend service. + +This sample project demonstrates how to use Testcontainers Service Connection with Azure Cosmos DB, Azure Storage Blob, and Azure Storage Queue in test cases. + +## What You Need + +- [Docker environment](https://java.testcontainers.org/supported_docker_environment/) +- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later +- Maven +- You can also import the code straight into your IDE: + - [IntelliJ IDEA](https://www.jetbrains.com/idea/download) + +## Run Locally +With docker environment running, you can directly run the tests. + +### Run the sample with Maven + +In your terminal, run `mvn clean test`. + +```shell +mvn clean test +``` + +### Run the sample in IDEs + +You can debug your sample by IDEs. + +* If your tool is `IDEA`, please refer to [Debug your first Java application](https://www.jetbrains.com/help/idea/debugging-your-first-java-application.html) and [add environment variables](https://www.jetbrains.com/help/objc/add-environment-variables-and-program-arguments.html#add-environment-variables). + +* If your tool is `ECLIPSE`, please refer to [Debugging the Eclipse IDE for Java Developers](https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php) and [Eclipse Environment Variable Setup](https://examples.javacodegeeks.com/desktop-java/ide/eclipse/eclipse-environment-variable-setup-example). diff --git a/testcontainers/cosmos/pom.xml b/testcontainers/cosmos/pom.xml new file mode 100644 index 000000000..f49a486b4 --- /dev/null +++ b/testcontainers/cosmos/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + + com.azure.spring + azure-spring-boot-samples + 1.0.0 + ../../pom.xml + + + spring-cloud-azure-testcontainers-for-cosmos-sample + 1.0.0 + jar + + TestContainers for Azure Cosmos DB + + + + com.azure.spring + spring-cloud-azure-starter-cosmos + test + + + com.azure.spring + spring-cloud-azure-testcontainers + test + + + org.testcontainers + junit-jupiter + test + + + org.testcontainers + azure + test + + + org.springframework.boot + spring-boot-test + test + + + org.junit.jupiter + junit-jupiter-api + test + + + org.assertj + assertj-core + test + + + + diff --git a/cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/src/test/java/CosmosTestcontainersTest.java b/testcontainers/cosmos/src/test/java/CosmosTestcontainersTest.java similarity index 100% rename from cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample/src/test/java/CosmosTestcontainersTest.java rename to testcontainers/cosmos/src/test/java/CosmosTestcontainersTest.java diff --git a/testcontainers/storage-blob/pom.xml b/testcontainers/storage-blob/pom.xml new file mode 100644 index 000000000..edee632bb --- /dev/null +++ b/testcontainers/storage-blob/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + + com.azure.spring + azure-spring-boot-samples + 1.0.0 + ../../pom.xml + + + spring-cloud-azure-testcontainers-for-storage-blob-sample + 1.0.0 + jar + + TestContainers for Azure Storage Blob + + + + com.azure.spring + spring-cloud-azure-starter-storage-blob + test + + + com.azure.spring + spring-cloud-azure-testcontainers + test + + + org.springframework.boot + spring-boot-test + test + + + org.assertj + assertj-core + test + + + org.testcontainers + junit-jupiter + test + + + diff --git a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/test/java/StorageBlobTestcontainersTest.java b/testcontainers/storage-blob/src/test/java/StorageBlobTestcontainersTest.java similarity index 100% rename from storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/test/java/StorageBlobTestcontainersTest.java rename to testcontainers/storage-blob/src/test/java/StorageBlobTestcontainersTest.java diff --git a/testcontainers/storage-queue/pom.xml b/testcontainers/storage-queue/pom.xml new file mode 100644 index 000000000..99f60c31c --- /dev/null +++ b/testcontainers/storage-queue/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + + com.azure.spring + azure-spring-boot-samples + 1.0.0 + ../../pom.xml + + + spring-cloud-azure-testcontainers-for-storage-queue-sample + 1.0.0 + jar + TestContainers for Storage Queue + + + + com.azure.spring + spring-cloud-azure-starter-storage-queue + test + + + com.azure.spring + spring-cloud-azure-testcontainers + test + + + org.testcontainers + junit-jupiter + test + + + org.springframework.boot + spring-boot-test + test + + + org.assertj + assertj-core + test + + + diff --git a/storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/src/test/java/StorageQueueTestcontainersTest.java b/testcontainers/storage-queue/src/test/java/StorageQueueTestcontainersTest.java similarity index 100% rename from storage/spring-cloud-azure-starter-storage-queue/storage-queue-client/src/test/java/StorageQueueTestcontainersTest.java rename to testcontainers/storage-queue/src/test/java/StorageQueueTestcontainersTest.java