|
| 1 | +package org.testcontainers.azure; |
| 2 | + |
| 3 | +import org.testcontainers.containers.GenericContainer; |
| 4 | +import org.testcontainers.containers.MSSQLServerContainer; |
| 5 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 6 | +import org.testcontainers.images.builder.Transferable; |
| 7 | +import org.testcontainers.utility.DockerImageName; |
| 8 | +import org.testcontainers.utility.LicenseAcceptance; |
| 9 | + |
| 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 | + * Exposed port: 5672 |
| 16 | + */ |
| 17 | +public class AzureServiceBusContainer extends GenericContainer<AzureServiceBusContainer> { |
| 18 | + |
| 19 | + private static final String CONNECTION_STRING_FORMAT = |
| 20 | + "Endpoint=sb://%s:%d;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;"; |
| 21 | + |
| 22 | + private static final int DEFAULT_PORT = 5672; |
| 23 | + |
| 24 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse( |
| 25 | + "mcr.microsoft.com/azure-messaging/servicebus-emulator" |
| 26 | + ); |
| 27 | + |
| 28 | + private MSSQLServerContainer<?> msSqlServerContainer; |
| 29 | + |
| 30 | + /** |
| 31 | + * @param dockerImageName The specified docker image name to run |
| 32 | + */ |
| 33 | + public AzureServiceBusContainer(final String dockerImageName) { |
| 34 | + this(DockerImageName.parse(dockerImageName)); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param dockerImageName The specified docker image name to run |
| 39 | + */ |
| 40 | + public AzureServiceBusContainer(final DockerImageName dockerImageName) { |
| 41 | + super(dockerImageName); |
| 42 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 43 | + withExposedPorts(DEFAULT_PORT); |
| 44 | + waitingFor(Wait.forLogMessage(".*Emulator Service is Successfully Up!.*", 1)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Sets the MS SQL Server dependency needed by the Service Bus Container, |
| 49 | + * |
| 50 | + * @param msSqlServerContainer The MS SQL Server container used by Service Bus as a dependency |
| 51 | + * @return this |
| 52 | + */ |
| 53 | + public AzureServiceBusContainer withMsSqlServerContainer(final MSSQLServerContainer<?> msSqlServerContainer) { |
| 54 | + dependsOn(msSqlServerContainer); |
| 55 | + this.msSqlServerContainer = msSqlServerContainer; |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Provide the Service Bus configuration JSON. |
| 61 | + * |
| 62 | + * @param config The configuration |
| 63 | + * @return this |
| 64 | + */ |
| 65 | + public AzureServiceBusContainer withConfig(final Transferable config) { |
| 66 | + withCopyToContainer(config, "/ServiceBus_Emulator/ConfigFiles/Config.json"); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Accepts the EULA of the container. |
| 72 | + * |
| 73 | + * @return this |
| 74 | + */ |
| 75 | + public AzureServiceBusContainer acceptLicense() { |
| 76 | + withEnv("ACCEPT_EULA", "Y"); |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void configure() { |
| 82 | + if (msSqlServerContainer == null) { |
| 83 | + throw new IllegalStateException( |
| 84 | + "The image " + |
| 85 | + getDockerImageName() + |
| 86 | + " requires a Microsoft SQL Server container. Please provide one with the withMsSqlServerContainer method!" |
| 87 | + ); |
| 88 | + } |
| 89 | + withEnv("SQL_SERVER", msSqlServerContainer.getNetworkAliases().get(0)); |
| 90 | + withEnv("MSSQL_SA_PASSWORD", msSqlServerContainer.getPassword()); |
| 91 | + // If license was not accepted programmatically, check if it was accepted via resource file |
| 92 | + if (!getEnvMap().containsKey("ACCEPT_EULA")) { |
| 93 | + LicenseAcceptance.assertLicenseAccepted(this.getDockerImageName()); |
| 94 | + acceptLicense(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Returns the connection string. |
| 100 | + * |
| 101 | + * @return connection string |
| 102 | + */ |
| 103 | + public String getConnectionString() { |
| 104 | + return String.format(CONNECTION_STRING_FORMAT, getHost(), getMappedPort(DEFAULT_PORT)); |
| 105 | + } |
| 106 | +} |
0 commit comments