Example #1
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();
        }
        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();
        }
Example #3
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();
        }