public async Task when_handling_the_query_via_Post() { QueryName = "FakeQuery"; FakeQueryProcessor.Setup(x => x.GetQueryType(QueryName)).Returns(typeof(FakeQuery)); async Task should_return_the_result_from_the_query_processor() { var expected = new FakeResult(); FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Returns(Task.FromResult(expected)); var result = await Subject.HandlePost(QueryName, Json) as OkNegotiatedContentResult <object>; (await result.ExecuteAsync(CancellationToken.None)).StatusCode.Should().Be(200); result.Content.Should().Be(expected); } async Task should_handle_QueryProcessorException() { FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryProcessorException("fail")); var result = await Subject.HandlePost(QueryName, Json); await result.ShouldBeErrorAsync("fail", 400); } async Task should_handle_QueryException() { FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryException("invalid")); var result = await Subject.HandlePost(QueryName, Json); await result.ShouldBeErrorAsync("invalid", 400); } async Task should_handle_Exception() { FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new Exception("fail")); var result = await Subject.HandlePost(QueryName, Json); await result.ShouldBeErrorAsync("fail", 500); } async Task should_log_errors() { var fakeTraceWriter = new Mock <ITraceWriter>(); var subject = new FakeQueryController(FakeQueryProcessor.Object, fakeTraceWriter.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new Exception("fail")); await subject.HandlePost(QueryName, Json); fakeTraceWriter.Verify(x => x.Trace(It.IsAny <HttpRequestMessage>(), "UnhandledQueryException", TraceLevel.Error, It.IsAny <Action <TraceRecord> >())); } }
public async Task when_handling_the_query_via_Get_with_real_QueryProcessor() { FakeQueryHandler = new Mock <IQueryHandler <FakeQuery, FakeResult> >(); var mock = new Mock <IServiceProvider>(); mock.Setup(x => x.GetService(typeof(IQueryHandler <FakeQuery, FakeResult>))).Returns(FakeQueryHandler.Object); Subject = new FakeQueryController(new QueryProcessor(new QueryTypeCollection(typeof(FakeQuery).GetTypeInfo().Assembly), mock.Object)) { ControllerContext = Fake.ControllerContext() }; async Task should_work() { var expected = new FakeResult(); FakeQueryHandler.Setup(x => x.HandleAsync(It.IsAny <FakeQuery>())).Returns(Task.FromResult(expected)); var queryName = "FakeQuery"; var result = await Subject.HandleGet(queryName) as OkObjectResult; result.Value.Should().Be(expected); } async Task should_handle_errors() { var queryName = "NotFoundQuery"; var result = await Subject.HandleGet(queryName) as BadRequestObjectResult; result.ShouldBeError("The query type 'NotFoundQuery' could not be found"); } }
public async Task when_processing_the_command_with_or_without_result() { FakeCommandProcessor = new Mock <ICommandProcessor>(); Subject = FakeCommandProcessor.Object; async Task should_invoke_the_correct_command_handler_for_commands_without_result() { var expectedCommandType = typeof(FakeCommand); FakeCommandProcessor.Setup(x => x.GetCommandType(expectedCommandType.Name)).Returns(expectedCommandType); FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Returns(Task.CompletedTask); var result = await Subject.ProcessWithOrWithoutResultAsync(expectedCommandType.Name, "{}"); result.Should().Be(CommandResult.None); } async Task should_invoke_the_correct_command_handler_for_commands_with_result() { var expectedResult = new FakeResult(); var expectedCommandType = typeof(FakeResultCommand); FakeCommandProcessor.Setup(x => x.GetCommandType(expectedCommandType.Name)).Returns(expectedCommandType); FakeCommandProcessor.Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Returns(Task.FromResult(expectedResult)); var result = await Subject.ProcessWithOrWithoutResultAsync(expectedCommandType.Name, "{}"); result.Value.Should().Be(expectedResult); } }
public void Process_WithRegisteredHandler_ShouldReturnNotNull() { // Assign FakeQuery query = new FakeQuery(); IQueryProcessor processor = new FakeQueryProcessor(); // Act FakeResult result = processor.Process(query); // Assert Assert.NotNull(result); }
public void Retrieve_GivenHandlerIsMapped_DispatcherShouldReturnResultsFromQuery() { var query = GetFakeQuery(); var guid = Guid.NewGuid(); query.Id = guid; var fakeResult = new FakeResult(); A.CallTo(() => _handler.Retrieve(query)).Returns(fakeResult); var result = _dispatcher.Dispatch <FakeQuery, FakeResult>(query); result.ShouldBeEquivalentTo(fakeResult); }
public async Task when_handling_the_query_via_Post() { Req = new DefaultHttpRequest(new DefaultHttpContext()) { Method = "POST", Body = new MemoryStream(Encoding.UTF8.GetBytes("{}")) }; async Task should_return_the_result_from_the_query_processor() { var expected = new FakeResult(); The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Returns(Task.FromResult(expected)); var result = await Subject.Handle(QueryName, Req, Logger) as OkObjectResult; result.StatusCode.Should().Be(200); result.Value.Should().Be(expected); } async Task should_handle_QueryProcessorException() { The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryProcessorException("fail")); var result = await Subject.Handle(QueryName, Req, Logger); result.ShouldBeError("fail", 400); } async Task should_handle_QueryException() { The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryException("invalid")); var result = await Subject.Handle(QueryName, Req, Logger); result.ShouldBeError("invalid", 400); } async Task should_handle_Exception() { The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new Exception("fail")); var result = await Subject.Handle(QueryName, Req, Logger); result.ShouldBeError("fail", 500); } }
public async Task when_handling_the_command_with_result() { CommandName = "FakeResultCommand"; The <Mock <ICommandProcessor> >().Setup(x => x.GetCommandType(CommandName)).Returns(typeof(FakeResultCommand)); async Task should_return_the_result_from_the_command_processor() { var expected = new FakeResult(); The <Mock <ICommandProcessor> >().Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Returns(Task.FromResult(expected)); var result = await Subject.Handle(CommandName, Req, Logger); result.StatusCode.Should().Be(200); result.Content.ReadAsAsync <FakeResult>().Should().NotBeNull(); } }
public void GivenFulfilledNeed_WhenAskingForValue_ThenItShouldReturnCorrectValue() { // arrange FakeEventMessage fakeEventMessage = new FakeEventMessage(new EventData(new ArraySegment <byte>())); FakeNeed fakeNeed = new FakeNeed(); const string stricklandPropaneLlc = "Strickland Propane, LLC"; ExpandoObject expected = JsonConvert.DeserializeObject <ExpandoObject>($"{{'BusinessName':'{stricklandPropaneLlc}'}}"); FakeResult fakeResult = new FakeResult(expected); INeedActivity needActivity = new Privateer().Object <GoogazonActivities.TopicNeedActivity.TopicNeedActivity>(fakeEventMessage, fakeNeed, fakeResult); // act dynamic actual = needActivity.ValueAsync().Result; // assert ((ExpandoObject)actual).Should().BeEquivalentTo(expected); ((string)actual.BusinessName).Should().Be(stricklandPropaneLlc); }
public async Task when_handling_the_query_via_Post() { Req = new HttpRequestMessage { Method = HttpMethod.Post, Content = new StringContent("{}") }; Req.SetConfiguration(new HttpConfiguration()); async Task should_return_the_result_from_the_query_processor() { var expected = new FakeResult(); The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Returns(Task.FromResult(expected)); var result = await Subject.Handle(QueryName, Req, Logger); result.StatusCode.Should().Be(200); result.Content.ReadAsAsync <FakeQuery>().Should().NotBeNull(); } async Task should_handle_QueryProcessorException() { The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryProcessorException("fail")); var result = await Subject.Handle(QueryName, Req, Logger); await result.ShouldBeErrorAsync("fail", 400); } async Task should_handle_QueryException() { The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryException("invalid")); var result = await Subject.Handle(QueryName, Req, Logger); await result.ShouldBeErrorAsync("invalid", 400); } async Task should_handle_Exception() { The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new Exception("fail")); var result = await Subject.Handle(QueryName, Req, Logger); await result.ShouldBeErrorAsync("fail", 500); } }
public async Task when_handling_the_command_with_result() { CommandName = "FakeResultCommand"; FakeCommandProcessor.Setup(x => x.GetCommandType(CommandName)).Returns(typeof(FakeResultCommand)); async Task should_return_the_result_from_the_command_processor() { var expected = new FakeResult(); FakeCommandProcessor.Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Returns(Task.FromResult(expected)); var result = await Subject.Handle(CommandName, Json) as OkNegotiatedContentResult <object>; (await result.ExecuteAsync(CancellationToken.None)).StatusCode.Should().Be(200); result.Content.Should().Be(expected); } }
public async Task when_handling_the_query_via_Get() { Subject.Request.RequestUri = new Uri("http://example.com?foo=bar"); QueryName = "FakeQuery"; FakeQueryProcessor.Setup(x => x.GetQueryType(QueryName)).Returns(typeof(FakeQuery)); async Task should_return_the_result_from_the_query_processor() { var expected = new FakeResult(); FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Returns(Task.FromResult(expected)); var result = await Subject.HandleGet(QueryName) as OkNegotiatedContentResult <object>; (await result.ExecuteAsync(CancellationToken.None)).StatusCode.Should().Be(200); result.Content.Should().Be(expected); } async Task should_handle_QueryProcessorException() { FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryProcessorException("fail")); var result = await Subject.HandleGet(QueryName); await result.ShouldBeErrorAsync("fail", 400); } async Task should_handle_QueryException() { FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryException("invalid")); var result = await Subject.HandleGet(QueryName); await result.ShouldBeErrorAsync("invalid", 400); } async Task should_handle_Exception() { FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new Exception("fail")); var result = await Subject.HandleGet(QueryName); await result.ShouldBeErrorAsync("fail", 500); } }
public async Task when_handling_the_query_via_Post() { Request = new APIGatewayProxyRequest { Body = "{}" }; async Task should_return_the_result_from_the_query_processor() { var expected = new FakeResult(); The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).ReturnsAsync(expected); var result = await Subject.Handle(QueryName, Request, Context.Object); result.StatusCode.Should().Be(200); result.Body.Should().NotBeEmpty(); } async Task should_handle_QueryProcessorException() { The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryProcessorException("fail")); var result = await Subject.Handle(QueryName, Request, Context.Object); result.ShouldBeError("fail", 400); } async Task should_handle_QueryException() { The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryException("invalid")); var result = await Subject.Handle(QueryName, Request, Context.Object); result.ShouldBeError("invalid", 400); } async Task should_handle_Exception() { The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new Exception("fail")); var result = await Subject.Handle(QueryName, Request, Context.Object); result.ShouldBeError("fail", 500); } }
public void GivenNeed_WhenAskingToExpressNeed_ThenItShouldCallExpressNeed() { // arrange FakeEventMessage fakeEventMessage = new FakeEventMessage(new EventData(new ArraySegment <byte>())); FakeNeed fakeNeed = new FakeNeed(); const string stricklandPropaneLlc = "Strickland Propane, LLC"; ExpandoObject expected = JsonConvert.DeserializeObject <ExpandoObject>($"{{'BusinessName':'{stricklandPropaneLlc}'}}"); FakeResult fakeResult = new FakeResult(expected); INeedActivity needActivity = new Privateer().Object <GoogazonActivities.TopicNeedActivity.TopicNeedActivity>(fakeEventMessage, fakeNeed, fakeResult); // act Func <Task> func = async() => await needActivity.ExpressNeed(); func.Invoke(); // assert fakeNeed.CallCountSpy.Should().Be(1); }
public async Task when_processing_the_query() { FakeQueryTypeCollection = new Mock <IQueryTypeCollection>(); FakeServiceProvider = new Mock <IServiceProvider>(); Subject = new QueryProcessor(FakeQueryTypeCollection.Object, FakeServiceProvider.Object); async Task should_invoke_the_correct_query_handler_and_return_a_result() { FakeQuery expectedQuery = null; var expectedResult = new FakeResult(); var fakeQueryHandler = new FakeQueryHandler(x => { expectedQuery = x; return(expectedResult); }); FakeServiceProvider.Setup(x => x.GetService(typeof(IEnumerable <IQueryHandler <FakeQuery, FakeResult> >))).Returns(new[] { fakeQueryHandler }); var query = new FakeQuery(); var result = await Subject.ProcessAsync(query); query.Should().Be(expectedQuery); result.Should().Be(expectedResult); } void should_throw_exception_if_the_query_handler_is_not_found() { var query = new Mock <IQuery <FakeResult> >().Object; Subject.Awaiting(x => x.ProcessAsync(query)).Should() .Throw <QueryProcessorException>() .WithMessage($"The query handler for '{query}' could not be found"); } void should_throw_exception_if_multiple_query_handlers_are_found() { var handlerType = typeof(IQueryHandler <FakeMultiQuery1, FakeResult>); var enumerableType = typeof(IEnumerable <IQueryHandler <FakeMultiQuery1, FakeResult> >); FakeServiceProvider.Setup(x => x.GetService(enumerableType)).Returns(new[] { new Mock <IQueryHandler <FakeMultiQuery1, FakeResult> >().Object, new Mock <IQueryHandler <FakeMultiQuery1, FakeResult> >().Object }); var query = new FakeMultiQuery1(); Subject.Awaiting(x => x.ProcessAsync(query)).Should() .Throw <QueryProcessorException>() .WithMessage($"Multiple query handlers for '{handlerType}' was found"); } }
public async Task when_processing_the_command_with_result() { FakeCommandTypeCollection = new Mock <ICommandTypeCollection>(); FakeServiceProvider = new Mock <IServiceProvider>(); Subject = new CommandProcessor(FakeCommandTypeCollection.Object, FakeServiceProvider.Object); async Task should_invoke_the_correct_command_handler_and_return_a_result() { FakeResultCommand expectedCommand = null; var expectedResult = new FakeResult(); var fakeCommandHandler = new FakeResultCommandHandler(x => { expectedCommand = x; return(expectedResult); }); FakeServiceProvider.Setup(x => x.GetService(typeof(IEnumerable <ICommandHandler <FakeResultCommand, FakeResult> >))).Returns(new[] { fakeCommandHandler }); var command = new FakeResultCommand(); var result = await Subject.ProcessWithResultAsync(command); command.Should().Be(expectedCommand); result.Should().Be(expectedResult); } void should_throw_exception_if_the_command_handler_is_not_found() { var command = new Mock <ICommand <object> >().Object; Subject.Awaiting(x => x.ProcessWithResultAsync(command)).Should() .Throw <CommandProcessorException>() .WithMessage($"The command handler for '{command}' could not be found"); } void should_throw_exception_if_multiple_command_handlers_are_found() { var handlerType = typeof(ICommandHandler <FakeMultiResultCommand1, FakeResult>); var enumerableType = typeof(IEnumerable <ICommandHandler <FakeMultiResultCommand1, FakeResult> >); FakeServiceProvider.Setup(x => x.GetService(enumerableType)).Returns(new[] { new Mock <ICommandHandler <FakeMultiResultCommand1, FakeResult> >().Object, new Mock <ICommandHandler <FakeMultiResultCommand1, FakeResult> >().Object }); var command = new FakeMultiResultCommand1(); Subject.Awaiting(x => x.ProcessWithResultAsync(command)).Should() .Throw <CommandProcessorException>() .WithMessage($"Multiple command handlers for '{handlerType}' was found"); } }
public async Task when_handling_the_query_via_Get() { Req = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri("http://example.com?foo=bar") }; Req.SetConfiguration(new HttpConfiguration()); async Task should_return_the_result_from_the_query_processor() { var expected = new FakeResult(); The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Returns(Task.FromResult(expected)); var result = await Subject.Handle(QueryName, Req, Logger); result.StatusCode.Should().Be(200); result.Content.ReadAsAsync <FakeQuery>().Should().NotBeNull(); } }
public async Task when_handling_the_command() { async Task should_return_the_result_from_the_command_processor() { var expected = new FakeResult(); FakeCommandProcessor.Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Returns(Task.FromResult(expected)); var result = await Subject.Handle(new FakeResultCommand()) as OkObjectResult; result.StatusCode.Should().Be(200); result.Value.Should().Be(expected); } async Task should_handle_CommandProcessorException() { FakeCommandProcessor.Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Throws(new CommandProcessorException("fail")); var result = await Subject.Handle(new FakeResultCommand()); result.ShouldBeError("fail", 400); } async Task should_handle_CommandException() { FakeCommandProcessor.Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Throws(new CommandException("invalid")); var result = await Subject.Handle(new FakeResultCommand()); result.ShouldBeError("invalid", 400); } async Task should_handle_Exception() { FakeCommandProcessor.Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Throws(new Exception("fail")); var result = await Subject.Handle(new FakeResultCommand()); result.ShouldBeError("fail", 500); } }
public async Task when_handling_the_query_via_Get() { Req = new DefaultHttpRequest(new DefaultHttpContext()) { Method = "GET", Query = new QueryCollection(new Dictionary <string, StringValues> { { "foo", new StringValues("bar") } }) }; async Task should_return_the_result_from_the_query_processor() { var expected = new FakeResult(); The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Returns(Task.FromResult(expected)); var result = await Subject.Handle(QueryName, Req, Logger) as OkObjectResult; result.StatusCode.Should().Be(200); result.Value.Should().Be(expected); } }
public async Task when_handling_the_query_via_Get() { Request = new APIGatewayProxyRequest { HttpMethod = "GET", MultiValueQueryStringParameters = new Dictionary <string, IList <string> > { { "foo", new List <string> { "bar" } } } }; async Task should_return_the_result_from_the_query_processor() { var expected = new FakeResult(); The <Mock <IQueryProcessor> >().Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).ReturnsAsync(expected); var result = await Subject.Handle(QueryName, Request, Context.Object); result.StatusCode.Should().Be(200); result.Body.Should().NotBeEmpty(); } }
public async Task when_handling_the_query_via_Post_with_real_QueryProcessor() { FakeQueryHandler = new Mock <IQueryHandler <FakeQuery, FakeResult> >(); var mock = new Mock <IServiceProvider>(); mock.Setup(x => x.GetService(typeof(IQueryHandler <FakeQuery, FakeResult>))).Returns(FakeQueryHandler.Object); Subject = new FakeQueryController(new QueryProcessor(new QueryTypeCollection(typeof(FakeQuery).Assembly), mock.Object)) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; async Task should_work() { var expected = new FakeResult(); FakeQueryHandler.Setup(x => x.HandleAsync(It.IsAny <FakeQuery>())).Returns(Task.FromResult(expected)); var queryName = "FakeQuery"; var json = JObject.Parse("{}"); var result = await Subject.HandlePost(queryName, json) as OkNegotiatedContentResult <object>; result.Content.Should().Be(expected); } async Task should_handle_errors() { var queryName = "NotFoundQuery"; var json = JObject.Parse("{}"); var result = await Subject.HandlePost(queryName, json) as BadRequestErrorMessageResult; await result.ShouldBeErrorAsync("The query type 'NotFoundQuery' could not be found"); } }
public async Task when_processing_the_command_with_or_without_result() { FakeCommandTypeCollection = new Mock <ICommandTypeCollection>(); FakeServiceProvider = new Mock <IServiceProvider>(); Subject = new CommandProcessor(FakeCommandTypeCollection.Object, FakeServiceProvider.Object); async Task should_invoke_the_correct_command_handler_for_commands_without_result() { var expectedCommandType = typeof(FakeCommand); var fakeCommandHandler = new Mock <ICommandHandler <FakeCommand> >(); FakeCommandTypeCollection.Setup(x => x.GetType(expectedCommandType.Name)).Returns(expectedCommandType); FakeServiceProvider.Setup(x => x.GetService(typeof(ICommandHandler <FakeCommand>))).Returns(fakeCommandHandler.Object); var result = await Subject.ProcessWithOrWithoutResultAsync(expectedCommandType.Name, "{}"); fakeCommandHandler.Verify(x => x.HandleAsync(It.IsAny <FakeCommand>())); result.Should().Be(CommandResult.None); } async Task should_invoke_the_correct_command_handler_for_commands_with_result() { var expectedResult = new FakeResult(); var expectedCommandType = typeof(FakeResultCommand); var fakeCommandHandler = new Mock <ICommandHandler <FakeResultCommand, FakeResult> >(); fakeCommandHandler.Setup(x => x.HandleAsync(It.IsAny <FakeResultCommand>())).Returns(Task.FromResult(expectedResult)); FakeCommandTypeCollection.Setup(x => x.GetType(expectedCommandType.Name)).Returns(expectedCommandType); FakeServiceProvider.Setup(x => x.GetService(typeof(ICommandHandler <FakeResultCommand, FakeResult>))).Returns(fakeCommandHandler.Object); var result = await Subject.ProcessWithOrWithoutResultAsync(expectedCommandType.Name, "{}"); fakeCommandHandler.Verify(x => x.HandleAsync(It.IsAny <FakeResultCommand>())); result.Value.Should().Be(expectedResult); } }
public async Task when_processing_the_query() { FakeQueryTypeCollection = new Mock <IQueryTypeCollection>(); FakeServiceProvider = new Mock <IServiceProvider>(); Subject = new QueryProcessor(FakeQueryTypeCollection.Object, FakeServiceProvider.Object); async Task should_invoke_the_correct_query_handler() { FakeQuery expectedQuery = null; var fakeQueryHandler = new FakeQueryHandler(x => { expectedQuery = x; return(new FakeResult()); }); FakeServiceProvider.Setup(x => x.GetService(typeof(IQueryHandler <FakeQuery, FakeResult>))).Returns(fakeQueryHandler); var query = new FakeQuery(); await Subject.ProcessAsync(query); query.Should().Be(expectedQuery); } async Task should_create_the_query_from_a_string() { var expectedQueryType = typeof(FakeQuery); var fakeQueryHandler = new Mock <IQueryHandler <FakeQuery, FakeResult> >(); FakeQueryTypeCollection.Setup(x => x.GetType(expectedQueryType.Name)).Returns(expectedQueryType); FakeServiceProvider.Setup(x => x.GetService(typeof(IQueryHandler <FakeQuery, FakeResult>))).Returns(fakeQueryHandler.Object); await Subject.ProcessAsync <FakeResult>(expectedQueryType.Name, "{}"); fakeQueryHandler.Verify(x => x.HandleAsync(It.IsAny <FakeQuery>())); } async Task should_create_the_query_from_a_dictionary() { var expectedQueryType = typeof(FakeQuery); var fakeQueryHandler = new Mock <IQueryHandler <FakeQuery, FakeResult> >(); FakeQueryTypeCollection.Setup(x => x.GetType(expectedQueryType.Name)).Returns(expectedQueryType); FakeServiceProvider.Setup(x => x.GetService(typeof(IQueryHandler <FakeQuery, FakeResult>))).Returns(fakeQueryHandler.Object); await Subject.ProcessAsync <FakeResult>(expectedQueryType.Name, new Dictionary <string, IEnumerable <string> >()); fakeQueryHandler.Verify(x => x.HandleAsync(It.IsAny <FakeQuery>())); } async Task should_create_a_complex_query_from_a_dictionary() { var expectedQueryType = typeof(FakeComplexQuery); FakeComplexQuery expectedQuery = null; var fakeQueryHandler = new FakeComplexQueryHandler(x => { expectedQuery = x; return(new List <FakeResult>()); }); FakeQueryTypeCollection.Setup(x => x.GetType(expectedQueryType.Name)).Returns(expectedQueryType); FakeServiceProvider.Setup(x => x.GetService(typeof(IQueryHandler <FakeComplexQuery, IEnumerable <FakeResult> >))).Returns(fakeQueryHandler); var query = new Dictionary <string, IEnumerable <string> > { { "String", new[] { "Value" } }, { "Int", new[] { "1" } }, { "Bool", new[] { "true" } }, { "DateTime", new[] { "2018-07-06" } }, { "Guid", new[] { "3B10C34C-D423-4EC3-8811-DA2E0606E241" } }, { "NullableDouble", new[] { "2.1" } }, { "UndefinedProperty", new[] { "should_not_be_used" } }, { "Array", new[] { "1", "2" } }, { "IEnumerable", new[] { "3", "4" } }, { "List", new[] { "5", "6" } } }; await Subject.ProcessAsync <IEnumerable <FakeResult> >(expectedQueryType.Name, query); expectedQuery.String.Should().Be("Value"); expectedQuery.Int.Should().Be(1); expectedQuery.Bool.Should().Be(true); expectedQuery.DateTime.Should().Be(DateTime.Parse("2018-07-06")); expectedQuery.Guid.Should().Be(new Guid("3B10C34C-D423-4EC3-8811-DA2E0606E241")); expectedQuery.NullableDouble.Should().Be(2.1); expectedQuery.Array.Should().Equal(1, 2); expectedQuery.IEnumerable.Should().Equal(3, 4); expectedQuery.List.Should().Equal(5, 6); } async Task should_return_the_result_from_the_query_handler() { var expected = new FakeResult(); var fakeQueryHandler = new FakeQueryHandler(x => expected); FakeServiceProvider.Setup(x => x.GetService(typeof(IQueryHandler <FakeQuery, FakeResult>))).Returns(fakeQueryHandler); var query = new FakeQuery(); var result = await Subject.ProcessAsync(query); result.Should().Be(expected); } void should_throw_exception_if_the_query_handler_is_not_found() { var query = new Mock <IQuery <FakeResult> >().Object; Subject.Awaiting(async x => await x.ProcessAsync(query)).Should() .Throw <QueryProcessorException>() .WithMessage($"The query handler for '{query}' could not be found"); } void should_throw_exception_if_the_query_type_is_not_found() { var queryName = "NotFoundQuery"; var json = JObject.Parse("{}"); Subject.Awaiting(async x => await x.ProcessAsync <object>(queryName, json)).Should() .Throw <QueryProcessorException>() .WithMessage("The query type 'NotFoundQuery' could not be found"); } void should_throw_exception_if_the_json_is_invalid() { var queryName = "FakeQuery"; FakeQueryTypeCollection.Setup(x => x.GetType(queryName)).Returns(typeof(FakeQuery)); Subject.Awaiting(async x => await x.ProcessAsync <object>(queryName, (JObject)null)).Should() .Throw <QueryProcessorException>() .WithMessage("The json could not be converted to an object"); } void should_throw_exception_if_the_dictionary_is_invalid() { var queryName = "FakeQuery"; FakeQueryTypeCollection.Setup(x => x.GetType(queryName)).Returns(typeof(FakeQuery)); Subject.Awaiting(async x => await x.ProcessAsync <object>(queryName, (IDictionary <string, IEnumerable <string> >)null)).Should() .Throw <QueryProcessorException>() .WithMessage("The dictionary could not be converted to an object"); } }
public async Task when_processing_the_command_with_result() { FakeCommandTypeCollection = new Mock <ICommandTypeCollection>(); FakeServiceProvider = new Mock <IServiceProvider>(); Subject = new CommandProcessor(FakeCommandTypeCollection.Object, FakeServiceProvider.Object); async Task should_invoke_the_correct_command_handler_and_return_a_result() { FakeResultCommand expectedCommand = null; var expectedResult = new FakeResult(); var fakeCommandHandler = new FakeResultCommandHandler(x => { expectedCommand = x; return(expectedResult); }); FakeServiceProvider.Setup(x => x.GetService(typeof(ICommandHandler <FakeResultCommand, FakeResult>))).Returns(fakeCommandHandler); var command = new FakeResultCommand(); var result = await Subject.ProcessWithResultAsync(command); command.Should().Be(expectedCommand); result.Should().Be(expectedResult); } async Task should_create_the_command_from_a_string() { var expectedCommandType = typeof(FakeResultCommand); var fakeCommandHandler = new Mock <ICommandHandler <FakeResultCommand, FakeResult> >(); FakeCommandTypeCollection.Setup(x => x.GetType(expectedCommandType.Name)).Returns(expectedCommandType); FakeServiceProvider.Setup(x => x.GetService(typeof(ICommandHandler <FakeResultCommand, FakeResult>))).Returns(fakeCommandHandler.Object); await Subject.ProcessWithResultAsync <FakeResult>(expectedCommandType.Name, "{}"); fakeCommandHandler.Verify(x => x.HandleAsync(It.IsAny <FakeResultCommand>())); } void should_throw_exception_if_the_command_handler_is_not_found() { var command = new Mock <ICommand <object> >().Object; Subject.Awaiting(async x => await x.ProcessWithResultAsync(command)).Should() .Throw <CommandProcessorException>() .WithMessage($"The command handler for '{command}' could not be found"); } void should_throw_exception_if_the_command_type_is_not_found() { var commandName = "NotFoundCommand"; var json = JObject.Parse("{}"); Subject.Awaiting(async x => await x.ProcessWithResultAsync <object>(commandName, json)).Should() .Throw <CommandProcessorException>() .WithMessage("The command type 'NotFoundCommand' could not be found"); } void should_throw_exception_if_the_json_is_invalid() { var commandName = "FakeCommand"; FakeCommandTypeCollection.Setup(x => x.GetType(commandName)).Returns(typeof(FakeCommand)); Subject.Awaiting(async x => await x.ProcessWithResultAsync <object>(commandName, (JObject)null)).Should() .Throw <CommandProcessorException>() .WithMessage("The json could not be converted to an object"); } }
public async Task when_processing_the_query() { FakeQueryTypeCollection = new Mock <IQueryTypeCollection>(); FakeServiceProvider = new Mock <IServiceProvider>(); Subject = new QueryProcessor(FakeQueryTypeCollection.Object, FakeServiceProvider.Object); async Task should_invoke_the_correct_query_handler() { var fakeQueryHandler = new FakeQueryHandler(x => { Expected = x; return(new FakeResult()); }); FakeServiceProvider.Setup(x => x.GetService(typeof(IQueryHandler <FakeQuery, FakeResult>))).Returns(fakeQueryHandler); var query = new FakeQuery(); await Subject.ProcessAsync(query); Expected.Should().Be(query); } async Task should_return_the_result_from_the_query_handler() { var expected = new FakeResult(); var query = new FakeQuery(); var fakeQueryHandler = new FakeQueryHandler(x => expected); FakeServiceProvider.Setup(x => x.GetService(typeof(IQueryHandler <FakeQuery, FakeResult>))).Returns(fakeQueryHandler); var result = await Subject.ProcessAsync(query); result.Should().Be(expected); } void should_throw_exception_if_the_query_handler_is_not_found() { var query = new Mock <IQuery <FakeResult> >().Object; Subject.Awaiting(async x => await x.ProcessAsync(query)).Should() .Throw <QueryProcessorException>() .WithMessage($"The query handler for '{query}' could not be found"); } void should_throw_exception_if_the_query_type_is_not_found() { var queryName = "NotFoundQuery"; var json = JObject.Parse("{}"); Subject.Awaiting(async x => await x.ProcessAsync <object>(queryName, json)).Should() .Throw <QueryProcessorException>() .WithMessage("The query type 'NotFoundQuery' could not be found"); } void should_throw_exception_if_the_json_is_invalid() { var queryName = "FakeQuery"; FakeQueryTypeCollection.Setup(x => x.GetType(queryName)).Returns(typeof(FakeQuery)); Subject.Awaiting(async x => await x.ProcessAsync <object>(queryName, (JObject)null)).Should() .Throw <QueryProcessorException>() .WithMessage("The json could not be converted to an object"); } void should_throw_exception_if_the_dictionary_is_invalid() { var queryName = "FakeQuery"; FakeQueryTypeCollection.Setup(x => x.GetType(queryName)).Returns(typeof(FakeQuery)); Subject.Awaiting(async x => await x.ProcessAsync <object>(queryName, (IDictionary <string, string>)null)).Should() .Throw <QueryProcessorException>() .WithMessage("The dictionary could not be converted to an object"); } }