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

Allow DeliveryChannels to be modified #778

Merged
merged 14 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
Expand Up @@ -9,6 +9,7 @@
using DLCS.Core.Caching;
using DLCS.Core.Types;
using DLCS.Model.Assets;
using DLCS.Model.Policies;
using DLCS.Repository;
using DLCS.Repository.Assets;
using DLCS.Repository.Entities;
Expand Down Expand Up @@ -49,15 +50,14 @@ public ApiAssetRepositoryTests(DlcsDatabaseFixture dbFixture)

var entityCounterRepo = new EntityCounterRepository(dbContext);

var assetRepository = new AssetRepository(
dbContext,
var assetRepositoryCachingHelper = new AssetCachingHelper(
new MockCachingService(),
entityCounterRepo,
Options.Create(new CacheSettings()),
new NullLogger<AssetRepository>()
new NullLogger<AssetCachingHelper>()
);

sut = new ApiAssetRepository(dbContext, assetRepository, entityCounterRepo);
sut = new ApiAssetRepository(dbContext, entityCounterRepo, assetRepositoryCachingHelper,
new NullLogger<ApiAssetRepository>());

dbFixture.CleanUp();
}
Expand Down Expand Up @@ -376,17 +376,17 @@ public async Task DeleteAsset_ReturnsImageDeliveryChannels_FromDeletedAsset()
new()
{
Channel = AssetDeliveryChannels.Image,
DeliveryChannelPolicyId = 1
DeliveryChannelPolicyId = KnownDeliveryChannelPolicies.ImageDefault
},
new()
{
Channel = AssetDeliveryChannels.Thumbnails,
DeliveryChannelPolicyId = 3
DeliveryChannelPolicyId = KnownDeliveryChannelPolicies.ThumbsDefault
},
new()
{
Channel = AssetDeliveryChannels.File,
DeliveryChannelPolicyId = 4
DeliveryChannelPolicyId = KnownDeliveryChannelPolicies.FileNone
}
});
await contextForTests.SaveChangesAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using API.Features.DeliveryChannels;
using API.Features.DeliveryChannels.DataAccess;
using API.Tests.Integration.Infrastructure;
using DLCS.Core.Caching;
using DLCS.Model.DeliveryChannels;
Expand All @@ -23,8 +24,7 @@ public class DefaultDeliveryChannelRepositoryTests
public DefaultDeliveryChannelRepositoryTests(DlcsDatabaseFixture dbFixture)
{
dbContext = dbFixture.DbContext;
sut = new DefaultDeliveryChannelRepository(new MockCachingService(), new NullLogger<DefaultDeliveryChannelRepository>(),
Options.Create(new CacheSettings()), dbFixture.DbContext);
sut = new DefaultDeliveryChannelRepository(new MockCachingService(), new NullLogger<DefaultDeliveryChannelRepository>(), Options.Create(new CacheSettings()), dbFixture.DbContext);

dbFixture.CleanUp();

Expand Down Expand Up @@ -52,51 +52,51 @@ public DefaultDeliveryChannelRepositoryTests(DlcsDatabaseFixture dbFixture)
{
Space = 0,
Customer = 2,
DeliveryChannelPolicyId = 1,
DeliveryChannelPolicyId = KnownDeliveryChannelPolicies.ImageDefault,
MediaType = "image/*"
});

dbContext.SaveChanges();
}

[Fact]
public void MatchedDeliveryChannels_ReturnsAllDeliveryChannelPolicies_WhenCalled()
public async Task MatchedDeliveryChannels_ReturnsAllDeliveryChannelPolicies_WhenCalled()
{
// Arrange and Act
var matches = sut.MatchedDeliveryChannels("image/tiff", 1, 2);
var matches = await sut.MatchedDeliveryChannels("image/tiff", 1, 2);

// Assert
matches.Count.Should().Be(1);
matches.Count(x => x.Channel == "iiif-img").Should().Be(1);
}

[Fact]
public void MatchedDeliveryChannels_ShouldNotMatchAnything_WhenCalledWithInvalidMediaType()
public async Task MatchedDeliveryChannels_ShouldNotMatchAnything_WhenCalledWithInvalidMediaType()
{
// Arrange and Act
var matches = sut.MatchedDeliveryChannels("notValid/tiff", 1, 2);
var matches = await sut.MatchedDeliveryChannels("notValid/tiff", 1, 2);

// Assert
matches.Count.Should().Be(0);
}

[Fact]
public void MatchDeliveryChannelPolicyForChannel_MatchesDeliveryChannel_WhenMatchAvailable()
public async Task MatchDeliveryChannelPolicyForChannel_MatchesDeliveryChannel_WhenMatchAvailable()
{
// Arrange and Act
var matches = sut.MatchDeliveryChannelPolicyForChannel("image/tiff", 1, 2, "iiif-img");
var matches = await sut.MatchDeliveryChannelPolicyForChannel("image/tiff", 1, 2, "iiif-img");

// Assert
matches.Should().NotBeNull();
}

[Fact]
public void MatchDeliveryChannelPolicyForChannel_ThrowsException_WhenNotMatched()
public async Task MatchDeliveryChannelPolicyForChannel_ThrowsException_WhenNotMatched()
{
// Arrange and Act
Action action = () => sut.MatchDeliveryChannelPolicyForChannel("notMatched/tiff", 1, 2, "iiif-img");
Func<Task> action = () => sut.MatchDeliveryChannelPolicyForChannel("notMatched/tiff", 1, 2, "iiif-img");

// Assert
action.Should().ThrowExactly<InvalidOperationException>();
await action.Should().ThrowExactlyAsync<InvalidOperationException>();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using API.Features.DeliveryChannels;
using API.Features.DeliveryChannels.DataAccess;
using API.Tests.Integration.Infrastructure;
using DLCS.Core.Caching;
using DLCS.Model.Policies;
Expand All @@ -13,15 +14,15 @@ namespace API.Tests.Features.DeliveryChannels;

[Trait("Category", "Database")]
[Collection(CollectionDefinitions.DatabaseCollection.CollectionName)]
public class DeliveryChannelPoliciesTests
public class DeliveryChannelPolicyRepositoryTests
{
private readonly DlcsContext dbContext;
private readonly DeliveryChannelPolicyRepository sut;

public DeliveryChannelPoliciesTests(DlcsDatabaseFixture dbFixture)
public DeliveryChannelPolicyRepositoryTests(DlcsDatabaseFixture dbFixture)
{
dbContext = dbFixture.DbContext;
sut = new DeliveryChannelPolicyRepository(new MockCachingService() ,new NullLogger<DeliveryChannelPolicyRepository>(), Options.Create(new CacheSettings()), dbFixture.DbContext);
sut = new DeliveryChannelPolicyRepository(new MockCachingService(), new NullLogger<DeliveryChannelPolicyRepository>(), Options.Create(new CacheSettings()), dbFixture.DbContext);

dbFixture.CleanUp();

Expand All @@ -44,35 +45,35 @@ public DeliveryChannelPoliciesTests(DlcsDatabaseFixture dbFixture)
[InlineData("space-specific-image")]
[InlineData("channel/space-specific-image")]
[InlineData("https://dlcs.api/customers/2/deliveryChannelPolicies/iiif-img/space-specific-image")]
public void RetrieveDeliveryChannelPolicy_RetrievesACustomerSpecificPolicy(string policy)
public async Task RetrieveDeliveryChannelPolicy_RetrievesACustomerSpecificPolicy(string policy)
{
// Arrange and Act
var deliveryChannelPolicy = sut.RetrieveDeliveryChannelPolicy(2, "iiif-img", policy);
var deliveryChannelPolicy = await sut.RetrieveDeliveryChannelPolicy(2, "iiif-img", policy);

// Assert
deliveryChannelPolicy.Channel.Should().Be("iiif-img");
deliveryChannelPolicy.Customer.Should().Be(2);
}

[Fact]
public void RetrieveDeliveryChannelPolicy_RetrievesADefaultPolicy()
public async Task RetrieveDeliveryChannelPolicy_RetrievesADefaultPolicy()
{
// Arrange and Act
var policy = sut.RetrieveDeliveryChannelPolicy(2, "iiif-img", "default");
var policy = await sut.RetrieveDeliveryChannelPolicy(2, "iiif-img", "default");

// Assert
policy.Channel.Should().Be("iiif-img");
policy.Customer.Should().Be(1);
}

[Fact]
public void RetrieveDeliveryChannelPolicy_RetrieveNonExistentPolicy()
public async Task RetrieveDeliveryChannelPolicy_RetrieveNonExistentPolicy()
{
// Arrange and Act
Action action = () => sut.RetrieveDeliveryChannelPolicy(2, "notAChannel", "notAPolicy");
Func<Task> action = () => sut.RetrieveDeliveryChannelPolicy(2, "notAChannel", "notAPolicy");

// Assert
action.Should()
.Throw<InvalidOperationException>();
await action.Should()
.ThrowAsync<InvalidOperationException>();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using API.Features.Assets;
using API.Features.Image;
using API.Features.Image.Ingest;
using API.Settings;
using DLCS.Core.Types;
using DLCS.HydraModel;
using DLCS.Model.Assets;
using DLCS.Model.DeliveryChannels;
using DLCS.Model.Storage;
using FakeItEasy;
using Microsoft.Extensions.Logging.Abstractions;
using Test.Helpers.Settings;
using CustomerStorage = DLCS.Model.Storage.CustomerStorage;
using StoragePolicy = DLCS.Model.Storage.StoragePolicy;
Expand All @@ -32,17 +31,20 @@ public AssetProcessorTest()
assetRepository = A.Fake<IApiAssetRepository>();
defaultDeliveryChannelRepository = A.Fake<IDefaultDeliveryChannelRepository>();
deliveryChannelPolicyRepository = A.Fake<IDeliveryChannelPolicyRepository>();

var deliveryChannelProcessor = new DeliveryChannelProcessor(defaultDeliveryChannelRepository, deliveryChannelPolicyRepository,
new NullLogger<DeliveryChannelProcessor>());

var optionsMonitor = OptionsHelpers.GetOptionsMonitor(apiSettings);

sut = new AssetProcessor(assetRepository, storageRepository, defaultDeliveryChannelRepository, deliveryChannelPolicyRepository, optionsMonitor);
sut = new AssetProcessor(assetRepository, storageRepository, deliveryChannelProcessor, optionsMonitor);
}

[Fact]
public async Task Process_ChecksForMaximumNumberOfImages_Exceeded()
{
// Arrange
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._)).Returns<Asset?>(null);
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._, A<bool>._)).Returns<Asset?>(null);

A.CallTo(() => storageRepository.GetStorageMetrics(A<int>._, A<CancellationToken>._))
.Returns(new AssetStorageMetric
Expand All @@ -65,7 +67,7 @@ public async Task Process_ChecksForMaximumNumberOfImages_Exceeded()
public async Task Process_ChecksForTotalImageSize_Exceeded()
{
// Arrange
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._)).Returns<Asset?>(null);
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._, A<bool>._)).Returns<Asset?>(null);
A.CallTo(() => storageRepository.GetStorageMetrics(A<int>._, A<CancellationToken>._))
.Returns(new AssetStorageMetric
{
Expand All @@ -87,7 +89,7 @@ public async Task Process_ChecksForTotalImageSize_Exceeded()
public async Task Process_RetrievesNoneDeliveryChannelPolicy_WhenCalledWithNoneDeliveryChannel()
{
// Arrange
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._)).Returns<Asset?>(null);
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._, A<bool>._)).Returns<Asset?>(null);

A.CallTo(() => storageRepository.GetStorageMetrics(A<int>._, A<CancellationToken>._))
.Returns(new AssetStorageMetric
Expand Down Expand Up @@ -123,7 +125,7 @@ public async Task Process_RetrievesNoneDeliveryChannelPolicy_WhenCalledWithNoneD
public async Task Process_RetrievesDeliveryChannelPolicy_WhenCalledWithDeliveryChannels()
{
// Arrange
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._)).Returns<Asset?>(null);
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._, A<bool>._)).Returns<Asset?>(null);

A.CallTo(() => storageRepository.GetStorageMetrics(A<int>._, A<CancellationToken>._))
.Returns(new AssetStorageMetric
Expand Down Expand Up @@ -167,7 +169,7 @@ public async Task Process_RetrievesDeliveryChannelPolicy_WhenCalledWithDeliveryC
public async Task Process_FailsToProcessImage_WhenDeliveryPolicyNotMatched()
{
// Arrange
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._)).Returns<Asset?>(null);
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._, A<bool>._)).Returns<Asset?>(null);

A.CallTo(() => storageRepository.GetStorageMetrics(A<int>._, A<CancellationToken>._))
.Returns(new AssetStorageMetric
Expand Down Expand Up @@ -202,7 +204,7 @@ public async Task Process_FailsToProcessImage_WhenDeliveryPolicyNotMatched()
public async Task Process_ProcessesImage_WhenDeliveryPolicyMatchedFromChannel()
{
// Arrange
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._)).Returns<Asset?>(null);
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._, A<bool>._)).Returns<Asset?>(null);

A.CallTo(() => storageRepository.GetStorageMetrics(A<int>._, A<CancellationToken>._))
.Returns(new AssetStorageMetric
Expand Down Expand Up @@ -235,7 +237,7 @@ public async Task Process_ProcessesImage_WhenDeliveryPolicyMatchedFromChannel()
public async Task Process_FailsToProcessesImage_WhenDeliveryPolicyNotMatchedFromChannel()
{
// Arrange
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._)).Returns<Asset?>(null);
A.CallTo(() => assetRepository.GetAsset(A<AssetId>._, A<bool>._, A<bool>._)).Returns<Asset?>(null);

A.CallTo(() => storageRepository.GetStorageMetrics(A<int>._, A<CancellationToken>._))
.Returns(new AssetStorageMetric
Expand Down
Loading
Loading