using System.Collections.Generic; using System.Linq; using Moq; using Ocelot.Configuration; using Ocelot.Configuration.Creator; using Ocelot.Configuration.Parser; using Ocelot.Errors; using Ocelot.Logging; using Ocelot.Responses; using Shouldly; using TestStack.BDDfy; using Xunit; namespace Ocelot.UnitTests.Configuration { public class ClaimsToThingCreatorTests { private readonly Mock _configParser; private Dictionary _claimsToThings; private ClaimsToThingCreator _claimsToThingsCreator; private Mock _loggerFactory; private List _result; private Mock _logger; public ClaimsToThingCreatorTests() { _loggerFactory = new Mock(); _logger = new Mock(); _loggerFactory .Setup(x => x.CreateLogger()) .Returns(_logger.Object); _configParser = new Mock(); _claimsToThingsCreator = new ClaimsToThingCreator(_configParser.Object, _loggerFactory.Object); } [Fact] public void should_return_claims_to_things() { var userInput = new Dictionary() { {"CustomerId", "Claims[CustomerId] > value"} }; var claimsToThing = new OkResponse(new ClaimToThing("CustomerId", "CustomerId", "", 0)); this.Given(x => x.GivenTheFollowingDictionary(userInput)) .And(x => x.GivenTheConfigHeaderExtractorReturns(claimsToThing)) .When(x => x.WhenIGetTheThings()) .Then(x => x.ThenTheConfigParserIsCalledCorrectly()) .And(x => x.ThenClaimsToThingsAreReturned()) .BDDfy(); } [Fact] public void should_log_error_if_cannot_parse_claim_to_thing() { var userInput = new Dictionary() { {"CustomerId", "Claims[CustomerId] > value"} }; var claimsToThing = new ErrorResponse(It.IsAny()); this.Given(x => x.GivenTheFollowingDictionary(userInput)) .And(x => x.GivenTheConfigHeaderExtractorReturns(claimsToThing)) .When(x => x.WhenIGetTheThings()) .Then(x => x.ThenTheConfigParserIsCalledCorrectly()) .And(x => x.ThenNoClaimsToThingsAreReturned()) .BDDfy(); } private void ThenTheLoggerIsCalledCorrectly() { _logger .Verify(x => x.LogDebug(It.IsAny(), It.IsAny()), Times.Once); } private void ThenClaimsToThingsAreReturned() { _result.Count.ShouldBeGreaterThan(0); } private void GivenTheFollowingDictionary(Dictionary claimsToThings) { _claimsToThings = claimsToThings; } private void GivenTheConfigHeaderExtractorReturns(Response expected) { _configParser .Setup(x => x.Extract(It.IsAny(), It.IsAny())) .Returns(expected); } private void ThenNoClaimsToThingsAreReturned() { _result.Count.ShouldBe(0); } private void WhenIGetTheThings() { _result = _claimsToThingsCreator.Create(_claimsToThings); } private void ThenTheConfigParserIsCalledCorrectly() { _configParser .Verify(x => x.Extract(_claimsToThings.First().Key, _claimsToThings.First().Value), Times.Once); } } }