-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#115 refactor: added builder to instance YamlConfigurationSerializer …
…and fixed tests + The new structure of the yaml serializer allows for better mocking and extendability + Some tests used mutable gateways, which could vary depending of the computer running the test. This has been solved via mocking + All dependencies are now fully injected, even the configuration manager + Adjusted margins for Wireguard.razor
- Loading branch information
1 parent
7d9bab5
commit 0332685
Showing
13 changed files
with
177 additions
and
63 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Net.NetworkInformation; | ||
using Moq; | ||
|
||
namespace Core.Test.Mocks; | ||
|
||
public class NetworkInterfaceMock : Mock<NetworkInterface> { | ||
public NetworkInterfaceMock(string name) { | ||
SetupGet(i => i.Name).Returns(name); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Linguard/Core.Test/Mocks/NetworkInterfaceTypeConverterMock.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Net.NetworkInformation; | ||
using Moq; | ||
using YamlDotNet.Core; | ||
using YamlDotNet.Core.Events; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace Core.Test.Mocks; | ||
|
||
public class NetworkInterfaceTypeConverterMock : IYamlTypeConverter { | ||
public bool Accepts(Type type) { | ||
return type == typeof(NetworkInterface) || type.BaseType == typeof(NetworkInterface); | ||
} | ||
|
||
public object? ReadYaml(IParser parser, Type type) { | ||
var value = (parser.Current as Scalar)?.Value; | ||
parser.MoveNext(); | ||
if (string.IsNullOrEmpty(value)) return default; | ||
var mock = new Mock<NetworkInterface>(); | ||
mock.SetupGet(i => i.Name).Returns(value); | ||
return mock.Object; | ||
} | ||
|
||
public void WriteYaml(IEmitter emitter, object? value, Type type) { | ||
var @interface = (value as NetworkInterface)?.Name; | ||
emitter.Emit(new Scalar(null, @interface ?? string.Empty)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Linguard/Core.Test/Mocks/YamlConfigurationSerializerMock.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Linguard.Core.Configuration; | ||
using Linguard.Core.Configuration.Serialization; | ||
using Linguard.Core.Drivers.TrafficStorage; | ||
using YamlDotNet.Serialization.NamingConventions; | ||
|
||
namespace Core.Test.Mocks; | ||
|
||
public static class YamlConfigurationSerializerMock { | ||
|
||
public static YamlConfigurationSerializer Instance => new YamlConfigurationSerializerBuilder() | ||
.WithNamingConvention(PascalCaseNamingConvention.Instance) | ||
.WithTypeConverter<IPAddressCidrTypeConverter>() | ||
.WithTypeConverter<NetworkInterfaceTypeConverterMock>() | ||
.WithTypeConverter<UriTypeConverter>() | ||
.WithTypeConverter<StyleTypeConverter>() | ||
.WithTagMapping<Configuration>("!Configuration") | ||
.WithTagMapping<WireguardConfiguration>("!Wireguard") | ||
.WithTagMapping<LoggingConfiguration>("!Logging") | ||
.WithTagMapping<WebConfiguration>("!Web") | ||
.WithTagMapping<TrafficConfiguration>("!Traffic") | ||
.WithTagMapping<JsonTrafficStorageDriver>("!Json") | ||
.Build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
Linguard/Core/Configuration/Serialization/DefaultYamlConfigurationSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Linguard.Core.Drivers.TrafficStorage; | ||
using YamlDotNet.Core; | ||
using YamlDotNet.Serialization; | ||
using YamlDotNet.Serialization.NamingConventions; | ||
|
||
namespace Linguard.Core.Configuration.Serialization; | ||
|
||
public static class DefaultYamlConfigurationSerializer { | ||
|
||
public static YamlConfigurationSerializer Instance => new YamlConfigurationSerializerBuilder() | ||
.WithNamingConvention(PascalCaseNamingConvention.Instance) | ||
.WithTypeConverter<IPAddressCidrTypeConverter>() | ||
.WithTypeConverter<NetworkInterfaceTypeConverter>() | ||
.WithTypeConverter<UriTypeConverter>() | ||
.WithTypeConverter<StyleTypeConverter>() | ||
.WithTagMapping<Configuration>("!Configuration") | ||
.WithTagMapping<WireguardConfiguration>("!Wireguard") | ||
.WithTagMapping<LoggingConfiguration>("!Logging") | ||
.WithTagMapping<WebConfiguration>("!Web") | ||
.WithTagMapping<TrafficConfiguration>("!Traffic") | ||
.WithTagMapping<JsonTrafficStorageDriver>("!Json") | ||
.Build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Linguard/Core/Configuration/Serialization/YamlConfigurationSerializerBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using YamlDotNet.Core; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace Linguard.Core.Configuration.Serialization; | ||
|
||
public class YamlConfigurationSerializerBuilder { | ||
private readonly DeserializerBuilder _deserializerBuilder = new(); | ||
private readonly SerializerBuilder _serializerBuilder = new(); | ||
|
||
public YamlConfigurationSerializerBuilder WithTagMapping<T>(TagName tag) { | ||
_deserializerBuilder.WithTagMapping(tag, typeof(T)); | ||
_serializerBuilder.WithTagMapping(tag, typeof(T)); | ||
return this; | ||
} | ||
|
||
public YamlConfigurationSerializerBuilder WithNamingConvention(INamingConvention namingConvention) { | ||
_deserializerBuilder.WithNamingConvention(namingConvention); | ||
_serializerBuilder.WithNamingConvention(namingConvention); | ||
return this; | ||
} | ||
|
||
public YamlConfigurationSerializerBuilder WithTypeConverter(IYamlTypeConverter converter) { | ||
_deserializerBuilder.WithTypeConverter(converter); | ||
_serializerBuilder.WithTypeConverter(converter); | ||
return this; | ||
} | ||
|
||
public YamlConfigurationSerializerBuilder WithTypeConverter<T>() where T : IYamlTypeConverter { | ||
return WithTypeConverter(Activator.CreateInstance<T>()); | ||
} | ||
|
||
public YamlConfigurationSerializer Build() { | ||
return new YamlConfigurationSerializer(_serializerBuilder.Build(), _deserializerBuilder.Build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.