Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding public API test coverage for Aspire.NATS.Net #5240

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ public static class AspireNatsClientExtensions
/// <exception cref="ArgumentNullException">Thrown if mandatory <paramref name="builder"/> is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when mandatory <see cref="NatsClientSettings.ConnectionString"/> is not provided.</exception>
public static void AddNatsClient(this IHostApplicationBuilder builder, string connectionName, Action<NatsClientSettings>? configureSettings = null, Func<NatsOpts, NatsOpts>? configureOptions = null)
=> AddNatsClient(builder, configurationSectionName: DefaultConfigSectionName, connectionName: connectionName, serviceKey: null, configureSettings: configureSettings, configureOptions: configureOptions);
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(connectionName);

AddNatsClient(builder, configurationSectionName: DefaultConfigSectionName, connectionName: connectionName, serviceKey: null, configureSettings: configureSettings, configureOptions: configureOptions);
}

/// <summary>
/// Registers <see cref="INatsConnection"/> as a keyed service for given <paramref name="name"/> for connecting NATS server with NATS client.
Expand All @@ -47,6 +52,7 @@ public static void AddNatsClient(this IHostApplicationBuilder builder, string co
/// <exception cref="InvalidOperationException">Thrown when mandatory <see cref="NatsClientSettings.ConnectionString"/> is not provided.</exception>
public static void AddKeyedNatsClient(this IHostApplicationBuilder builder, string name, Action<NatsClientSettings>? configureSettings = null, Func<NatsOpts, NatsOpts>? configureOptions = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(name);

AddNatsClient(builder, configurationSectionName: $"{DefaultConfigSectionName}:{name}", connectionName: name, serviceKey: name, configureSettings: configureSettings, configureOptions: configureOptions);
Expand Down
99 changes: 99 additions & 0 deletions tests/Aspire.NATS.Net.Tests/NatsClientPublicApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Hosting;
using Xunit;

namespace Aspire.NATS.Net.Tests;

public class NatsClientPublicApiTests
{
[Fact]
public void AddNatsClientShouldThrowWhenBuilderIsNull()
{
IHostApplicationBuilder builder = null!;

var connectionName = "Nats";

var action = () => builder.AddNatsClient(connectionName);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void AddNatsClientShouldThrowWhenConnectionNameIsNull()
{
var builder = Host.CreateEmptyApplicationBuilder(null);

string connectionName = null!;

var action = () => builder.AddNatsClient(connectionName);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(connectionName), exception.ParamName);
}

[Fact]
public void AddNatsClientShouldThrowWhenConnectionNameIsEmpty()
{
var builder = Host.CreateEmptyApplicationBuilder(null);

var connectionName = "";

var action = () => builder.AddNatsClient(connectionName);

var exception = Assert.Throws<ArgumentException>(action);
Assert.Equal(nameof(connectionName), exception.ParamName);
}

[Fact]
public void AddKeyedNatsClientShouldThrowWhenBuilderIsNull()
{
IHostApplicationBuilder builder = null!;

var connectionName = "Nats";

var action = () => builder.AddKeyedNatsClient(connectionName);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void AddKeyedNatsClientShouldThrowWhenNameIsNull()
{
var builder = Host.CreateEmptyApplicationBuilder(null);

string name = null!;

var action = () => builder.AddKeyedNatsClient(name);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Fact]
public void AddKeyedNatsClientShouldThrowWhenNameIsEmpty()
{
var builder = Host.CreateEmptyApplicationBuilder(null);

var name = "";

var action = () => builder.AddKeyedNatsClient(name);

var exception = Assert.Throws<ArgumentException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Fact]
public void AddNatsJetStreamShouldThrowWhenBuilderIsNull()
{
IHostApplicationBuilder builder = null!;

var action = builder.AddNatsJetStream;

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}
}