mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-07-06 05:43:03 +02:00
Add tests for special Omdb json
This commit is contained in:
parent
043d045448
commit
6ddbe8420f
|
@ -0,0 +1,36 @@
|
||||||
|
using System;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Common.Json.Converters
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Json Omdb converter factory.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Remove when Omdb is moved to plugin.
|
||||||
|
/// </remarks>
|
||||||
|
public class JsonOmdbNotAvailableConverterFactory : JsonConverterFactory
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override bool CanConvert(Type typeToConvert)
|
||||||
|
{
|
||||||
|
return (typeToConvert.IsGenericType
|
||||||
|
&& typeToConvert.GetGenericTypeDefinition() == typeof(Nullable<>)
|
||||||
|
&& typeToConvert.GenericTypeArguments[0].IsValueType)
|
||||||
|
|| typeToConvert == typeof(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (typeToConvert == typeof(string))
|
||||||
|
{
|
||||||
|
return (JsonConverter)Activator.CreateInstance(typeof(JsonOmdbNotAvailableStringConverter));
|
||||||
|
}
|
||||||
|
|
||||||
|
var structType = typeToConvert.GenericTypeArguments[0];
|
||||||
|
return (JsonConverter)Activator.CreateInstance(typeof(JsonOmdbNotAvailableStructConverter<>).MakeGenericType(structType));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
using System;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Common.Json.Converters
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a string <c>N/A</c> to <c>string.Empty</c>.
|
||||||
|
/// </summary>
|
||||||
|
public class JsonOmdbNotAvailableStringConverter : JsonConverter<string>
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (reader.TokenType == JsonTokenType.String)
|
||||||
|
{
|
||||||
|
var str = reader.GetString();
|
||||||
|
if (str != null && str.Equals("N/A", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return JsonSerializer.Deserialize<string>(ref reader, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
JsonSerializer.Serialize(value, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
using System;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Common.Json.Converters
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a string <c>N/A</c> to <c>string.Empty</c>.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The resulting type.</typeparam>
|
||||||
|
public class JsonOmdbNotAvailableStructConverter<T> : JsonConverter<T?>
|
||||||
|
where T : struct
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (reader.TokenType == JsonTokenType.String)
|
||||||
|
{
|
||||||
|
var str = reader.GetString();
|
||||||
|
if (str != null && str.Equals("N/A", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return JsonSerializer.Deserialize<T>(ref reader, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
JsonSerializer.Serialize(value, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,10 +8,12 @@ using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MediaBrowser.Common;
|
using MediaBrowser.Common;
|
||||||
using MediaBrowser.Common.Json;
|
using MediaBrowser.Common.Json;
|
||||||
|
using MediaBrowser.Common.Json.Converters;
|
||||||
using MediaBrowser.Common.Net;
|
using MediaBrowser.Common.Net;
|
||||||
using MediaBrowser.Controller.Configuration;
|
using MediaBrowser.Controller.Configuration;
|
||||||
using MediaBrowser.Controller.Entities;
|
using MediaBrowser.Controller.Entities;
|
||||||
|
@ -467,74 +469,104 @@ namespace MediaBrowser.Providers.Plugins.Omdb
|
||||||
return string.Equals(lang, "en", StringComparison.OrdinalIgnoreCase);
|
return string.Equals(lang, "en", StringComparison.OrdinalIgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class SeasonRootObject
|
public class SeasonRootObject
|
||||||
{
|
{
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string seriesID { get; set; }
|
public string seriesID { get; set; }
|
||||||
|
|
||||||
public int Season { get; set; }
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
|
public int? Season { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public int? totalSeasons { get; set; }
|
public int? totalSeasons { get; set; }
|
||||||
|
|
||||||
public RootObject[] Episodes { get; set; }
|
public RootObject[] Episodes { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Response { get; set; }
|
public string Response { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class RootObject
|
public class RootObject
|
||||||
{
|
{
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Year { get; set; }
|
public string Year { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Rated { get; set; }
|
public string Rated { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Released { get; set; }
|
public string Released { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Runtime { get; set; }
|
public string Runtime { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Genre { get; set; }
|
public string Genre { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Director { get; set; }
|
public string Director { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Writer { get; set; }
|
public string Writer { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Actors { get; set; }
|
public string Actors { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Plot { get; set; }
|
public string Plot { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Language { get; set; }
|
public string Language { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Country { get; set; }
|
public string Country { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Awards { get; set; }
|
public string Awards { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Poster { get; set; }
|
public string Poster { get; set; }
|
||||||
|
|
||||||
public List<OmdbRating> Ratings { get; set; }
|
public List<OmdbRating> Ratings { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Metascore { get; set; }
|
public string Metascore { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string imdbRating { get; set; }
|
public string imdbRating { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string imdbVotes { get; set; }
|
public string imdbVotes { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string imdbID { get; set; }
|
public string imdbID { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Type { get; set; }
|
public string Type { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string DVD { get; set; }
|
public string DVD { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string BoxOffice { get; set; }
|
public string BoxOffice { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Production { get; set; }
|
public string Production { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Website { get; set; }
|
public string Website { get; set; }
|
||||||
|
|
||||||
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
public string Response { get; set; }
|
public string Response { get; set; }
|
||||||
|
|
||||||
public int Episode { get; set; }
|
[JsonConverter(typeof(JsonOmdbNotAvailableConverterFactory))]
|
||||||
|
public int? Episode { get; set; }
|
||||||
|
|
||||||
public float? GetRottenTomatoScore()
|
public float? GetRottenTomatoScore()
|
||||||
{
|
{
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="../../MediaBrowser.Common/MediaBrowser.Common.csproj" />
|
<ProjectReference Include="../../MediaBrowser.Common/MediaBrowser.Common.csproj" />
|
||||||
|
<ProjectReference Include="..\..\MediaBrowser.Providers\MediaBrowser.Providers.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||||
|
|
21
tests/Jellyfin.Common.Tests/Json/JsonOmdbConverterTests.cs
Normal file
21
tests/Jellyfin.Common.Tests/Json/JsonOmdbConverterTests.cs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
using System.Text.Json;
|
||||||
|
using MediaBrowser.Common.Json;
|
||||||
|
using MediaBrowser.Providers.Plugins.Omdb;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Jellyfin.Common.Tests.Json
|
||||||
|
{
|
||||||
|
public static class JsonOmdbConverterTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public static 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, JsonDefaults.GetOptions());
|
||||||
|
Assert.NotNull(seasonRootObject);
|
||||||
|
Assert.Null(seasonRootObject?.Awards);
|
||||||
|
Assert.Null(seasonRootObject?.Episode);
|
||||||
|
Assert.Null(seasonRootObject?.Metascore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue