public void Errors_compensated_for_are_rethrown()
        {
            var operation = new FakeOperation { ThrowOnExecute = new Exception() };
            var sut = new CompensatingOperationBehavior(new FakeOperation());
            sut.AttachTo(operation);

            Assert.Throws<Exception>(() => sut.Execute());
        }
        public void When_no_error_occurs_in_the_operation_the_compensating_operation_is_not_executed()
        {
            var operation = new FakeOperation();
            var compensatingOperation = new FakeOperation();
            var sut = new CompensatingOperationBehavior(compensatingOperation);
            sut.AttachTo(operation);

            sut.Execute();

            Assert.False(compensatingOperation.HasExecuted);
        }
        public void When_an_error_occurs_in_the_operation_the_compensating_operation_is_immediately_executed()
        {
            var operation = new FakeOperation { ThrowOnExecute = new Exception() };
            var compensatingOperation = new FakeOperation();
            var sut = new CompensatingOperationBehavior(compensatingOperation);
            sut.AttachTo(operation);

            ExecuteIgnoringErrors(sut.Execute);

            Assert.True(compensatingOperation.HasExecuted);
        }
        public void Compensating_operations_have_input_values_supplied_from_the_original_operation(object input)
        {
            var operation = new FakeInputOperation<object> { ThrowOnExecute = new Exception() };
            var compensatingOperation = new FakeInputOperation<object>();
            var sut = new CompensatingOperationBehavior(compensatingOperation);
            operation.Input(input);
            var parentOperation = new FakeOperation(new FakeOutputOperation<object> { OutputValue = input }, sut.AttachTo(new FakeOperationBehavior().AttachTo(operation)));

            ExecuteIgnoringErrors(parentOperation.Execute);

            Assert.True(compensatingOperation.InputWasProvided);
            Assert.Equal(input, compensatingOperation.ProvidedInput);
        }
        public void When_specifying_a_compensated_exception_type_other_types_of_exceptions_will_not_be_compensated()
        {
            var operation = new FakeOperation { ThrowOnExecute = new Exception() };
            var compensatingOperation = new FakeOperation();
            var sut = new CompensatingOperationBehavior(compensatingOperation, typeof(ArgumentException));
            sut.AttachTo(operation);

            ExecuteIgnoringErrors(sut.Execute);

            Assert.False(compensatingOperation.HasExecuted);
        }
        public void When_no_compensating_action_is_triggered_no_event_is_logged(FakeWorkflowLogger log)
        {
            var operation = new FakeOperation { ThrowOnExecute = new Exception() };
            var compensatingOperation = new FakeOperation();
            var sut = new CompensatingOperationBehavior(compensatingOperation, typeof(ArgumentException));
            sut.AttachTo(operation);
            sut.Initialize(new FakeWorkflowConfiguration { Logger = log });

            ExecuteIgnoringErrors(sut.Execute);

            Assert.Equal(0, log.AppliedBehaviors.Count);
        }
        public void Executing_a_compensating_operation_logs_an_event(FakeWorkflowLogger log)
        {
            var operation = new FakeOperation { ThrowOnExecute = new ArgumentException() };
            var compensatingOperation = new FakeOperation();
            var sut = new CompensatingOperationBehavior(compensatingOperation, typeof(Exception));
            sut.AttachTo(operation);
            sut.Initialize(new FakeWorkflowConfiguration { Logger = log });

            ExecuteIgnoringErrors(sut.Execute);

            Assert.Equal(1, log.AppliedBehaviors.Count);
            Assert.Equal("Executing compensating operation", log.AppliedBehaviors[0].Description);
        }
        public void When_specifying_a_compensated_exception_type_the_compensating_operation_will_be_executed_in_case_of_an_exception_inheriting_from_this_exception()
        {
            var operation = new FakeOperation { ThrowOnExecute = new ArgumentException() };
            var compensatingOperation = new FakeOperation();
            var sut = new CompensatingOperationBehavior(compensatingOperation, typeof(Exception));
            sut.AttachTo(operation);

            ExecuteIgnoringErrors(sut.Execute);

            Assert.True(compensatingOperation.HasExecuted);
        }