Add UpdatedAt property to Mapping (#859)

* Add UpdatedAt property to Mapping

* .
This commit is contained in:
Stef Heyenrath
2022-12-09 14:18:50 +01:00
committed by GitHub
parent 080efe4fcb
commit e2f3ffd33a
15 changed files with 383 additions and 277 deletions

View File

@@ -14,6 +14,11 @@ public class MappingModel
/// </summary>
public Guid? Guid { get; set; }
/// <summary>
/// The datetime when this mapping was created or updated.
/// </summary>
public DateTime? UpdatedAt { get; set; }
/// <summary>
/// Gets or sets the TimeSettings when which this mapping should be used.
/// </summary>

View File

@@ -17,6 +17,11 @@ public interface IMapping
/// </summary>
Guid Guid { get; }
/// <summary>
/// The datetime when this mapping was created or updated.
/// </summary>
public DateTime? UpdatedAt { get; set; }
/// <summary>
/// Gets the TimeSettings (Start, End and TTL).
/// </summary>

View File

@@ -15,6 +15,9 @@ public class Mapping : IMapping
/// <inheritdoc />
public Guid Guid { get; }
/// <inheritdoc />
public DateTime? UpdatedAt { get; set; }
/// <inheritdoc />
public string? Title { get; }
@@ -73,6 +76,7 @@ public class Mapping : IMapping
/// Initializes a new instance of the <see cref="Mapping"/> class.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <param name="updatedAt">The datetime when this mapping was created.</param>
/// <param name="title">The unique title (can be null).</param>
/// <param name="description">The description (can be null).</param>
/// <param name="path">The full file path from this mapping title (can be null).</param>
@@ -89,6 +93,7 @@ public class Mapping : IMapping
/// <param name="timeSettings">The TimeSettings. [Optional]</param>
public Mapping(
Guid guid,
DateTime updatedAt,
string? title,
string? description,
string? path,
@@ -105,6 +110,7 @@ public class Mapping : IMapping
ITimeSettings? timeSettings)
{
Guid = guid;
UpdatedAt = updatedAt;
Title = title;
Description = description;
Path = path;

View File

@@ -12,13 +12,13 @@ internal class MappingMatcher : IMappingMatcher
public MappingMatcher(IWireMockMiddlewareOptions options)
{
Guard.NotNull(options, nameof(options));
_options = options;
_options = Guard.NotNull(options);
}
public (MappingMatcherResult? Match, MappingMatcherResult? Partial) FindBestMatch(RequestMessage request)
{
Guard.NotNull(request);
var possibleMappings = new List<MappingMatcherResult>();
foreach (var mapping in _options.Mappings.Values.Where(m => m.TimeSettings.IsValid()))
@@ -41,8 +41,7 @@ internal class MappingMatcher : IMappingMatcher
var partialMappings = possibleMappings
.Where(pm => (pm.Mapping.IsAdminInterface && pm.RequestMatchResult.IsPerfectMatch) || !pm.Mapping.IsAdminInterface)
.OrderBy(m => m.RequestMatchResult)
.ThenBy(m => m.Mapping.Priority)
.OrderBy(m => m.RequestMatchResult).ThenBy(m => m.Mapping.Priority).ThenByDescending(m => m.Mapping.UpdatedAt)
.ToList();
var partialMatch = partialMappings.FirstOrDefault(pm => pm.RequestMatchResult.AverageTotalScore > 0.0);
@@ -53,7 +52,7 @@ internal class MappingMatcher : IMappingMatcher
var match = possibleMappings
.Where(m => m.RequestMatchResult.IsPerfectMatch)
.OrderBy(m => m.Mapping.Priority).ThenBy(m => m.RequestMatchResult)
.OrderBy(m => m.Mapping.Priority).ThenBy(m => m.RequestMatchResult).ThenByDescending(m => m.Mapping.UpdatedAt)
.FirstOrDefault();
return (match, partialMatch);

View File

@@ -17,7 +17,7 @@ internal class ProxyHelper
public ProxyHelper(WireMockServerSettings settings)
{
_settings = Guard.NotNull(settings);
_proxyMappingConverter = new ProxyMappingConverter(settings, new GuidUtils());
_proxyMappingConverter = new ProxyMappingConverter(settings, new GuidUtils(), new DateTimeUtils());
}
public async Task<(IResponseMessage Message, IMapping? Mapping)> SendAsync(

View File

@@ -40,6 +40,7 @@ internal class MappingConverter
var mappingModel = new MappingModel
{
Guid = mapping.Guid,
UpdatedAt = mapping.UpdatedAt,
TimeSettings = TimeSettingsMapper.Map(mapping.TimeSettings),
Title = mapping.Title,
Description = mapping.Description,

View File

@@ -1,7 +1,7 @@
using Stef.Validation;
using System;
using System.Collections.Generic;
using System.Linq;
using Stef.Validation;
using WireMock.Constants;
using WireMock.Matchers;
using WireMock.Matchers.Request;
@@ -17,11 +17,13 @@ internal class ProxyMappingConverter
{
private readonly WireMockServerSettings _settings;
private readonly IGuidUtils _guidUtils;
private readonly IDateTimeUtils _dateTimeUtils;
public ProxyMappingConverter(WireMockServerSettings settings, IGuidUtils guidUtils)
public ProxyMappingConverter(WireMockServerSettings settings, IGuidUtils guidUtils, IDateTimeUtils dateTimeUtils)
{
_settings = Guard.NotNull(settings);
_guidUtils = Guard.NotNull(guidUtils);
_dateTimeUtils = Guard.NotNull(dateTimeUtils);
}
public IMapping ToMapping(IMapping? mapping, ProxyAndRecordSettings proxyAndRecordSettings, IRequestMessage requestMessage, ResponseMessage responseMessage)
@@ -162,6 +164,7 @@ internal class ProxyMappingConverter
return new Mapping
(
guid: _guidUtils.NewGuid(),
updatedAt: _dateTimeUtils.UtcNow,
title: title,
description: description,
path: null,

View File

@@ -29,10 +29,12 @@ internal class RespondWithAProvider : IRespondWithAProvider
private readonly IRequestMatcher _requestMatcher;
private readonly WireMockServerSettings _settings;
private readonly bool _saveToFile;
private readonly IGuidUtils _guidUtils = new GuidUtils();
private readonly IDateTimeUtils _dateTimeUtils = new DateTimeUtils();
private bool _useWebhookFireAndForget;
public Guid Guid { get; private set; } = Guid.NewGuid();
public Guid Guid { get; private set; }
public IWebhook[]? Webhooks { get; private set; }
@@ -45,12 +47,19 @@ internal class RespondWithAProvider : IRespondWithAProvider
/// <param name="requestMatcher">The request matcher.</param>
/// <param name="settings">The WireMockServerSettings.</param>
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
public RespondWithAProvider(RegistrationCallback registrationCallback, IRequestMatcher requestMatcher, WireMockServerSettings settings, bool saveToFile = false)
public RespondWithAProvider(
RegistrationCallback registrationCallback,
IRequestMatcher requestMatcher,
WireMockServerSettings settings,
bool saveToFile = false
)
{
_registrationCallback = registrationCallback;
_requestMatcher = requestMatcher;
_settings = settings;
_saveToFile = saveToFile;
_registrationCallback = Guard.NotNull(registrationCallback);
_requestMatcher = Guard.NotNull(requestMatcher);
_settings = Guard.NotNull(settings);
_saveToFile = Guard.NotNull(saveToFile);
Guid = _guidUtils.NewGuid();
}
/// <summary>
@@ -59,7 +68,24 @@ internal class RespondWithAProvider : IRespondWithAProvider
/// <param name="provider">The provider.</param>
public void RespondWith(IResponseProvider provider)
{
_registrationCallback(new Mapping(Guid, _title, _description, _path, _settings, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState, _timesInSameState, Webhooks, _useWebhookFireAndForget, TimeSettings), _saveToFile);
var mapping = new Mapping(
Guid,
_dateTimeUtils.UtcNow,
_title,
_description,
_path,
_settings,
_requestMatcher,
provider,
_priority,
_scenario,
_executionConditionState,
_nextState,
_timesInSameState,
Webhooks,
_useWebhookFireAndForget,
TimeSettings);
_registrationCallback(mapping, _saveToFile);
}
/// <inheritdoc />

View File

@@ -524,9 +524,10 @@ public partial class WireMockServer : IWireMockServer
private void RegisterMapping(IMapping mapping, bool saveToFile)
{
// Check a mapping exists with the same Guid, if so, replace it.
// Check a mapping exists with the same Guid. If so, update the datetime and replace it.
if (_options.Mappings.ContainsKey(mapping.Guid))
{
mapping.UpdatedAt = DateTime.UtcNow;
_options.Mappings[mapping.Guid] = mapping;
}
else

View File

@@ -0,0 +1,13 @@
using System;
namespace WireMock.Util;
internal interface IDateTimeUtils
{
DateTime UtcNow { get; }
}
internal class DateTimeUtils : IDateTimeUtils
{
public DateTime UtcNow => DateTime.UtcNow;
}