using JetBrains.Annotations;
using System;
using System.Threading.Tasks;
using WireMock.Matchers.Request;
using WireMock.ResponseProviders;
using WireMock.Settings;
namespace WireMock
{
///
/// The Mapping.
///
public class Mapping : IMapping
{
///
public Guid Guid { get; }
///
public string Title { get; }
///
public string Path { get; set; }
///
public int Priority { get; }
///
public string Scenario { get; }
///
public string ExecutionConditionState { get; }
///
public string NextState { get; }
///
public IRequestMatcher RequestMatcher { get; }
///
public IResponseProvider Provider { get; }
///
public IFluentMockServerSettings Settings { get; }
///
public bool IsStartState => Scenario == null || Scenario != null && NextState != null && ExecutionConditionState == null;
///
public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider || Provider is ProxyAsyncResponseProvider;
///
public bool LogMapping => !(Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider);
///
/// Initializes a new instance of the class.
///
/// The unique identifier.
/// The unique title (can be null).
/// The full file path from this mapping title (can be null).
/// The FluentMockServerSettings.
/// 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]
public Mapping(Guid guid, [CanBeNull] string title, [CanBeNull] string path,
[NotNull] IFluentMockServerSettings settings, [NotNull] IRequestMatcher requestMatcher, [NotNull] IResponseProvider provider,
int priority, [CanBeNull] string scenario, [CanBeNull] string executionConditionState, [CanBeNull] string nextState)
{
Guid = guid;
Title = title;
Path = path;
Settings = settings;
RequestMatcher = requestMatcher;
Provider = provider;
Priority = priority;
Scenario = scenario;
ExecutionConditionState = executionConditionState;
NextState = nextState;
}
///
public async Task ProvideResponseAsync(RequestMessage requestMessage)
{
return await Provider.ProvideResponseAsync(requestMessage, Settings);
}
///
public RequestMatchResult GetRequestMatchResult(RequestMessage 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;
}
}
}