Example #1
0
        public Task <IResult <TheaterLocation> > SendCreateTheaterCommand(
            CreateNewTheater command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            return(Run());

            async Task <IResult <TheaterLocation> > Run()
            {
                string path = "api/commands/SendCreateTheaterCommand";

                HttpResponseMessage response = await
                                               _client.PostAsJsonAsync(new Uri(_endpoint, path), command);

                switch (response.StatusCode)
                {
                case BadRequest:
                    return(await ReadError <TheaterLocation>(response));
                }

                var location = new TheaterLocation(response.Headers.Location);

                return(new Success <TheaterLocation>(location));
            }
        }
Example #2
0
        public async Task given_valid_command_then_SendCreateTheaterCommand_returns_location_result(
            HttpMessageHandlerDouble handlerStub,
            Uri endpoint,
            CreateNewTheater command,
            TheaterLocation location)
        {
            // Arrange
            bool predicate(HttpRequestMessage req)
            {
                string path = "api/commands/SendCreateTheaterCommand";

                return(req.Method == HttpMethod.Post &&
                       req.RequestUri == new Uri(endpoint, path) &&
                       req.Content is ObjectContent <CreateNewTheater> 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 <TheaterLocation> actual = await
                                               sut.SendCreateTheaterCommand(command);

            // Assert
            actual.Should().BeOfType <Success <TheaterLocation> >();
            actual.Should().BeEquivalentTo(new { Value = location });
        }
Example #3
0
        public async Task <IActionResult> SendCreateTheaterCommand(
            [FromBody] CreateNewTheater source,
            [FromServices] IMessageBus messageBus)
        {
            CreateTheater command = Translate(source);
            await messageBus.Send(new Envelope(command));

            return(Accepted(uri: $"api/queries/Theaters/{command.TheaterId}"));
        }
Example #4
0
 private static CreateTheater Translate(CreateNewTheater source)
 {
     return(new CreateTheater
     {
         TheaterId = Guid.NewGuid(),
         Name = source.Name,
         SeatRowCount = source.SeatRowCount,
         SeatColumnCount = source.SeatColumnCount,
     });
 }
Example #5
0
        public async Task SendCreateTheaterCommand_returns_AcceptedResult(
            CreateNewTheater content,
            InProcessMessageLogger messageBusDummy,
            [NoAutoProperties] CommandsController sut)
        {
            // Act
            IActionResult actual = await
                                   sut.SendCreateTheaterCommand(content, messageBusDummy);

            // Assert
            actual.Should().BeOfType <AcceptedResult>();
        }
Example #6
0
        public async Task SendCreateTheaterCommand_sets_location_correctly(
            CreateNewTheater content,
            InProcessMessageLogger messageBusSpy,
            [NoAutoProperties] CommandsController sut)
        {
            // Act
            IActionResult result = await
                                   sut.SendCreateTheaterCommand(content, messageBusSpy);

            // Assert
            var accepted = (AcceptedResult)result;
            IEnumerable <Envelope> log = messageBusSpy.Log;
            Guid theaterId             = log.Single().Message.As <CreateTheater>().TheaterId;

            accepted.Location.Should().Be($"api/queries/Theaters/{theaterId}");
        }
Example #7
0
        public async Task <ActionResult> Create(
            [FromForm] CreateTheaterViewModel model,
            [FromServices] ISendCreateTheaterCommandService service)
        {
            CreateNewTheater command = model.CreateCommand();

            IResult <TheaterLocation> result = await
                                               service.SendCreateTheaterCommand(command);

            switch (result)
            {
            case Success <TheaterLocation> success:
                return(RedirectToAction(nameof(Index)));

            case Error <TheaterLocation> error:
                ModelState.AddModelError(string.Empty, error.Message);
                break;
            }

            return(View(model));
        }
Example #8
0
        public async Task given_bad_command_then_SendCreateTheaterCommand_returns_error_result(
            HttpMessageHandlerDouble handlerStub,
            Uri endpoint,
            CreateNewTheater command,
            ModelStateDictionary state,
            string key,
            string errorMessage)
        {
            // Arrange
            bool predicate(HttpRequestMessage req)
            {
                string path = "api/commands/SendCreateTheaterCommand";

                return(req.Method == HttpMethod.Post &&
                       req.RequestUri == new Uri(endpoint, path) &&
                       req.Content is ObjectContent <CreateNewTheater> 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 <TheaterLocation> actual = await
                                               sut.SendCreateTheaterCommand(command);

            // Assert
            actual.Should().BeOfType <Error <TheaterLocation> >();
            actual.Should().BeEquivalentTo(new { Message = errorMessage });
        }
Example #9
0
        public async Task SendCreateTheaterCommand_sends_command_correctly(
            CreateNewTheater source,
            InProcessMessageLogger messageBusSpy,
            [NoAutoProperties] CommandsController sut)
        {
            // Act
            await sut.SendCreateTheaterCommand(source, messageBusSpy);

            // Assert
            IEnumerable <Envelope> log = messageBusSpy.Log;

            log.Should().ContainSingle();
            log.Single().Message.Should().BeOfType <CreateTheater>();

            var actual = (CreateTheater)log.Single().Message;

            actual.TheaterId.Should().NotBeEmpty();
            actual.Should().BeEquivalentTo(new
            {
                source.Name,
                source.SeatRowCount,
                source.SeatColumnCount,
            });
        }