From 9f960a08fc6c183485b866d185e1b7254487ba0d Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Tue, 15 Aug 2023 13:36:37 +0200 Subject: [PATCH] Added helpers for easier setup and fixed built-in checks for retry policy --- .../API/ApiSpecificationExtensions.cs | 67 +++++++++++++++---- src/Ogooreck/API/RetryPolicy.cs | 5 +- src/Ogooreck/API/TestBuilders.cs | 2 +- 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/Ogooreck/API/ApiSpecificationExtensions.cs b/src/Ogooreck/API/ApiSpecificationExtensions.cs index 984703a..4c551b8 100644 --- a/src/Ogooreck/API/ApiSpecificationExtensions.cs +++ b/src/Ogooreck/API/ApiSpecificationExtensions.cs @@ -12,6 +12,16 @@ public static class ApiSpecification //// GIVEN //// /////////////////// + public static GivenApiSpecificationBuilder Given(this ApiSpecification api, + params RequestTransform[] when) where TProgram : class => + api.Given(new RequestDefinition(when)); + + + public static GivenApiSpecificationBuilder Given(this ApiSpecification api, + string description, + params RequestTransform[] when) where TProgram : class => + api.Given(description, new RequestDefinition(when, description)); + /////////////////// //// WHEN //// /////////////////// @@ -103,11 +113,29 @@ public static Task And(this Task result, Task and) => result.ContinueWith(_ => and); public static Task And(this Task result) => - result.ContinueWith(_ => new GivenApiSpecificationBuilder(result.Result.TestContext, result.Result.CreateClient)); + result.ContinueWith( + _ => new GivenApiSpecificationBuilder(result.Result.TestContext, result.Result.CreateClient)); + + public static Task AndWhen( + this Task result, + string description, + params RequestTransform[] when + ) => + result.And().When(description, when); public static Task AndWhen(this Task result, params RequestTransform[] when) => result.And().When(when); + public static Task AndWhen( + this Task result, + string description, + Func when + ) => + result.ContinueWith(r => + new GivenApiSpecificationBuilder(r.Result.TestContext, r.Result.CreateClient) + .When(description, when(r.Result.Response)) + ); + public static Task AndWhen( this Task result, Func when @@ -116,6 +144,13 @@ Func when new GivenApiSpecificationBuilder(r.Result.TestContext, r.Result.CreateClient).When(when(r.Result.Response)) ); + public static Task When( + this Task result, + string description, + params RequestTransform[] when + ) => + result.ContinueWith(_ => result.Result.When(description, when)); + public static Task When( this Task result, params RequestTransform[] when @@ -124,7 +159,7 @@ params RequestTransform[] when public static Task Until( this Task when, - Func> check, + RetryCheck check, int maxNumberOfRetries = 5, int retryIntervalInMs = 1000 ) => @@ -216,14 +251,6 @@ public static Func> RESPONSE_BODY() => public static Func> CREATED_ID() => response => Task.FromResult(response.GetCreatedId()); - public static Func> RESPONSE_ETAG_IS(object eTag, - bool isWeak = true) => - async (response, ctx) => - { - await RESPONSE_ETAG_HEADER(eTag, isWeak)(response, ctx); - return true; - }; - public static ResponseAssert RESPONSE_ETAG_HEADER(object eTag, bool isWeak = true) => RESPONSE_HEADERS(headers => { @@ -260,15 +287,27 @@ public static ResponseAssert RESPONSE_HEADERS(params Action return ValueTask.CompletedTask; }; - public static Func> RESPONSE_SUCCEEDED() => - response => + ///////////////// + // UNTIL + //////////////// + + public static RetryCheck RESPONSE_ETAG_IS(object eTag, + bool isWeak = true) => + async (response, ctx) => + { + await RESPONSE_ETAG_HEADER(eTag, isWeak)(response, ctx); + return true; + }; + + public static RetryCheck RESPONSE_SUCCEEDED() => + (response, ctx) => { response.EnsureSuccessStatusCode(); return new ValueTask(true); }; - public static Func> RESPONSE_BODY_MATCHES(Func assert) => - async response => + public static RetryCheck RESPONSE_BODY_MATCHES(Func assert) => + async (response, ctx) => { response.EnsureSuccessStatusCode(); diff --git a/src/Ogooreck/API/RetryPolicy.cs b/src/Ogooreck/API/RetryPolicy.cs index c481cf8..65b7bca 100644 --- a/src/Ogooreck/API/RetryPolicy.cs +++ b/src/Ogooreck/API/RetryPolicy.cs @@ -4,7 +4,7 @@ namespace Ogooreck.API; #pragma warning disable CS1591 public record RetryPolicy( - Func> Check, + RetryCheck Check, int MaxNumberOfRetries = 5, int RetryIntervalInMs = 1000 ) @@ -44,3 +44,6 @@ public async Task Perform(Func RetryCheck(HttpResponseMessage responseMessage, TestContext testContext); + diff --git a/src/Ogooreck/API/TestBuilders.cs b/src/Ogooreck/API/TestBuilders.cs index 264a417..1795032 100644 --- a/src/Ogooreck/API/TestBuilders.cs +++ b/src/Ogooreck/API/TestBuilders.cs @@ -70,7 +70,7 @@ ApiTestStep when } public WhenApiSpecificationBuilder Until( - Func> check, + RetryCheck check, int maxNumberOfRetries = 5, int retryIntervalInMs = 1000 )