Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the way the S3 top level collection id is generated to match automated tests #120

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using API.Converters;
using API.Converters.Streaming;
using LateApexEarlySpeed.Xunit.Assertion.Json;

Expand All @@ -9,24 +10,38 @@ public class S3StoredJsonProcessorTests
[Fact]
public void ProcessJson_ChangesTopLevelId()
{
const string requestSlug = "fnord/quark/4534";
const string requestSlug =
"0004_hierarchical_iiif_collection_2O71nF/0004_hierarchical_iiif_collection_child_PWZnry";
const int customerId = 52;
var urlRoots = new UrlRoots
{
BaseUrl = "http://localhost"
};

var result = GetProcessed(ManifestJsonWithId, new(requestSlug));
var result = GetProcessed(ManifestJsonWithId, new(requestSlug, customerId, urlRoots));

JsonAssertion.Meet(root => root.IsJsonObject()
.HasProperty("id", p => p.IsJsonString().Equal($"managed:{requestSlug}")),
.HasProperty("id", p => p.IsJsonString().Equal(
$"http://localhost/52/{requestSlug}")),
result);
}

[Fact]
public void ProcessJson_AddsTopLevelId()
{
const string requestSlug = "fnord/quark/4534";
const string requestSlug =
"0004_hierarchical_iiif_collection_2O71nF/0004_hierarchical_iiif_collection_child_PWZnry";
const int customerId = 52;
var urlRoots = new UrlRoots
{
BaseUrl = "http://localhost"
};

var result = GetProcessed(ManifestJsonWithoutId, new(requestSlug));
var result = GetProcessed(ManifestJsonWithoutId, new(requestSlug, customerId, urlRoots));

JsonAssertion.Meet(root => root.IsJsonObject()
.HasProperty("id", p => p.IsJsonString().Equal($"managed:{requestSlug}")),
.HasProperty("id", p => p.IsJsonString().Equal(
$"http://localhost/52/{requestSlug}")),
result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

namespace API.Converters.Streaming;

public class S3StoredJsonProcessor(string requestSlug)
public class S3StoredJsonProcessor(string requestSlug, int customerId, UrlRoots roots)
: StreamingProcessorImplBase<S3StoredJsonProcessor.S3ProcessorCustomState>
{
private const string IdPropertyName = "id";

private string GeneratedId { get; } = new UriBuilder(roots.BaseUrl!)
{
Path = $"{customerId}/{requestSlug}"
}.Uri.ToString();

#region Overrides of StreamingProcessorImplBase<S3ProcessorCustomState>

public override object GetInitialState() => new S3ProcessorCustomState();
Expand All @@ -31,7 +36,7 @@ protected override void OnEndObject(ref Utf8JsonReader reader, Utf8JsonWriter wr
if (reader.CurrentDepth == 0 && !currentState.IdSet)
{
writer.WritePropertyName(IdPropertyName);
writer.WriteStringValue($"managed:{requestSlug}");
writer.WriteStringValue(GeneratedId);
}

base.OnEndObject(ref reader, writer, ref currentState);
Expand All @@ -56,7 +61,7 @@ protected override void OnEndObject(ref Utf8JsonReader reader, Utf8JsonWriter wr
currentState.IdSet = true;

// Found id in the top-level object
return $"managed:{requestSlug}";
return GeneratedId;
}

return v;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using API.Converters.Streaming;
using API.Converters;
using API.Converters.Streaming;
using API.Features.Storage.Helpers;
using API.Features.Storage.Models;
using API.Helpers;
Expand All @@ -9,18 +10,19 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Models.Database.Collections;
using Models.Database.General;
using Repository;
using Repository.Helpers;

namespace API.Features.Storage.Requests;

public class GetHierarchicalCollection(int customerId, string slug) : IRequest<CollectionWithItems>
public class GetHierarchicalCollection(int customerId, string slug, UrlRoots urlRoots) : IRequest<CollectionWithItems>
{
public int CustomerId { get; } = customerId;

public string Slug { get; } = slug;

public UrlRoots UrlRoots { get; } = urlRoots;
}

public class GetHierarchicalCollectionHandler(PresentationContext dbContext, IBucketReader bucketReader,
Expand Down Expand Up @@ -49,7 +51,8 @@ public async Task<CollectionWithItems> Handle(GetHierarchicalCollection request,
using var memoryStream = new MemoryStream();
using var reader = new StreamReader(memoryStream);
StreamingJsonProcessor.ProcessJson(objectFromS3.Stream, memoryStream,
objectFromS3.Headers.ContentLength, new S3StoredJsonProcessor(request.Slug));
objectFromS3.Headers.ContentLength,
new S3StoredJsonProcessor(request.Slug, request.CustomerId, request.UrlRoots));
memoryStream.Seek(0, SeekOrigin.Begin);
collectionFromS3 = await reader.ReadToEndAsync(cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class StorageController(IAuthenticator authenticator, IOptions<ApiSetting
[VaryHeader]
public async Task<IActionResult> GetHierarchicalCollection(int customerId, string slug = "")
{
var storageRoot = await Mediator.Send(new GetHierarchicalCollection(customerId, slug));
var storageRoot = await Mediator.Send(new GetHierarchicalCollection(customerId, slug, GetUrlRoots()));

if (storageRoot.Collection is not { IsPublic: true }) return this.PresentationNotFound();

Expand Down
Loading