-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from digirati-co-uk/cookbook-recipe-tests
Make iiif-net parse all the Manifests in the Cookbook
- Loading branch information
Showing
12 changed files
with
167 additions
and
15 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/IIIF/IIIF.Tests/Serialisation/CookbookDeserialization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using IIIF.Presentation.V3; | ||
using IIIF.Tests.Serialisation.Data; | ||
|
||
namespace IIIF.Tests.Serialisation; | ||
|
||
[Trait("Category", "Cookbook")] | ||
public class CookbookDeserialization | ||
{ | ||
[Theory] | ||
[ClassData(typeof(CookbookManifestData))] | ||
public void Can_Deserialize_Cookbook_Manifest(string manifestId, Manifest manifest) | ||
{ | ||
// perfunctory assertion | ||
manifest.Should().NotBeNull($"{manifestId} is a valid cookbook manifest"); | ||
manifest.Id.Should().Be(manifestId); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/IIIF/IIIF.Tests/Serialisation/Data/CookbookManifestData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using IIIF.Presentation.V3; | ||
using IIIF.Serialisation; | ||
|
||
namespace IIIF.Tests.Serialisation.Data; | ||
|
||
/// <summary> | ||
/// Used as [ClassData] - contains Manifests from IIIF Cookbook to validate deserialisation | ||
/// </summary> | ||
public class CookbookManifestData : IEnumerable<object[]> | ||
{ | ||
// This will store { manifest-id, deserialized-manifest } | ||
private readonly List<object[]> data = new(); | ||
|
||
// these have bugs in the cookbook, see https://github.com/IIIF/cookbook-recipes/pull/546 | ||
private List<string> skip = new() | ||
{ | ||
"https://iiif.io/api/cookbook/recipe/0219-using-caption-file/manifest.json", | ||
"https://iiif.io/api/cookbook/recipe/0040-image-rotation-service/manifest-service.json" | ||
}; | ||
|
||
public CookbookManifestData() | ||
{ | ||
using var httpClient = new HttpClient(); | ||
var theseusCollection = | ||
GetIIIFResource<Collection>("https://theseus-viewer.netlify.app/cookbook-collection.json", true); | ||
|
||
foreach (var item in theseusCollection.Items!) | ||
{ | ||
if (item is Manifest manifestRef) | ||
{ | ||
if (skip.Contains(manifestRef.Id)) continue; | ||
|
||
var iiif = GetIIIFResource<Manifest>(manifestRef.Id); | ||
data.Add(new object[] { manifestRef.Id, iiif }); | ||
} | ||
} | ||
|
||
T GetIIIFResource<T>(string url, bool mustSucceed = false) where T : JsonLdBase | ||
{ | ||
var resource = httpClient.GetAsync(url).Result; | ||
if (mustSucceed) resource.EnsureSuccessStatusCode(); | ||
if (!resource.IsSuccessStatusCode) return null; | ||
|
||
try | ||
{ | ||
var iiif = resource.Content.ReadAsStream().FromJsonStream<T>(); | ||
return iiif; | ||
} | ||
catch (Exception) | ||
{ | ||
if (mustSucceed) throw; | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
public IEnumerator<object[]> GetEnumerator() => data.GetEnumerator(); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace IIIF.Presentation.V3.Selectors; | ||
|
||
public class SvgSelector : ISelector | ||
{ | ||
public string? Type => nameof(SvgSelector); | ||
public string? Value { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
using IIIF.Presentation.V3.Selectors; | ||
using Newtonsoft.Json; | ||
using IIIF.Presentation.V3.Annotation; | ||
using IIIF.Presentation.V3.Selectors; | ||
using IIIF.Serialisation; | ||
|
||
namespace IIIF.Presentation.V3; | ||
|
||
public class SpecificResource : ResourceBase, IStructuralLocation | ||
public class SpecificResource : ResourceBase, IStructuralLocation, IPaintable | ||
{ | ||
public override string Type => nameof(SpecificResource); | ||
|
||
[JsonProperty(Order = 101)] public string Source { get; set; } | ||
[JsonConverter(typeof(SourceConverter))] | ||
[JsonProperty(Order = 101)] public IPaintable Source { get; set; } | ||
|
||
[JsonProperty(Order = 102)] public ISelector Selector { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using IIIF.Presentation.V3; | ||
using IIIF.Presentation.V3.Annotation; | ||
using IIIF.Presentation.V3.Content; | ||
using IIIF.Utils; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace IIIF.Serialisation; | ||
|
||
public class SourceConverter : JsonConverter<IPaintable> | ||
{ | ||
public override IPaintable? ReadJson(JsonReader reader, Type objectType, IPaintable? existingValue, bool hasExistingValue, | ||
JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType == JsonToken.String) | ||
{ | ||
// We do not know that this is a Canvas... | ||
// We would need knowledge of the rest of the IIIF Resource | ||
return new Canvas { Id = reader.Value.ToString() }; | ||
} | ||
else if (reader.TokenType == JsonToken.StartObject) | ||
{ | ||
var obj = JObject.Load(reader); | ||
var type = obj["type"].Value<string>(); | ||
IPaintable paintable = type switch | ||
{ | ||
nameof(Sound) => new Sound(), | ||
nameof(Video) => new Video(), | ||
nameof(Image) => new Image(), | ||
nameof(Canvas) => new Canvas(), | ||
nameof(SpecificResource) => new SpecificResource() | ||
}; | ||
serializer.Populate(obj.CreateReader(), paintable); | ||
return paintable; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, IPaintable? value, JsonSerializer serializer) | ||
{ | ||
if (value is Canvas canvas && (canvas.SerialiseTargetAsId || IsSimpleCanvas(canvas))) | ||
{ | ||
writer.WriteValue(canvas.Id); | ||
return; | ||
} | ||
|
||
// Default, pass through behaviour: | ||
JObject.FromObject(value, serializer).WriteTo(writer); | ||
} | ||
|
||
private static bool IsSimpleCanvas(Canvas canvas) | ||
{ | ||
return canvas.Width == null && canvas.Duration == null && canvas.Items.IsNullOrEmpty(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters