public async Task given_valid_command_then_SendAddScreeningCommand_returns_location_result( HttpMessageHandlerDouble handlerStub, Uri endpoint, AddNewScreening command, ScreeningLocation location) { // Arrange bool predicate(HttpRequestMessage req) { string path = "api/commands/SendAddScreeningCommand"; return(req.Method == HttpMethod.Post && req.RequestUri == new Uri(endpoint, path) && req.Content is ObjectContent <AddNewScreening> 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 <ScreeningLocation> actual = await sut.SendAddScreeningCommand(command); // Assert actual.Should().BeOfType <Success <ScreeningLocation> >(); actual.Should().BeEquivalentTo(new { Value = location }); }
public async Task <ActionResult> AddScreening( [FromRoute] Guid movieId, [FromForm] AddScreeningViewModel model, [FromServices] ISendAddScreeningCommandService service, [FromServices] IResourceAwaiter awaiter) { AddNewScreening command = model.CreateCommand(); IResult <ScreeningLocation> result = await service.SendAddScreeningCommand(command); switch (result) { case Success <ScreeningLocation> success: await awaiter.AwaitResource(success.Value.Uri); var routeValues = new { movieId }; return(RedirectToAction("Screenings", routeValues)); case Error <ScreeningLocation> error: ModelState.AddModelError(string.Empty, error.Message); break; } return(View(model)); }
public Task <IResult <ScreeningLocation> > SendAddScreeningCommand( AddNewScreening command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } return(Run()); async Task <IResult <ScreeningLocation> > Run() { string path = "api/commands/SendAddScreeningCommand"; HttpResponseMessage response = await _client.PostAsJsonAsync(new Uri(_endpoint, path), command); switch (response.StatusCode) { case BadRequest: return(await ReadError <ScreeningLocation>(response)); } var location = new ScreeningLocation(response.Headers.Location); return(new Success <ScreeningLocation>(location)); } }
public async Task <IActionResult> SendAddScreeningCommand( [FromBody] AddNewScreening source, [FromServices] IMessageBus messageBus) { AddScreening command = Translate(source); await messageBus.Send(new Envelope(command)); string uri = $"api/queries/Movies/{command.MovieId}/Screenings/{command.ScreeningId}"; return(Accepted(uri)); }
public async Task SendAddScreeningCommand_returns_AcceptedResult( AddNewScreening source, IMessageBus messageBusDummy, [NoAutoProperties] CommandsController sut) { // Act IActionResult actual = await sut.SendAddScreeningCommand(source, messageBusDummy); // Assert actual.Should().BeOfType <AcceptedResult>(); }
private static AddScreening Translate(AddNewScreening source) { return(new AddScreening { MovieId = source.MovieId, ScreeningId = Guid.NewGuid(), TheaterId = source.TheaterId, ScreeningTime = source.ScreeningTime, DefaultFee = source.DefaultFee, ChildrenFee = source.ChildrenFee, }); }
public async Task SendAddScreeningCommand_sends_command_correctly( AddNewScreening source, InProcessMessageLogger messageBusSpy, [NoAutoProperties] CommandsController sut) { // Act await sut.SendAddScreeningCommand(source, messageBusSpy); // Assert IEnumerable <Envelope> log = messageBusSpy.Log; log.Should().ContainSingle(); log.Single().Message.Should().BeOfType <AddScreening>(); var actual = (AddScreening)log.Single().Message; actual.MovieId.Should().NotBeEmpty(); actual.Should().BeEquivalentTo(source); }
public async Task SendAddScreeningCommand_sets_location_correctly( AddNewScreening source, InProcessMessageLogger messageBusSpy, [NoAutoProperties] CommandsController sut) { // Act dynamic result = await sut.SendAddScreeningCommand(source, messageBusSpy); // Assert Guid movieId = source.MovieId; IEnumerable <Envelope> log = messageBusSpy.Log; dynamic message = log.Single().Message; Guid screeningId = message.ScreeningId; string actual = result.Location; string uri = $"api/queries/Movies/{movieId}/Screenings/{screeningId}"; actual.Should().Be(uri); }
public async Task given_bad_command_then_SendAddScreeningCommand_returns_error_result( HttpMessageHandlerDouble handlerStub, Uri endpoint, AddNewScreening command, ModelStateDictionary state, string key, string errorMessage) { // Arrange bool predicate(HttpRequestMessage req) { string path = "api/commands/SendAddScreeningCommand"; return(req.Method == HttpMethod.Post && req.RequestUri == new Uri(endpoint, path) && req.Content is ObjectContent <AddNewScreening> 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 <ScreeningLocation> actual = await sut.SendAddScreeningCommand(command); // Assert actual.Should().BeOfType <Error <ScreeningLocation> >(); actual.Should().BeEquivalentTo(new { Message = errorMessage }); }