diff --git a/src/Testcontainers.ServiceBus/ServiceBusContainer.cs b/src/Testcontainers.ServiceBus/ServiceBusContainer.cs index 32c35d3e7..d27d7a056 100644 --- a/src/Testcontainers.ServiceBus/ServiceBusContainer.cs +++ b/src/Testcontainers.ServiceBus/ServiceBusContainer.cs @@ -26,4 +26,21 @@ public string GetConnectionString() properties.Add("UseDevelopmentEmulator", "true"); return string.Join(";", properties.Select(property => string.Join("=", property.Key, property.Value))); } + + /// + /// Gets the Service Bus HTTP connection string. + /// + /// + /// This connection string is intended for use with the ServiceBusAdministrationClient. + /// + /// The Service Bus HTTP connection string. + public string GetHttpConnectionString() + { + var properties = new Dictionary(); + properties.Add("Endpoint", new UriBuilder("sb", Hostname, GetMappedPublicPort(ServiceBusBuilder.ServiceBusHttpPort)).ToString()); + properties.Add("SharedAccessKeyName", "RootManageSharedAccessKey"); + properties.Add("SharedAccessKey", "SAS_KEY_VALUE"); + properties.Add("UseDevelopmentEmulator", "true"); + return string.Join(";", properties.Select(property => string.Join("=", property.Key, property.Value))); + } } \ No newline at end of file diff --git a/tests/Testcontainers.ServiceBus.Tests/ServiceBusAdministrationClientTest.cs b/tests/Testcontainers.ServiceBus.Tests/ServiceBusAdministrationClientTest.cs new file mode 100644 index 000000000..fe01c1834 --- /dev/null +++ b/tests/Testcontainers.ServiceBus.Tests/ServiceBusAdministrationClientTest.cs @@ -0,0 +1,77 @@ +namespace Testcontainers.ServiceBus; + +public abstract class ServiceBusAdministrationClientTest : IAsyncLifetime +{ + private readonly ServiceBusContainer _serviceBusContainer; + + private ServiceBusAdministrationClientTest(ServiceBusContainer serviceBusContainer) + { + _serviceBusContainer = serviceBusContainer; + } + + public async ValueTask InitializeAsync() + { + await _serviceBusContainer.StartAsync() + .ConfigureAwait(false); + } + + public async ValueTask DisposeAsync() + { + await DisposeAsyncCore() + .ConfigureAwait(false); + + GC.SuppressFinalize(this); + } + + [Fact] + [Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] + public async Task CreateQueueViaServiceBusAdministrationClient() + { + // Given + const string helloServiceBus = "Hello, Service Bus!"; + + var queueName = $"sample-queue-{Guid.NewGuid()}"; + + // By default, the emulator uses the following configuration: + // https://learn.microsoft.com/en-us/azure/service-bus-messaging/test-locally-with-service-bus-emulator?tabs=automated-script#interact-with-the-emulator. + + var message = new ServiceBusMessage(helloServiceBus); + + await using var client = new ServiceBusClient(_serviceBusContainer.GetConnectionString()); + var adminClient = new ServiceBusAdministrationClient(_serviceBusContainer.GetHttpConnectionString()); + + var sender = client.CreateSender(queueName); + + var receiver = client.CreateReceiver(queueName); + + // When + await adminClient.CreateQueueAsync(new CreateQueueOptions(queueName), TestContext.Current.CancellationToken) + .ConfigureAwait(true); + + await sender.SendMessageAsync(message, TestContext.Current.CancellationToken) + .ConfigureAwait(true); + + var receivedMessage = await receiver.ReceiveMessageAsync(cancellationToken: TestContext.Current.CancellationToken) + .ConfigureAwait(true); + + // Then + Assert.NotNull(receivedMessage); + Assert.Equal(helloServiceBus, receivedMessage.Body.ToString()); + } + + protected virtual ValueTask DisposeAsyncCore() + { + return _serviceBusContainer.DisposeAsync(); + } + + [UsedImplicitly] + public sealed class ServiceBusDefaultMsSqlConfiguration : ServiceBusAdministrationClientTest + { + public ServiceBusDefaultMsSqlConfiguration() + : base(new ServiceBusBuilder(TestSession.GetImageFromDockerfile()) + .WithAcceptLicenseAgreement(true) + .Build()) + { + } + } +} \ No newline at end of file diff --git a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs index 425428201..6b18afbdc 100644 --- a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs +++ b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs @@ -56,6 +56,7 @@ await sender.SendMessageAsync(message, TestContext.Current.CancellationToken) .ConfigureAwait(true); // Then + Assert.NotNull(receivedMessage); Assert.Equal(helloServiceBus, receivedMessage.Body.ToString()); } // # --8<-- [end:UseServiceBusContainer] diff --git a/tests/Testcontainers.ServiceBus.Tests/Usings.cs b/tests/Testcontainers.ServiceBus.Tests/Usings.cs index 8fb60a0cc..d4f26cebd 100644 --- a/tests/Testcontainers.ServiceBus.Tests/Usings.cs +++ b/tests/Testcontainers.ServiceBus.Tests/Usings.cs @@ -2,6 +2,7 @@ global using System.Text.RegularExpressions; global using System.Threading.Tasks; global using Azure.Messaging.ServiceBus; +global using Azure.Messaging.ServiceBus.Administration; global using DotNet.Testcontainers.Builders; global using DotNet.Testcontainers.Commons; global using DotNet.Testcontainers.Networks;