Skip to content

Commit

Permalink
Adding public API test coverage for Aspire.Elastic.Clients.Elasticsea…
Browse files Browse the repository at this point in the history
…rch (#5169)
  • Loading branch information
Alirexaa authored Aug 6, 2024
1 parent 5373547 commit 0ac9c4c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ public static void AddElasticsearchClient(
string connectionName,
Action<ElasticClientsElasticsearchSettings>? configureSettings = null,
Action<ElasticsearchClientSettings>? configureClientSettings = null
) => builder.AddElasticsearchClient(DefaultConfigSectionName, configureSettings, configureClientSettings, connectionName, serviceKey: null);
)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(connectionName);

builder.AddElasticsearchClient(DefaultConfigSectionName, configureSettings, configureClientSettings, connectionName, serviceKey: null);
}

/// <summary>
/// Registers <see cref="ElasticsearchClient"/> instance for connecting to Elasticsearch with Elastic.Clients.Elasticsearch client.
Expand All @@ -47,7 +53,9 @@ public static void AddKeyedElasticsearchClient(
Action<ElasticClientsElasticsearchSettings>? configureSettings = null,
Action<ElasticsearchClientSettings>? configureClientSettings = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(name);

builder.AddElasticsearchClient(
$"{DefaultConfigSectionName}:{name}",
configureSettings,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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.Elastic.Clients.Elasticsearch.Tests;

public class ElasticsearchClientPublicApiTests
{
[Fact]
public void AddElasticsearchClientShouldThrowWhenBuilderIsNull()
{
IHostApplicationBuilder builder = null!;

var connectionName = "elasticseach";

var action = () => builder.AddElasticsearchClient(connectionName);

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

[Fact]
public void AddElasticsearchClientShouldThrowWhenNameIsNull()
{
var builder = Host.CreateEmptyApplicationBuilder(null);

string connectionName = null!;

var action = () => builder.AddElasticsearchClient(connectionName);

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

[Fact]
public void AddElasticsearchClientShouldThrowWhenNameIsEmpty()
{
var builder = Host.CreateEmptyApplicationBuilder(null);

string connectionName = "";

var action = () => builder.AddElasticsearchClient(connectionName);

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

[Fact]
public void AddKeyedElasticsearchClientShouldThrowWhenBuilderIsNull()
{
IHostApplicationBuilder builder = null!;

var connectionName = "elasticseach";

var action = () => builder.AddKeyedElasticsearchClient(connectionName);

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

[Fact]
public void AddKeyedElasticsearchClientShouldThrowWhenNameIsNull()
{
var builder = Host.CreateEmptyApplicationBuilder(null);

string name = null!;

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

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

[Fact]
public void AddKeyedElasticsearchClientShouldThrowWhenNameIsEmpty()
{
var builder = Host.CreateEmptyApplicationBuilder(null);

string name = "";

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

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

0 comments on commit 0ac9c4c

Please sign in to comment.