From 246fb548c066a9cda00a07fa358ca9221e9d243a Mon Sep 17 00:00:00 2001 From: Simon Hornby Date: Mon, 8 Jul 2024 16:37:38 +0200 Subject: [PATCH] chore: make metrics and regsitration tests actually useful (#227) --- .../Communication/BaseUnleashApiClientTest.cs | 52 ------------------ .../Communication/MockHttpClient.cs | 37 +++++++++++++ .../UnleashApiClient_FetchToggles_Tests.cs | 53 ------------------- .../UnleashApiClient_RegisterClient_Tests.cs | 32 ++++++----- .../UnleashApiClient_SendMetrics_Tests.cs | 34 ++++++------ tests/Unleash.Tests/Unleash.Tests.csproj | 1 + 6 files changed, 75 insertions(+), 134 deletions(-) delete mode 100644 tests/Unleash.Tests/Communication/BaseUnleashApiClientTest.cs create mode 100644 tests/Unleash.Tests/Communication/MockHttpClient.cs delete mode 100644 tests/Unleash.Tests/Communication/UnleashApiClient_FetchToggles_Tests.cs diff --git a/tests/Unleash.Tests/Communication/BaseUnleashApiClientTest.cs b/tests/Unleash.Tests/Communication/BaseUnleashApiClientTest.cs deleted file mode 100644 index dc7413ec..00000000 --- a/tests/Unleash.Tests/Communication/BaseUnleashApiClientTest.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using NUnit.Framework; -using NUnit.Framework.Internal; -using Unleash.Communication; -using Unleash.Events; -using Unleash.Internal; -using Unleash.Serialization; - -namespace Unleash.Tests.Communication -{ - public abstract class BaseUnleashApiClientTest - { - private static IUnleashApiClient CreateApiClient() - { - var apiUri = new Uri("http://unleash.herokuapp.com/api/"); - - var jsonSerializer = new DynamicNewtonsoftJsonSerializer(); - jsonSerializer.TryLoad(); - - var httpClientFactory = new DefaultHttpClientFactory(); - - var requestHeaders = new UnleashApiClientRequestHeaders - { - AppName = "api-test-client", - CustomHttpHeaders = new Dictionary() - { - // "Test" token from 21.10.2021 - { "Authorization", "*:default.77c45b703a681983b714fee87e575a823bfb1fd0ab282d9399647243" } - }, - CustomHttpHeaderProvider = null - }; - - var httpClient = httpClientFactory.Create(apiUri); - var client = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders, new EventCallbackConfig()); - return client; - } - - internal IUnleashApiClient api - { - get => TestExecutionContext.CurrentContext.CurrentTest.Properties.Get("api") as IUnleashApiClient; - set => TestExecutionContext.CurrentContext.CurrentTest.Properties.Set("api", value); - } - - [SetUp] - public void SetupTest() - { - api = CreateApiClient(); - } - } -} \ No newline at end of file diff --git a/tests/Unleash.Tests/Communication/MockHttpClient.cs b/tests/Unleash.Tests/Communication/MockHttpClient.cs new file mode 100644 index 00000000..b5969e5b --- /dev/null +++ b/tests/Unleash.Tests/Communication/MockHttpClient.cs @@ -0,0 +1,37 @@ +using Unleash.Communication; +using Unleash.Internal; +using Unleash.Serialization; +using RichardSzalay.MockHttp; + +namespace Unleash.Tests.Communication +{ + internal static class MockHttpClient + { + internal static Tuple MakeMockClient(string url) + { + DynamicNewtonsoftJsonSerializer jsonSerializer = new DynamicNewtonsoftJsonSerializer(); + jsonSerializer.TryLoad(); + + var mockHttp = new MockHttpMessageHandler(); + + var httpClient = new HttpClient(mockHttp) + { + BaseAddress = new Uri(url) + }; + + var requestHeaders = new UnleashApiClientRequestHeaders + { + AppName = "api-test-client", + CustomHttpHeaders = new Dictionary() + { + { "Authorization", "*:default.some-mock-hash" } + }, + CustomHttpHeaderProvider = null + }; + + var unleashClient = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders, new EventCallbackConfig()); + return new Tuple(mockHttp, unleashClient); + + } + } +} \ No newline at end of file diff --git a/tests/Unleash.Tests/Communication/UnleashApiClient_FetchToggles_Tests.cs b/tests/Unleash.Tests/Communication/UnleashApiClient_FetchToggles_Tests.cs deleted file mode 100644 index e0a3c68b..00000000 --- a/tests/Unleash.Tests/Communication/UnleashApiClient_FetchToggles_Tests.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Diagnostics; -using System.Threading; -using System.Threading.Tasks; -using FluentAssertions; -using NUnit.Framework; - -namespace Unleash.Tests.Communication -{ - public class UnleashApiClient_FetchToggles_Tests : BaseUnleashApiClientTest - { - [Test] - [Ignore("Requires a valid accesstoken")] - public async Task Success() - { - var etag = ""; // first request - var result1 = await api.FetchToggles(etag, CancellationToken.None); - - result1.HasChanged.Should().BeTrue(); - result1.ToggleCollection.Should().NotBeNull(); - - result1.ToggleCollection.TraceToJson(); - - // - // NB: Could fail below if server content has changed after previous call. Just try again.. - // - - // With etag from previous response - var result2 = await api.FetchToggles(result1.Etag, CancellationToken.None); - result2.HasChanged.Should().BeFalse(); - result2.ToggleCollection.Should().BeNull(); - result2.Etag.Should().Be(result1.Etag); - } - - [Test] - [Ignore("Requires a valid accesstoken")] - public async Task Timer_Test() - { - // Warmup - await api.FetchToggles("", CancellationToken.None); - - var stopwatch = Stopwatch.StartNew(); - - var result = await api.FetchToggles("etag", CancellationToken.None); - - stopwatch.Stop(); - Console.WriteLine("Elapsed: " + stopwatch.ElapsedMilliseconds + "ms"); - - result.Should().NotBeNull(); - stopwatch.ElapsedMilliseconds.Should().BeLessOrEqualTo(1000); - } - } -} \ No newline at end of file diff --git a/tests/Unleash.Tests/Communication/UnleashApiClient_RegisterClient_Tests.cs b/tests/Unleash.Tests/Communication/UnleashApiClient_RegisterClient_Tests.cs index 4128cce9..ee59af34 100644 --- a/tests/Unleash.Tests/Communication/UnleashApiClient_RegisterClient_Tests.cs +++ b/tests/Unleash.Tests/Communication/UnleashApiClient_RegisterClient_Tests.cs @@ -1,34 +1,38 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using FluentAssertions; -using NUnit.Framework; +using NUnit.Framework; +using RichardSzalay.MockHttp; using Unleash.Metrics; namespace Unleash.Tests.Communication { - public class UnleashApiClient_RegisterClient_Tests : BaseUnleashApiClientTest + public class UnleashApiClient_RegisterClient_Tests { + private const string BASE_URL = "http://some-mock-url/api/client"; + [Test] - [Ignore("Requires a valid accesstoken")] public async Task RegisterClient_Success() { + var (mockHttp, client) = MockHttpClient.MakeMockClient(BASE_URL); + + mockHttp.When($"{BASE_URL}/register") + .WithPartialContent("\"appName\":\"SomeTestAppName\"") + .WithPartialContent("\"interval\":1000") + .WithPartialContent("\"sdkVersion\":\"1.0.1\"") + .WithPartialContent("\"strategies\":[\"abc\"]") + .Respond("application/json", "{ 'status': 'ok' }"); + var clientRegistration = new ClientRegistration() { - AppName = GetType().Name, - InstanceId = "instance1", + AppName = "SomeTestAppName", Interval = 1000, - SdkVersion = "sdk101", - Started = DateTimeOffset.UtcNow, + SdkVersion = "1.0.1", Strategies = new List { "abc" } }; - var result = await api.RegisterClient(clientRegistration, CancellationToken.None); - result.Should().Be(true); + var result = await client.RegisterClient(clientRegistration, CancellationToken.None); + Assert.IsTrue(result); } } } \ No newline at end of file diff --git a/tests/Unleash.Tests/Communication/UnleashApiClient_SendMetrics_Tests.cs b/tests/Unleash.Tests/Communication/UnleashApiClient_SendMetrics_Tests.cs index 6136864f..40fde266 100644 --- a/tests/Unleash.Tests/Communication/UnleashApiClient_SendMetrics_Tests.cs +++ b/tests/Unleash.Tests/Communication/UnleashApiClient_SendMetrics_Tests.cs @@ -1,28 +1,32 @@ -using System.Threading; -using System.Threading.Tasks; -using FluentAssertions; -using NUnit.Framework; +using NUnit.Framework; using Unleash.Metrics; +using RichardSzalay.MockHttp; +using NUnit.Framework.Internal; namespace Unleash.Tests.Communication { - public class UnleashApiClient_SendMetrics_Tests : BaseUnleashApiClientTest + public class UnleashApiClient_SendMetrics_Tests { + private const string BASE_URL = "http://some-mock-url/api/client"; + [Test] - [Ignore("Requires a valid accesstoken")] public async Task SendMetrics_Success() { + var (mockHttp, client) = MockHttpClient.MakeMockClient(BASE_URL); + + mockHttp.When($"{BASE_URL}/metrics") + .WithPartialContent("appName") + .WithPartialContent("instanceId") + .WithPartialContent("\"no\":0") + .WithPartialContent("\"yes\":1") + .Respond("application/json", "{ 'status': 'ok' }"); + var metricsBucket = new ThreadSafeMetricsBucket(); - metricsBucket.RegisterCount("Demo123", true); - metricsBucket.RegisterCount("Demo123", false); - - var result = await api.SendMetrics(metricsBucket, CancellationToken.None); - result.Should().Be(true); - // Check result: - // http://unleash.herokuapp.com/#/features/view/Demo123 - // http://unleash.herokuapp.com/api/admin/metrics/feature-toggles + metricsBucket.RegisterCount("someTestToggle", true); + + var result = await client.SendMetrics(metricsBucket, CancellationToken.None); + Assert.IsTrue(result); } - } } \ No newline at end of file diff --git a/tests/Unleash.Tests/Unleash.Tests.csproj b/tests/Unleash.Tests/Unleash.Tests.csproj index b0a40623..b26e8e53 100644 --- a/tests/Unleash.Tests/Unleash.Tests.csproj +++ b/tests/Unleash.Tests/Unleash.Tests.csproj @@ -21,6 +21,7 @@ +