public void Chain_AddTypeThatIsNotAMiddleware_ThrowsException() { var responsibilityChain = new AsyncResponsibilityChain <Exception, bool>(new ActivatorMiddlewareResolver()); PipelineNetAssert.ThrowsException <ArgumentException>(() => { responsibilityChain.Chain(typeof(ResponsibilityChainTests)); }); }
public void Execute_SynchronousChainOfResponsibility_SuccessfullyExecute() { var responsibilityChain = new AsyncResponsibilityChain <string, string>(new ActivatorMiddlewareResolver()) .Chain <SyncReplaceNewLineMiddleware>() .Chain <SyncTrimMiddleware>() .Finally(input => Task.FromResult(input)); var resultTask = responsibilityChain.Execute(" Test\nwith spaces\n and new lines \n "); var result = resultTask.Result; Assert.Equal("Test with spaces and new lines", result); }
public async Task Execute_ChainOfMiddlewareThatDoesNotHandleTheException_ChainReturnsDefaultValue() { var responsibilityChain = new AsyncResponsibilityChain <Exception, bool>(new ActivatorMiddlewareResolver()) .Chain <UnavailableResourcesExceptionHandler>() .Chain <InvalidateDataExceptionHandler>() .Chain <MyExceptionHandler>(); // Creates an ArgumentNullException, that will not be handled by any middleware. var excception = new ArgumentNullException(); // The result should be the default for 'bool'. var result = await responsibilityChain.Execute(excception); Assert.AreEqual(default(bool), result); }
public async Task Execute_CreateChainOfMiddlewareToHandleException_TheRightMiddleHandlesTheException() { var responsibilityChain = new AsyncResponsibilityChain <Exception, bool>(new ActivatorMiddlewareResolver()) .Chain <UnavailableResourcesExceptionHandler>() .Chain <InvalidateDataExceptionHandler>() .Chain <MyExceptionHandler>(); // Creates an invalid exception that should be handled by 'InvalidateDataExceptionHandler'. var invalidException = new InvalidateDataException(); var result = await responsibilityChain.Execute(invalidException); // Check if the given exception was handled Assert.IsTrue(result); // Check if the correct handler handled the exception. Assert.AreEqual(typeof(InvalidateDataExceptionHandler).Name, invalidException.HandlerName); }
public async Task Execute_ChainOfMiddlewareWithFinallyFunc_FinallyFuncIsExecuted() { const string ExceptionSource = "EXCEPTION_SOURCE"; var responsibilityChain = new AsyncResponsibilityChain <Exception, bool>(new ActivatorMiddlewareResolver()) .Chain <UnavailableResourcesExceptionHandler>() .Chain(typeof(InvalidateDataExceptionHandler)) .Chain <MyExceptionHandler>() .Finally((ex) => { ex.Source = ExceptionSource; return(Task.FromResult(true)); }); // Creates an ArgumentNullException, that will not be handled by any middleware. var exception = new ArgumentNullException(); // The result should true, since the finally function will be executed. var result = await responsibilityChain.Execute(exception); Assert.IsTrue(result); Assert.AreEqual(ExceptionSource, exception.Source); }