Skip to content

feat!: Invoke async message scenario factories on request #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ public interface IMessageScenarioBuilder
IMessageScenarioBuilder WithMetadata(dynamic metadata);

/// <summary>
/// Set the action of the scenario
/// Set the content factory of the scenario. The factory is invoked each time the scenario is required.
/// </summary>
/// <param name="factory">Content factory</param>
void WithContent(Func<dynamic> factory);

/// <summary>
/// Set the content of the scenario
/// Set the content factory of the scenario. The factory is invoked each time the scenario is required.
/// </summary>
/// <param name="factory">Content factory</param>
/// <param name="settings">Custom JSON serializer settings</param>
void WithContent(Func<dynamic> factory, JsonSerializerOptions settings);

/// <summary>
/// Set the action of the scenario
/// Set the content factory of the scenario. The factory is invoked each time the scenario is required.
/// </summary>
/// <param name="factory">Content factory</param>
Task WithContentAsync(Func<Task<dynamic>> factory);
void WithAsyncContent(Func<Task<dynamic>> factory);

/// <summary>
/// Set the content of the scenario
/// Set the content factory of the scenario. The factory is invoked each time the scenario is required.
/// </summary>
/// <param name="factory">Content factory</param>
/// <param name="settings">Custom JSON serializer settings</param>
Task WithContentAsync(Func<Task<dynamic>> factory, JsonSerializerOptions settings);
void WithAsyncContent(Func<Task<dynamic>> factory, JsonSerializerOptions settings);
}
}
13 changes: 2 additions & 11 deletions src/PactNet.Abstractions/Verifier/Messaging/IMessageScenarios.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PactNet.Verifier.Messaging
{
Expand All @@ -22,19 +21,11 @@ public interface IMessageScenarios
IMessageScenarios Add(string description, Func<dynamic> factory);

/// <summary>
/// Add a message scenario
/// Add a message scenario by configuring a scenario builder
/// </summary>
/// <param name="description">Scenario description</param>
/// <param name="configure">Scenario configure</param>
/// <returns></returns>
/// <returns>Fluent builder</returns>
IMessageScenarios Add(string description, Action<IMessageScenarioBuilder> configure);

/// <summary>
/// Add a message scenario
/// </summary>
/// <param name="description">Scenario description</param>
/// <param name="configure">Scenario configure</param>
/// <returns></returns>
IMessageScenarios Add(string description, Func<IMessageScenarioBuilder, Task> configure);
}
}
26 changes: 17 additions & 9 deletions src/PactNet/Verifier/Messaging/MessageScenarioBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace PactNet.Verifier.Messaging
internal class MessageScenarioBuilder : IMessageScenarioBuilder
{
private readonly string description;

private Func<dynamic> factory;
private dynamic metadata = new { ContentType = "application/json" };
private JsonSerializerOptions settings;
Expand Down Expand Up @@ -55,25 +56,32 @@ public void WithContent(Func<dynamic> factory, JsonSerializerOptions settings)
}

/// <summary>
/// Set the action of the scenario
/// Set the content factory of the scenario. The factory is invoked each time the scenario is required.
/// </summary>
/// <param name="factory">Content factory</param>
public async Task WithContentAsync(Func<Task<dynamic>> factory)
public void WithAsyncContent(Func<Task<dynamic>> factory)
{
dynamic value = await factory();
this.factory = () => value;
if (factory == null)
{
throw new ArgumentNullException(nameof(factory));
}

this.WithContent(() => factory().GetAwaiter().GetResult());
}

/// <summary>
/// Set the content of the scenario
/// Set the content factory of the scenario. The factory is invoked each time the scenario is required.
/// </summary>
/// <param name="factory">Content factory</param>
/// <param name="settings">Custom JSON serializer settings</param>
public async Task WithContentAsync(Func<Task<dynamic>> factory, JsonSerializerOptions settings)
public void WithAsyncContent(Func<Task<dynamic>> factory, JsonSerializerOptions settings)
{
dynamic value = await factory();
this.factory = () => value;
this.settings = settings;
if (factory == null)
{
throw new ArgumentNullException(nameof(factory));
}

this.WithContent(() => factory().GetAwaiter().GetResult(), settings);
}

/// <summary>
Expand Down
22 changes: 2 additions & 20 deletions src/PactNet/Verifier/Messaging/MessageScenarios.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace PactNet.Verifier.Messaging
{
Expand Down Expand Up @@ -44,11 +43,11 @@ public IMessageScenarios Add(string description, Func<dynamic> factory)
}

/// <summary>
/// Add a message scenario
/// Add a message scenario by configuring a scenario builder
/// </summary>
/// <param name="description">Scenario description</param>
/// <param name="configure">Scenario configure</param>
/// <returns></returns>
/// <returns>Fluent builder</returns>
public IMessageScenarios Add(string description, Action<IMessageScenarioBuilder> configure)
{
var builder = new MessageScenarioBuilder(description);
Expand All @@ -59,22 +58,5 @@ public IMessageScenarios Add(string description, Action<IMessageScenarioBuilder>

return this;
}

/// <summary>
/// Add a message scenario
/// </summary>
/// <param name="description">Scenario description</param>
/// <param name="configure">Scenario configure</param>
/// <returns></returns>
public IMessageScenarios Add(string description, Func<IMessageScenarioBuilder, Task> configure)
{
var builder = new MessageScenarioBuilder(description);
configure?.Invoke(builder).GetAwaiter().GetResult();

Scenario scenario = builder.Build();
this.scenarios.Add(description, scenario);

return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ public void WithContent_WithCustomSettings_SetsSettings()
}

[Fact]
public async Task WithContentAsync_WhenCalled_SetsContent()
public void WithAsyncContent_WhenCalled_SetsContent()
{
dynamic expected = new { Foo = 42 };

await this.builder.WithContentAsync(() => Task.FromResult<dynamic>(expected));
this.builder.WithAsyncContent(() => Task.FromResult<dynamic>(expected));
object actual = this.builder.Build().Invoke();

actual.Should().Be(expected);
}

[Fact]
public async Task WithContentAsync_WithCustomSettings_SetsSettings()
public void WithAsyncContent_WithCustomSettings_SetsSettings()
{
var expected = new JsonSerializerOptions();

await this.builder.WithContentAsync(() => Task.FromResult<dynamic>(new { Foo = "Bar" }), expected);
this.builder.WithAsyncContent(() => Task.FromResult<dynamic>(new { Foo = "Bar" }), expected);
var actual = this.builder.Build().JsonSettings;

actual.Should().Be(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Add_AsyncBuilder_AddsScenario()
Func<Task<dynamic>> factory = () => Task.FromResult<dynamic>(new { Foo = 42 });
JsonSerializerOptions settings = new JsonSerializerOptions();

this.scenarios.Add("description", async builder => await builder.WithMetadata(metadata).WithContentAsync(factory, settings));
this.scenarios.Add("description", builder => builder.WithMetadata(metadata).WithAsyncContent(factory, settings));

this.scenarios.Scenarios.Should().BeEquivalentTo(new Dictionary<string, Scenario>
{
Expand Down