unit tests for cache clearing passing

This commit is contained in:
Tom Gardham-Pallister
2017-06-27 18:54:15 +01:00
parent 239dcfb6bd
commit e4e7fcc943
10 changed files with 244 additions and 5 deletions

View File

@ -30,6 +30,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.MiddlewareAnalysis" Version="1.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta2-build1317" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />

View File

@ -5,6 +5,7 @@ using System.Net.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Moq;
using Ocelot.Cache;
using Ocelot.Cache.Middleware;
@ -43,6 +44,7 @@ namespace Ocelot.UnitTests.Cache
x.AddLogging();
x.AddSingleton(_cacheManager.Object);
x.AddSingleton(_scopedRepo.Object);
x.AddSingleton<IRegionCreator, RegionCreator>();
})
.UseUrls(_url)
.UseKestrel()

View File

@ -0,0 +1,46 @@
using System.Collections.Generic;
using Ocelot.Cache;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Cache
{
public class RegionCreatorTests
{
private string _result;
private ReRoute _reRoute;
[Fact]
public void should_create_region()
{
var reRoute = new ReRouteBuilder()
.WithUpstreamHttpMethod(new List<string>{"Get"})
.WithUpstreamPathTemplate("/test/dummy")
.Build();
this.Given(_ => GivenTheReRoute(reRoute))
.When(_ => WhenICreateTheRegion())
.Then(_ => ThenTheRegionIs("Get /test/dummy"))
.BDDfy();
}
private void GivenTheReRoute(ReRoute reRoute)
{
_reRoute = reRoute;
}
private void WhenICreateTheRegion()
{
RegionCreator regionCreator = new RegionCreator();
_result = regionCreator.Region(_reRoute);
}
private void ThenTheRegionIs(string expected)
{
_result.ShouldBe(expected);
}
}
}

View File

@ -0,0 +1,109 @@
using Xunit;
using TestStack.BDDfy;
using Shouldly;
using Ocelot.Cache;
using Moq;
using Ocelot.Configuration.Provider;
using System.Collections.Generic;
using Ocelot.Responses;
using Ocelot.Configuration;
using System.Threading.Tasks;
using Ocelot.Configuration.Builder;
using System;
using Ocelot.Errors;
using Ocelot.Logging;
namespace Ocelot.UnitTests.Cache
{
public class RegionsGetterTests
{
private RegionsGetter _regionsGetter;
private readonly Mock<IOcelotConfigurationProvider> _provider;
private readonly Mock<IRegionCreator> _creator;
private readonly Mock<IOcelotLoggerFactory> _factory;
private List<string> _result;
public RegionsGetterTests()
{
_provider = new Mock<IOcelotConfigurationProvider>();
_creator = new Mock<IRegionCreator>();
_factory = new Mock<IOcelotLoggerFactory>();
var logger = new Mock<IOcelotLogger>();
_factory
.Setup(x => x.CreateLogger<RegionsGetter>())
.Returns(logger.Object);
_regionsGetter = new RegionsGetter(_provider.Object, _creator.Object, _factory.Object);
}
[Fact]
public void should_get_regions()
{
var reRoute = new ReRouteBuilder()
.WithUpstreamHttpMethod(new List<string>{"Get"})
.WithUpstreamPathTemplate("/")
.Build();
var reRoutes = new List<ReRoute>
{
reRoute
};
var config = new OcelotConfiguration(reRoutes, "whocares!");
var expected = new List<string>
{
"balls"
};
this.Given(_ => GivenTheFollowingConfig(config))
.And(_ => GivenTheProviderReturns("balls"))
.When(_ => WhenIGetTheRegions())
.Then(_ => ThenTheFollowingIsReturned(expected))
.BDDfy();
}
[Fact]
public void should_return_empty_regions()
{
var expected = new List<string>();
this.Given(_ => GivenAnErrorGettingTheConfig())
.When(_ => WhenIGetTheRegions())
.Then(_ => ThenTheFollowingIsReturned(expected))
.BDDfy();
}
private void GivenAnErrorGettingTheConfig()
{
var config = new OcelotConfiguration(new List<ReRoute>(), "whocares!");
_provider
.Setup(x => x.Get())
.ReturnsAsync(new ErrorResponse<IOcelotConfiguration>(It.IsAny<Error>()));
}
private void GivenTheProviderReturns(string expected)
{
_creator
.Setup(x => x.Region(It.IsAny<ReRoute>()))
.Returns(expected);
}
private void GivenTheFollowingConfig(IOcelotConfiguration config)
{
_provider
.Setup(x => x.Get())
.ReturnsAsync(new OkResponse<IOcelotConfiguration>(config));
}
private void WhenIGetTheRegions()
{
_result = _regionsGetter.Regions().Result;
}
private void ThenTheFollowingIsReturned(List<string> expected)
{
_result.ShouldBe(expected);
}
}
}

View File

@ -15,12 +15,14 @@ namespace Ocelot.UnitTests.Controllers
{
private OutputCacheController _controller;
private Mock<IOcelotCache<HttpResponseMessage>> _cache;
private Mock<IRegionsGetter> _getter;
private IActionResult _result;
public OutputCacheControllerTests()
{
_cache = new Mock<IOcelotCache<HttpResponseMessage>>();
_controller = new OutputCacheController(_cache.Object);
_getter = new Mock<IRegionsGetter>();
_controller = new OutputCacheController(_cache.Object, _getter.Object);
}
[Fact]