Add TimeSettings (Start, End and TTL) (#661)

* time

* .

* UT

* using JetBrains.Annotations;
This commit is contained in:
Stef Heyenrath
2021-10-27 18:57:13 +02:00
committed by GitHub
parent a0e661fae9
commit e9db520cc3
20 changed files with 253 additions and 46 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System;
using WireMock.Models;
namespace WireMock.Admin.Mappings
{
@@ -13,6 +14,11 @@ namespace WireMock.Admin.Mappings
/// </summary>
public Guid? Guid { get; set; }
/// <summary>
/// Gets or sets the TimeSettings when which this mapping should be used.
/// </summary>
public TimeSettingsModel TimeSettings { get; set; }
/// <summary>
/// The unique title.
/// </summary>

View File

@@ -0,0 +1,26 @@
using System;
namespace WireMock.Models
{
/// <summary>
/// TimeSettingsModel: Start, End and TTL
/// </summary>
[FluentBuilder.AutoGenerateBuilder]
public class TimeSettingsModel
{
/// <summary>
/// Gets or sets the DateTime from which this mapping should be used. In case this is not defined, it's used (default behavior).
/// </summary>
public DateTime? Start { get; set; }
/// <summary>
/// Gets or sets the DateTime from until this mapping should be used. In case this is not defined, it's used forever (default behavior).
/// </summary>
public DateTime? End { get; set; }
/// <summary>
/// Gets or sets the TTL (Time To Live) in seconds for this mapping. In case this is not defined, it's used (default behavior).
/// </summary>
public int? TTL { get; set; }
}
}

View File

@@ -0,0 +1,25 @@
using System;
namespace WireMock.Models
{
/// <summary>
/// TimeSettings: Start, End and TTL
/// </summary>
public interface ITimeSettings
{
/// <summary>
/// Gets or sets the DateTime from which this mapping should be used. In case this is not defined, it's used (default behavior).
/// </summary>
DateTime? Start { get; set; }
/// <summary>
/// Gets or sets the DateTime from until this mapping should be used. In case this is not defined, it's used forever (default behavior).
/// </summary>
DateTime? End { get; set; }
/// <summary>
/// Gets or sets the TTL (Time To Live) in seconds for this mapping. In case this is not defined, it's used (default behavior).
/// </summary>
int? TTL { get; set; }
}
}

View File

@@ -1,23 +1,24 @@
using System.Collections.Generic;
using System.Linq;
using AnyOfTypes;
using JetBrains.Annotations;
using WireMock.Models;
namespace WireMock.Extensions
{
internal static class AnyOfExtensions
{
public static string GetPattern(this AnyOf<string, StringPattern> value)
public static string GetPattern([NotNull] this AnyOf<string, StringPattern> value)
{
return value.IsFirst ? value.First : value.Second.Pattern;
}
public static AnyOf<string, StringPattern>[] ToAnyOfPatterns(this IEnumerable<string> patterns)
public static AnyOf<string, StringPattern>[] ToAnyOfPatterns([NotNull] this IEnumerable<string> patterns)
{
return patterns.Select(p => p.ToAnyOfPattern()).ToArray();
}
public static AnyOf<string, StringPattern> ToAnyOfPattern(this string pattern)
public static AnyOf<string, StringPattern> ToAnyOfPattern([CanBeNull] this string pattern)
{
return new AnyOf<string, StringPattern>(pattern);
}

View File

@@ -0,0 +1,35 @@
using System;
using JetBrains.Annotations;
using WireMock.Models;
namespace WireMock.Extensions
{
internal static class TimeSettingsExtensions
{
public static bool IsValid([CanBeNull] this ITimeSettings settings)
{
if (settings == null)
{
return true;
}
var now = DateTime.Now;
var start = settings.Start != null ? settings.Start.Value : now;
DateTime end;
if (settings.End != null)
{
end = settings.End.Value;
}
else if (settings.TTL != null)
{
end = start.AddSeconds(settings.TTL.Value);
}
else
{
end = DateTime.MaxValue;
}
return now >= start && now <= end;
}
}
}

View File

@@ -1,4 +1,4 @@
using JetBrains.Annotations;
using JetBrains.Annotations;
using System;
using System.Threading.Tasks;
using WireMock.Matchers.Request;
@@ -18,6 +18,11 @@ namespace WireMock
/// </summary>
Guid Guid { get; }
/// <summary>
/// Gets the TimeSettings (Start, End and TTL).
/// </summary>
ITimeSettings TimeSettings { get; }
/// <summary>
/// Gets the unique title.
/// </summary>

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Matchers.Request;
@@ -13,51 +13,54 @@ namespace WireMock
/// </summary>
public class Mapping : IMapping
{
/// <inheritdoc cref="IMapping.Guid" />
/// <inheritdoc />
public Guid Guid { get; }
/// <inheritdoc cref="IMapping.Title" />
/// <inheritdoc />
public string Title { get; }
/// <inheritdoc cref="IMapping.Path" />
/// <inheritdoc />
public string Path { get; set; }
/// <inheritdoc cref="IMapping.Priority" />
/// <inheritdoc />
public int Priority { get; }
/// <inheritdoc cref="IMapping.Scenario" />
/// <inheritdoc />
public string Scenario { get; }
/// <inheritdoc cref="IMapping.ExecutionConditionState" />
/// <inheritdoc />
public string ExecutionConditionState { get; }
/// <inheritdoc cref="IMapping.NextState" />
/// <inheritdoc />
public string NextState { get; }
/// <inheritdoc cref="IMapping.StateTimes" />
/// <inheritdoc />
public int? StateTimes { get; }
/// <inheritdoc cref="IMapping.RequestMatcher" />
/// <inheritdoc />
public IRequestMatcher RequestMatcher { get; }
/// <inheritdoc cref="IMapping.Provider" />
/// <inheritdoc />
public IResponseProvider Provider { get; }
/// <inheritdoc cref="IMapping.Settings" />
/// <inheritdoc />
public IWireMockServerSettings Settings { get; }
/// <inheritdoc cref="IMapping.IsStartState" />
/// <inheritdoc />
public bool IsStartState => Scenario == null || Scenario != null && NextState != null && ExecutionConditionState == null;
/// <inheritdoc cref="IMapping.IsAdminInterface" />
/// <inheritdoc />
public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider || Provider is ProxyAsyncResponseProvider;
/// <inheritdoc cref="IMapping.LogMapping" />
/// <inheritdoc />
public bool LogMapping => !(Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider);
/// <inheritdoc cref="IMapping.Webhooks" />
/// <inheritdoc />
public IWebhook[] Webhooks { get; }
/// <inheritdoc />
public ITimeSettings TimeSettings { get; }
/// <summary>
/// Initializes a new instance of the <see cref="Mapping"/> class.
/// </summary>
@@ -73,6 +76,7 @@ namespace WireMock
/// <param name="nextState">The next state which will occur after the current mapping execution. [Optional]</param>
/// <param name="stateTimes">Only when the current state is executed this number, the next state which will occur. [Optional]</param>
/// <param name="webhooks">The Webhooks. [Optional]</param>
/// <param name="timeSettings">The TimeSettings. [Optional]</param>
public Mapping(
Guid guid,
[CanBeNull] string title,
@@ -85,7 +89,8 @@ namespace WireMock
[CanBeNull] string executionConditionState,
[CanBeNull] string nextState,
[CanBeNull] int? stateTimes,
[CanBeNull] IWebhook[] webhooks)
[CanBeNull] IWebhook[] webhooks,
[CanBeNull] ITimeSettings timeSettings)
{
Guid = guid;
Title = title;
@@ -99,6 +104,7 @@ namespace WireMock
NextState = nextState;
StateTimes = stateTimes;
Webhooks = webhooks;
TimeSettings = timeSettings;
}
/// <inheritdoc cref="IMapping.ProvideResponseAsync" />

View File

@@ -0,0 +1,19 @@
using System;
namespace WireMock.Models
{
/// <summary>
/// TimeSettingsModel: Start, End and TTL
/// </summary>
public class TimeSettings : ITimeSettings
{
/// <inheritdoc />
public DateTime? Start { get; set; }
/// <inheritdoc />
public DateTime? End { get; set; }
/// <inheritdoc />
public int? TTL { get; set; }
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using WireMock.Extensions;
using WireMock.Validation;
namespace WireMock.Owin
@@ -19,7 +20,8 @@ namespace WireMock.Owin
public (MappingMatcherResult Match, MappingMatcherResult Partial) FindBestMatch(RequestMessage request)
{
var mappings = new List<MappingMatcherResult>();
foreach (var mapping in _options.Mappings.Values)
foreach (var mapping in _options.Mappings.Values.Where(m => m.TimeSettings.IsValid()))
{
try
{

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
@@ -21,8 +21,7 @@ namespace WireMock.Proxy
public ProxyHelper([NotNull] IWireMockServerSettings settings)
{
Check.NotNull(settings, nameof(settings));
_settings = settings;
_settings = Check.NotNull(settings, nameof(settings));
}
public async Task<(ResponseMessage Message, IMapping Mapping)> SendAsync(
@@ -105,7 +104,7 @@ namespace WireMock.Proxy
var response = Response.Create(responseMessage);
return new Mapping(Guid.NewGuid(), string.Empty, null, _settings, request, response, 0, null, null, null, null, null);
return new Mapping(Guid.NewGuid(), string.Empty, null, _settings, request, response, 0, null, null, null, null, null, null);
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using WireMock.Admin.Mappings;
using WireMock.Matchers.Request;
using WireMock.Models;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Settings;
@@ -36,6 +37,7 @@ namespace WireMock.Serialization
var mappingModel = new MappingModel
{
Guid = mapping.Guid,
TimeSettings = TimeSettingsMapper.Map(mapping.TimeSettings),
Title = mapping.Title,
Priority = mapping.Priority != 0 ? mapping.Priority : (int?)null,
Scenario = mapping.Scenario,

View File

@@ -0,0 +1,27 @@
using WireMock.Models;
namespace WireMock.Serialization
{
internal static class TimeSettingsMapper
{
public static TimeSettingsModel Map(ITimeSettings settings)
{
return settings != null ? new TimeSettingsModel
{
Start = settings.Start,
End = settings.End,
TTL = settings.TTL
} : null;
}
public static ITimeSettings Map(TimeSettingsModel settings)
{
return settings != null ? new TimeSettings
{
Start = settings.Start,
End = settings.End,
TTL = settings.TTL
} : null;
}
}
}

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using WireMock.Models;
@@ -24,6 +24,13 @@ namespace WireMock.Server
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(Guid guid);
/// <summary>
/// Define the TimeSettings for this mapping.
/// </summary>
/// <param name="timeSettings">The TimeSettings.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithTimeSettings(ITimeSettings timeSettings);
/// <summary>
/// Define a unique title for this mapping.
/// </summary>

View File

@@ -1,4 +1,4 @@
// This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License.
// This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License.
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
using System;
using System.Collections.Generic;
@@ -34,6 +34,8 @@ namespace WireMock.Server
public IWebhook[] Webhooks { get; private set; }
public ITimeSettings TimeSettings { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="RespondWithAProvider"/> class.
/// </summary>
@@ -55,7 +57,7 @@ namespace WireMock.Server
/// <param name="provider">The provider.</param>
public void RespondWith(IResponseProvider provider)
{
_registrationCallback(new Mapping(Guid, _title, _path, _settings, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState, _timesInSameState, Webhooks), _saveToFile);
_registrationCallback(new Mapping(Guid, _title, _path, _settings, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState, _timesInSameState, Webhooks, TimeSettings), _saveToFile);
}
/// <see cref="IRespondWithAProvider.WithGuid(string)"/>
@@ -149,6 +151,16 @@ namespace WireMock.Server
return WillSetStateTo(state.ToString(), times);
}
/// <inheritdoc />
public IRespondWithAProvider WithTimeSettings(ITimeSettings timeSettings)
{
Check.NotNull(timeSettings, nameof(timeSettings));
TimeSettings = timeSettings;
return this;
}
/// <see cref="IRespondWithAProvider.WithWebhook(IWebhook[])"/>
public IRespondWithAProvider WithWebhook(params IWebhook[] webhooks)
{
@@ -207,7 +219,7 @@ namespace WireMock.Server
return this;
}
private IWebhook InitWebhook(
private static IWebhook InitWebhook(
string url,
string method,
IDictionary<string, WireMockList<string>> headers,

View File

@@ -450,6 +450,11 @@ namespace WireMock.Server
respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value);
}
if (mappingModel.TimeSettings != null)
{
respondProvider = respondProvider.WithTimeSettings(TimeSettingsMapper.Map(mappingModel.TimeSettings));
}
if (path != null)
{
respondProvider = respondProvider.WithPath(path);

View File

@@ -4,7 +4,6 @@ using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using JetBrains.Annotations;
using Newtonsoft.Json;
@@ -13,7 +12,6 @@ using WireMock.Authentication;
using WireMock.Exceptions;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.Owin;
using WireMock.RequestBuilders;

View File

@@ -60,6 +60,8 @@
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.12" />
<PackageReference Include="JmesPath.Net" Version="1.0.125" />
<PackageReference Include="AnyOf" Version="0.2.0" />
<!--<PackageReference Include="TinyMapper" Version="3.0.3" />-->
<!--<PackageReference Include="Mapster" Version="7.2.0" />-->
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug - Sonar'">
@@ -81,6 +83,7 @@
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.6" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="Scriban.Signed" Version="2.1.4" />
<PackageReference Include="Mapster" Version="7.2.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Threading.Tasks;
@@ -176,7 +176,7 @@ namespace WireMock.Net.Tests.Owin
_mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder);
_mappingMock.SetupGet(m => m.Settings).Returns(settings);
var newMappingFromProxy = new Mapping(Guid.NewGuid(), "", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null);
var newMappingFromProxy = new Mapping(Guid.NewGuid(), "", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, null);
_mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny<RequestMessage>())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy));
var requestBuilder = Request.Create().UsingAnyMethod();
@@ -230,7 +230,7 @@ namespace WireMock.Net.Tests.Owin
_mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder);
_mappingMock.SetupGet(m => m.Settings).Returns(settings);
var newMappingFromProxy = new Mapping(Guid.NewGuid(), "", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null);
var newMappingFromProxy = new Mapping(Guid.NewGuid(), "", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, null);
_mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny<RequestMessage>())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy));
var requestBuilder = Request.Create().UsingAnyMethod();

View File

@@ -51,7 +51,7 @@ namespace WireMock.Net.Tests.Serialization
}
}
};
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 0, null, null, null, null, webhooks);
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 0, null, null, null, null, webhooks, null);
// Act
var model = _sut.ToMappingModel(mapping);
@@ -120,8 +120,7 @@ namespace WireMock.Net.Tests.Serialization
}
}
};
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 0, null, null, null, null, webhooks
);
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 0, null, null, null, null, webhooks, null);
// Act
var model = _sut.ToMappingModel(mapping);
@@ -153,7 +152,7 @@ namespace WireMock.Net.Tests.Serialization
// Assign
var request = Request.Create();
var response = Response.Create().WithBodyAsJson(new { x = "x" }).WithTransformer();
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null);
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null, null);
// Act
var model = _sut.ToMappingModel(mapping);
@@ -164,6 +163,34 @@ namespace WireMock.Net.Tests.Serialization
model.Response.UseTransformer.Should().BeTrue();
}
[Fact]
public void ToMappingModel_WithTimeSetrtings_ReturnsCorrectTimeSettings()
{
// Assign
var start = DateTime.Now;
var ttl = 100;
var end = start.AddSeconds(ttl);
var request = Request.Create();
var response = Response.Create();
var timeSettings = new TimeSettings
{
Start = start,
End = end,
TTL = ttl
};
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null, timeSettings);
// Act
var model = _sut.ToMappingModel(mapping);
// Assert
model.Should().NotBeNull();
model.TimeSettings.Should().NotBeNull();
model.TimeSettings.Start.Should().Be(start);
model.TimeSettings.End.Should().Be(end);
model.TimeSettings.TTL.Should().Be(ttl);
}
[Fact]
public void ToMappingModel_WithDelay_ReturnsCorrectModel()
{
@@ -171,7 +198,7 @@ namespace WireMock.Net.Tests.Serialization
int delay = 1000;
var request = Request.Create();
var response = Response.Create().WithDelay(delay);
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null);
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null, null);
// Act
var model = _sut.ToMappingModel(mapping);
@@ -182,13 +209,13 @@ namespace WireMock.Net.Tests.Serialization
}
[Fact]
public void ToMappingModel_WithRandomMininumDelay_ReturnsCorrectModel()
public void ToMappingModel_WithRandomMinimumDelay_ReturnsCorrectModel()
{
// Assign
int minimumDelay = 1000;
var request = Request.Create();
var response = Response.Create().WithRandomDelay(minimumDelay);
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null);
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null, null);
// Act
var model = _sut.ToMappingModel(mapping);
@@ -208,7 +235,7 @@ namespace WireMock.Net.Tests.Serialization
int maximumDelay = 2000;
var request = Request.Create();
var response = Response.Create().WithRandomDelay(minimumDelay, maximumDelay);
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null);
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null, null);
// Act
var model = _sut.ToMappingModel(mapping);

View File

@@ -37,6 +37,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<!--<PackageReference Include="Mapster" Version="7.2.0" />-->
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
@@ -59,8 +60,9 @@
<!--<PackageReference Include="ReportGenerator" Version="4.8.1" />-->
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.12" />
<!--<PackageReference Include="StrongNamer" Version="0.0.8" />-->
<!--<PackageReference Include="StrongNamer" Version="0.2.5" />-->
<PackageReference Include="AnyOf" Version="0.2.0" />
<!--<PackageReference Include="TinyMapper" Version="3.0.3" />-->
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net452' or '$(TargetFramework)' == 'net461'">