Skip to content

Commit

Permalink
Merge pull request #254 from Kentico/feature/service_collection_exten…
Browse files Browse the repository at this point in the history
…sions

Feature/service collection extensions
  • Loading branch information
petrsvihlik authored Jan 22, 2021
2 parents 38de668 + 54379a8 commit 41631cb
Show file tree
Hide file tree
Showing 32 changed files with 1,006 additions and 474 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,10 @@ public class DeliveryOptions
/// This behavior can also be enabled for individual requests with the IncludeTotalCountParameter.
/// </summary>
public bool IncludeTotalCount { get; set; }

/// <summary>
/// The name of the service configuration this options object is related to.
/// </summary>
internal string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using Kentico.Kontent.Delivery.Abstractions;

namespace Kentico.Kontent.Delivery.Extensions
namespace Kentico.Kontent.Delivery.Abstractions.Extensions
{
internal static class DeliveryOptionsExtensions
/// <summary>
/// A class which contains extension methods on <see cref="DeliveryOptions"/>.
/// </summary>
public static class DeliveryOptionsExtensions
{
/// <summary>
/// Maps one <see cref="DeliveryOptions"/> object to another.
/// </summary>
/// <param name="o">A destination.</param>
/// <param name="options">A source.</param>
public static void Configure(this DeliveryOptions o, DeliveryOptions options)
{
o.ProjectId = options.ProjectId;
Expand All @@ -17,6 +23,7 @@ public static void Configure(this DeliveryOptions o, DeliveryOptions options)
o.EnableRetryPolicy = options.EnableRetryPolicy;
o.DefaultRetryPolicyOptions = options.DefaultRetryPolicyOptions;
o.IncludeTotalCount = options.IncludeTotalCount;
o.Name = options.Name;
}
}
}
10 changes: 5 additions & 5 deletions Kentico.Kontent.Delivery.Abstractions/IDeliveryClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Kentico.Kontent.Delivery.Abstractions
{
/// <summary>
/// Defines a methods for getting a <see cref="IDeliveryClient"/>
/// A factory class for <see cref="IDeliveryClient"/>
/// </summary>
public interface IDeliveryClientFactory
{
Expand All @@ -12,10 +12,10 @@ public interface IDeliveryClientFactory
/// <returns>Returns an <see cref="IDeliveryClient"/> instance with the given name.</returns>
IDeliveryClient Get(string name);

/// <summary>
/// Returns a default instance of the <see cref="IDeliveryClient"/>.
/// </summary>
/// <returns>Returns a default instance of the <see cref="IDeliveryClient"/>.</returns>
/// <summary>
/// Returns a default instance of the <see cref="IDeliveryClient"/>.
/// </summary>
/// <returns>Returns a default instance of the <see cref="IDeliveryClient"/>.</returns>
IDeliveryClient Get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
[assembly: Guid("234c7376-d90c-4207-9933-c9e55a16d934")]
[assembly: InternalsVisibleTo("Kentico.Kontent.Delivery.Tests")]
[assembly: InternalsVisibleTo("Kentico.Kontent.Delivery")]
[assembly: InternalsVisibleTo("Kentico.Kontent.Delivery.Extensions.DependencyInjection")]
[assembly: InternalsVisibleTo("Kentico.Kontent.Delivery.Extensions.DependencyInjection.Tests")]
[assembly: InternalsVisibleTo("Kentico.Kontent.Delivery.Rx.Tests")]
40 changes: 40 additions & 0 deletions Kentico.Kontent.Delivery.Caching.Tests/CacheManagerFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using FakeItEasy;
using FluentAssertions;
using Kentico.Kontent.Delivery.Caching.Factories;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Xunit;

namespace Kentico.Kontent.Delivery.Caching.Tests
{
public class CacheManagerFactoryTests
{
private IOptions<DeliveryCacheOptions> _options;
private IDistributedCache _distributedCache;
private IMemoryCache _memoryCache;

public CacheManagerFactoryTests()
{
_options = A.Fake<IOptions<DeliveryCacheOptions>>();
_distributedCache = A.Fake<IDistributedCache>();
_memoryCache = A.Fake<IMemoryCache>();
}

[Fact]
public void Create_DistributedCache()
{
var deliveryCacheManager = CacheManagerFactory.Create(_distributedCache, _options);

deliveryCacheManager.Should().NotBeNull();
}

[Fact]
public void Create_MemoryCache()
{
var deliveryCacheManager = CacheManagerFactory.Create(_memoryCache, _options);

deliveryCacheManager.Should().NotBeNull();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,112 +26,52 @@ public void AddDeliveryClientCacheWithDeliveryCacheOptions_ThrowsMissingTypeRegi
Assert.Throws<MissingTypeRegistrationException>(() => _serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions() { CacheType = cacheType }));
}

[Theory]
[InlineData(CacheTypeEnum.Memory)]
[InlineData(CacheTypeEnum.Distributed)]
public void AddDeliveryClient_WithNoCache_GetClient(CacheTypeEnum cacheType)
[Fact]
public void AddDeliveryClientCacheWithNullDeliveryCacheOptions_ThrowsArgumentNullException()
{
_serviceCollection.AddDeliveryClient(new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() });
_serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions()
{
CacheType = cacheType
});

var sp = _serviceCollection.BuildServiceProvider();
var factory = sp.GetRequiredService<IDeliveryClientFactory>();

var client = factory.Get();

client.Should().NotBeNull();
Assert.Throws<ArgumentNullException>(() => _serviceCollection.AddDeliveryClientCache(null));
}

[Theory]
[InlineData(CacheTypeEnum.Memory)]
[InlineData(CacheTypeEnum.Distributed)]
public void AddDeliveryClient_CacheWithDeliveryCacheOptions_GetNull(CacheTypeEnum cacheType)
[Fact]
public void AddDeliveryClientCacheWitNoPreviousRegistrationDeliveryClient_ThrowsMissingTypeRegistrationException()
{
_serviceCollection.AddDeliveryClient(new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() });
_serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions()
{
CacheType = cacheType
});

var sp = _serviceCollection.BuildServiceProvider();
var factory = sp.GetRequiredService<IDeliveryClientFactory>();

var client = factory.Get("WrongName");

client.Should().BeNull();
Assert.Throws<MissingTypeRegistrationException>(() => _serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions()));
}

[Theory]
[InlineData(CacheTypeEnum.Memory)]
[InlineData(CacheTypeEnum.Distributed)]
public void AddDeliveryNamedClient_CacheWithDeliveryCacheOptions_GetNamedClient(CacheTypeEnum cacheType)
public void AddDeliveryClient_WithNoCache_GetClient(CacheTypeEnum cacheType)
{
_serviceCollection.AddDeliveryClient("named", new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() });
_serviceCollection.AddDeliveryClientCache("named", new DeliveryCacheOptions()
_serviceCollection.AddDeliveryClient(new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() });
_serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions()
{
CacheType = cacheType
});

var sp = _serviceCollection.BuildServiceProvider();
var factory = sp.GetRequiredService<IDeliveryClientFactory>();

var client = factory.Get("named");
var client = factory.Get();

client.Should().NotBeNull();
}

[Theory]
[InlineData(CacheTypeEnum.Memory)]
[InlineData(CacheTypeEnum.Distributed)]
public void AddDeliveryNamedClient_CacheWithDeliveryCacheOptions_GetNull(CacheTypeEnum cacheType)
public void AddDeliveryClient_CacheWithDeliveryCacheOptions_ThrowsNotImeplementException(CacheTypeEnum cacheType)
{
_serviceCollection.AddDeliveryClient("named", new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() });
_serviceCollection.AddDeliveryClientCache("named", new DeliveryCacheOptions()
_serviceCollection.AddDeliveryClient(new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() });
_serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions()
{
CacheType = cacheType
});

var sp = _serviceCollection.BuildServiceProvider();
var factory = sp.GetRequiredService<IDeliveryClientFactory>();

var client = factory.Get("WrongName");

client.Should().BeNull();
}

[Theory]
[InlineData(CacheTypeEnum.Memory)]
[InlineData(CacheTypeEnum.Distributed)]
public void AddDeliveryClientCacheNamedWithDeliveryCacheOptions_ThrowsInvalidOperationException(CacheTypeEnum cacheType)
{
Assert.Throws<MissingTypeRegistrationException>(() => _serviceCollection.AddDeliveryClientCache("named", new DeliveryCacheOptions() { CacheType = cacheType }));
}

[Fact]
public void AddDeliveryClientCacheWithNullDeliveryCacheOptions_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => _serviceCollection.AddDeliveryClientCache(null));
}

[Fact]
public void AddDeliveryClientCacheNamedWithNullDeliveryCacheOptions_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => _serviceCollection.AddDeliveryClientCache("named", null));
}

[Fact]
public void AddDeliveryClientCacheWitNoPreviousRegistrationDeliveryClient_ThrowsMissingTypeRegistrationException()
{
Assert.Throws<MissingTypeRegistrationException>(() => _serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions()));
}

[Fact]
public void AddDeliveryClientNamedCacheWitNoPreviousRegistrationDeliveryClient_ThrowsMissingTypeRegistrationException()
{
Assert.Throws<MissingTypeRegistrationException>(() => _serviceCollection.AddDeliveryClientCache("named", new DeliveryCacheOptions()));
Assert.Throws<NotImplementedException>(() => factory.Get("WrongName"));
}
}
}
2 changes: 1 addition & 1 deletion Kentico.Kontent.Delivery.Caching/DeliveryCacheOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class DeliveryCacheOptions
public CacheTypeEnum CacheType { get; set; } = CacheTypeEnum.Memory;

/// <summary>
/// Name of an <see cref="IDeliveryCacheManager"/> instance the options are bound to.
/// The name of the service configuration this options object is related to.
/// </summary>
internal string Name { get; set; }
}
Expand Down
81 changes: 0 additions & 81 deletions Kentico.Kontent.Delivery.Caching/DeliveryClientCacheFactory.cs

This file was deleted.

Loading

0 comments on commit 41631cb

Please sign in to comment.