public void Should_throw_when_no_violation_handler_has_been_set_and_no_violation_handler_match_name()
        {
            // Arrange
            var violationHandlers = Enumerable.Empty<IPolicyViolationHandler>();
            var failureResult = PolicyResult.CreateFailureResult(new IgnorePolicy(), "Access denied");
            var policy = new DelegatePolicy("Test", c => failureResult);
            var delegatePolicyResult = new DelegatePolicyResult(failureResult, policy.Name, policy.ViolationHandler);
            var exception = new PolicyViolationException(delegatePolicyResult);
            var handler = new DelegatePolicyViolationHandler(violationHandlers);

            // Act & assert
            var caughtException = Assert.Throws<PolicyViolationException>(() => handler.Handle(exception));
            Assert.That(caughtException, Is.EqualTo(exception));
        }
        public void Should_have_values_from_policyresult_but_no_violation_handler()
        {
            // Arrange
            const string policyName = "PolicyName2";
            var policyResult = PolicyResult.CreateFailureResult(new IgnorePolicy(), "Failure message");

            // Act
            var delegatePolicyResult = new DelegatePolicyResult(policyResult, policyName, null);

            // Assert
            Assert.That(delegatePolicyResult.Message, Is.EqualTo(policyResult.Message));
            Assert.That(delegatePolicyResult.PolicyType, Is.EqualTo(policyResult.PolicyType));
            Assert.That(delegatePolicyResult.PolicyName, Is.EqualTo(policyName));
            Assert.That(delegatePolicyResult.ViolationOccured, Is.EqualTo(policyResult.ViolationOccured));
            Assert.That(delegatePolicyResult.ViolationHandler, Is.Null);
        }
        public void Should_have_values_from_policyresult_and_violation_handler()
        {
            // Arrange
            const string policyName = "PolicyName1";
            var policyResult = PolicyResult.CreateFailureResult(new IgnorePolicy(), "Failure message");
            Func<PolicyViolationException, ActionResult> violationHandler = e => new EmptyResult();

            // Act
            var delegatePolicyResult = new DelegatePolicyResult(policyResult, policyName, violationHandler);

            // Assert
            Assert.That(delegatePolicyResult.Message, Is.EqualTo(policyResult.Message));
            Assert.That(delegatePolicyResult.PolicyType, Is.EqualTo(policyResult.PolicyType));
            Assert.That(delegatePolicyResult.PolicyName, Is.EqualTo(policyName));
            Assert.That(delegatePolicyResult.ViolationOccured, Is.EqualTo(policyResult.ViolationOccured));
            Assert.That(delegatePolicyResult.ViolationHandler, Is.EqualTo(violationHandler));
        }
        public void Should_return_action_result_from_explicitly_set_violation_handler()
        {
            // Arrange
            var violationHandlers = Enumerable.Empty<IPolicyViolationHandler>();
            var expectedResult = new ContentResult { Content = "Some content" };
            var failureResult = PolicyResult.CreateFailureResult(new IgnorePolicy(), "Access denied");
            var policy = new DelegatePolicy("Test", c => failureResult, e => expectedResult);
            var delegatePolicyResult = new DelegatePolicyResult(failureResult, policy.Name, policy.ViolationHandler);
            var exception = new PolicyViolationException(delegatePolicyResult);
            var handler = new DelegatePolicyViolationHandler(violationHandlers);

            // Act
            var result = handler.Handle(exception);

            // Assert
            Assert.That(result, Is.EqualTo(expectedResult));
        }
        public void Should_return_action_result_from_violation_handler_that_match_name()
        {
            // Arrange
            var nonMatchingNameViolationHandler = new NonMatchingNameViolationHandler();
            var matchingNameViolationHandler = new MatchingNameViolationHandler();
            var violationHandlers = new List<IPolicyViolationHandler>
            {
                nonMatchingNameViolationHandler,
                matchingNameViolationHandler
            };
            var failureResult = PolicyResult.CreateFailureResult(new IgnorePolicy(), "Access denied");
            var policy = new DelegatePolicy("MatchingName", c => failureResult);
            var delegatePolicyResult = new DelegatePolicyResult(failureResult, policy.Name, policy.ViolationHandler);
            var exception = new PolicyViolationException(delegatePolicyResult);
            var handler = new DelegatePolicyViolationHandler(violationHandlers);

            // Act
            var result = handler.Handle(exception);

            // Assert
            Assert.That(result, Is.EqualTo(matchingNameViolationHandler.ActionResult));
        }