aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Common.Tests/Json/JsonOmdbConverterTests.cs
blob: faed086a12f542a2f70d8d8d06988134e4333645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Text.Json;
using System.Text.Json.Serialization;
using MediaBrowser.Common.Json.Converters;
using MediaBrowser.Providers.Plugins.Omdb;
using Xunit;

namespace Jellyfin.Common.Tests.Json
{
    public class JsonOmdbConverterTests
    {
        private readonly JsonSerializerOptions _options;

        public JsonOmdbConverterTests()
        {
            _options = new JsonSerializerOptions();
            _options.Converters.Add(new JsonOmdbNotAvailableStringConverter());
            _options.Converters.Add(new JsonOmdbNotAvailableInt32Converter());
            _options.NumberHandling = JsonNumberHandling.AllowReadingFromString;
        }

        [Fact]
        public void Deserialize_Omdb_Response_Not_Available_Success()
        {
            const string Input = "{\"Title\":\"Chapter 1\",\"Year\":\"2013\",\"Rated\":\"TV-MA\",\"Released\":\"01 Feb 2013\",\"Season\":\"N/A\",\"Episode\":\"N/A\",\"Runtime\":\"55 min\",\"Genre\":\"Drama\",\"Director\":\"David Fincher\",\"Writer\":\"Michael Dobbs (based on the novels by), Andrew Davies (based on the mini-series by), Beau Willimon (created for television by), Beau Willimon, Sam Forman (staff writer)\",\"Actors\":\"Kevin Spacey, Robin Wright, Kate Mara, Corey Stoll\",\"Plot\":\"Congressman Francis Underwood has been declined the chair for Secretary of State. He's now gathering his own team to plot his revenge. Zoe Barnes, a reporter for the Washington Herald, will do anything to get her big break.\",\"Language\":\"English\",\"Country\":\"USA\",\"Awards\":\"N/A\",\"Poster\":\"https://m.media-amazon.com/images/M/MV5BMTY5MTU4NDQzNV5BMl5BanBnXkFtZTgwMzk2ODcxMzE@._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"8.7/10\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"8.7\",\"imdbVotes\":\"6736\",\"imdbID\":\"tt2161930\",\"seriesID\":\"N/A\",\"Type\":\"episode\",\"Response\":\"True\"}";
            var seasonRootObject = JsonSerializer.Deserialize<OmdbProvider.RootObject>(Input, _options);
            Assert.NotNull(seasonRootObject);
            Assert.Null(seasonRootObject?.Awards);
            Assert.Null(seasonRootObject?.Episode);
            Assert.Null(seasonRootObject?.Metascore);
        }

        [Theory]
        [InlineData("\"N/A\"")]
        [InlineData("null")]
        public void Deserialization_To_Nullable_Int_Shoud_Be_Null(string input)
        {
            var result = JsonSerializer.Deserialize<int?>(input, _options);
            Assert.Null(result);
        }

        [Theory]
        [InlineData("\"N/A\"")]
        [InlineData("null")]
        public void Deserialization_To_Nullable_String_Shoud_Be_Null(string input)
        {
            var result = JsonSerializer.Deserialize<string?>(input, _options);
            Assert.Null(result);
        }

        [Theory]
        [InlineData("\"8\"", 8)]
        [InlineData("8", 8)]
        public void Deserialize_Int_Success(string input, int expected)
        {
            var result = JsonSerializer.Deserialize<int>(input, _options);
            Assert.Equal(result, expected);
        }

        [Fact]
        public void Deserialize_Normal_String_Success()
        {
            const string Input = "\"Jellyfin\"";
            const string Expected = "Jellyfin";
            var result = JsonSerializer.Deserialize<string>(Input, _options);
            Assert.Equal(Expected, result);
        }

        [Fact]
        public void Roundtrip_Valid_Success()
        {
            const string Input = "{\"Title\":\"Chapter 1\",\"Year\":\"2013\",\"Rated\":\"TV-MA\",\"Released\":\"01 Feb 2013\",\"Season\":\"N/A\",\"Episode\":\"N/A\",\"Runtime\":\"55 min\",\"Genre\":\"Drama\",\"Director\":\"David Fincher\",\"Writer\":\"Michael Dobbs (based on the novels by), Andrew Davies (based on the mini-series by), Beau Willimon (created for television by), Beau Willimon, Sam Forman (staff writer)\",\"Actors\":\"Kevin Spacey, Robin Wright, Kate Mara, Corey Stoll\",\"Plot\":\"Congressman Francis Underwood has been declined the chair for Secretary of State. He's now gathering his own team to plot his revenge. Zoe Barnes, a reporter for the Washington Herald, will do anything to get her big break.\",\"Language\":\"English\",\"Country\":\"USA\",\"Awards\":\"N/A\",\"Poster\":\"https://m.media-amazon.com/images/M/MV5BMTY5MTU4NDQzNV5BMl5BanBnXkFtZTgwMzk2ODcxMzE@._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"8.7/10\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"8.7\",\"imdbVotes\":\"6736\",\"imdbID\":\"tt2161930\",\"seriesID\":\"N/A\",\"Type\":\"episode\",\"Response\":\"True\"}";
            var trip1 = JsonSerializer.Deserialize<OmdbProvider.RootObject>(Input, _options);
            Assert.NotNull(trip1);
            Assert.NotNull(trip1?.Title);
            Assert.Null(trip1?.Awards);
            Assert.Null(trip1?.Episode);
            Assert.Null(trip1?.Metascore);

            var serializedTrip1 = JsonSerializer.Serialize(trip1!, _options);
            var trip2 = JsonSerializer.Deserialize<OmdbProvider.RootObject>(serializedTrip1, _options);
            Assert.NotNull(trip2);
            Assert.NotNull(trip2?.Title);
            Assert.Null(trip2?.Awards);
            Assert.Null(trip2?.Episode);
            Assert.Null(trip2?.Metascore);
        }
    }
}