From 0ac9c4cb052ab1be09bf62c90d9658e8bbb2b72b Mon Sep 17 00:00:00 2001 From: Alireza Baloochi Date: Wed, 7 Aug 2024 01:33:57 +0330 Subject: [PATCH] Adding public API test coverage for Aspire.Elastic.Clients.Elasticsearch (#5169) --- ...reElasticClientsElasticsearchExtensions.cs | 10 ++- .../ElasticsearchClientPublicApiTests.cs | 88 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tests/Aspire.Elastic.Clients.Elasticsearch.Tests/ElasticsearchClientPublicApiTests.cs diff --git a/src/Components/Aspire.Elastic.Clients.Elasticsearch/AspireElasticClientsElasticsearchExtensions.cs b/src/Components/Aspire.Elastic.Clients.Elasticsearch/AspireElasticClientsElasticsearchExtensions.cs index 1dd731cb0b..4b85d66ea9 100644 --- a/src/Components/Aspire.Elastic.Clients.Elasticsearch/AspireElasticClientsElasticsearchExtensions.cs +++ b/src/Components/Aspire.Elastic.Clients.Elasticsearch/AspireElasticClientsElasticsearchExtensions.cs @@ -31,7 +31,13 @@ public static void AddElasticsearchClient( string connectionName, Action? configureSettings = null, Action? configureClientSettings = null - ) => builder.AddElasticsearchClient(DefaultConfigSectionName, configureSettings, configureClientSettings, connectionName, serviceKey: null); + ) + { + ArgumentNullException.ThrowIfNull(builder); + ArgumentException.ThrowIfNullOrEmpty(connectionName); + + builder.AddElasticsearchClient(DefaultConfigSectionName, configureSettings, configureClientSettings, connectionName, serviceKey: null); + } /// /// Registers instance for connecting to Elasticsearch with Elastic.Clients.Elasticsearch client. @@ -47,7 +53,9 @@ public static void AddKeyedElasticsearchClient( Action? configureSettings = null, Action? configureClientSettings = null) { + ArgumentNullException.ThrowIfNull(builder); ArgumentException.ThrowIfNullOrEmpty(name); + builder.AddElasticsearchClient( $"{DefaultConfigSectionName}:{name}", configureSettings, diff --git a/tests/Aspire.Elastic.Clients.Elasticsearch.Tests/ElasticsearchClientPublicApiTests.cs b/tests/Aspire.Elastic.Clients.Elasticsearch.Tests/ElasticsearchClientPublicApiTests.cs new file mode 100644 index 0000000000..5adfb45c96 --- /dev/null +++ b/tests/Aspire.Elastic.Clients.Elasticsearch.Tests/ElasticsearchClientPublicApiTests.cs @@ -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(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(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(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(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(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(action); + Assert.Equal(nameof(name), exception.ParamName); + } +}