Skip to content
17 changes: 16 additions & 1 deletion src/Testcontainers.ServiceBus/ServiceBusContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,19 @@ public string GetConnectionString()
properties.Add("UseDevelopmentEmulator", "true");
return string.Join(";", properties.Select(property => string.Join("=", property.Key, property.Value)));
}
}

/// <summary>
/// Gets the Service Bus HTTP connection string.
/// The connection string to use, for instance, in the ServiceBusAdministrationClient.
Comment thread
NelsonBN marked this conversation as resolved.
Resolved
/// </summary>
/// <returns>The Service Bus HTTP connection string.</returns>
public string GetHttpConnectionString()
{
var properties = new Dictionary<string, string>();
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)));
}
Comment thread
NelsonBN marked this conversation as resolved.
Resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Azure.Messaging.ServiceBus.Administration;

namespace Testcontainers.ServiceBus.Tests;
Comment thread
NelsonBN marked this conversation as resolved.
Resolved

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);
}

protected virtual ValueTask DisposeAsyncCore()
{
return _serviceBusContainer.DisposeAsync();
}


Comment thread
NelsonBN marked this conversation as resolved.
Resolved
[UsedImplicitly]
public sealed class ServiceBusDefaultMsSqlConfiguration : ServiceBusAdministrationClientTest
{
public ServiceBusDefaultMsSqlConfiguration()
Comment thread
NelsonBN marked this conversation as resolved.
Resolved
: base(new ServiceBusBuilder(TestSession.GetImageFromDockerfile())
.WithAcceptLicenseAgreement(true)
.Build())
{
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved


Comment thread
NelsonBN marked this conversation as resolved.
Resolved
[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());
Comment thread
NelsonBN marked this conversation as resolved.
Resolved

var sender = client.CreateSender(queueName);

var receiver = client.CreateReceiver(queueName);
Comment thread
NelsonBN marked this conversation as resolved.
Resolved

// 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.Equal(helloServiceBus, receivedMessage.Body.ToString());
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}