From 9f560b019aa755093ec3f28e6b9127a2de663a56 Mon Sep 17 00:00:00 2001 From: raman-m Date: Sat, 30 Sep 2023 15:14:25 +0300 Subject: [PATCH] Fix unit tests for ConsulServiceDiscoveryProvider --- src/Ocelot.Provider.Consul/Consul.cs | 10 ++-- .../ConsulServiceDiscoveryProviderTests.cs | 49 ++++--------------- 2 files changed, 15 insertions(+), 44 deletions(-) diff --git a/src/Ocelot.Provider.Consul/Consul.cs b/src/Ocelot.Provider.Consul/Consul.cs index 8c7caf0a49..d6281c5146 100644 --- a/src/Ocelot.Provider.Consul/Consul.cs +++ b/src/Ocelot.Provider.Consul/Consul.cs @@ -19,6 +19,8 @@ public Consul(ConsulRegistryConfiguration config, IOcelotLoggerFactory factory, _logger = factory.CreateLogger(); } + public const string ServiceValidationWarningFormat = "Unable to use service address: '{0}' and port: {1} as it is invalid for the service: '{2}'. Address must contain host only e.g. 'localhost', and port must be greater than 0."; + public async Task> GetAsync() { var queryResult = await _consul.Health.Service(_config.KeyOfServiceInConsul, string.Empty, true); @@ -27,8 +29,7 @@ public async Task> GetAsync() foreach (var serviceEntry in queryResult.Response) { - var address = serviceEntry.Service.Address; - var port = serviceEntry.Service.Port; + var service = serviceEntry.Service; if (IsValid(serviceEntry)) { var nodes = await _consul.Catalog.Nodes(); @@ -38,14 +39,13 @@ public async Task> GetAsync() } else { - var serviceNode = nodes.Response.FirstOrDefault(n => n.Address == address); + var serviceNode = nodes.Response.FirstOrDefault(n => n.Address == service.Address); services.Add(BuildService(serviceEntry, serviceNode)); } } else { - _logger.LogWarning( - $"Unable to use service address: {address} and Port: {port} as it is invalid. Address must contain host only e.g. 'localhost', and port must be greater than 0."); + _logger.LogWarning(string.Format(ServiceValidationWarningFormat, service.Address, service.Port, service.Service)); } } diff --git a/test/Ocelot.UnitTests/Consul/ConsulServiceDiscoveryProviderTests.cs b/test/Ocelot.UnitTests/Consul/ConsulServiceDiscoveryProviderTests.cs index 4a9daa41b3..23761a2122 100644 --- a/test/Ocelot.UnitTests/Consul/ConsulServiceDiscoveryProviderTests.cs +++ b/test/Ocelot.UnitTests/Consul/ConsulServiceDiscoveryProviderTests.cs @@ -123,7 +123,7 @@ public void should_not_return_services_with_invalid_address() .And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo)) .When(x => WhenIGetTheServices()) .Then(x => ThenTheCountIs(0)) - .And(x => ThenTheLoggerHasBeenCalledCorrectlyForInvalidAddress()) + .And(x => ThenTheLoggerHasBeenCalledCorrectlyWithValidationWarning(serviceEntryOne, serviceEntryTwo)) .BDDfy(); } @@ -158,7 +158,7 @@ public void should_not_return_services_with_empty_address() .And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo)) .When(x => WhenIGetTheServices()) .Then(x => ThenTheCountIs(0)) - .And(x => ThenTheLoggerHasBeenCalledCorrectlyForEmptyAddress()) + .And(x => ThenTheLoggerHasBeenCalledCorrectlyWithValidationWarning(serviceEntryOne, serviceEntryTwo)) .BDDfy(); } @@ -193,47 +193,18 @@ public void should_not_return_services_with_invalid_port() .And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo)) .When(x => WhenIGetTheServices()) .Then(x => ThenTheCountIs(0)) - .And(x => ThenTheLoggerHasBeenCalledCorrectlyForInvalidPorts()) + .And(x => ThenTheLoggerHasBeenCalledCorrectlyWithValidationWarning(serviceEntryOne, serviceEntryTwo)) .BDDfy(); } - private void ThenTheLoggerHasBeenCalledCorrectlyForInvalidAddress() + private void ThenTheLoggerHasBeenCalledCorrectlyWithValidationWarning(params ServiceEntry[] serviceEntries) { - _logger.Verify( - x => x.LogWarning( - "Unable to use service Address: http://localhost and Port: 50881 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"), - Times.Once); - - _logger.Verify( - x => x.LogWarning( - "Unable to use service Address: http://localhost and Port: 50888 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"), - Times.Once); - } - - private void ThenTheLoggerHasBeenCalledCorrectlyForEmptyAddress() - { - _logger.Verify( - x => x.LogWarning( - "Unable to use service Address: and Port: 50881 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"), - Times.Once); - - _logger.Verify( - x => x.LogWarning( - "Unable to use service Address: and Port: 50888 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"), - Times.Once); - } - - private void ThenTheLoggerHasBeenCalledCorrectlyForInvalidPorts() - { - _logger.Verify( - x => x.LogWarning( - "Unable to use service Address: localhost and Port: -1 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"), - Times.Once); - - _logger.Verify( - x => x.LogWarning( - "Unable to use service Address: localhost and Port: 0 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"), - Times.Once); + foreach (var entry in serviceEntries) + { + var service = entry.Service; + var expected = string.Format(ConsulProvider.ServiceValidationWarningFormat, service.Address, service.Port, service.Service); + _logger.Verify(x => x.LogWarning(expected), Times.Once); + } } private void ThenTheCountIs(int count)