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 13, 2021
1 parent edb5860 commit c6d0459
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,43 @@ public void AddDeliveryClientCacheWitNoPreviousRegistrationDeliveryClient_Throws
{
Assert.Throws<MissingTypeRegistrationException>(() => _serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions()));
}

[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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Kentico.Kontent.Delivery.Abstractions;
using Kentico.Kontent.Delivery.Caching;
using Microsoft.Extensions.DependencyInjection;
using Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Extensions;
using System;
using Xunit;
using FluentAssertions;
using Autofac;
using FakeItEasy;

namespace Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests
{
public class ServiceCollectionExtensionsTests
{
private readonly ServiceCollection _serviceCollection;
private readonly IComponentContext _container;

public ServiceCollectionExtensionsTests()
{
_serviceCollection = new ServiceCollection();
_container = A.Fake<IComponentContext>();
}

[Theory]
[InlineData(CacheTypeEnum.Memory)]
[InlineData(CacheTypeEnum.Distributed)]
public void AddDeliveryNamedClient_CacheWithDeliveryCacheOptions_GetNamedClient(CacheTypeEnum cacheType)
{
_serviceCollection.AddSingleton(_container);
_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.AddSingleton(_container);
_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();
}

[Fact]
public void AddDeliveryNamedClient_DeliveryOptions_GetNamedClient()
{
_serviceCollection.AddSingleton(_container);
_serviceCollection.AddDeliveryClient("named", new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() });
var sp = _serviceCollection.BuildServiceProvider();

var factory = sp.GetRequiredService<IDeliveryClientFactory>();
var client = factory.Get("named");

client.Should().NotBeNull();
}

[Fact]
public void AddDeliveryNamedClient_DeliveryOptions_GetNull()
{
_serviceCollection.AddSingleton(_container);
_serviceCollection.AddDeliveryClient("named", new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() });
var sp = _serviceCollection.BuildServiceProvider();

var factory = sp.GetRequiredService<IDeliveryClientFactory>();
var client = factory.Get("WrongName");

client.Should().BeNull();
}

}
}

0 comments on commit c6d0459

Please sign in to comment.