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.MongoDB #5201

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
30 changes: 27 additions & 3 deletions src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public static class MongoDBBuilderExtensions
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<MongoDBServerResource> AddMongoDB(this IDistributedApplicationBuilder builder, string name, int? port = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);

var mongoDBContainer = new MongoDBServerResource(name);

return builder
Expand All @@ -42,6 +45,9 @@ public static IResourceBuilder<MongoDBServerResource> AddMongoDB(this IDistribut
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(this IResourceBuilder<MongoDBServerResource> builder, string name, string? databaseName = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);

// Use the resource name as the database name if it's not provided
databaseName ??= name;

Expand All @@ -61,6 +67,8 @@ public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(this IResour
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<T> WithMongoExpress<T>(this IResourceBuilder<T> builder, Action<IResourceBuilder<MongoExpressContainerResource>>? configureContainer = null, string? containerName = null) where T : MongoDBServerResource
{
ArgumentNullException.ThrowIfNull(builder);

containerName ??= $"{builder.Resource.Name}-mongoexpress";

var mongoExpressContainer = new MongoExpressContainerResource(containerName);
Expand All @@ -84,6 +92,8 @@ public static IResourceBuilder<T> WithMongoExpress<T>(this IResourceBuilder<T> b
/// <returns>The resource builder for PGAdmin.</returns>
public static IResourceBuilder<MongoExpressContainerResource> WithHostPort(this IResourceBuilder<MongoExpressContainerResource> builder, int? port)
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithEndpoint("http", endpoint =>
{
endpoint.Port = port;
Expand All @@ -98,7 +108,11 @@ public static IResourceBuilder<MongoExpressContainerResource> WithHostPort(this
/// <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<MongoDBServerResource> WithDataVolume(this IResourceBuilder<MongoDBServerResource> builder, string? name = null, bool isReadOnly = false)
=> builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/data/db", isReadOnly);
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/data/db", isReadOnly);
}

/// <summary>
/// Adds a bind mount for the data folder to a MongoDB container resource.
Expand All @@ -108,7 +122,12 @@ public static IResourceBuilder<MongoDBServerResource> WithDataVolume(this IResou
/// <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<MongoDBServerResource> WithDataBindMount(this IResourceBuilder<MongoDBServerResource> builder, string source, bool isReadOnly = false)
=> builder.WithBindMount(source, "/data/db", isReadOnly);
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

return builder.WithBindMount(source, "/data/db", isReadOnly);
}

/// <summary>
/// Adds a bind mount for the init folder to a MongoDB container resource.
Expand All @@ -118,7 +137,12 @@ public static IResourceBuilder<MongoDBServerResource> WithDataBindMount(this IRe
/// <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<MongoDBServerResource> WithInitBindMount(this IResourceBuilder<MongoDBServerResource> builder, string source, bool isReadOnly = true)
=> builder.WithBindMount(source, "/docker-entrypoint-initdb.d", isReadOnly);
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

return builder.WithBindMount(source, "/docker-entrypoint-initdb.d", isReadOnly);
}

private static void ConfigureMongoExpressContainer(EnvironmentCallbackContext context, MongoDBServerResource resource)
{
Expand Down
24 changes: 18 additions & 6 deletions src/Aspire.Hosting.MongoDB/MongoDBDatabaseResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@ namespace Aspire.Hosting.ApplicationModel;
/// <summary>
/// A resource that represents a MongoDB database. This is a child resource of a <see cref="MongoDBServerResource"/>.
/// </summary>
/// <param name="name">The name of the resource.</param>
/// <param name="databaseName">The database name.</param>
/// <param name="parent">The MongoDB server resource associated with this database.</param>
public class MongoDBDatabaseResource(string name, string databaseName, MongoDBServerResource parent) : Resource(name), IResourceWithParent<MongoDBServerResource>, IResourceWithConnectionString
public class MongoDBDatabaseResource : Resource, IResourceWithParent<MongoDBServerResource>, IResourceWithConnectionString
{
/// <summary>
/// A resource that represents a MongoDB database. This is a child resource of a <see cref="MongoDBServerResource"/>.
/// </summary>
/// <param name="name">The name of the resource.</param>
/// <param name="databaseName">The database name.</param>
/// <param name="parent">The MongoDB server resource associated with this database.</param>
public MongoDBDatabaseResource(string name, string databaseName, MongoDBServerResource parent) : base(name)
{
ArgumentNullException.ThrowIfNull(databaseName);
ArgumentNullException.ThrowIfNull(parent);

Parent = parent;
DatabaseName = databaseName;
}

/// <summary>
/// Gets the connection string expression for the MongoDB database.
/// </summary>
Expand All @@ -20,10 +32,10 @@ public ReferenceExpression ConnectionStringExpression
/// <summary>
/// Gets the parent MongoDB container resource.
/// </summary>
public MongoDBServerResource Parent => parent;
public MongoDBServerResource Parent { get; }

/// <summary>
/// Gets the database name.
/// </summary>
public string DatabaseName { get; } = databaseName;
public string DatabaseName { get; }
}
203 changes: 203 additions & 0 deletions tests/Aspire.Hosting.MongoDB.Tests/MongoDBPublicApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
// 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.MongoDB.Tests;

public class MongoDBPublicApiTests
{
[Fact]
public void AddMongoDBContainerShouldThrowWhenBuilderIsNull()
{
IDistributedApplicationBuilder builder = null!;
const string name = "MongoDB";

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

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

[Fact]
public void AddMongoDBContainerShouldThrowWhenNameIsNull()
{
var builder = DistributedApplication.CreateBuilder([]);
string name = null!;

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

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

[Fact]
public void AddDatabaseShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<MongoDBServerResource> builder = null!;
const string name = "db1";

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

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

[Fact]
public void AddDatabaseShouldThrowWhenNameIsNull()
{
var builderResource = TestDistributedApplicationBuilder.Create();
var MongoDB = builderResource.AddMongoDB("MongoDB");
string name = null!;

var action = () => MongoDB.AddDatabase(name);

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

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

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

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

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

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

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

[Fact]
public void WithDataBindMountShouldThrowWhenSourceIsNull()
{
var builderResource = TestDistributedApplicationBuilder.Create();
var MongoDB = builderResource.AddMongoDB("MongoDB");
string source = null!;

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

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

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

var action = () => builder.WithInitBindMount("init.js");

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

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

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

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

[Fact]
public void WithInitBindMountShouldThrowWhenSourceIsNull()
{
var builderResource = TestDistributedApplicationBuilder.Create();
var MongoDB = builderResource.AddMongoDB("MongoDB");
string source = null!;

var action = () => MongoDB.WithInitBindMount(source);

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

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

var action = () => builder.WithHostPort(6601);

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

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

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

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

[Fact]
public void CtorMongoMongoDBDatabaseResourceShouldThrowWhenNameIsNull()
{
string name = null!;
var databaseName = "db1";
var parent = new MongoDBServerResource("mongodb");

var action = () => new MongoDBDatabaseResource(name, databaseName, parent);

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

[Fact]
public void CtorMongoMongoDBDatabaseResourceShouldThrowWhenDatabaseNameIsNull()
{
var name = "mongodb";
string databaseName = null!;
var parent = new MongoDBServerResource(name);

var action = () => new MongoDBDatabaseResource(name, databaseName, parent);

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

[Fact]
public void CtorMongoMongoDBDatabaseResourceShouldThrowWhenDatabaseParentIsNull()
{
var name = "mongodb";
var databaseName = "db1";
MongoDBServerResource parent = null!;

var action = () => new MongoDBDatabaseResource(name, databaseName, parent);

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

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

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

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