mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-19 21:13:28 -07:00
Fixed: Handling of unknown status types in DownloadStation
This commit is contained in:
parent
443e536005
commit
96c59e2b2b
9 changed files with 126 additions and 5 deletions
|
@ -216,6 +216,7 @@
|
||||||
<Compile Include="Serializer\IntConverter.cs" />
|
<Compile Include="Serializer\IntConverter.cs" />
|
||||||
<Compile Include="Serializer\Json.cs" />
|
<Compile Include="Serializer\Json.cs" />
|
||||||
<Compile Include="Serializer\JsonVisitor.cs" />
|
<Compile Include="Serializer\JsonVisitor.cs" />
|
||||||
|
<Compile Include="Serializer\UnderscoreStringEnumConverter.cs" />
|
||||||
<Compile Include="ServiceFactory.cs" />
|
<Compile Include="ServiceFactory.cs" />
|
||||||
<Compile Include="ServiceProvider.cs" />
|
<Compile Include="ServiceProvider.cs" />
|
||||||
<Compile Include="Extensions\StringExtensions.cs" />
|
<Compile Include="Extensions\StringExtensions.cs" />
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace NzbDrone.Common.Serializer
|
||||||
|
{
|
||||||
|
public class UnderscoreStringEnumConverter : JsonConverter
|
||||||
|
{
|
||||||
|
public object UnknownValue { get; set; }
|
||||||
|
|
||||||
|
public UnderscoreStringEnumConverter(object unknownValue)
|
||||||
|
{
|
||||||
|
UnknownValue = unknownValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool CanConvert(Type objectType)
|
||||||
|
{
|
||||||
|
return objectType.IsEnum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
var enumString = reader.Value.ToString().Replace("_", string.Empty);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Enum.Parse(objectType, enumString, true);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
if (UnknownValue == null)
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
return UnknownValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
var enumText = value.ToString();
|
||||||
|
var builder = new StringBuilder(enumText.Length + 4);
|
||||||
|
builder.Append(char.ToLower(enumText[0]));
|
||||||
|
for (int i = 1; i < enumText.Length; i++)
|
||||||
|
{
|
||||||
|
if (char.IsUpper(enumText[i]))
|
||||||
|
{
|
||||||
|
builder.Append('_');
|
||||||
|
}
|
||||||
|
builder.Append(char.ToLower(enumText[i]));
|
||||||
|
}
|
||||||
|
enumText = builder.ToString();
|
||||||
|
|
||||||
|
writer.WriteValue(enumText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
using FluentAssertions;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NzbDrone.Core.Download.Clients.DownloadStation;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class DownloadStationsTaskStatusJsonConverterFixture
|
||||||
|
{
|
||||||
|
[TestCase("captcha_needed", DownloadStationTaskStatus.CaptchaNeeded)]
|
||||||
|
[TestCase("filehosting_waiting", DownloadStationTaskStatus.FilehostingWaiting)]
|
||||||
|
[TestCase("hash_checking", DownloadStationTaskStatus.HashChecking)]
|
||||||
|
[TestCase("error", DownloadStationTaskStatus.Error)]
|
||||||
|
[TestCase("downloading", DownloadStationTaskStatus.Downloading)]
|
||||||
|
public void should_parse_enum_correctly(string value, DownloadStationTaskStatus expected)
|
||||||
|
{
|
||||||
|
var task = "{\"Status\": \"" + value + "\"}";
|
||||||
|
|
||||||
|
var item = JsonConvert.DeserializeObject<DownloadStationTask>(task);
|
||||||
|
|
||||||
|
item.Status.Should().Be(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase("captcha_needed", DownloadStationTaskStatus.CaptchaNeeded)]
|
||||||
|
[TestCase("filehosting_waiting", DownloadStationTaskStatus.FilehostingWaiting)]
|
||||||
|
[TestCase("hash_checking", DownloadStationTaskStatus.HashChecking)]
|
||||||
|
[TestCase("error", DownloadStationTaskStatus.Error)]
|
||||||
|
[TestCase("downloading", DownloadStationTaskStatus.Downloading)]
|
||||||
|
public void should_serialize_enum_correctly(string expected, DownloadStationTaskStatus value)
|
||||||
|
{
|
||||||
|
var task = new DownloadStationTask { Status = value };
|
||||||
|
|
||||||
|
var item = JsonConvert.SerializeObject(task);
|
||||||
|
|
||||||
|
item.Should().Contain(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_return_unknown_if_unknown_enum_value()
|
||||||
|
{
|
||||||
|
var task = "{\"Status\": \"some_unknown_value\"}";
|
||||||
|
|
||||||
|
var item = JsonConvert.DeserializeObject<DownloadStationTask>(task);
|
||||||
|
|
||||||
|
item.Status.Should().Be(DownloadStationTaskStatus.Unknown);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -604,9 +604,12 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
|
||||||
[TestCase(DownloadStationTaskStatus.Finished, DownloadItemStatus.Completed)]
|
[TestCase(DownloadStationTaskStatus.Finished, DownloadItemStatus.Completed)]
|
||||||
[TestCase(DownloadStationTaskStatus.Finishing, DownloadItemStatus.Downloading)]
|
[TestCase(DownloadStationTaskStatus.Finishing, DownloadItemStatus.Downloading)]
|
||||||
[TestCase(DownloadStationTaskStatus.HashChecking, DownloadItemStatus.Downloading)]
|
[TestCase(DownloadStationTaskStatus.HashChecking, DownloadItemStatus.Downloading)]
|
||||||
|
[TestCase(DownloadStationTaskStatus.CaptchaNeeded, DownloadItemStatus.Downloading)]
|
||||||
[TestCase(DownloadStationTaskStatus.Paused, DownloadItemStatus.Paused)]
|
[TestCase(DownloadStationTaskStatus.Paused, DownloadItemStatus.Paused)]
|
||||||
[TestCase(DownloadStationTaskStatus.Seeding, DownloadItemStatus.Completed)]
|
[TestCase(DownloadStationTaskStatus.Seeding, DownloadItemStatus.Completed)]
|
||||||
|
[TestCase(DownloadStationTaskStatus.FilehostingWaiting, DownloadItemStatus.Queued)]
|
||||||
[TestCase(DownloadStationTaskStatus.Waiting, DownloadItemStatus.Queued)]
|
[TestCase(DownloadStationTaskStatus.Waiting, DownloadItemStatus.Queued)]
|
||||||
|
[TestCase(DownloadStationTaskStatus.Unknown, DownloadItemStatus.Queued)]
|
||||||
public void GetItems_should_return_item_as_downloadItemStatus(DownloadStationTaskStatus apiStatus, DownloadItemStatus expectedItemStatus)
|
public void GetItems_should_return_item_as_downloadItemStatus(DownloadStationTaskStatus apiStatus, DownloadItemStatus expectedItemStatus)
|
||||||
{
|
{
|
||||||
GivenSerialNumber();
|
GivenSerialNumber();
|
||||||
|
|
|
@ -414,8 +414,12 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
|
||||||
[TestCase(DownloadStationTaskStatus.Finished, DownloadItemStatus.Completed)]
|
[TestCase(DownloadStationTaskStatus.Finished, DownloadItemStatus.Completed)]
|
||||||
[TestCase(DownloadStationTaskStatus.Finishing, DownloadItemStatus.Downloading)]
|
[TestCase(DownloadStationTaskStatus.Finishing, DownloadItemStatus.Downloading)]
|
||||||
[TestCase(DownloadStationTaskStatus.HashChecking, DownloadItemStatus.Downloading)]
|
[TestCase(DownloadStationTaskStatus.HashChecking, DownloadItemStatus.Downloading)]
|
||||||
|
[TestCase(DownloadStationTaskStatus.CaptchaNeeded, DownloadItemStatus.Downloading)]
|
||||||
[TestCase(DownloadStationTaskStatus.Paused, DownloadItemStatus.Paused)]
|
[TestCase(DownloadStationTaskStatus.Paused, DownloadItemStatus.Paused)]
|
||||||
|
[TestCase(DownloadStationTaskStatus.Seeding, DownloadItemStatus.Completed)]
|
||||||
|
[TestCase(DownloadStationTaskStatus.FilehostingWaiting, DownloadItemStatus.Queued)]
|
||||||
[TestCase(DownloadStationTaskStatus.Waiting, DownloadItemStatus.Queued)]
|
[TestCase(DownloadStationTaskStatus.Waiting, DownloadItemStatus.Queued)]
|
||||||
|
[TestCase(DownloadStationTaskStatus.Unknown, DownloadItemStatus.Queued)]
|
||||||
public void GetItems_should_return_item_as_downloadItemStatus(DownloadStationTaskStatus apiStatus, DownloadItemStatus expectedItemStatus)
|
public void GetItems_should_return_item_as_downloadItemStatus(DownloadStationTaskStatus apiStatus, DownloadItemStatus expectedItemStatus)
|
||||||
{
|
{
|
||||||
GivenSerialNumber();
|
GivenSerialNumber();
|
||||||
|
|
|
@ -147,6 +147,7 @@
|
||||||
<Compile Include="Download\DownloadClientTests\Blackhole\UsenetBlackholeFixture.cs" />
|
<Compile Include="Download\DownloadClientTests\Blackhole\UsenetBlackholeFixture.cs" />
|
||||||
<Compile Include="Download\DownloadClientTests\DelugeTests\DelugeFixture.cs" />
|
<Compile Include="Download\DownloadClientTests\DelugeTests\DelugeFixture.cs" />
|
||||||
<Compile Include="Download\DownloadClientTests\DownloadClientFixtureBase.cs" />
|
<Compile Include="Download\DownloadClientTests\DownloadClientFixtureBase.cs" />
|
||||||
|
<Compile Include="Download\DownloadClientTests\DownloadStationTests\DownloadStationsTaskStatusJsonConverterFixture.cs" />
|
||||||
<Compile Include="Download\DownloadClientTests\DownloadStationTests\TorrentDownloadStationFixture.cs" />
|
<Compile Include="Download\DownloadClientTests\DownloadStationTests\TorrentDownloadStationFixture.cs" />
|
||||||
<Compile Include="Download\DownloadClientTests\DownloadStationTests\SerialNumberProviderFixture.cs" />
|
<Compile Include="Download\DownloadClientTests\DownloadStationTests\SerialNumberProviderFixture.cs" />
|
||||||
<Compile Include="Download\DownloadClientTests\DownloadStationTests\SharedFolderResolverFixture.cs" />
|
<Compile Include="Download\DownloadClientTests\DownloadStationTests\SharedFolderResolverFixture.cs" />
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Converters;
|
using NzbDrone.Common.Serializer;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Download.Clients.DownloadStation
|
namespace NzbDrone.Core.Download.Clients.DownloadStation
|
||||||
{
|
{
|
||||||
|
@ -23,7 +22,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
|
||||||
[JsonProperty(PropertyName = "status_extra")]
|
[JsonProperty(PropertyName = "status_extra")]
|
||||||
public Dictionary<string, string> StatusExtra { get; set; }
|
public Dictionary<string, string> StatusExtra { get; set; }
|
||||||
|
|
||||||
[JsonConverter(typeof(StringEnumConverter))]
|
[JsonConverter(typeof(UnderscoreStringEnumConverter), DownloadStationTaskStatus.Unknown)]
|
||||||
public DownloadStationTaskStatus Status { get; set; }
|
public DownloadStationTaskStatus Status { get; set; }
|
||||||
|
|
||||||
public DownloadStationTaskAdditional Additional { get; set; }
|
public DownloadStationTaskAdditional Additional { get; set; }
|
||||||
|
@ -41,6 +40,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
|
||||||
|
|
||||||
public enum DownloadStationTaskStatus
|
public enum DownloadStationTaskStatus
|
||||||
{
|
{
|
||||||
|
Unknown,
|
||||||
Waiting,
|
Waiting,
|
||||||
Downloading,
|
Downloading,
|
||||||
Paused,
|
Paused,
|
||||||
|
@ -48,9 +48,10 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
|
||||||
Finished,
|
Finished,
|
||||||
HashChecking,
|
HashChecking,
|
||||||
Seeding,
|
Seeding,
|
||||||
FileHostingWaiting,
|
FilehostingWaiting,
|
||||||
Extracting,
|
Extracting,
|
||||||
Error
|
Error,
|
||||||
|
CaptchaNeeded
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum DownloadStationPriority
|
public enum DownloadStationPriority
|
||||||
|
|
|
@ -227,7 +227,9 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
|
||||||
{
|
{
|
||||||
switch (torrent.Status)
|
switch (torrent.Status)
|
||||||
{
|
{
|
||||||
|
case DownloadStationTaskStatus.Unknown:
|
||||||
case DownloadStationTaskStatus.Waiting:
|
case DownloadStationTaskStatus.Waiting:
|
||||||
|
case DownloadStationTaskStatus.FilehostingWaiting:
|
||||||
return torrent.Size == 0 || GetRemainingSize(torrent) > 0 ? DownloadItemStatus.Queued : DownloadItemStatus.Completed;
|
return torrent.Size == 0 || GetRemainingSize(torrent) > 0 ? DownloadItemStatus.Queued : DownloadItemStatus.Completed;
|
||||||
case DownloadStationTaskStatus.Paused:
|
case DownloadStationTaskStatus.Paused:
|
||||||
return DownloadItemStatus.Paused;
|
return DownloadItemStatus.Paused;
|
||||||
|
|
|
@ -315,7 +315,9 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
|
||||||
{
|
{
|
||||||
switch (task.Status)
|
switch (task.Status)
|
||||||
{
|
{
|
||||||
|
case DownloadStationTaskStatus.Unknown:
|
||||||
case DownloadStationTaskStatus.Waiting:
|
case DownloadStationTaskStatus.Waiting:
|
||||||
|
case DownloadStationTaskStatus.FilehostingWaiting:
|
||||||
return task.Size == 0 || GetRemainingSize(task) > 0 ? DownloadItemStatus.Queued : DownloadItemStatus.Completed;
|
return task.Size == 0 || GetRemainingSize(task) > 0 ? DownloadItemStatus.Queued : DownloadItemStatus.Completed;
|
||||||
case DownloadStationTaskStatus.Paused:
|
case DownloadStationTaskStatus.Paused:
|
||||||
return DownloadItemStatus.Paused;
|
return DownloadItemStatus.Paused;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue