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

Implementing default delivery channel matching #745

Merged
merged 20 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
@@ -0,0 +1,102 @@
using System;
using System.Linq;
using API.Features.DeliveryChannels;
using API.Tests.Integration.Infrastructure;
using DLCS.Core.Caching;
using DLCS.Model.DeliveryChannels;
using DLCS.Model.Policies;
using DLCS.Repository;
using LazyCache.Mocks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Test.Helpers.Integration;

namespace API.Tests.Features.DeliveryChannels;

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

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

dbFixture.CleanUp();

var newPolicy = dbContext.DeliveryChannelPolicies.Add(new DeliveryChannelPolicy()
{
Created = DateTime.UtcNow,
Modified = DateTime.UtcNow,
DisplayName = "test policy - space specific",
PolicyData = null,
Name = "space-specific-image",
Channel = "iiif-img",
Customer = 2,
Id = 260
});

dbContext.DefaultDeliveryChannels.Add(new DefaultDeliveryChannel
{
Space = 2,
Customer = 2,
DeliveryChannelPolicyId = newPolicy.Entity.Id,
MediaType = "image/tiff"
});

dbContext.DefaultDeliveryChannels.Add(new DefaultDeliveryChannel
{
Space = 0,
Customer = 2,
DeliveryChannelPolicyId = 1,
MediaType = "image/*"
});

dbContext.SaveChanges();
}

[Fact]
public void MatchedDeliveryChannels_ReturnsAllDeliveryChannelPolicies_WhenCalled()
{
// Arrange and Act
var matches = 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()
{
// Arrange and Act
var matches = sut.MatchedDeliveryChannels("notValid/tiff", 1, 2);

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

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

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

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

// Assert
action.Should().ThrowExactly<InvalidOperationException>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using API.Features.DeliveryChannels;
using API.Tests.Integration.Infrastructure;
using DLCS.Core.Caching;
using DLCS.Model.Policies;
using DLCS.Repository;
using LazyCache.Mocks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Test.Helpers.Integration;

namespace API.Tests.Features.DeliveryChannels;

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

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

dbFixture.CleanUp();

dbContext.DeliveryChannelPolicies.Add(new DeliveryChannelPolicy()
{
Created = DateTime.UtcNow,
Modified = DateTime.UtcNow,
DisplayName = "test policy - space specific",
PolicyData = null,
Name = "space-specific-image",
Channel = "iiif-img",
Customer = 2,
Id = 260
});

dbContext.SaveChanges();
}

[Theory]
[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)
{
// Arrange and Act
var deliveryChannelPolicy = sut.RetrieveDeliveryChannelPolicy(2, "iiif-img", policy);

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

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

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

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

// Assert
action.Should()
.Throw<InvalidOperationException>();
}
}
Loading
Loading