Skip to content

Commit

Permalink
Merge pull request #745 from dlcs/feature/implementDefaultDeliveryCha…
Browse files Browse the repository at this point in the history
…nnels

Implementing default delivery channel matching
  • Loading branch information
JackLewis-digirati committed Mar 5, 2024
2 parents 1bbd043 + deb776f commit b3fa21f
Show file tree
Hide file tree
Showing 32 changed files with 1,061 additions and 395 deletions.
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

0 comments on commit b3fa21f

Please sign in to comment.