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

@@ -1,6 +1,6 @@
using FluentAssertions;
using System;
using FluentAssertions;
using Moq;
using System;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Serialization;
@@ -13,6 +13,13 @@ namespace WireMock.Net.Tests.Serialization
{
private readonly Mock<IFluentMockServerSettings> _settingsMock = new Mock<IFluentMockServerSettings>();
private readonly MappingConverter _sut;
public MappingConverterTests()
{
_sut = new MappingConverter(new MatcherMapper(_settingsMock.Object));
}
[Fact]
public void ToMappingModel()
{
@@ -22,7 +29,7 @@ namespace WireMock.Net.Tests.Serialization
var mapping = new Mapping(Guid.NewGuid(), "", null, _settingsMock.Object, request, response, 0, null, null, null);
// Act
var model = MappingConverter.ToMappingModel(mapping);
var model = _sut.ToMappingModel(mapping);
// Assert
model.Should().NotBeNull();
@@ -40,7 +47,7 @@ namespace WireMock.Net.Tests.Serialization
var mapping = new Mapping(Guid.NewGuid(), "", null, _settingsMock.Object, request, response, 42, null, null, null);
// Act
var model = MappingConverter.ToMappingModel(mapping);
var model = _sut.ToMappingModel(mapping);
// Assert
model.Should().NotBeNull();

View File

@@ -4,17 +4,30 @@ using NFluent;
using WireMock.Admin.Mappings;
using WireMock.Matchers;
using WireMock.Serialization;
using WireMock.Settings;
using Xunit;
namespace WireMock.Net.Tests.Serialization
{
public class MatcherMapperTests
{
private readonly Mock<IFluentMockServerSettings> _settingsMock;
private readonly MatcherMapper _sut;
public MatcherMapperTests()
{
_settingsMock = new Mock<IFluentMockServerSettings>();
_settingsMock.SetupAllProperties();
_sut = new MatcherMapper(_settingsMock.Object);
}
[Fact]
public void MatcherMapper_Map_IMatcher_Null()
{
// Act
var model = MatcherMapper.Map((IMatcher)null);
var model = _sut.Map((IMatcher)null);
// Assert
Check.That(model).IsNull();
@@ -24,7 +37,7 @@ namespace WireMock.Net.Tests.Serialization
public void MatcherMapper_Map_IMatchers_Null()
{
// Act
var model = MatcherMapper.Map((IMatcher[])null);
var model = _sut.Map((IMatcher[])null);
// Assert
Check.That(model).IsNull();
@@ -38,7 +51,7 @@ namespace WireMock.Net.Tests.Serialization
var matcherMock2 = new Mock<IStringMatcher>();
// Act
var models = MatcherMapper.Map(new[] { matcherMock1.Object, matcherMock2.Object });
var models = _sut.Map(new[] { matcherMock1.Object, matcherMock2.Object });
// Assert
Check.That(models).HasSize(2);
@@ -53,7 +66,7 @@ namespace WireMock.Net.Tests.Serialization
matcherMock.Setup(m => m.GetPatterns()).Returns(new[] { "p1", "p2" });
// Act
var model = MatcherMapper.Map(matcherMock.Object);
var model = _sut.Map(matcherMock.Object);
// Assert
Check.That(model.IgnoreCase).IsNull();
@@ -70,7 +83,7 @@ namespace WireMock.Net.Tests.Serialization
matcherMock.Setup(m => m.IgnoreCase).Returns(true);
// Act
var model = MatcherMapper.Map(matcherMock.Object);
var model = _sut.Map(matcherMock.Object);
// Assert
Check.That(model.IgnoreCase).Equals(true);
@@ -80,7 +93,7 @@ namespace WireMock.Net.Tests.Serialization
public void MatcherMapper_Map_MatcherModel_Null()
{
// Act
var result = MatcherMapper.Map((MatcherModel)null);
var result = _sut.Map((MatcherModel)null);
// Assert
Check.That(result).IsNull();
@@ -93,7 +106,7 @@ namespace WireMock.Net.Tests.Serialization
var model = new MatcherModel { Name = "test" };
// Act and Assert
Check.ThatCode(() => MatcherMapper.Map(model)).Throws<NotSupportedException>();
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
}
[Fact]
@@ -107,7 +120,7 @@ namespace WireMock.Net.Tests.Serialization
};
// Act
var matcher = (LinqMatcher)MatcherMapper.Map(model);
var matcher = (LinqMatcher)_sut.Map(model);
// Assert
Check.That(matcher.MatchBehaviour).IsEqualTo(MatchBehaviour.AcceptOnMatch);
@@ -125,7 +138,7 @@ namespace WireMock.Net.Tests.Serialization
};
// Act
var matcher = (LinqMatcher)MatcherMapper.Map(model);
var matcher = (LinqMatcher)_sut.Map(model);
// Assert
Check.That(matcher.MatchBehaviour).IsEqualTo(MatchBehaviour.AcceptOnMatch);

View File

@@ -1,19 +1,33 @@
using NFluent;
using System;
using Moq;
using WireMock.Admin.Mappings;
using WireMock.Matchers;
using WireMock.Serialization;
using WireMock.Settings;
using Xunit;
namespace WireMock.Net.Tests.Serialization
{
public class MatcherModelMapperTests
{
private readonly Mock<IFluentMockServerSettings> _settingsMock;
private readonly MatcherMapper _sut;
public MatcherModelMapperTests()
{
_settingsMock = new Mock<IFluentMockServerSettings>();
_settingsMock.SetupAllProperties();
_sut = new MatcherMapper(_settingsMock.Object);
}
[Fact]
public void MatcherModelMapper_Map_Null()
{
// Act
IMatcher matcher = MatcherMapper.Map((MatcherModel)null);
IMatcher matcher = _sut.Map((MatcherModel)null);
// Assert
Check.That(matcher).IsNull();
@@ -30,7 +44,7 @@ namespace WireMock.Net.Tests.Serialization
};
// Act
var matcher = (ExactMatcher)MatcherMapper.Map(model);
var matcher = (ExactMatcher)_sut.Map(model);
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x");
@@ -47,7 +61,7 @@ namespace WireMock.Net.Tests.Serialization
};
// Act
var matcher = (ExactMatcher)MatcherMapper.Map(model);
var matcher = (ExactMatcher)_sut.Map(model);
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
@@ -64,7 +78,7 @@ namespace WireMock.Net.Tests.Serialization
};
// Act
var matcher = (ExactObjectMatcher)MatcherMapper.Map(model);
var matcher = (ExactObjectMatcher)_sut.Map(model);
// Assert
Check.That(matcher.ValueAsBytes).ContainsExactly(new byte[] { 115, 116, 101, 102 });
@@ -82,7 +96,7 @@ namespace WireMock.Net.Tests.Serialization
};
// Act
var matcher = (RegexMatcher)MatcherMapper.Map(model);
var matcher = (RegexMatcher)_sut.Map(model);
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
@@ -101,7 +115,7 @@ namespace WireMock.Net.Tests.Serialization
};
// Act
var matcher = (WildcardMatcher)MatcherMapper.Map(model);
var matcher = (WildcardMatcher)_sut.Map(model);
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
@@ -119,7 +133,7 @@ namespace WireMock.Net.Tests.Serialization
};
// Act
var matcher = (SimMetricsMatcher)MatcherMapper.Map(model);
var matcher = (SimMetricsMatcher)_sut.Map(model);
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x");
@@ -136,7 +150,7 @@ namespace WireMock.Net.Tests.Serialization
};
// Act
var matcher = (SimMetricsMatcher)MatcherMapper.Map(model);
var matcher = (SimMetricsMatcher)_sut.Map(model);
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x");
@@ -153,7 +167,7 @@ namespace WireMock.Net.Tests.Serialization
};
// Act
Check.ThatCode(() => MatcherMapper.Map(model)).Throws<NotSupportedException>();
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
}
[Fact]
@@ -167,7 +181,7 @@ namespace WireMock.Net.Tests.Serialization
};
// Act
Check.ThatCode(() => MatcherMapper.Map(model)).Throws<NotSupportedException>();
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
}
}
}