Skip to content

Commit

Permalink
Merge pull request #15 from digirati-co-uk/feature/imageservice
Browse files Browse the repository at this point in the history
Updates related to ImageService3
  • Loading branch information
donaldgray authored May 12, 2022
2 parents 365a7cd + 3051776 commit 3a7f2fd
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 44 deletions.
72 changes: 69 additions & 3 deletions src/IIIF/IIIF.Tests/Presentation/V3/CanvasTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FluentAssertions;
using IIIF.Presentation.V3;
using IIIF.Presentation.V3.Annotation;
using IIIF.Presentation.V3.Content;
using IIIF.Presentation.V3.Strings;
using IIIF.Serialisation;
using Xunit;
Expand Down Expand Up @@ -85,7 +86,7 @@ public void SerialiseTargetAsId_True_RendersIdAsTarget()
[Fact]
public void SerialiseTargetAsId_False_RendersFullCanvasAsTarget()
{
var targetIsIdOnlyCanvas = new Canvas
var targetIsFullCanvas = new Canvas
{
Id = "https://test.example.com/canvas/full",
Width = 1000,
Expand All @@ -102,7 +103,7 @@ public void SerialiseTargetAsId_False_RendersFullCanvasAsTarget()
Id = "https://test.example.com/canvas/referencing/page",
Items = new List<IAnnotation>
{
new PaintingAnnotation { Target = targetIsIdOnlyCanvas, }
new PaintingAnnotation { Target = targetIsFullCanvas, }
}
}
}
Expand All @@ -113,7 +114,7 @@ public void SerialiseTargetAsId_False_RendersFullCanvasAsTarget()
Context = "http://iiif.io/api/presentation/3/context.json",
Id = "https://test.example.com/manifest",
Label = new LanguageMap("en", "Test string"),
Items = new List<Canvas> { targetIsIdOnlyCanvas, referencingCanvas }
Items = new List<Canvas> { targetIsFullCanvas, referencingCanvas }
};

var serialisedManifest = manifest.AsJson().Replace("\r\n", "\n");
Expand Down Expand Up @@ -157,5 +158,70 @@ public void SerialiseTargetAsId_False_RendersFullCanvasAsTarget()

serialisedManifest.Should().BeEquivalentTo(expected);
}

[Fact]
public void CanSelfReferenceCanvas()
{
var canvas = new Canvas
{
Id = "https://test.example.com/canvas/target-id-only",
SerialiseTargetAsId = true
};

canvas.Items = new List<AnnotationPage>
{
new()
{
Id = "https://test.example.com/canvas/referencing/page",
Items = new List<IAnnotation>
{
new PaintingAnnotation
{
Id = "https://test.example.com/canvas/referencing/page/anno",
Target = canvas,
}
}
}
};

var manifest = new Manifest
{
Context = "http://iiif.io/api/presentation/3/context.json",
Id = "https://test.example.com/manifest",
Label = new LanguageMap("en", "Test string"),
Items = new List<Canvas> { canvas }
};

var serialisedManifest = manifest.AsJson().Replace("\r\n", "\n");

const string expected = @"{
""@context"": ""http://iiif.io/api/presentation/3/context.json"",
""id"": ""https://test.example.com/manifest"",
""type"": ""Manifest"",
""label"": {""en"":[""Test string""]},
""items"": [
{
""id"": ""https://test.example.com/canvas/target-id-only"",
""type"": ""Canvas"",
""items"": [
{
""id"": ""https://test.example.com/canvas/referencing/page"",
""type"": ""AnnotationPage"",
""items"": [
{
""id"": ""https://test.example.com/canvas/referencing/page/anno"",
""type"": ""Annotation"",
""motivation"": ""painting"",
""target"": ""https://test.example.com/canvas/target-id-only""
}
]
}
]
}
]
}";

serialisedManifest.Should().BeEquivalentTo(expected);
}
}
}
38 changes: 38 additions & 0 deletions src/IIIF/IIIF.Tests/Serialisation/ImageService3SerialiserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using FluentAssertions;
using IIIF.ImageApi.Service;
using IIIF.Serialisation;
using Xunit;

namespace IIIF.Tests.Serialisation
{
public class ImageService3SerialiserTests
{
[Fact]
public void WriteJson_OutputsExpected_IfNoProfileOrProfileDescription()
{
// Arrange
var imageService = new ImageService3 { Id = "foo" };
const string expected = "{\n \"id\": \"foo\",\n \"type\": \"ImageService3\"\n}";

// Act
var result = imageService.AsJson().Replace("\r\n", "\n");

// Assert
result.Should().Be(expected);
}

[Fact]
public void WriteJson_OutputsExpected_ProfileOnly()
{
// Arrange
var imageService = new ImageService3 { Id = "foo", Profile = "bar" };
const string expected = "{\n \"id\": \"foo\",\n \"type\": \"ImageService3\",\n \"profile\": \"bar\"\n}";

// Act
var result = imageService.AsJson().Replace("\r\n", "\n");

// Assert
result.Should().Be(expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public void CanDeserialiseSerialisedManifest()
Context = ImageService2.Image2Context,
Width = 1000,
Height = 1001
},
new ImageService3
{
Id = "https://test.example.com/canvas/1/image/3",
Profile = ImageService3.Level2Profile,
Context = ImageService3.Image3Context,
Width = 1000,
Height = 1001
}
},
}
Expand Down
14 changes: 6 additions & 8 deletions src/IIIF/IIIF/ImageApi/Service/ImageService3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@

namespace IIIF.ImageApi.Service
{
public class ImageService3 : ResourceBase, IService
public class ImageService3 : ResourceBase
{
public const string Image3Context = "http://iiif.io/api/image/3/context.json";
public const string Level0Profile = "level0";
public const string Level1Profile = "level1";
public const string Level2Profile = "level2";
public const string ImageProtocol = "http://iiif.io/api/image";

public ImageService3()
{
Context = Image3Context;
}

public override string Type => nameof(ImageService3);

[JsonProperty(Order = 3)]
public string Protocol => ImageProtocol;
[JsonProperty(Order = 10)]
public string Protocol { get; set; }

[JsonProperty(Order = 11)]
public int Width { get; set; }
Expand Down
14 changes: 0 additions & 14 deletions src/IIIF/IIIF/Presentation/V2/ImageService2Reference.cs

This file was deleted.

9 changes: 0 additions & 9 deletions src/IIIF/IIIF/Presentation/V3/ImageService3Reference.cs

This file was deleted.

27 changes: 22 additions & 5 deletions src/IIIF/IIIF/Serialisation/Deserialisation/ServiceConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,29 @@ public class ServiceConverter : ReadOnlyConverter<IService>

if (service == null)
{
service = jsonObject["@type"].Value<string>() switch
var atType = jsonObject["@type"];
if (atType != null)
{
"SearchService1" => new Search.V1.SearchService(),
nameof(ImageApi.Service.ImageService2) => new ImageApi.Service.ImageService2(),
_ => null
};
service = atType.Value<string>() switch
{
"SearchService1" => new Search.V1.SearchService(),
nameof(ImageApi.Service.ImageService2) => new ImageApi.Service.ImageService2(),
_ => null
};
}
}

if (service == null)
{
var type = jsonObject["type"];
if (type != null)
{
service = type.Value<string>() switch
{
nameof(ImageApi.Service.ImageService3) => new ImageApi.Service.ImageService3(),
_ => null
};
}
}

// TODO handle ResourceBase items?
Expand Down
1 change: 1 addition & 0 deletions src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static class IIIFSerialiserX
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new PrettyIIIFContractResolver(),
Formatting = Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
Converters = new List<JsonConverter>
{
new SizeConverter(), new StringArrayConverter(), new ServiceReferenceConverter(),
Expand Down
3 changes: 1 addition & 2 deletions src/IIIF/IIIF/Serialisation/ServiceReferenceConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using IIIF.Presentation.V2.Serialisation;
using Newtonsoft.Json;
using Newtonsoft.Json;

namespace IIIF.Serialisation
{
Expand Down
3 changes: 1 addition & 2 deletions src/IIIF/IIIF/Serialisation/SizeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using IIIF.Presentation.V2.Serialisation;
using Newtonsoft.Json;
using Newtonsoft.Json;

namespace IIIF.Serialisation
{
Expand Down
1 change: 0 additions & 1 deletion src/IIIF/IIIF/Serialisation/TargetConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using IIIF.Presentation.V3;
using IIIF.Utils;
using Newtonsoft.Json;
Expand Down

0 comments on commit 3a7f2fd

Please sign in to comment.