// Copyright © WireMock.Net
using System;
using System.Threading.Tasks;
using Stef.Validation;
using WireMock.Matchers.Request;
using WireMock.Models;
using WireMock.ResponseProviders;
using WireMock.Settings;
namespace WireMock;
///
/// The Mapping.
///
public class Mapping : IMapping
{
///
public Guid Guid { get; }
///
public DateTime? UpdatedAt { get; set; }
///
public string? Title { get; }
///
public string? Description { get; }
///
public string? Path { get; set; }
///
public int Priority { get; }
///
public string? Scenario { get; private set; }
///
public string? ExecutionConditionState { get; }
///
public string? NextState { get; }
///
public int? StateTimes { get; }
///
public IRequestMatcher RequestMatcher { get; }
///
public IResponseProvider Provider { get; }
///
public WireMockServerSettings Settings { get; }
///
public bool IsStartState => Scenario == null || Scenario != null && NextState != null && ExecutionConditionState == null;
///
public bool IsAdminInterface => Provider is DynamicResponseProvider or DynamicAsyncResponseProvider or ProxyAsyncResponseProvider;
///
public bool IsProxy => Provider is ProxyAsyncResponseProvider;
///
public bool LogMapping => Provider is not (DynamicResponseProvider or DynamicAsyncResponseProvider);
///
public IWebhook[]? Webhooks { get; }
///
public bool? UseWebhooksFireAndForget { get; }
///
public ITimeSettings? TimeSettings { get; }
///
public object? Data { get; }
///
public double? Probability { get; private set; }
///
public IdOrTexts? ProtoDefinition { get; private set; }
///
/// Initializes a new instance of the class.
///
/// The unique identifier.
/// The datetime when this mapping was created.
/// The unique title (can be null).
/// The description (can be null).
/// The full file path from this mapping title (can be null).
/// The WireMockServerSettings.
/// The request matcher.
/// The provider.
/// The priority for this mapping.
/// The scenario. [Optional]
/// State in which the current mapping can occur. [Optional]
/// The next state which will occur after the current mapping execution. [Optional]
/// Only when the current state is executed this number, the next state which will occur. [Optional]
/// The Webhooks. [Optional]
/// Use Fire and Forget for the defined webhook(s). [Optional]
/// The TimeSettings. [Optional]
/// The data object. [Optional]
public Mapping
(
Guid guid,
DateTime updatedAt,
string? title,
string? description,
string? path,
WireMockServerSettings settings,
IRequestMatcher requestMatcher,
IResponseProvider provider,
int priority,
string? scenario,
string? executionConditionState,
string? nextState,
int? stateTimes,
IWebhook[]? webhooks,
bool? useWebhooksFireAndForget,
ITimeSettings? timeSettings,
object? data
)
{
Guid = guid;
UpdatedAt = updatedAt;
Title = title;
Description = description;
Path = path;
Settings = settings;
RequestMatcher = requestMatcher;
Provider = provider;
Priority = priority;
Scenario = scenario;
ExecutionConditionState = executionConditionState;
NextState = nextState;
StateTimes = stateTimes;
Webhooks = webhooks;
UseWebhooksFireAndForget = useWebhooksFireAndForget;
TimeSettings = timeSettings;
Data = data;
}
///
public Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IRequestMessage requestMessage)
{
return Provider.ProvideResponseAsync(this, requestMessage, Settings);
}
///
public IRequestMatchResult GetRequestMatchResult(IRequestMessage requestMessage, string? nextState)
{
var result = new RequestMatchResult();
RequestMatcher.GetMatchingScore(requestMessage, result);
// Only check state if Scenario is defined
if (Scenario != null)
{
var matcher = new RequestMessageScenarioAndStateMatcher(nextState, ExecutionConditionState);
matcher.GetMatchingScore(requestMessage, result);
//// If ExecutionConditionState is null, this means that request is the start from a scenario. So just return.
//if (ExecutionConditionState != null)
//{
// // ExecutionConditionState is not null, so get score for matching with the nextState.
// var matcher = new RequestMessageScenarioAndStateMatcher(nextState, ExecutionConditionState);
// matcher.GetMatchingScore(requestMessage, result);
//}
}
return result;
}
///
public IMapping WithProbability(double probability)
{
Probability = Guard.NotNull(probability);
return this;
}
///
public IMapping WithScenario(string scenario)
{
Scenario = Guard.NotNullOrWhiteSpace(scenario);
return this;
}
///
public IMapping WithProtoDefinition(IdOrTexts protoDefinition)
{
ProtoDefinition = protoDefinition;
return this;
}
}