-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add testcontainers sample * add testcontainers sample * fix package
- Loading branch information
Showing
9 changed files
with
425 additions
and
1 deletion.
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
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
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,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). |
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,68 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>azure-spring-boot-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>spring-cloud-azure-testcontainers-for-cosmos-sample</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>TestContainers for Azure Cosmos DB</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>spring-cloud-azure-starter-cosmos</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>spring-cloud-azure-testcontainers</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>azure</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
72 changes: 72 additions & 0 deletions
72
testcontainers/cosmos/src/test/java/CosmosTestcontainersTest.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,72 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
import com.azure.cosmos.CosmosClient; | ||
import com.azure.cosmos.models.CosmosContainerResponse; | ||
import com.azure.cosmos.models.CosmosDatabaseResponse; | ||
import com.azure.spring.cloud.autoconfigure.implementation.context.AzureGlobalPropertiesAutoConfiguration; | ||
import com.azure.spring.cloud.autoconfigure.implementation.cosmos.AzureCosmosAutoConfiguration; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.testcontainers.containers.CosmosDBEmulatorContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.security.KeyStore; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest(classes = CosmosTestcontainersTest.class) | ||
@Testcontainers | ||
@RunWith(SpringRunner.class) | ||
@ImportAutoConfiguration(classes = { AzureGlobalPropertiesAutoConfiguration.class, AzureCosmosAutoConfiguration.class}) | ||
public class CosmosTestcontainersTest { | ||
|
||
@TempDir | ||
private static File tempFolder; | ||
|
||
@Autowired | ||
private CosmosClient client; | ||
|
||
@Container | ||
@ServiceConnection | ||
static CosmosDBEmulatorContainer cosmos = new CosmosDBEmulatorContainer( | ||
DockerImageName.parse("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest")); | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
cosmos.start(); | ||
Path keyStoreFile = new File(tempFolder, "azure-cosmos-emulator.keystore").toPath(); | ||
KeyStore keyStore = cosmos.buildNewKeyStore(); | ||
try { | ||
keyStore.store(Files.newOutputStream(keyStoreFile.toFile().toPath()), cosmos.getEmulatorKey().toCharArray()); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
System.setProperty("javax.net.ssl.trustStore", keyStoreFile.toString()); | ||
System.setProperty("javax.net.ssl.trustStorePassword", cosmos.getEmulatorKey()); | ||
System.setProperty("javax.net.ssl.trustStoreType", "PKCS12"); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists("Azure"); | ||
assertThat(databaseResponse.getStatusCode()).isEqualTo(201); | ||
CosmosContainerResponse containerResponse = client | ||
.getDatabase("Azure") | ||
.createContainerIfNotExists("ServiceContainer", "/name"); | ||
assertThat(containerResponse.getStatusCode()).isEqualTo(201); | ||
} | ||
|
||
} |
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,59 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>azure-spring-boot-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>spring-cloud-azure-testcontainers-for-storage-blob-sample</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>TestContainers for Azure Storage Blob</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>spring-cloud-azure-starter-storage-blob</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>spring-cloud-azure-testcontainers</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
56 changes: 56 additions & 0 deletions
56
testcontainers/storage-blob/src/test/java/StorageBlobTestcontainersTest.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,56 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
import com.azure.spring.cloud.autoconfigure.implementation.context.AzureGlobalPropertiesAutoConfiguration; | ||
import com.azure.spring.cloud.autoconfigure.implementation.storage.blob.AzureStorageBlobAutoConfiguration; | ||
import com.azure.spring.cloud.autoconfigure.implementation.storage.blob.AzureStorageBlobResourceAutoConfiguration; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.WritableResource; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.util.StreamUtils; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.charset.Charset; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest(classes = StorageBlobTestcontainersTest.class) | ||
@Testcontainers | ||
@RunWith(SpringRunner.class) | ||
@ImportAutoConfiguration(classes = { AzureGlobalPropertiesAutoConfiguration.class, AzureStorageBlobAutoConfiguration.class, AzureStorageBlobResourceAutoConfiguration.class}) | ||
public class StorageBlobTestcontainersTest { | ||
@Container | ||
@ServiceConnection | ||
private static final GenericContainer<?> AZURITE_CONTAINER = new GenericContainer<>( | ||
"mcr.microsoft.com/azure-storage/azurite:latest") | ||
.withExposedPorts(10000); | ||
|
||
@Value("azure-blob://testcontainers/message.txt") | ||
private Resource blobFile; | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
AZURITE_CONTAINER.start(); | ||
} | ||
|
||
@Test | ||
public void test() throws IOException { | ||
String originalContent = "Hello World!"; | ||
try (OutputStream os = ((WritableResource) this.blobFile).getOutputStream()) { | ||
os.write(originalContent.getBytes()); | ||
} | ||
String resultContent = StreamUtils.copyToString(this.blobFile.getInputStream(), Charset.defaultCharset()); | ||
assertThat(resultContent).isEqualTo(originalContent); | ||
} | ||
|
||
} |
Oops, something went wrong.