diff --git a/src/Components/Aspire.MongoDB.Driver/AspireMongoDBDriverExtensions.cs b/src/Components/Aspire.MongoDB.Driver/AspireMongoDBDriverExtensions.cs index e8dba2120f..b3d54cbbc6 100644 --- a/src/Components/Aspire.MongoDB.Driver/AspireMongoDBDriverExtensions.cs +++ b/src/Components/Aspire.MongoDB.Driver/AspireMongoDBDriverExtensions.cs @@ -34,7 +34,12 @@ public static void AddMongoDBClient( string connectionName, Action? configureSettings = null, Action? configureClientSettings = null) - => builder.AddMongoDBClient(DefaultConfigSectionName, configureSettings, configureClientSettings, connectionName, serviceKey: null); + { + ArgumentNullException.ThrowIfNull(builder); + ArgumentException.ThrowIfNullOrEmpty(connectionName); + + builder.AddMongoDBClient(DefaultConfigSectionName, configureSettings, configureClientSettings, connectionName, serviceKey: null); + } /// /// Registers and instances for connecting MongoDB database with MongoDB.Driver client. @@ -52,6 +57,7 @@ public static void AddKeyedMongoDBClient( Action? configureSettings = null, Action? configureClientSettings = null) { + ArgumentNullException.ThrowIfNull(builder); ArgumentException.ThrowIfNullOrEmpty(name); builder.AddMongoDBClient( diff --git a/tests/Aspire.MongoDB.Driver.Tests/MongoDBDriverPublicApiTests.cs b/tests/Aspire.MongoDB.Driver.Tests/MongoDBDriverPublicApiTests.cs new file mode 100644 index 0000000000..a047463a29 --- /dev/null +++ b/tests/Aspire.MongoDB.Driver.Tests/MongoDBDriverPublicApiTests.cs @@ -0,0 +1,82 @@ +// 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.MongoDB.Driver.Tests; + +public class MongoDBClientPublicApiTests +{ + [Fact] + public void AddMongoDBClientShouldThrowWhenBuilderIsNull() + { + IHostApplicationBuilder builder = null!; + var connectionName = "mongodb"; + + var action = () => builder.AddMongoDBClient(connectionName); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); + } + + [Fact] + public void AddMongoDBClientShouldThrowWhenNameIsNull() + { + var builder = Host.CreateEmptyApplicationBuilder(null); + string connectionName = null!; + + var action = () => builder.AddMongoDBClient(connectionName); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(connectionName), exception.ParamName); + } + + [Fact] + public void AddMongoDBClientShouldThrowWhenNameIsEmpty() + { + var builder = Host.CreateEmptyApplicationBuilder(null); + string connectionName = ""; + + var action = () => builder.AddMongoDBClient(connectionName); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(connectionName), exception.ParamName); + } + + [Fact] + public void AddKeyedMongoDBClientShouldThrowWhenBuilderIsNull() + { + IHostApplicationBuilder builder = null!; + var connectionName = "mongodb"; + + var action = () => builder.AddKeyedMongoDBClient(connectionName); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); + } + + [Fact] + public void AddKeyedMongoDBClientShouldThrowWhenNameIsNull() + { + var builder = Host.CreateEmptyApplicationBuilder(null); + string name = null!; + + var action = () => builder.AddKeyedMongoDBClient(name); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); + } + + [Fact] + public void AddKeyedMongoDBClientShouldThrowWhenNameIsEmpty() + { + var builder = Host.CreateEmptyApplicationBuilder(null); + string name = ""; + + var action = () => builder.AddKeyedMongoDBClient(name); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); + } +}