Refactor MappingConverter & MatcherMapper (#323)

This commit is contained in:
Stef Heyenrath
2019-08-17 17:00:44 +00:00
committed by GitHub
parent 94f179ba17
commit d55e2fb920
7 changed files with 110 additions and 49 deletions

View File

@@ -5,12 +5,22 @@ using WireMock.Matchers.Request;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Util;
using WireMock.Validation;
namespace WireMock.Serialization
{
internal static class MappingConverter
internal class MappingConverter
{
public static MappingModel ToMappingModel(IMapping mapping)
private readonly MatcherMapper _mapper;
public MappingConverter(MatcherMapper mapper)
{
Check.NotNull(mapper, nameof(mapper));
_mapper = mapper;
}
public MappingModel ToMappingModel(IMapping mapping)
{
var request = (Request)mapping.RequestMatcher;
var response = (Response)mapping.Provider;
@@ -36,17 +46,17 @@ namespace WireMock.Serialization
{
ClientIP = clientIPMatchers != null && clientIPMatchers.Any() ? new ClientIPModel
{
Matchers = MatcherMapper.Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
Matchers = _mapper.Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
} : null,
Path = pathMatchers != null && pathMatchers.Any() ? new PathModel
{
Matchers = MatcherMapper.Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
Matchers = _mapper.Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
} : null,
Url = urlMatchers != null && urlMatchers.Any() ? new UrlModel
{
Matchers = MatcherMapper.Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
Matchers = _mapper.Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
} : null,
Methods = methodMatcher?.Methods,
@@ -54,20 +64,20 @@ namespace WireMock.Serialization
Headers = headerMatchers != null && headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderModel
{
Name = hm.Name,
Matchers = MatcherMapper.Map(hm.Matchers)
Matchers = _mapper.Map(hm.Matchers)
}).ToList() : null,
Cookies = cookieMatchers != null && cookieMatchers.Any() ? cookieMatchers.Select(cm => new CookieModel
{
Name = cm.Name,
Matchers = MatcherMapper.Map(cm.Matchers)
Matchers = _mapper.Map(cm.Matchers)
}).ToList() : null,
Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel
{
Name = pm.Key,
IgnoreCase = pm.IgnoreCase == true ? true : (bool?)null,
Matchers = MatcherMapper.Map(pm.Matchers)
Matchers = _mapper.Map(pm.Matchers)
}).ToList() : null
},
Response = new ResponseModel
@@ -82,11 +92,11 @@ namespace WireMock.Serialization
if (bodyMatcher.Matchers.Length == 1)
{
mappingModel.Request.Body.Matcher = MatcherMapper.Map(bodyMatcher.Matchers[0]);
mappingModel.Request.Body.Matcher = _mapper.Map(bodyMatcher.Matchers[0]);
}
else if (bodyMatcher.Matchers.Length > 1)
{
mappingModel.Request.Body.Matchers = MatcherMapper.Map(bodyMatcher.Matchers);
mappingModel.Request.Body.Matchers = _mapper.Map(bodyMatcher.Matchers);
}
}

View File

@@ -5,17 +5,27 @@ using System.Collections.Generic;
using System.Linq;
using WireMock.Admin.Mappings;
using WireMock.Matchers;
using WireMock.Settings;
using WireMock.Validation;
namespace WireMock.Serialization
{
internal static class MatcherMapper
internal class MatcherMapper
{
public static IMatcher[] Map([CanBeNull] IEnumerable<MatcherModel> matchers)
private readonly IFluentMockServerSettings _settings;
public MatcherMapper(IFluentMockServerSettings settings)
{
Check.NotNull(settings, nameof(settings));
_settings = settings;
}
public IMatcher[] Map([CanBeNull] IEnumerable<MatcherModel> matchers)
{
return matchers?.Select(Map).Where(m => m != null).ToArray();
}
public static IMatcher Map([CanBeNull] MatcherModel matcher)
public IMatcher Map([CanBeNull] MatcherModel matcher)
{
if (matcher == null)
{
@@ -73,12 +83,12 @@ namespace WireMock.Serialization
}
}
public static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
public MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
{
return matchers?.Select(Map).Where(m => m != null).ToArray();
}
public static MatcherModel Map([CanBeNull] IMatcher matcher)
public MatcherModel Map([CanBeNull] IMatcher matcher)
{
if (matcher == null)
{