Add MappingBuilder to build mappings in code and export to Models or JSON (#869)

* MappingBuilder

* .

* ...

* sc

* t

* .
This commit is contained in:
Stef Heyenrath
2023-01-06 19:11:56 +01:00
committed by GitHub
parent 742f1d1f0a
commit 20eb37b0c8
10 changed files with 465 additions and 142 deletions

View File

@@ -4,103 +4,102 @@ using NFluent;
using WireMock.Handlers;
using Xunit;
namespace WireMock.Net.Tests.Handlers
namespace WireMock.Net.Tests.Handlers;
public class LocalFileSystemHandlerTests
{
public class LocalFileSystemHandlerTests
private readonly LocalFileSystemHandler _sut = new();
[Fact]
public void LocalFileSystemHandler_GetMappingFolder()
{
private readonly LocalFileSystemHandler _sut = new LocalFileSystemHandler();
// Act
string result = _sut.GetMappingFolder();
[Fact]
public void LocalFileSystemHandler_GetMappingFolder()
{
// Act
string result = _sut.GetMappingFolder();
// Assert
Check.That(result).EndsWith(Path.Combine("__admin", "mappings"));
}
// Assert
Check.That(result).EndsWith(Path.Combine("__admin", "mappings"));
}
[Fact]
public void LocalFileSystemHandler_CreateFolder_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.CreateFolder(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_CreateFolder_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.CreateFolder(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_WriteMappingFile_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.WriteMappingFile(null, null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_WriteMappingFile_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.WriteMappingFile(null, null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_ReadResponseBodyAsFile_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.ReadResponseBodyAsFile(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_ReadResponseBodyAsFile_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.ReadResponseBodyAsFile(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_FileExists_ReturnsFalse()
{
// Act
var result = _sut.FileExists("x.x");
[Fact]
public void LocalFileSystemHandler_FileExists_ReturnsFalse()
{
// Act
var result = _sut.FileExists("x.x");
// Assert
Check.That(result).IsFalse();
}
// Assert
Check.That(result).IsFalse();
}
[Fact]
public void LocalFileSystemHandler_FileExists_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.FileExists(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_FileExists_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.FileExists(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_ReadFile_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.ReadFile(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_ReadFile_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.ReadFile(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_ReadFileAsString_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.ReadFileAsString(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_ReadFileAsString_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.ReadFileAsString(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_WriteFile_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.WriteFile(null, null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_WriteFile_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.WriteFile(null, null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_DeleteFile_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.DeleteFile(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_DeleteFile_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.DeleteFile(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_GetUnmatchedRequestsFolder()
{
// Act
string result = _sut.GetUnmatchedRequestsFolder();
[Fact]
public void LocalFileSystemHandler_GetUnmatchedRequestsFolder()
{
// Act
string result = _sut.GetUnmatchedRequestsFolder();
// Assert
Check.That(result).EndsWith(Path.Combine("requests", "unmatched"));
}
// Assert
Check.That(result).EndsWith(Path.Combine("requests", "unmatched"));
}
[Fact]
public void LocalFileSystemHandler_WriteUnmatchedRequest()
{
// Act
Check.ThatCode(() => _sut.WriteUnmatchedRequest(null, null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_WriteUnmatchedRequest()
{
// Act
Check.ThatCode(() => _sut.WriteUnmatchedRequest(null, null)).Throws<ArgumentNullException>();
}
}

View File

@@ -0,0 +1,136 @@
using FluentAssertions;
using Moq;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Owin;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Serialization;
using WireMock.Settings;
using Xunit;
namespace WireMock.Net.Tests;
public class MappingBuilderTests
{
private static readonly string MappingGuid = "41372914-1838-4c67-916b-b9aacdd096ce";
private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock;
private readonly MappingBuilder _sut;
public MappingBuilderTests()
{
_fileSystemHandlerMock = new Mock<IFileSystemHandler>();
var settings = new WireMockServerSettings
{
FileSystemHandler = _fileSystemHandlerMock.Object,
Logger = Mock.Of<IWireMockLogger>()
};
var options = new WireMockMiddlewareOptions();
var matcherMapper = new MatcherMapper(settings);
var mappingConverter = new MappingConverter(matcherMapper);
var mappingToFileSaver = new MappingToFileSaver(settings, mappingConverter);
_sut = new MappingBuilder(settings, options, mappingConverter, mappingToFileSaver);
_sut.Given(Request.Create()
.WithPath("/foo")
.UsingGet()
)
.WithGuid(MappingGuid)
.RespondWith(Response.Create()
.WithBody(@"{ msg: ""Hello world!""}")
);
}
[Fact]
public void GetMappings()
{
// Act
var mappings = _sut.GetMappings();
// Assert
mappings.Should().HaveCount(1);
}
[Fact]
public void ToJson()
{
// Act
var json = _sut.ToJson();
// Assert
json.Should().NotBeEmpty();
}
[Fact]
public void SaveMappingsToFile_FolderExists_IsFalse()
{
// Arrange
var path = "path";
// Act
_sut.SaveMappingsToFile(path);
// Verify
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Never);
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(path), Times.Once);
_fileSystemHandlerMock.Verify(fs => fs.CreateFolder(path), Times.Once);
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(path, It.IsAny<string>()), Times.Once);
_fileSystemHandlerMock.VerifyNoOtherCalls();
}
[Fact]
public void SaveMappingsToFile_FolderExists_IsTrue()
{
// Arrange
var path = "path";
_fileSystemHandlerMock.Setup(fs => fs.FolderExists(It.IsAny<string>())).Returns(true);
// Act
_sut.SaveMappingsToFile(path);
// Verify
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Never);
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(path), Times.Once);
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(path, It.IsAny<string>()), Times.Once);
_fileSystemHandlerMock.VerifyNoOtherCalls();
}
[Fact]
public void SaveMappingsToFolder_FolderIsNull()
{
// Arrange
var mappingFolder = "mapping-folder";
_fileSystemHandlerMock.Setup(fs => fs.GetMappingFolder()).Returns(mappingFolder);
_fileSystemHandlerMock.Setup(fs => fs.FolderExists(It.IsAny<string>())).Returns(true);
// Act
_sut.SaveMappingsToFolder(null);
// Verify
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Once);
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(mappingFolder), Times.Once);
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
_fileSystemHandlerMock.VerifyNoOtherCalls();
}
[Fact]
public void SaveMappingsToFolder_FolderExists_IsTrue()
{
// Arrange
var path = "path";
_fileSystemHandlerMock.Setup(fs => fs.FolderExists(It.IsAny<string>())).Returns(true);
// Act
_sut.SaveMappingsToFolder(path);
// Verify
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Never);
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(path), Times.Once);
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
_fileSystemHandlerMock.VerifyNoOtherCalls();
}
}