using System; using System.Collections.Generic; using AutoMapper; using FluentAssertions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; using NSubstitute; using NUnit.Framework; using TrashLib.Sonarr; using TrashLib.Sonarr.Api; using TrashLib.Sonarr.Api.Objects; using TrashLib.Startup; namespace TrashLib.Tests.Sonarr.Api { [TestFixture] [Parallelizable(ParallelScope.All)] public class SonarrReleaseProfileCompatibilityHandlerTest { private class TestContext : IDisposable { private readonly JsonSerializerSettings _jsonSettings; public TestContext() { _jsonSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; Mapper = AutoMapperConfig.Setup(); } public IMapper Mapper { get; } public void Dispose() { } public string SerializeJson(T obj) { return JsonConvert.SerializeObject(obj, _jsonSettings); } } [Test] public void Receive_v1_to_v2() { using var ctx = new TestContext(); var compat = Substitute.For(); var dataV1 = new SonarrReleaseProfileV1 {Ignored = "one,two,three"}; var sut = new SonarrReleaseProfileCompatibilityHandler(compat, ctx.Mapper); var result = sut.CompatibleReleaseProfileForReceiving(JObject.Parse(ctx.SerializeJson(dataV1))); _ = compat.DidNotReceive().ArraysNeededForReleaseProfileRequiredAndIgnored; result.Should().BeEquivalentTo(new SonarrReleaseProfile { Ignored = new List {"one", "two", "three"} }); } [Test] public void Receive_v2_to_v2() { using var ctx = new TestContext(); var compat = Substitute.For(); var dataV2 = new SonarrReleaseProfile {Ignored = new List {"one", "two", "three"}}; var sut = new SonarrReleaseProfileCompatibilityHandler(compat, ctx.Mapper); var result = sut.CompatibleReleaseProfileForReceiving(JObject.Parse(ctx.SerializeJson(dataV2))); _ = compat.DidNotReceive().ArraysNeededForReleaseProfileRequiredAndIgnored; result.Should().BeEquivalentTo(dataV2); } [Test] public void Send_v2_to_v1() { using var ctx = new TestContext(); var compat = Substitute.For(); compat.ArraysNeededForReleaseProfileRequiredAndIgnored.Returns(false); var data = new SonarrReleaseProfile {Ignored = new List {"one", "two", "three"}}; var sut = new SonarrReleaseProfileCompatibilityHandler(compat, ctx.Mapper); var result = sut.CompatibleReleaseProfileForSending(data); result.Should().BeEquivalentTo(new SonarrReleaseProfileV1 {Ignored = "one,two,three"}); } [Test] public void Send_v2_to_v2() { using var ctx = new TestContext(); var compat = Substitute.For(); compat.ArraysNeededForReleaseProfileRequiredAndIgnored.Returns(true); var data = new SonarrReleaseProfile {Ignored = new List {"one", "two", "three"}}; var sut = new SonarrReleaseProfileCompatibilityHandler(compat, ctx.Mapper); var result = sut.CompatibleReleaseProfileForSending(data); result.Should().BeEquivalentTo(data); } } }