예제 #1
0
        public void ExceptionIsThrownWithCorrectDetaisl()
        {
            int createdNoteId = 0;
            UnProcessableEntityException couldNotCreateNoteException = null;

            try
            {
                var userId  = _fixture.LandmarkContext.Users.First().Id;
                var command = new CreateNoteCommand
                {
                    UserId    = userId,
                    Text      = "a sample note",
                    Latitude  = 150,
                    Longitude = 50
                };
                createdNoteId = new CreateNoteCommandHandler(_fixture.LandmarkContext, new FakeDatetimeProvider()).Handle(command, CancellationToken.None).GetAwaiter().GetResult();
            }
            catch (UnProcessableEntityException ex)
            {
                couldNotCreateNoteException = ex;
            }

            couldNotCreateNoteException.ShouldNotBeNull();
            couldNotCreateNoteException.ModelStateErrors.Count().ShouldBe(1);
            var modelStateError = couldNotCreateNoteException.ModelStateErrors.First();

            modelStateError.PropertyName.ShouldBe(nameof(CreateNoteCommand.Latitude));
        }
예제 #2
0
        public void ANoteIsCreated()
        {
            int createdNoteId = 0;
            UnProcessableEntityException couldNotCreateNoteException = null;

            try
            {
                var userId  = _fixture.LandmarkContext.Users.First().Id;
                var command = new CreateNoteCommand
                {
                    UserId    = userId,
                    Text      = "a sample note",
                    Latitude  = 50,
                    Longitude = 50
                };
                createdNoteId = new CreateNoteCommandHandler(_fixture.LandmarkContext, new FakeDatetimeProvider()).Handle(command, CancellationToken.None).GetAwaiter().GetResult();
            }
            catch (UnProcessableEntityException ex)
            {
                couldNotCreateNoteException = ex;
            }


            couldNotCreateNoteException.ShouldBeNull();
            createdNoteId.ShouldBeGreaterThan(0);
        }
예제 #3
0
        public void UserIsCreatedWithCorrectInformation()
        {
            UserDetailModel createdUser = null;
            UnProcessableEntityException couldNotCreateUserException  = null;
            EntityNotFoundException      createdUserNotFoundException = null;

            var command = new CreateUserCommand
            {
                FirstName = "Marco",
                LastName  = "Polo",
                Password  = "******",
                Username  = "******"
            };

            try
            {
                var createdUserId = new CreateUserCommandHandler(_fixture.LandmarkContext, new FakeDatetimeProvider()).Handle(command, CancellationToken.None).GetAwaiter().GetResult();
                createdUser = new GetUserByIdQueryHandler(_fixture.LandmarkContext).Handle(new GetUserByIdQuery {
                    Id = createdUserId
                }, CancellationToken.None).GetAwaiter()
                              .GetResult();
            }
            catch (UnProcessableEntityException ex)
            {
                couldNotCreateUserException = ex;
            }
            catch (EntityNotFoundException ex)
            {
                createdUserNotFoundException = ex;
            }

            couldNotCreateUserException.ShouldBeNull();
            createdUserNotFoundException.ShouldBeNull();
            createdUser.ShouldSatisfyAllConditions(
                () => createdUser.FirstName.ShouldBe("Marco"),
                () => createdUser.LastName.ShouldBe("Polo"),
                () => createdUser.UserName.ShouldBe("marco.polo")
                );
        }
        public static void ValidateAndThrowUnProcessableEntityException <T>(this AbstractValidator <T> validator, T model)
        {
            var validationResult = validator.Validate(model);

            if (validationResult.IsValid)
            {
                return;
            }

            var modelStateException = new UnProcessableEntityException("Please see Errors and Property Errors for more information");
            var propertyErrors      = new List <ModelStateError>();

            foreach (var validationError in validationResult.Errors)
            {
                propertyErrors.Add(new ModelStateError
                {
                    PropertyName = validationError.PropertyName,
                    Error        = validationError.ErrorMessage
                });
            }
            modelStateException.ModelStateErrors = propertyErrors;

            throw modelStateException;
        }
예제 #5
0
        public void UserIsNotCreatedAndExceptionWithDetailsIsThrown()
        {
            UserDetailModel createdUser;
            UnProcessableEntityException couldNotCreateUserException  = null;
            EntityNotFoundException      createdUserNotFoundException = null;

            try
            {
                var command = new CreateUserCommand
                {
                    LastName = "Polo",
                    Password = "******",
                    Username = "******"
                };
                var createdUserId = new CreateUserCommandHandler(_fixture.LandmarkContext, new FakeDatetimeProvider()).Handle(command, CancellationToken.None).GetAwaiter().GetResult();
                createdUser = new GetUserByIdQueryHandler(_fixture.LandmarkContext).Handle(new GetUserByIdQuery {
                    Id = createdUserId
                }, CancellationToken.None).GetAwaiter()
                              .GetResult();
            }
            catch (UnProcessableEntityException ex)
            {
                couldNotCreateUserException = ex;
            }
            catch (EntityNotFoundException ex)
            {
                createdUserNotFoundException = ex;
            }

            couldNotCreateUserException.ShouldNotBeNull();
            couldNotCreateUserException.ModelStateErrors.Count().ShouldBe(1);
            var firstError = couldNotCreateUserException.ModelStateErrors.First();

            firstError.ShouldSatisfyAllConditions(
                () => { firstError.PropertyName.ShouldBe(nameof(CreateUserCommand.FirstName)); });
        }