Skip to content

Commit

Permalink
extensions package for a named client
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasjurasek committed Jan 9, 2021
1 parent c88baee commit 0c83d09
Show file tree
Hide file tree
Showing 19 changed files with 654 additions and 381 deletions.
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"/>
/// Defines a method for getting a named <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();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,112 +26,16 @@ 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)
{
_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();
}

[Theory]
[InlineData(CacheTypeEnum.Memory)]
[InlineData(CacheTypeEnum.Distributed)]
public void AddDeliveryClient_CacheWithDeliveryCacheOptions_GetNull(CacheTypeEnum cacheType)
{
_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 AddDeliveryNamedClient_CacheWithDeliveryCacheOptions_GetNamedClient(CacheTypeEnum cacheType)
{
_serviceCollection.AddDeliveryClient("named", new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() });
_serviceCollection.AddDeliveryClientCache("named", new DeliveryCacheOptions()
{
CacheType = cacheType
});

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

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

client.Should().NotBeNull();
}

[Theory]
[InlineData(CacheTypeEnum.Memory)]
[InlineData(CacheTypeEnum.Distributed)]
public void AddDeliveryNamedClient_CacheWithDeliveryCacheOptions_GetNull(CacheTypeEnum cacheType)
{
_serviceCollection.AddDeliveryClient("named", new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() });
_serviceCollection.AddDeliveryClientCache("named", 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()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,7 @@ public static IServiceCollection AddDeliveryClientCache(this IServiceCollection
.Decorate<IDeliveryClient, DeliveryClientCache>();
}

/// <summary>
/// Registers a delegate that will be used to configure a cached <see cref="IDeliveryClient"/>.
/// </summary>
/// <param name="services">A <see cref="IServiceCollection"/> instance for registering and resolving dependencies.</param>
/// <param name="name">A name of named client which want to use cached <see cref="IDeliveryClient"/></param>
/// <param name="options">A <see cref="DeliveryCacheOptions"/> instance. </param>
/// <returns>The <paramref name="services"/> instance with cache services registered in it</returns>
public static IServiceCollection AddDeliveryClientCache(this IServiceCollection services, string name, DeliveryCacheOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options), "The Delivery cache options object is not specified.");
}

return services
.RegisterCacheOptions(options, name)
.RegisterDependencies(options.CacheType, name)
.Decorate<IDeliveryClientFactory, DeliveryClientCacheFactory>();
}

private static IServiceCollection RegisterCacheOptions(this IServiceCollection services, DeliveryCacheOptions options, string name = null)
internal static IServiceCollection RegisterCacheOptions(this IServiceCollection services, DeliveryCacheOptions options, string name = null)
{
if (name == null)
{
Expand All @@ -65,7 +45,7 @@ private static IServiceCollection RegisterCacheOptions(this IServiceCollection s
return services;
}

private static IServiceCollection RegisterDependencies(this IServiceCollection services, CacheTypeEnum cacheType, string name = null)
internal static IServiceCollection RegisterDependencies(this IServiceCollection services, CacheTypeEnum cacheType, string name = null)
{
switch (cacheType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Kentico.Kontent.Delivery.Caching.Tests</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Kentico.Kontent.Delivery.Extensions.DependencyInjection</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Kentico.Kontent.Delivery.Extensions.DependencyInjection.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using FakeItEasy;
using FluentAssertions;
using Kentico.Kontent.Delivery.Abstractions;
using Kentico.Kontent.Delivery.Caching;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Kentico.Kontent.Delivery.Extensions.DependencyInjection.Tests
{
public class DeliveryClientCacheFactoryTests
{
private readonly IOptionsMonitor<DeliveryCacheOptions> _deliveryCacheOptionsMock;
private readonly IDeliveryClientFactory _innerDeliveryClientFactoryMock;
private readonly IServiceProvider _serviceProvider;

private const string _clientName = "ClientName";

public DeliveryClientCacheFactoryTests()
{
_deliveryCacheOptionsMock = A.Fake<IOptionsMonitor<DeliveryCacheOptions>>();
_innerDeliveryClientFactoryMock = A.Fake<IDeliveryClientFactory>();
_serviceProvider = new ServiceCollection().BuildServiceProvider();
}

[Fact]
public void GetNamedCacheClient_WithCorrectName_GetClient()
{
var deliveryCacheOptions = new DeliveryCacheOptions();
A.CallTo(() => _deliveryCacheOptionsMock.Get(_clientName))
.Returns(deliveryCacheOptions);

var deliveryClientFactory = new DeliveryClientCacheFactory(_innerDeliveryClientFactoryMock, _deliveryCacheOptionsMock, _serviceProvider);

var result = deliveryClientFactory.Get(_clientName);

result.Should().NotBeNull();
}

[Fact]
public void GetNamedCacheClient_WithWrongName_GetNull()
{
var deliveryCacheOptions = new DeliveryCacheOptions();
A.CallTo(() => _deliveryCacheOptionsMock.Get(_clientName))
.Returns(deliveryCacheOptions);

var deliveryClientFactory = new DeliveryClientCacheFactory(_innerDeliveryClientFactoryMock, _deliveryCacheOptionsMock, _serviceProvider);

var result = deliveryClientFactory.Get("WrongName");

result.Should().NotBeNull();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using FakeItEasy;
using FluentAssertions;
using Kentico.Kontent.Delivery.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using Xunit;

namespace Kentico.Kontent.Delivery.Extensions.DependencyInjection.Tests
{
public class DeliveryClientFactoryTests
{
private readonly IOptionsMonitor<DeliveryOptions> _deliveryOptionsMock;
private readonly IServiceProvider _serviceProvider;

private const string _clientName = "ClientName";

public DeliveryClientFactoryTests()
{
_deliveryOptionsMock = A.Fake<IOptionsMonitor<DeliveryOptions>>();
_serviceProvider = new ServiceCollection().BuildServiceProvider();
}

[Fact]
public void GetNamedClient_WithCorrectName_GetClient()
{
var deliveryOptions = new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() };
A.CallTo(() => _deliveryOptionsMock.Get(_clientName))
.Returns(deliveryOptions);

var deliveryClientFactory = new DeliveryClientFactory(_deliveryOptionsMock, _serviceProvider);

var result = deliveryClientFactory.Get(_clientName);

result.Should().NotBeNull();
}

[Fact]
public void GetNamedClient_WithWrongName_GetNull()
{
var deliveryOptions = new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() };
A.CallTo(() => _deliveryOptionsMock.Get(_clientName))
.Returns(deliveryOptions);

var deliveryClientFactory = new DeliveryClientFactory(_deliveryOptionsMock, _serviceProvider);

var result = deliveryClientFactory.Get("WrongName");

result.Should().BeNull();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FakeItEasy" Version="6.2.1" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Kentico.Kontent.Delivery.Abstractions\Kentico.Kontent.Delivery.Abstractions.csproj" />
<ProjectReference Include="..\Kentico.Kontent.Delivery.Extensions.DependencyInjection\Kentico.Kontent.Delivery.Extensions.DependencyInjection.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 0c83d09

Please sign in to comment.