Ejemplo n.º 1
0
        public async Task UpdateStateAsync_Should_Return_OkObjectResult_With_StateResponse()
        {
            var rowVersion         = new byte[] { 1, 2, 4, 8, 16, 32, 64 };
            var stateId            = Guid.NewGuid();
            var updateStateRequest = new UpdateStateRequest
            {
                Id         = stateId,
                Name       = "Name",
                PolishName = "PolishName"
            };
            var updateStateCommand = new UpdateStateCommand(updateStateRequest.Id, rowVersion, updateStateRequest.Name,
                                                            updateStateRequest.PolishName);
            var stateOutputQuery = new StateOutputQuery(updateStateRequest.Id, new byte[] { 1, 2, 4, 8, 16, 32, 128 },
                                                        updateStateRequest.Name, updateStateRequest.PolishName);
            var stateResponse = new StateResponse(stateOutputQuery.Id, stateOutputQuery.RowVersion, stateOutputQuery.Name, stateOutputQuery.PolishName);

            _mapperMock.Setup(x => x.Map <UpdateStateRequest, UpdateStateCommand>(It.IsAny <UpdateStateRequest>()))
            .Returns(updateStateCommand);
            _communicationBusMock.Setup(x => x.SendCommandAsync(It.IsAny <UpdateStateCommand>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);
            _getStateQueryHandlerMock
            .Setup(x => x.HandleAsync(It.IsAny <GetStateInputQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(stateOutputQuery);
            _mapperMock.Setup(x => x.Map <StateOutputQuery, StateResponse>(It.IsAny <StateOutputQuery>())).Returns(stateResponse);

            var result = await _controller.UpdateStateAsync(stateId, updateStateRequest, rowVersion);

            var okObjectResult = result.As <OkObjectResult>();

            okObjectResult.Value.Should().BeEquivalentTo(stateResponse);
        }
Ejemplo n.º 2
0
        public async Task HandleAsync_Should_Update_State()
        {
            var command = new UpdateStateCommand(Guid.NewGuid(), Array.Empty <byte>(), "NewName", "NewPolishName");
            var state   = State.Builder()
                          .SetId(Guid.NewGuid())
                          .SetRowVersion(command.RowVersion)
                          .SetName("Name")
                          .SetPolishName("PolishName")
                          .Build();
            var getStateResult = GetResult <State> .Ok(state);

            var nameIsNotTakenVerificationResult       = VerificationResult.Ok();
            var polishNameIsNotTakenVerificationResult = VerificationResult.Ok();

            _stateGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(getStateResult);
            _stateVerificationServiceMock.Setup(x => x.VerifyNameIsNotTakenAsync(It.IsAny <string>()))
            .ReturnsAsync(nameIsNotTakenVerificationResult);
            _stateVerificationServiceMock.Setup(x => x.VerifyPolishNameIsNotTakenAsync(It.IsAny <string>()))
            .ReturnsAsync(polishNameIsNotTakenVerificationResult);
            _stateRepositoryMock.Setup(x => x.UpdateAsync(It.IsAny <State>())).Returns(Task.CompletedTask).Verifiable();

            Func <Task> result = async() => await _commandHandler.HandleAsync(command);

            await result.Should().NotThrowAsync <Exception>();

            _stateRepositoryMock.Verify(x =>
                                        x.UpdateAsync(It.Is <State>(s => s.Name.Equals(state.Name) && s.PolishName.Equals(state.PolishName))));
        }
Ejemplo n.º 3
0
        public async Task HandleAsync_Should_Throw_ConflictException_When_Only_PolishName_Is_Already_Used()
        {
            var command = new UpdateStateCommand(Guid.NewGuid(), Array.Empty <byte>(), "NewName", "NewPolishName");
            var state   = State.Builder()
                          .SetId(Guid.NewGuid())
                          .SetRowVersion(command.RowVersion)
                          .SetName("Name")
                          .SetPolishName("PolishName")
                          .Build();
            var getStateResult = GetResult <State> .Ok(state);

            var duplicatePolishNameError               = new Error(StateErrorCodeEnumeration.PolishNameAlreadyInUse, StateErrorMessage.PolishNameAlreadyInUse);
            var nameIsNotTakenVerificationResult       = VerificationResult.Ok();
            var polishNameIsNotTakenVerificationResult = VerificationResult.Fail(new Collection <IError> {
                duplicatePolishNameError
            });
            var errors = new Collection <IError> {
                duplicatePolishNameError
            };

            _stateGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(getStateResult);
            _stateVerificationServiceMock.Setup(x => x.VerifyNameIsNotTakenAsync(It.IsAny <string>()))
            .ReturnsAsync(nameIsNotTakenVerificationResult);
            _stateVerificationServiceMock.Setup(x => x.VerifyPolishNameIsNotTakenAsync(It.IsAny <string>()))
            .ReturnsAsync(polishNameIsNotTakenVerificationResult);

            Func <Task> result = async() => await _commandHandler.HandleAsync(command);

            var exceptionResult = await result.Should().ThrowExactlyAsync <ConflictException>();

            exceptionResult.And.Errors.Should().BeEquivalentTo(errors);
        }
Ejemplo n.º 4
0
        public async Task HandleAsync_Should_Throw_ResourceNotFoundException_When_State_Does_Not_Exist()
        {
            var command = new UpdateStateCommand(Guid.NewGuid(), Array.Empty <byte>(), "NewName", "NewPolishName");
            var errors  = new Collection <IError>
            {
                new Error(StateErrorCodeEnumeration.NotFound, StateErrorMessage.NotFound)
            };
            var getStateResult = GetResult <State> .Fail(errors);

            _stateGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(getStateResult);

            Func <Task> result = async() => await _commandHandler.HandleAsync(command);

            var exceptionResult = await result.Should().ThrowExactlyAsync <ResourceNotFoundException>();

            exceptionResult.And.Errors.Should().BeEquivalentTo(errors);
        }
Ejemplo n.º 5
0
        public async Task HandleAsync_Should_Throw_PreconditionFailedException_When_RowVersion_Is_Incorrect()
        {
            var command = new UpdateStateCommand(Guid.NewGuid(), new byte[] { 1, 2, 4, 8, 16, 32, 64 }, "NewName",
                                                 "NewPolishName");
            var state = State.Builder()
                        .SetId(Guid.NewGuid())
                        .SetRowVersion(new byte[] { 1, 2, 4, 8, 16, 32, 128 })
                        .SetName("Name")
                        .SetPolishName("PolishName")
                        .Build();
            var getStateResult = GetResult <State> .Ok(state);

            _stateGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(getStateResult);

            Func <Task> result = async() => await _commandHandler.HandleAsync(command);

            await result.Should().ThrowExactlyAsync <PreconditionFailedException>();
        }
Ejemplo n.º 6
0
 public async Task <ActionResult> UpdateState(
     int id, UpdateStateCommand command)
 => await this.Send(command.SetId(id));
 // Run 'CanExecute' for all buttons
 private void RunAllCanExecute()
 {
     NewStateCommand.RaiseCanExecuteChanged();
     UpdateStateCommand.RaiseCanExecuteChanged();
     DeleteStateCommand.RaiseCanExecuteChanged();
 }