Skip to content

Commit

Permalink
Adding public API test coverage for Aspire.Hosting.Elasticsearch (#5119)
Browse files Browse the repository at this point in the history
* Add tests and argument null exception

* Apply suggested changes
  • Loading branch information
Alirexaa authored Aug 21, 2024
1 parent 57f5e43 commit 319bcbb
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Aspire.Hosting.Elasticsearch/ElasticsearchBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public static IResourceBuilder<ElasticsearchResource> AddElasticsearch(
IResourceBuilder<ParameterResource>? 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);
Expand Down Expand Up @@ -82,7 +85,11 @@ public static IResourceBuilder<ElasticsearchResource> AddElasticsearch(
/// </code>
/// </example>
public static IResourceBuilder<ElasticsearchResource> WithDataVolume(this IResourceBuilder<ElasticsearchResource> 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");
}

/// <summary>
/// Adds a bind mount for the data folder to a Elasticsearch container resource.
Expand All @@ -105,6 +112,10 @@ public static IResourceBuilder<ElasticsearchResource> WithDataVolume(this IResou
/// </code>
/// </example>
public static IResourceBuilder<ElasticsearchResource> WithDataBindMount(this IResourceBuilder<ElasticsearchResource> builder, string source)
=> builder.WithBindMount(source, "/usr/share/elasticsearch/data");
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

return builder.WithBindMount(source, "/usr/share/elasticsearch/data");
}
}
Original file line number Diff line number Diff line change
@@ -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<ArgumentNullException>(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<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void WithDataShouldThrowWhenBuilderIsNull(bool useVolume)
{
IResourceBuilder<ElasticsearchResource> builder = null!;

Func<IResourceBuilder<ElasticsearchResource>>? action = null;

if (useVolume)
{
action = () => builder.WithDataVolume();
}
else
{
const string source = "/data";

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

var exception = Assert.Throws<ArgumentNullException>(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<ArgumentNullException>(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<ArgumentNullException>(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<ArgumentNullException>(action);
Assert.Equal(nameof(password), exception.ParamName);
}
}

0 comments on commit 319bcbb

Please sign in to comment.