Skip to content

Commit 66f2fbc

Browse files
committed
Updated documentation and tests
1 parent 6b8af57 commit 66f2fbc

File tree

5 files changed

+137
-58
lines changed

5 files changed

+137
-58
lines changed

docs/modules/azure.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ This module is INCUBATING. While it is ready for use and operational in the curr
55

66
Testcontainers module for the Microsoft Azure's [SDK](https://github.com/Azure/azure-sdk-for-java).
77

8-
Currently, the module supports `Azurite` and `CosmosDB` emulators. In order to use them, you should use the following classes:
8+
Currently, the module supports `Azurite`, `CosmosDB`, and `Servicebus` emulators. In order to use them, you should use the following classes:
99

1010
Class | Container Image
1111
-|-
1212
AzuriteContainer | [mcr.microsoft.com/azure-storage/azurite](https://github.com/microsoft/containerregistry)
1313
CosmosDBEmulatorContainer | [mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator](https://github.com/microsoft/containerregistry)
14+
AzureServicebusEmulatorContainer | [mcr.microsoft.com/azure-messaging/servicebus-emulator](https://github.com/microsoft/containerregistry)
1415

1516
## Usage example
1617

@@ -104,6 +105,33 @@ Test against the Emulator:
104105
[Testing against Azure CosmosDB Emulator container](../../modules/azure/src/test/java/org/testcontainers/containers/CosmosDBEmulatorContainerTest.java) inside_block:testWithClientAgainstEmulatorContainer
105106
<!--/codeinclude-->
106107

108+
### Azure service bus Emulator
109+
110+
Start Azure service bus Emulator during a test:
111+
112+
<!--codeinclude-->
113+
[Starting a Azure Service bus Emulator container](../../modules/azure/src/test/java/org/testcontainers/azure/AzureServicebusEmulatorContainerTest.java) inside_block:emulatorContainerDefaultConfig
114+
<!--/codeinclude-->
115+
116+
!!! note
117+
This starts the service bus emulator with the [default config](https://github.com/Azure/azure-service-bus-emulator-installer/blob/main/ServiceBus-Emulator/Config/Config.json)
118+
119+
Start Azure service bus Emulator with custom config during a test:
120+
121+
<!--codeinclude-->
122+
[Starting a Azure Service bus Emulator container with custom config](../../modules/azure/src/test/java/org/testcontainers/azure/AzureServicebusEmulatorContainerTest.java) inside_block:emulatorContainerCustomConfig
123+
<!--/codeinclude-->
124+
125+
Build Azure Service bus sender client:
126+
127+
<!--codeinclude-->
128+
[Testing against Azure Service bus Emulator container](../../modules/azure/src/test/java/org/testcontainers/azure/AzureServicebusEmulatorContainerTest.java) inside_block:buildClient
129+
<!--/codeinclude-->
130+
131+
* See [Overview of the Azure Service Bus emulator](https://learn.microsoft.com/en-us/azure/service-bus-messaging/overview-emulator) for features and limitations.
132+
* [Test locally by using the Azure Service Bus emulator](https://learn.microsoft.com/en-us/azure/service-bus-messaging/test-locally-with-service-bus-emulator?tabs=docker-linux-container)
133+
134+
107135
## Adding this module to your project dependencies
108136

109137
Add the following dependency to your `pom.xml`/`build.gradle` file:

modules/azure/src/main/java/org/testcontainers/containers/ServicebusEmulatorContainer.java renamed to modules/azure/src/main/java/org/testcontainers/azure/AzureServicebusEmulatorContainer.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
package org.testcontainers.containers;
1+
package org.testcontainers.azure;
22

3+
import org.testcontainers.containers.GenericContainer;
4+
import org.testcontainers.containers.MSSQLServerContainer;
5+
import org.testcontainers.containers.Network;
36
import org.testcontainers.containers.wait.strategy.Wait;
47
import org.testcontainers.utility.DockerImageName;
58
import org.testcontainers.utility.MountableFile;
69

7-
public class ServicebusEmulatorContainer<SELF extends ServicebusEmulatorContainer<SELF>> extends GenericContainer<SELF> {
10+
/**
11+
* Testcontainers implementation for Azure service bus emulator.
12+
* <p>
13+
* Supported image: {@code mcr.microsoft.com/azure-messaging/servicebus-emulator}
14+
* </p>
15+
* <p> Exposed ports: 5672</p>
16+
* <p>
17+
* If the official client, azure-messaging-servicebus, is used the oldest version supported is 7.17.8.
18+
* </p>
19+
* <p>
20+
* By default, emulator uses <a href="https://github.com/Azure/azure-service-bus-emulator-installer/blob/main/ServiceBus-Emulator/Config/Config.json">config.json</a> configuration file.
21+
* To supply your own config your own config using {@code withConfigFile(MountableFile)}
22+
* </p>
23+
* <p>
24+
* The service bus emulator requires a database, so a {@code MSSQLServerContainer} is also started.
25+
* </p>
26+
*/
27+
public class AzureServicebusEmulatorContainer extends GenericContainer<AzureServicebusEmulatorContainer> {
828
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse(
929
"mcr.microsoft.com/azure-messaging/servicebus-emulator"
1030
);
@@ -14,7 +34,7 @@ public class ServicebusEmulatorContainer<SELF extends ServicebusEmulatorContaine
1434
/**
1535
* @param dockerImageName specified docker image name to run
1636
*/
17-
public ServicebusEmulatorContainer(final DockerImageName dockerImageName) {
37+
public AzureServicebusEmulatorContainer(final DockerImageName dockerImageName) {
1838
super(dockerImageName);
1939
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
2040
withExposedPorts(PORT);
@@ -25,7 +45,7 @@ public ServicebusEmulatorContainer(final DockerImageName dockerImageName) {
2545
withNetworkAliases("sb-emulator");
2646
MSSQLServerContainer mssqlServerContainer = new MSSQLServerContainer(DockerImageName.parse("mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04"))
2747
.acceptLicense();
28-
String mssqlNetworkAlias = "sqledge";
48+
String mssqlNetworkAlias = "sqlserver";
2949
dependsOn(
3050
mssqlServerContainer
3151
.withNetwork(Network.SHARED)
@@ -36,7 +56,11 @@ public ServicebusEmulatorContainer(final DockerImageName dockerImageName) {
3656
acceptLicense();
3757
}
3858

39-
public SELF withConfigFile(MountableFile configFile) {
59+
/**
60+
* @param configFile <a href="https://github.com/Azure/azure-service-bus-emulator-installer/blob/main/ServiceBus-Emulator/Config/Config.json">config.json</a>
61+
* @return this
62+
*/
63+
public AzureServicebusEmulatorContainer withConfigFile(MountableFile configFile) {
4064
return withCopyFileToContainer(
4165
configFile,
4266
"/ServiceBus_Emulator/ConfigFiles/Config.json"
@@ -47,11 +71,14 @@ public SELF withConfigFile(MountableFile configFile) {
4771
* Accepts the license for the Azure Service Bus Emulator container by setting the ACCEPT_EULA=Y
4872
* variable as described at <a href="https://github.com/Azure/azure-service-bus-emulator-installer/blob/main/README.md">https://github.com/Azure/azure-service-bus-emulator-installer/blob/main/README.md#license</a>
4973
*/
50-
public SELF acceptLicense() {
74+
public AzureServicebusEmulatorContainer acceptLicense() {
5175
addEnv("ACCEPT_EULA", "Y");
5276
return self();
5377
}
5478

79+
/**
80+
* @return connection string for connecting to the service bus.
81+
*/
5582
public String getConnectionString() {
5683
Integer mappedPort = getMappedPort(5672);
5784
return "Endpoint=sb://localhost:" + mappedPort + ";SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;";
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.testcontainers.azure;
2+
3+
import com.azure.core.util.IterableStream;
4+
import com.azure.messaging.servicebus.ServiceBusClientBuilder;
5+
import com.azure.messaging.servicebus.ServiceBusMessage;
6+
import com.azure.messaging.servicebus.ServiceBusReceivedMessage;
7+
import com.azure.messaging.servicebus.ServiceBusReceiverClient;
8+
import com.azure.messaging.servicebus.ServiceBusSenderClient;
9+
import com.azure.messaging.servicebus.models.ServiceBusReceiveMode;
10+
import org.junit.Test;
11+
import org.testcontainers.utility.DockerImageName;
12+
import org.testcontainers.utility.MountableFile;
13+
14+
import java.util.Arrays;
15+
import java.util.List;
16+
import java.util.stream.Collectors;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
public class AzureServicebusEmulatorContainerTest {
21+
22+
@Test
23+
public void testWithDefaultConfig() {
24+
try(
25+
// emulatorContainerDefaultConfig {
26+
AzureServicebusEmulatorContainer azureServicebusEmulatorContainer = new AzureServicebusEmulatorContainer(
27+
DockerImageName.parse("mcr.microsoft.com/azure-messaging/servicebus-emulator")
28+
)
29+
// }
30+
) {
31+
sendAndReceive(azureServicebusEmulatorContainer, "queue.1");
32+
}
33+
}
34+
35+
@Test
36+
public void testWithCustomConfig() {
37+
try(
38+
// emulatorContainerCustomConfig {
39+
AzureServicebusEmulatorContainer azureServicebusEmulatorContainer = new AzureServicebusEmulatorContainer(
40+
DockerImageName.parse("mcr.microsoft.com/azure-messaging/servicebus-emulator")
41+
).withConfigFile(MountableFile.forClasspathResource("/servicebus-config.json"))
42+
// }
43+
) {
44+
sendAndReceive(azureServicebusEmulatorContainer, "our.queue");
45+
}
46+
}
47+
48+
private static void sendAndReceive(AzureServicebusEmulatorContainer azureServicebusEmulatorContainer, String queueName) {
49+
List<String> sentMessages = Arrays.asList("Hello World");
50+
try (
51+
// buildClient {
52+
ServiceBusSenderClient sender = new ServiceBusClientBuilder()
53+
.connectionString(azureServicebusEmulatorContainer.getConnectionString())
54+
.sender()
55+
.queueName(queueName)
56+
.buildClient()
57+
// }
58+
) {
59+
for (String m : sentMessages) {
60+
sender.sendMessage(new ServiceBusMessage(m));
61+
}
62+
}
63+
try (ServiceBusReceiverClient reciever = new ServiceBusClientBuilder()
64+
.connectionString(azureServicebusEmulatorContainer.getConnectionString())
65+
.receiver()
66+
.queueName(queueName)
67+
.receiveMode(ServiceBusReceiveMode.RECEIVE_AND_DELETE)
68+
.buildClient()) {
69+
IterableStream<ServiceBusReceivedMessage> messagesStream = reciever.receiveMessages(sentMessages.size());
70+
List<String> recievedMessages = messagesStream.stream().map(m -> m.getBody().toString()).collect(Collectors.toList());
71+
assertThat(recievedMessages).isEqualTo(sentMessages);
72+
}
73+
}
74+
}

modules/azure/src/test/java/org/testcontainers/containers/ServicebusEmulatorContainerTest.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

modules/azure/src/test/resources/servicebus-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"Name": "sbemulatorns",
66
"Queues": [
77
{
8-
"Name": "queue.1",
8+
"Name": "our.queue",
99
"Properties": {
1010
"DeadLetteringOnMessageExpiration": false,
1111
"DefaultMessageTimeToLive": "PT1H",

0 commit comments

Comments
 (0)