Esempio n. 1
0
        public async void ShouldThrowValidationExceptionOnCreateWhenCourseIsNullAndLogItAsync()
        {
            // given
            Course randomCourse = null;
            Course nullCourse   = randomCourse;

            var nullCourseException = new NullCourseException();

            var expectedCourseValidationException =
                new CourseValidationException(nullCourseException);

            // when
            ValueTask <Course> createCourseTask =
                this.courseService.CreateCourseAsync(nullCourse);

            // then
            await Assert.ThrowsAsync <CourseValidationException>(() =>
                                                                 createCourseTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedCourseValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseByIdAsync(It.IsAny <Guid>()),
                                          Times.Never);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidationExceptionOnModifyWhenCourseNameIsInvalidAndLogItAsync(
            string invalidCourseName)
        {
            // given
            Course randomCourse  = CreateRandomCourse(DateTime.Now);
            Course invalidCourse = randomCourse;

            invalidCourse.Name = invalidCourseName;

            var invalidCourseException = new InvalidCourseInputException(
                parameterName: nameof(Course.Name),
                parameterValue: invalidCourse.Name);

            var expectedCourseValidationException =
                new CourseValidationException(invalidCourseException);

            // when
            ValueTask <Course> modifyCourseTask =
                this.courseService.ModifyCourseAsync(invalidCourse);

            // then
            await Assert.ThrowsAsync <CourseValidationException>(() =>
                                                                 modifyCourseTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidationExceptionOnModifyWhenCourseIsNullAndLogItAsync()
        {
            //given
            Course invalidCourse       = null;
            var    nullCourseException = new NullCourseException();

            var expectedCourseValidationException =
                new CourseValidationException(nullCourseException);

            //when
            ValueTask <Course> modifyCourseTask =
                this.courseService.ModifyCourseAsync(invalidCourse);

            //then
            await Assert.ThrowsAsync <CourseValidationException>(() =>
                                                                 modifyCourseTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsSameAsCreatedDateAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime     = GetRandomDateTime();
            Course         randomCourse = CreateRandomCourse(dateTime);
            Course         inputCourse  = randomCourse;

            var invalidCourseInputException = new InvalidCourseInputException(
                parameterName: nameof(Course.UpdatedDate),
                parameterValue: inputCourse.UpdatedDate);

            var expectedCourseValidationException =
                new CourseValidationException(invalidCourseInputException);

            // when
            ValueTask <Course> modifyCourseTask =
                this.courseService.ModifyCourseAsync(inputCourse);

            // then
            await Assert.ThrowsAsync <CourseValidationException>(() =>
                                                                 modifyCourseTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseByIdAsync(It.IsAny <Guid>()),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 5
0
        public async void ShouldThrowValidationExceptionOnRetrieveByIdWhenStorageCourseIsNullAndLogItAsync()
        {
            //given
            Guid   randomCourseId          = Guid.NewGuid();
            Guid   someCourseId            = randomCourseId;
            Course invalidStorageCourse    = null;
            var    notFoundCourseException = new NotFoundCourseException(someCourseId);

            var expectedCourseValidationException =
                new CourseValidationException(notFoundCourseException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCourseByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(invalidStorageCourse);

            //when
            ValueTask <Course> retrieveCourseByIdTask =
                this.courseService.RetrieveCourseByIdAsync(someCourseId);

            //then
            await Assert.ThrowsAsync <CourseValidationException>(() =>
                                                                 retrieveCourseByIdTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseByIdAsync(It.IsAny <Guid>()),
                                          Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 6
0
        public async void ShouldThrowValidationExceptionOnRetrieveByIdWhenIdIsInvalidAndLogItAsync()
        {
            //given
            Guid randomCourseId = default;
            Guid inputCourseId  = randomCourseId;

            var invalidCourseException = new InvalidCourseException(
                parameterName: nameof(Course.Id),
                parameterValue: inputCourseId);

            var expectedCourseValidationException =
                new CourseValidationException(invalidCourseException);

            //when
            ValueTask <Course> retrieveCourseByIdTask =
                this.courseService.RetrieveCourseByIdAsync(inputCourseId);

            //then
            await Assert.ThrowsAsync <CourseValidationException>(() => retrieveCourseByIdTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseByIdAsync(It.IsAny <Guid>()),
                                          Times.Never);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            Guid           differentId           = Guid.NewGuid();
            Guid           invalidCreatedBy      = differentId;
            DateTimeOffset randomDate            = GetRandomDateTime();
            Course         randomCourse          = CreateRandomCourse(randomDate);
            Course         invalidCourse         = randomCourse;

            invalidCourse.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes);
            Course storageCourse = randomCourse.DeepClone();
            Guid   courseId      = invalidCourse.Id;

            invalidCourse.CreatedBy = invalidCreatedBy;

            var invalidCourseException = new InvalidCourseException(
                parameterName: nameof(Course.CreatedBy),
                parameterValue: invalidCourse.CreatedBy);

            var expectedCourseValidationException =
                new CourseValidationException(invalidCourseException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(randomDate);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCourseByIdAsync(courseId))
            .ReturnsAsync(storageCourse);

            // when
            ValueTask <Course> modifyCourseTask =
                this.courseService.ModifyCourseAsync(invalidCourse);

            // then
            await Assert.ThrowsAsync <CourseValidationException>(() =>
                                                                 modifyCourseTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseByIdAsync(invalidCourse.Id),
                                          Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedCourseValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.UpdateCourseAsync(It.IsAny <Course>()),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 8
0
        private CourseValidationException CreateAndLogValidationException(Exception exception)
        {
            var courseValidationException = new CourseValidationException(exception);

            this.loggingBroker.LogError(courseValidationException);

            return(courseValidationException);
        }
        public async Task ShouldThrowValidationExceptionOnModifyIfCourseDoesntExistAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            DateTimeOffset dateTime          = GetRandomDateTime();
            Course         randomCourse      = CreateRandomCourse(dateTime);
            Course         nonExistentCourse = randomCourse;

            nonExistentCourse.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes);
            Course noCourse = null;
            var    notFoundCourseException = new NotFoundCourseException(nonExistentCourse.Id);

            var expectedCourseValidationException =
                new CourseValidationException(notFoundCourseException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(dateTime);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCourseByIdAsync(nonExistentCourse.Id))
            .ReturnsAsync(noCourse);

            // when
            ValueTask <Course> modifyCourseTask =
                this.courseService.ModifyCourseAsync(nonExistentCourse);

            // then
            await Assert.ThrowsAsync <CourseValidationException>(() =>
                                                                 modifyCourseTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseByIdAsync(nonExistentCourse.Id),
                                          Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedCourseValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.UpdateCourseAsync(It.IsAny <Course>()),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 10
0
        public async void ShouldThrowValidationExceptionOnCreateWhenCreatedDateIsNotRecentAndLogItAsync(
            int minutes)
        {
            // given
            DateTimeOffset dateTime     = GetRandomDateTime();
            Course         randomCourse = CreateRandomCourse(dateTime);
            Course         inputCourse  = randomCourse;

            inputCourse.UpdatedBy   = inputCourse.CreatedBy;
            inputCourse.CreatedDate = dateTime.AddMinutes(minutes);
            inputCourse.UpdatedDate = inputCourse.CreatedDate;

            var invalidCourseException = new InvalidCourseException();

            invalidCourseException.AddData(
                key: nameof(Course.CreatedDate),
                values: $"Date is not recent");

            var expectedCourseValidationException =
                new CourseValidationException(invalidCourseException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(dateTime);

            // when
            ValueTask <Course> createCourseTask =
                this.courseService.CreateCourseAsync(inputCourse);

            // then
            await Assert.ThrowsAsync <CourseValidationException>(() =>
                                                                 createCourseTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedCourseValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseByIdAsync(It.IsAny <Guid>()),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnCreateWhenCourseAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime            = GetRandomDateTime();
            Course         randomCourse        = CreateRandomCourse(dateTime);
            Course         alreadyExistsCourse = randomCourse;

            alreadyExistsCourse.UpdatedBy = alreadyExistsCourse.CreatedBy;
            string randomMessage         = GetRandomMessage();
            string exceptionMessage      = randomMessage;
            var    duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsCourseException =
                new AlreadyExistsCourseException(duplicateKeyException);

            var expectedCourseValidationException =
                new CourseValidationException(alreadyExistsCourseException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(dateTime);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCourseAsync(alreadyExistsCourse))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <Course> createCourseTask =
                this.courseService.CreateCourseAsync(alreadyExistsCourse);

            // then
            await Assert.ThrowsAsync <CourseValidationException>(() =>
                                                                 createCourseTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertCourseAsync(alreadyExistsCourse),
                                          Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 12
0
        public async void ShouldThrowValidationExceptionOnCreateIfCourseStatusIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset randomDateTime = GetRandomDateTime();
            Course         randomCourse   = CreateRandomCourse(randomDateTime);
            Course         invalidCourse  = randomCourse;

            invalidCourse.Status = GetInvalidEnum <CourseStatus>();
            var invalidCourseException = new InvalidCourseException();

            invalidCourseException.AddData(
                key: nameof(Course.Status),
                values: "Value is not recognized");

            var expectedCourseValidationException = new CourseValidationException(invalidCourseException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime()).
            Returns(randomDateTime);

            // when
            ValueTask <Course> createCourseTask =
                this.courseService.CreateCourseAsync(invalidCourse);

            // then
            await Assert.ThrowsAsync <CourseValidationException>(() =>
                                                                 createCourseTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertCourseAsync(It.IsAny <Course>()),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 13
0
        public async Task ShouldThrowValidatonExceptionOnDeleteWhenStorageCourseIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime          = GetRandomDateTime();
            Course         randomCourse      = CreateRandomCourse(dateTime);
            Guid           inputCourseId     = randomCourse.Id;
            Course         inputCourse       = randomCourse;
            Course         nullStorageCourse = null;

            var notFoundCourseException = new NotFoundCourseException(inputCourseId);

            var expectedCourseValidationException =
                new CourseValidationException(notFoundCourseException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCourseByIdAsync(inputCourseId))
            .ReturnsAsync(nullStorageCourse);

            // when
            ValueTask <Course> actualCourseTask =
                this.courseService.RemoveCourseAsync(inputCourseId);

            // then
            await Assert.ThrowsAsync <CourseValidationException>(() => actualCourseTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseByIdAsync(inputCourseId),
                                          Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedCourseValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.DeleteCourseAsync(It.IsAny <Course>()),
                                          Times.Never);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 14
0
        public async void ShouldThrowValidationExceptionOnCreateWhenCourseIsInvalidAndLogItAsync(
            string invalidText)
        {
            // given
            var invalidCourse = new Course
            {
                Name = invalidText
            };

            var invalidCourseException = new InvalidCourseException();

            invalidCourseException.AddData(
                key: nameof(Course.Id),
                values: "Id is required");

            invalidCourseException.AddData(
                key: nameof(Course.Name),
                values: "Text is required");

            invalidCourseException.AddData(
                key: nameof(Course.Description),
                values: "Text is required");

            invalidCourseException.AddData(
                key: nameof(Course.CreatedBy),
                values: "Id is required");

            invalidCourseException.AddData(
                key: nameof(Course.UpdatedBy),
                values: "Id is required");

            invalidCourseException.AddData(
                key: nameof(Course.CreatedDate),
                values: "Date is required");

            invalidCourseException.AddData(
                key: nameof(Course.UpdatedDate),
                values: "Date is required");

            var expectedCourseValidationException =
                new CourseValidationException(invalidCourseException);

            // when
            ValueTask <Course> createCourseTask =
                this.courseService.CreateCourseAsync(invalidCourse);

            // then
            await Assert.ThrowsAsync <CourseValidationException>(() =>
                                                                 createCourseTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameValidationExceptionAs(
                                                                    expectedCourseValidationException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseByIdAsync(It.IsAny <Guid>()),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }