public async Task given_valid_command_then_SendCreateMovieCommand_returns_location_result( HttpMessageHandlerDouble handlerStub, Uri endpoint, CreateNewMovie command, MovieLocation location) { // Arrange bool predicate(HttpRequestMessage req) { string path = "api/commands/SendCreateMovieCommand"; return(req.Method == HttpMethod.Post && req.RequestUri == new Uri(endpoint, path) && req.Content is ObjectContent <CreateNewMovie> content && content.Value == command && content.Formatter is JsonMediaTypeFormatter); } var response = new HttpResponseMessage(HttpStatusCode.Accepted); response.Headers.Location = location.Uri; handlerStub.AddAnswer(predicate, answer: response); var sut = new ApiClient(new HttpClient(handlerStub), endpoint); // Act IResult <MovieLocation> actual = await sut.SendCreateMovieCommand(command); // Assert actual.Should().BeOfType <Success <MovieLocation> >(); actual.Should().BeEquivalentTo(new { Value = location }); }
public Task <IResult <MovieLocation> > SendCreateMovieCommand( CreateNewMovie command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } return(Run()); async Task <IResult <MovieLocation> > Run() { string path = "api/commands/SendCreateMovieCommand"; HttpResponseMessage response = await _client.PostAsJsonAsync(new Uri(_endpoint, path), command); switch (response.StatusCode) { case BadRequest: return(await ReadError <MovieLocation>(response)); } var location = new MovieLocation(response.Headers.Location); return(new Success <MovieLocation>(location)); } }
private static CreateMovie Translate(CreateNewMovie source) { return(new CreateMovie { MovieId = Guid.NewGuid(), Title = source.Title, }); }
public async Task <IActionResult> SendCreateMovieCommand( [FromBody] CreateNewMovie source, [FromServices] IMessageBus messageBus) { CreateMovie command = Translate(source); await messageBus.Send(new Envelope(command)); return(Accepted(uri: $"api/queries/Movies/{command.MovieId}")); }
public async Task SendCreateMovieCommand_returns_AcceptedResult( CreateNewMovie content, InProcessMessageLogger messageBusDummy, [NoAutoProperties] CommandsController sut) { // Act IActionResult actual = await sut.SendCreateMovieCommand(content, messageBusDummy); // Assert actual.Should().BeOfType <AcceptedResult>(); }
public async Task SendCreateMovieCommand_sets_location_correctly( CreateNewMovie content, InProcessMessageLogger messageBusSpy, [NoAutoProperties] CommandsController sut) { // Act IActionResult result = await sut.SendCreateMovieCommand(content, messageBusSpy); // Assert var accepted = (AcceptedResult)result; IEnumerable <Envelope> log = messageBusSpy.Log; Guid movieId = log.Single().Message.As <CreateMovie>().MovieId; accepted.Location.Should().Be($"api/queries/Movies/{movieId}"); }
public async Task SendCreateMovieCommand_sends_command_correctly( CreateNewMovie source, InProcessMessageLogger messageBusSpy, [NoAutoProperties] CommandsController sut) { // Act await sut.SendCreateMovieCommand(source, messageBusSpy); // Assert IEnumerable <Envelope> log = messageBusSpy.Log; log.Should().ContainSingle(); log.Single().Message.Should().BeOfType <CreateMovie>(); var actual = (CreateMovie)log.Single().Message; actual.MovieId.Should().NotBeEmpty(); actual.Should().BeEquivalentTo(new { source.Title }); }
public async Task given_bad_command_then_SendCreateMovieCommand_returns_error_result( HttpMessageHandlerDouble handlerStub, Uri endpoint, CreateNewMovie command, ModelStateDictionary state, string key, string errorMessage) { // Arrange bool predicate(HttpRequestMessage req) { string path = "api/commands/SendCreateMovieCommand"; return(req.Method == HttpMethod.Post && req.RequestUri == new Uri(endpoint, path) && req.Content is ObjectContent <CreateNewMovie> content && content.Value == command && content.Formatter is JsonMediaTypeFormatter); } var response = new HttpResponseMessage(HttpStatusCode.BadRequest); state.AddModelError(key, errorMessage); response.Content = CreateContent(new SerializableError(state)); handlerStub.AddAnswer(predicate, answer: response); var sut = new ApiClient(new HttpClient(handlerStub), endpoint); // Act IResult <MovieLocation> actual = await sut.SendCreateMovieCommand(command); // Assert actual.Should().BeOfType <Error <MovieLocation> >(); actual.Should().BeEquivalentTo(new { Message = errorMessage }); }
public async Task <ActionResult> Create( [FromForm] CreateMovieViewModel model, [FromServices] ISendCreateMovieCommandService service, [FromServices] IResourceAwaiter awaiter) { CreateNewMovie command = model.CreateCommand(); IResult <MovieLocation> result = await service.SendCreateMovieCommand(command); switch (result) { case Success <MovieLocation> success: await awaiter.AwaitResource(success.Value.Uri); return(RedirectToAction(nameof(Index))); case Error <MovieLocation> error: ModelState.AddModelError(string.Empty, error.Message); break; } return(View(model)); }