diff --git a/src/Aspire.Hosting.Elasticsearch/ElasticsearchBuilderExtensions.cs b/src/Aspire.Hosting.Elasticsearch/ElasticsearchBuilderExtensions.cs index a719d3f62a..c746ffdeaf 100644 --- a/src/Aspire.Hosting.Elasticsearch/ElasticsearchBuilderExtensions.cs +++ b/src/Aspire.Hosting.Elasticsearch/ElasticsearchBuilderExtensions.cs @@ -44,6 +44,9 @@ public static IResourceBuilder AddElasticsearch( IResourceBuilder? password = null, int? port = null) { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(name); + var passwordParameter = password?.Resource ?? ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $"{name}-password"); var elasticsearch = new ElasticsearchResource(name, passwordParameter); @@ -82,7 +85,11 @@ public static IResourceBuilder AddElasticsearch( /// /// public static IResourceBuilder WithDataVolume(this IResourceBuilder builder, string? name = null) - => builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/usr/share/elasticsearch/data"); + { + ArgumentNullException.ThrowIfNull(builder); + + return builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/usr/share/elasticsearch/data"); + } /// /// Adds a bind mount for the data folder to a Elasticsearch container resource. @@ -105,6 +112,10 @@ public static IResourceBuilder WithDataVolume(this IResou /// /// public static IResourceBuilder WithDataBindMount(this IResourceBuilder builder, string source) - => builder.WithBindMount(source, "/usr/share/elasticsearch/data"); + { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(source); + return builder.WithBindMount(source, "/usr/share/elasticsearch/data"); + } } diff --git a/tests/Aspire.Hosting.Elasticsearch.Tests/ElasticsearchPublicApiTests.cs b/tests/Aspire.Hosting.Elasticsearch.Tests/ElasticsearchPublicApiTests.cs new file mode 100644 index 0000000000..c6f55a77ec --- /dev/null +++ b/tests/Aspire.Hosting.Elasticsearch.Tests/ElasticsearchPublicApiTests.cs @@ -0,0 +1,97 @@ +// 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 Xunit; + +namespace Aspire.Hosting.Elasticsearch.Tests; + +public class ElasticsearchPublicApiTests +{ + [Fact] + public void AddElasticsearchContainerShouldThrowWhenBuilderIsNull() + { + IDistributedApplicationBuilder builder = null!; + const string name = "Elasticsearch"; + + var action = () => builder.AddElasticsearch(name); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); + } + + [Fact] + public void AddElasticsearchContainerShouldThrowWhenNameIsNull() + { + IDistributedApplicationBuilder builder = new DistributedApplicationBuilder([]); + string name = null!; + + var action = () => builder.AddElasticsearch(name); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void WithDataShouldThrowWhenBuilderIsNull(bool useVolume) + { + IResourceBuilder builder = null!; + + Func>? action = null; + + if (useVolume) + { + action = () => builder.WithDataVolume(); + } + else + { + const string source = "/data"; + + action = () => builder.WithDataBindMount(source); + } + + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); + } + + [Fact] + public void WithDataBindMountShouldThrowWhenSourceIsNull() + { + var builder = new DistributedApplicationBuilder([]); + var resourceBuilder = builder.AddElasticsearch("Elasticsearch"); + + string source = null!; + + var action = () => resourceBuilder.WithDataBindMount(source); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(source), exception.ParamName); + } + + [Fact] + public void CtorElasticsearchResourceShouldThrowWhenNameIsNull() + { + var builder = new DistributedApplicationBuilder([]); + builder.Configuration["Parameters:Password"] = "p@ssw0rd"; + var password = builder.AddParameter("Password"); + const string name = null!; + + var action = () => new ElasticsearchResource(name, password.Resource); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); + } + [Fact] + public void CtorElasticsearchResourceShouldThrowWhenPasswordIsNull() + { + const string name = "Elasticsearch"; + ParameterResource password = null!; + + var action = () => new ElasticsearchResource(name, password); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(password), exception.ParamName); + } +}