Skip to content

Commit

Permalink
Merge pull request #214 from dlcs/feature/jobRunnerBatchCompletion
Browse files Browse the repository at this point in the history
Adding ability to update a manifest with details from the DLCS
  • Loading branch information
JackLewis-digirati authored Jan 15, 2025
2 parents 0010def + 0d25f9a commit 0b41aa2
Show file tree
Hide file tree
Showing 51 changed files with 2,462 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using API.Tests.Helpers;
using Models.API.Manifest;
using Models.Database.General;
using Models.DLCS;
using CanvasPainting = Models.Database.CanvasPainting;
using DBManifest = Models.Database.Collections.Manifest;

Expand Down Expand Up @@ -168,7 +169,7 @@ public void SetGeneratedFields_SetsCanvasPainting_IfPresent()
Id = "the-canvas",
ChoiceOrder = 10,
CanvasOrder = 100,
AssetId = "1/2/assetId"
AssetId = new AssetId(1, 2, "assetId")
}
]
};
Expand All @@ -177,12 +178,52 @@ public void SetGeneratedFields_SetsCanvasPainting_IfPresent()
var result = iiifManifest.SetGeneratedFields(dbManifest, pathGenerator);

// Assert
var cp = result.PaintedResources.Single().CanvasPainting;
cp.CanvasId.Should().Be("http://base/123/canvases/the-canvas");
cp.ChoiceOrder.Should().Be(10);
cp.CanvasOrder.Should().Be(100);
cp.CanvasOriginalId.Should().Be("http://example.test/canvas1");
cp.AssetId.Should().Be("https://dlcs.test/customers/1/spaces/2/images/assetId");
var paintedResource = result.PaintedResources.Single();
paintedResource.CanvasPainting.CanvasId.Should().Be("http://base/123/canvases/the-canvas");
paintedResource.CanvasPainting.ChoiceOrder.Should().Be(10);
paintedResource.CanvasPainting.CanvasOrder.Should().Be(100);
paintedResource.CanvasPainting.CanvasOriginalId.Should().Be("http://example.test/canvas1");

paintedResource.Asset.GetValue("@id").ToString().Should().Be("https://dlcs.test/customers/1/spaces/2/images/assetId");
}

[Fact]
public void SetGeneratedFields_SetsCanvasPainting_WithoutAssetId()
{
// Arrange
var iiifManifest = new PresentationManifest();
var dbManifest = new DBManifest
{
CustomerId = 123,
Created = DateTime.UtcNow,
Modified = DateTime.UtcNow.AddDays(1),
CreatedBy = "creator",
ModifiedBy = "modifier",
Id = "id",
Hierarchy = [new Hierarchy { Slug = "slug" }],
CanvasPaintings =
[
new CanvasPainting
{
CanvasOriginalId = new Uri("http://example.test/canvas1"),
CustomerId = 123,
Id = "the-canvas",
ChoiceOrder = 10,
CanvasOrder = 100
}
]
};

// Act
var result = iiifManifest.SetGeneratedFields(dbManifest, pathGenerator);

// Assert
var paintedResource = result.PaintedResources.Single();
paintedResource.CanvasPainting.CanvasId.Should().Be("http://base/123/canvases/the-canvas");
paintedResource.CanvasPainting.ChoiceOrder.Should().Be(10);
paintedResource.CanvasPainting.CanvasOrder.Should().Be(100);
paintedResource.CanvasPainting.CanvasOriginalId.Should().Be("http://example.test/canvas1");
paintedResource.Asset.Should().BeNull();
}

[Fact]
Expand Down
19 changes: 4 additions & 15 deletions src/IIIFPresentation/API.Tests/Helpers/PathGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using API.Helpers;
using AWS.Helpers;
using Models.Database;
using Models.Database.Collections;
using Models.Database.General;
using Models.DLCS;

namespace API.Tests.Helpers;

Expand Down Expand Up @@ -384,31 +386,18 @@ public void GenerateAssetUri_Correct_IfCanvasPaintingHasAssetWithThreeSlashes()
{
Id = "hello",
CustomerId = 123,
AssetId = "5/4/12"
AssetId = new AssetId(5, 4, "12")
};

var expected = "https://dlcs.test/customers/5/spaces/4/images/12";

pathGenerator.GenerateAssetUri(manifest).Should().Be(expected);
}

[Fact]
public void GenerateAssetUri_Null_IfCanvasPaintingHasAssetWithNotThreeSlashes()
{
var manifest = new CanvasPainting()
{
Id = "hello",
CustomerId = 123,
AssetId = "5/4"
};

pathGenerator.GenerateAssetUri(manifest).Should().BeNull();
}

[Fact]
public void GenerateAssetUri_Null_IfCanvasPaintingDoesNotHaveAsset()
{
var manifest = new CanvasPainting()
var manifest = new CanvasPainting
{
Id = "hello",
CustomerId = 123,
Expand Down
20 changes: 11 additions & 9 deletions src/IIIFPresentation/API.Tests/Helpers/TestPathGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ namespace API.Tests.Helpers;

public static class TestPathGenerator
{
public static PathGenerator CreatePathGenerator(string baseUrl, string scheme)
public static PathGenerator CreatePathGenerator(string baseUrl, string scheme)
=> new(new HttpContextAccessor
{
HttpContext = new DefaultHttpContext
{
Request =
HttpContext = new DefaultHttpContext
{
Scheme = scheme,
Host = new HostString(baseUrl)
Request =
{
Scheme = scheme,
Host = new HostString(baseUrl)
}
}
}
}, Options.Create(new DlcsSettings { ApiUri = new Uri("https://dlcs.test") }));
}
},
Options.Create(new DlcsSettings
{ ApiUri = new Uri("https://dlcs.test") }));
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public async Task CreateManifest_CorrectlyCreatesAssetRequests_WithSpace()
dbManifest.CanvasPaintings.Should().HaveCount(1);
dbManifest.CanvasPaintings!.First().Label!.First().Value[0].Should().Be("canvas testing");
// space comes from the asset
dbManifest.CanvasPaintings!.First().AssetId.Should().Be($"{Customer}/{space}/{assetId}");
dbManifest.CanvasPaintings!.First().AssetId.ToString().Should().Be($"{Customer}/{space}/{assetId}");
dbManifest.Batches.Should().HaveCount(1);
dbManifest.Batches!.First().Status.Should().Be(BatchStatus.Ingesting);
dbManifest.Batches!.First().Id.Should().Be(batchId);
Expand Down Expand Up @@ -288,7 +288,7 @@ public async Task CreateManifest_CorrectlyCreatesAssetRequests_WithoutSpace()
dbManifest.CanvasPaintings.Should().HaveCount(1);
dbManifest.CanvasPaintings!.First().Label!.First().Value[0].Should().Be("canvas testing");
// space added using the manifest space
dbManifest.CanvasPaintings!.First().AssetId.Should()
dbManifest.CanvasPaintings!.First().AssetId.ToString().Should()
.Be($"{Customer}/{NewlyCreatedSpace}/{assetId}");
dbManifest.Batches.Should().HaveCount(1);
dbManifest.Batches!.First().Status.Should().Be(BatchStatus.Ingesting);
Expand Down Expand Up @@ -383,7 +383,7 @@ await amazonS3.GetObjectAsync(LocalStackFixture.StorageBucketName,
dbManifest.CanvasPaintings.Should().HaveCount(1);
dbManifest.CanvasPaintings!.First().Label?.First().Should().BeNull();
// space added using the manifest space
dbManifest.CanvasPaintings!.First().AssetId.Should()
dbManifest.CanvasPaintings!.First().AssetId.ToString().Should()
.Be($"{Customer}/{NewlyCreatedSpace}/{assetId}");
dbManifest.Batches.Should().HaveCount(1);
dbManifest.Batches!.First().Status.Should().Be(BatchStatus.Ingesting);
Expand Down Expand Up @@ -595,7 +595,7 @@ public async Task CreateManifest_CorrectlyCreatesAssetRequests_WhenMultipleAsset
foreach (var canvasPainting in dbManifest.CanvasPaintings!)
{
canvasPainting.CanvasOrder.Should().Be(currentCanvasOrder);
canvasPainting.AssetId.Should()
canvasPainting.AssetId.ToString().Should()
.Be($"{Customer}/{NewlyCreatedSpace}/testAssetByPresentation-multipleAssets-{currentCanvasOrder}");
currentCanvasOrder++;
}
Expand Down
7 changes: 6 additions & 1 deletion src/IIIFPresentation/API/Converters/ManifestConverter.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using API.Helpers;
using Core.Helpers;
using Core.IIIF;
using DLCS.Models;
using IIIF;
using IIIF.Presentation;
using Models.API.Manifest;
using Models.Database.Collections;
using Models.Database.General;
using Newtonsoft.Json.Linq;

namespace API.Converters;

Expand Down Expand Up @@ -62,7 +64,10 @@ public static PresentationManifest SetGeneratedFields(this PresentationManifest
Label = cp.Label,
CanvasOriginalId = cp.CanvasOriginalId?.ToString(),
CanvasLabel = cp.CanvasLabel,
AssetId = pathGenerator.GenerateAssetUri(cp)?.ToString()
},
Asset = cp.AssetId == null ? null : new JObject
{
["@id"] = pathGenerator.GenerateAssetUri(cp)?.ToString()
}
}).ToList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Models.API.General;
using Models.API.Manifest;
using Models.Database;
using Models.DLCS;
using Newtonsoft.Json.Linq;
using Repository.Manifests;
using CanvasPainting = Models.Database.CanvasPainting;
Expand Down Expand Up @@ -217,8 +218,9 @@ public class CanvasPaintingResolver(
Created = DateTime.UtcNow,
CustomerId = customerId,
CanvasOrder = count,
AssetId = $"{customerId}/{space}/{id}",
ChoiceOrder = -1
AssetId = AssetId.FromString($"{customerId}/{space}/{id}"),
ChoiceOrder = -1,
Ingesting = true
};

count++;
Expand Down
5 changes: 3 additions & 2 deletions src/IIIFPresentation/API/Features/Manifest/ManifestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using API.Features.Manifest.Helpers;
using API.Features.Storage.Helpers;
using API.Helpers;
using API.Infrastructure.AWS;
using API.Infrastructure.Helpers;
using API.Infrastructure.IdGenerator;
using API.Infrastructure.Validation;
using API.Settings;
using AWS.Helpers;
using Core;
using Core.Auth;
using Core.Helpers;
Expand All @@ -19,6 +19,7 @@
using Models.API.Manifest;
using Models.Database.Collections;
using Models.Database.General;
using Newtonsoft.Json.Linq;
using Repository;
using Repository.Helpers;
using Collection = Models.Database.Collections.Collection;
Expand Down Expand Up @@ -254,7 +255,7 @@ await canvasPaintingResolver.GenerateCanvasPaintings(request.CustomerId, request
var saveErrors = await SaveAndPopulateEntity(request, dbManifest, cancellationToken);
return (saveErrors, dbManifest);
}

private async Task<int?> CreateSpaceIfRequired(int customerId, string manifestId, bool createSpace,
CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using API.Features.Storage.Helpers;
using API.Infrastructure.AWS;
using AWS.Helpers;
using Core;
using MediatR;
using Microsoft.EntityFrameworkCore;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using API.Converters;
using API.Features.Storage.Helpers;
using API.Helpers;
using API.Infrastructure.AWS;
using API.Infrastructure.Requests;
using AWS.Helpers;
using MediatR;
using Models.API.Manifest;
using Repository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using API.Converters;
using API.Converters.Streaming;
using API.Converters.Streaming;
using API.Helpers;
using AWS.Helpers;
using AWS.S3;
using AWS.Settings;
using Core.Streams;
Expand Down Expand Up @@ -56,4 +56,4 @@ public class GetManifestHierarchicalHandler(
memoryStream.Seek(0, SeekOrigin.Begin);
return await reader.ReadToEndAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using API.Features.Storage.Helpers;
using API.Features.Storage.Models;
using API.Helpers;
using API.Infrastructure.AWS;
using API.Infrastructure.IdGenerator;
using API.Infrastructure.Requests;
using API.Infrastructure.Validation;
using API.Settings;
using AWS.Helpers;
using Core;
using Core.Auth;
using Core.Helpers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using API.Features.Storage.Helpers;
using API.Infrastructure.AWS;
using AWS.Helpers;
using Core;
using MediatR;
using Microsoft.EntityFrameworkCore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using API.Features.Storage.Helpers;
using API.Features.Storage.Models;
using API.Helpers;
using AWS.Helpers;
using AWS.S3;
using AWS.S3.Models;
using AWS.Settings;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Data;
using API.Features.Storage.Helpers;
using API.Helpers;
using API.Infrastructure.AWS;
using API.Infrastructure.IdGenerator;
using API.Infrastructure.Requests;
using AWS.Helpers;
using Core;
using Core.Auth;
using Core.IIIF;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using API.Features.Storage.Helpers;
using API.Features.Storage.Models;
using API.Helpers;
using API.Infrastructure.AWS;
using API.Infrastructure.Helpers;
using API.Infrastructure.Requests;
using API.Infrastructure.Validation;
using API.Settings;
using AWS.Helpers;
using Core;
using Core.Auth;
using Core.Exceptions;
Expand Down
10 changes: 5 additions & 5 deletions src/IIIFPresentation/API/Helpers/PathGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using API.Infrastructure.Requests;
using DLCS;
using DLCS.Models;
using Microsoft.Extensions.Options;
using Models.Database.Collections;
using Models.Database.General;
using Models.DLCS;
using CanvasPainting = Models.Database.CanvasPainting;

namespace API.Helpers;
Expand Down Expand Up @@ -94,13 +96,11 @@ public string GenerateCanvasId(CanvasPainting canvasPainting)

public Uri? GenerateAssetUri(CanvasPainting canvasPainting)
{
if (string.IsNullOrEmpty(canvasPainting.AssetId)) return null;
var assetId = canvasPainting.AssetId.Split('/');
if (assetId.Length != 3) return null;

if (canvasPainting.AssetId == null) return null;

var uriBuilder = new UriBuilder(dlcsSettings.ApiUri)
{
Path = $"/customers/{assetId[0]}/spaces/{assetId[1]}/images/{assetId[2]}",
Path = $"/customers/{canvasPainting.AssetId.Customer}/spaces/{canvasPainting.AssetId.Space}/images/{canvasPainting.AssetId.Asset}",
};
return uriBuilder.Uri;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net;
using API.Converters;
using API.Exceptions;
using API.Infrastructure.Requests;
using API.Settings;
Expand Down Expand Up @@ -180,4 +179,4 @@ protected async Task<IActionResult> HandleRequest(Func<Task<IActionResult>> hand
return Problem(ex.Message, null, 500, errorTitle);
}
}
}
}
Loading

0 comments on commit 0b41aa2

Please sign in to comment.