Beispiel #1
0
        public async Task HandleAsync_Should_Throw_ValidationException_When_Parent_Is_Not_Found()
        {
            var city = City.Builder()
                       .SetId(Guid.NewGuid())
                       .SetRowVersion(Array.Empty <byte>())
                       .SetName("Name")
                       .SetPolishName("PolishName")
                       .SetStateId(Guid.NewGuid())
                       .Build();
            var getCityResult = GetResult <City> .Ok(city);

            var errors = new Collection <IError>
            {
                new Error(CityDistrictErrorCodeEnumeration.ParentNotFound, CityDistrictErrorMessage.ParentNotFound)
            };
            var parentExistsVerificationResult = VerificationResult.Fail(errors);
            var command = new CreateCityDistrictCommand(Guid.NewGuid(), "Name",
                                                        "PolishName", city.Id, Guid.NewGuid(), new Collection <string> {
                "NameVariant"
            });

            _cityGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(getCityResult);
            _cityDistrictVerificationServiceMock.Setup(x => x.VerifyParentExistsAsync(It.IsAny <Guid>()))
            .ReturnsAsync(parentExistsVerificationResult);

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

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

            exceptionResult.And.Errors.Should().BeEquivalentTo(errors);
        }
Beispiel #2
0
        public async Task CreateCityDistrictAsync_Should_Return_CreatedAtRouteResult_With_CityDistrictResponse()
        {
            var createCityDistrictRequest = new CreateCityDistrictRequest
            {
                Name         = "Name",
                PolishName   = "PolishName",
                CityId       = Guid.NewGuid(),
                ParentId     = Guid.NewGuid(),
                NameVariants = new List <string> {
                    "NameVariant"
                }
            };
            var createCityDistrictCommand = new CreateCityDistrictCommand(Guid.NewGuid(),
                                                                          createCityDistrictRequest.Name,
                                                                          createCityDistrictRequest.PolishName,
                                                                          createCityDistrictRequest.CityId,
                                                                          createCityDistrictRequest.ParentId,
                                                                          createCityDistrictRequest.NameVariants);
            var cityDistrictOutputQuery = new CityDistrictOutputQuery(createCityDistrictCommand.CityDistrictId,
                                                                      Array.Empty <byte>(),
                                                                      createCityDistrictCommand.Name,
                                                                      createCityDistrictCommand.PolishName,
                                                                      createCityDistrictCommand.CityId,
                                                                      createCityDistrictCommand.ParentId,
                                                                      createCityDistrictCommand.NameVariants);
            var cityDistrictResponse = new CityDistrictResponse(cityDistrictOutputQuery.Id,
                                                                cityDistrictOutputQuery.RowVersion,
                                                                cityDistrictOutputQuery.Name,
                                                                cityDistrictOutputQuery.PolishName,
                                                                cityDistrictOutputQuery.CityId,
                                                                cityDistrictOutputQuery.ParentId,
                                                                cityDistrictOutputQuery.NameVariants);

            _mapperMock.Setup(x =>
                              x.Map <CreateCityDistrictRequest, CreateCityDistrictCommand>(It.IsAny <CreateCityDistrictRequest>()))
            .Returns(createCityDistrictCommand);
            _communicationBusMock.Setup(x =>
                                        x.SendCommandAsync(It.IsAny <CreateCityDistrictCommand>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);
            _getCityDistrictQueryHandlerMock
            .Setup(x => x.HandleAsync(It.IsAny <GetCityDistrictInputQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(cityDistrictOutputQuery);
            _mapperMock.Setup(x => x.Map <CityDistrictOutputQuery, CityDistrictResponse>(It.IsAny <CityDistrictOutputQuery>())).Returns(cityDistrictResponse);

            var result = await _controller.CreateCityDistrictAsync(createCityDistrictRequest);

            var createdAtRouteResult = result.As <CreatedAtRouteResult>();

            createdAtRouteResult.Value.Should().BeEquivalentTo(cityDistrictResponse);
            createdAtRouteResult.RouteName.Should().BeEquivalentTo("GetCityDistrict");
            createdAtRouteResult.RouteValues.Should().BeEquivalentTo(new RouteValueDictionary(new { id = cityDistrictResponse.Id }));
        }
Beispiel #3
0
        public async Task HandleAsync_Should_Create_CityDistrict()
        {
            var city = City.Builder()
                       .SetId(Guid.NewGuid())
                       .SetRowVersion(Array.Empty <byte>())
                       .SetName("Name")
                       .SetPolishName("PolishName")
                       .SetStateId(Guid.NewGuid())
                       .Build();
            var getCityResult = GetResult <City> .Ok(city);

            var parentExistsVerificationResult         = VerificationResult.Ok();
            var nameIsNotTakenVerificationResult       = VerificationResult.Ok();
            var polishNameIsNotTakenVerificationResult = VerificationResult.Ok();
            var cityDistrict = CityDistrict.Builder()
                               .SetId(Guid.NewGuid())
                               .SetRowVersion(Array.Empty <byte>())
                               .SetName("Name")
                               .SetPolishName("PolishName")
                               .SetCityId(city.StateId)
                               .SetParentId(Guid.NewGuid())
                               .SetNameVariants(new List <string> {
                "NameVariant"
            })
                               .Build();
            var command = new CreateCityDistrictCommand(cityDistrict.Id, cityDistrict.Name,
                                                        cityDistrict.PolishName, cityDistrict.CityId, cityDistrict.ParentId, cityDistrict.NameVariants);

            _cityGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(getCityResult);
            _cityDistrictVerificationServiceMock.Setup(x => x.VerifyParentExistsAsync(It.IsAny <Guid>()))
            .ReturnsAsync(parentExistsVerificationResult);
            _cityDistrictVerificationServiceMock
            .Setup(x => x.VerifyNameIsNotTakenAsync(It.IsAny <string>(), It.IsAny <Guid>()))
            .ReturnsAsync(nameIsNotTakenVerificationResult);
            _cityDistrictVerificationServiceMock
            .Setup(x => x.VerifyPolishNameIsNotTakenAsync(It.IsAny <string>(), It.IsAny <Guid>()))
            .ReturnsAsync(polishNameIsNotTakenVerificationResult);
            _mapperMock.Setup(
                x => x.Map <CreateCityDistrictCommand, CityDistrict>(It.IsAny <CreateCityDistrictCommand>()))
            .Returns(cityDistrict);
            _cityDistrictRepositoryMock.Setup(x => x.AddAsync(It.IsAny <CityDistrict>())).Returns(Task.CompletedTask);

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

            await result.Should().NotThrowAsync <Exception>();
        }
Beispiel #4
0
        public async Task HandleAsync_Should_Throw_ConflictException_When_Name_And_PolishName_Are_Already_Used()
        {
            var city = City.Builder()
                       .SetId(Guid.NewGuid())
                       .SetRowVersion(Array.Empty <byte>())
                       .SetName("Name")
                       .SetPolishName("PolishName")
                       .SetStateId(Guid.NewGuid())
                       .Build();
            var getCityResult = GetResult <City> .Ok(city);

            var parentExistsVerificationResult   = VerificationResult.Ok();
            var nameIsNotTakenError              = new Error(CityDistrictErrorCodeEnumeration.NameAlreadyInUse, CityDistrictErrorMessage.NameAlreadyInUse);
            var nameIsNotTakenVerificationResult = VerificationResult.Fail(new Collection <IError> {
                nameIsNotTakenError
            });
            var polishNameIsNotTakenError = new Error(CityDistrictErrorCodeEnumeration.PolishNameAlreadyInUse, CityDistrictErrorMessage.PolishNameAlreadyInUse);
            var polishNameIsNotTakenVerificationResult = VerificationResult.Fail(new Collection <IError> {
                polishNameIsNotTakenError
            });
            var command = new CreateCityDistrictCommand(Guid.NewGuid(), "Name",
                                                        "PolishName", city.Id, Guid.NewGuid(), new Collection <string> {
                "NameVariant"
            });
            var errors = new Collection <IError> {
                nameIsNotTakenError, polishNameIsNotTakenError
            };

            _cityGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(getCityResult);
            _cityDistrictVerificationServiceMock.Setup(x => x.VerifyParentExistsAsync(It.IsAny <Guid>()))
            .ReturnsAsync(parentExistsVerificationResult);
            _cityDistrictVerificationServiceMock
            .Setup(x => x.VerifyNameIsNotTakenAsync(It.IsAny <string>(), It.IsAny <Guid>()))
            .ReturnsAsync(nameIsNotTakenVerificationResult);
            _cityDistrictVerificationServiceMock
            .Setup(x => x.VerifyPolishNameIsNotTakenAsync(It.IsAny <string>(), It.IsAny <Guid>()))
            .ReturnsAsync(polishNameIsNotTakenVerificationResult);

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

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

            exceptionResult.And.Errors.Should().BeEquivalentTo(errors);
        }
Beispiel #5
0
        public async Task HandleAsync_Should_Throw_ValidationException_When_City_Is_Not_Found()
        {
            var errors = new Collection <IError>
            {
                new Error(CityErrorCodeEnumeration.NotFound, CityErrorMessage.NotFound)
            };
            var getCityResult = GetResult <City> .Fail(errors);

            var command = new CreateCityDistrictCommand(Guid.NewGuid(), "Name",
                                                        "PolishName", Guid.NewGuid(), Guid.NewGuid(), new Collection <string> {
                "NameVariant"
            });

            _cityGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(getCityResult);

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

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

            exceptionResult.And.Errors.Should().BeEquivalentTo(errors);
        }