// Copyright © WireMock.Net using JsonConverter.Newtonsoft.Json; using JsonConverter.System.Text.Json; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using WireMock.Settings; using WireMock.Transformers; namespace WireMock.Net.Tests.Settings; public class WireMockServerSettingsJsonTests { [Fact] public void NewtonsoftJson_Serialize_WireMockServerSettings_HandlesSpecialProperties_AndKeepsOtherPropertiesAsIs() { // Arrange var settings = new WireMockServerSettings { Port = 1234, AdminPath = "/custom-admin", StartAdminInterface = true, DefaultJsonSerializer = new SystemTextJsonConverter() }; settings.DefaultJsonBodyTransformer = new SystemTextJsonBodyTransformer(settings); // Act var json = JsonConvert.SerializeObject(settings); var jsonObject = JObject.Parse(json); // Assert jsonObject[nameof(WireMockServerSettings.Port)]!.Value().Should().Be(1234); jsonObject[nameof(WireMockServerSettings.AdminPath)]!.Value().Should().Be("/custom-admin"); jsonObject[nameof(WireMockServerSettings.StartAdminInterface)]!.Value().Should().BeTrue(); jsonObject[nameof(WireMockServerSettings.DefaultJsonSerializer)]!.Type.Should().Be(JTokenType.String); jsonObject[nameof(WireMockServerSettings.DefaultJsonSerializer)]!.Value().Should().Be(typeof(SystemTextJsonConverter).FullName); jsonObject[nameof(WireMockServerSettings.DefaultJsonBodyTransformer)]!.Type.Should().Be(JTokenType.String); jsonObject[nameof(WireMockServerSettings.DefaultJsonBodyTransformer)]!.Value().Should().Be(typeof(SystemTextJsonBodyTransformer).FullName); } [Fact] public void NewtonsoftJson_Deserialize_WireMockServerSettings_HandlesSpecialProperties_AndKeepsOtherPropertiesAsIs() { // Arrange var json = $"{{\"{nameof(WireMockServerSettings.Port)}\":4321," + $"\"{nameof(WireMockServerSettings.AdminPath)}\":\"/custom-admin\"," + $"\"{nameof(WireMockServerSettings.StartAdminInterface)}\":true," + $"\"{nameof(WireMockServerSettings.DefaultJsonSerializer)}\":\"{typeof(SystemTextJsonConverter).FullName}\"," + $"\"{nameof(WireMockServerSettings.DefaultJsonBodyTransformer)}\":\"{typeof(SystemTextJsonBodyTransformer).FullName}\"}}"; // Act var settings = JsonConvert.DeserializeObject(json); // Assert settings.Should().NotBeNull(); settings!.Port.Should().Be(4321); settings.AdminPath.Should().Be("/custom-admin"); settings.StartAdminInterface.Should().BeTrue(); settings.DefaultJsonSerializer.Should().BeOfType(); settings.DefaultJsonBodyTransformer.Should().BeOfType(); } [Fact] public void NewtonsoftJson_Deserialize_WireMockServerSettings_LegacyObjects_AreIgnored_AndOtherPropertiesStayUntouched() { // Arrange var json = $"{{\"{nameof(WireMockServerSettings.Port)}\":9876," + $"\"{nameof(WireMockServerSettings.DefaultJsonSerializer)}\":{{}}," + $"\"{nameof(WireMockServerSettings.DefaultJsonBodyTransformer)}\":{{}}}}"; // Act var settings = JsonConvert.DeserializeObject(json); // Assert settings.Should().NotBeNull(); settings!.Port.Should().Be(9876); settings.DefaultJsonSerializer.Should().BeOfType(); settings.DefaultJsonBodyTransformer.Should().BeOfType(); } }