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.Hosting.Nats #5129

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
26 changes: 21 additions & 5 deletions src/Aspire.Hosting.Nats/NatsBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public static class NatsBuilderExtensions
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NatsServerResource> AddNats(this IDistributedApplicationBuilder builder, string name, int? port = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);

var nats = new NatsServerResource(name);
return builder.AddResource(nats)
.WithEndpoint(targetPort: 4222, port: port, name: NatsServerResource.PrimaryEndpointName)
Expand Down Expand Up @@ -54,7 +57,11 @@ public static IResourceBuilder<NatsServerResource> WithJetStream(this IResourceB
/// <param name="builder">The resource builder.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NatsServerResource> WithJetStream(this IResourceBuilder<NatsServerResource> builder)
=> builder.WithArgs("-js");
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithArgs("-js");
}

/// <summary>
/// Adds a named volume for the data folder to a NATS container resource.
Expand All @@ -64,8 +71,13 @@ public static IResourceBuilder<NatsServerResource> WithJetStream(this IResourceB
/// <param name="isReadOnly">A flag that indicates if this is a read-only volume.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NatsServerResource> WithDataVolume(this IResourceBuilder<NatsServerResource> builder, string? name = null, bool isReadOnly = false)
=> builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/var/lib/nats", isReadOnly)
.WithArgs("-sd", "/var/lib/nats");
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/var/lib/nats",
isReadOnly)
.WithArgs("-sd", "/var/lib/nats");
}

/// <summary>
/// Adds a bind mount for the data folder to a NATS container resource.
Expand All @@ -75,7 +87,11 @@ public static IResourceBuilder<NatsServerResource> WithDataVolume(this IResource
/// <param name="isReadOnly">A flag that indicates if this is a read-only mount.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<NatsServerResource> WithDataBindMount(this IResourceBuilder<NatsServerResource> builder, string source, bool isReadOnly = false)
=> builder.WithBindMount(source, "/var/lib/nats", isReadOnly)
.WithArgs("-sd", "/var/lib/nats");
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

return builder.WithBindMount(source, "/var/lib/nats", isReadOnly)
.WithArgs("-sd", "/var/lib/nats");
}
}
8 changes: 7 additions & 1 deletion src/Aspire.Hosting.Nats/NatsServerResource.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// A resource that represents a NATS server container.
/// </summary>
/// <param name="name">The name of the resource.</param>

public class NatsServerResource(string name) : ContainerResource(name), IResourceWithConnectionString
public class NatsServerResource(string name) : ContainerResource(ThrowIfNull(name)), IResourceWithConnectionString
{
internal const string PrimaryEndpointName = "tcp";
internal const string PrimaryNatsSchemeName = "nats";
Expand All @@ -26,4 +29,7 @@ public class NatsServerResource(string name) : ContainerResource(name), IResourc
public ReferenceExpression ConnectionStringExpression =>
ReferenceExpression.Create(
$"{PrimaryNatsSchemeName}://{PrimaryEndpoint.Property(EndpointProperty.Host)}:{PrimaryEndpoint.Property(EndpointProperty.Port)}");

private static string ThrowIfNull([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
=> argument ?? throw new ArgumentNullException(paramName);
}
93 changes: 93 additions & 0 deletions tests/Aspire.Hosting.Nats.Tests/NatsPublicApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Utils;
using Xunit;

namespace Aspire.Hosting.Nats.Tests;

public class NatsPublicApiTests
{
[Fact]
public void AddNatsContainerShouldThrowWhenBuilderIsNull()
{
IDistributedApplicationBuilder builder = null!;
const string name = "Nats";

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

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

[Fact]
public void AddNatsContainerShouldThrowWhenNameIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
string name = null!;

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

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

[Fact]
public void WithJetStreamShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<NatsServerResource> builder = null!;

var action = () => builder.WithJetStream();

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

[Fact]
public void WithDataVolumeShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<NatsServerResource> builder = null!;

var action = () => builder.WithDataVolume();

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

[Fact]
public void WithDataBindMountShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<NatsServerResource> builder = null!;
const string source = "/data";

var action = () => builder.WithDataBindMount(source);

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

[Fact]
public void WithDataBindMountShouldThrowWhenSourceIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
var nats = builder.AddNats("Nats");
string source = null!;

var action = () => nats.WithDataBindMount(source);

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

[Fact]
public void CtorNatsServerResourceShouldThrowWhenNameIsNull()
{
string name = null!;

var action = () => new NatsServerResource(name);

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