Пример #1
0
        public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            CourseAttachment someCourseAttachment = CreateRandomCourseAttachment();
            var databaseUpdateException           = new DbUpdateException();

            var expectedCourseAttachmentDependencyException =
                new CourseAttachmentDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCourseAttachmentAsync(It.IsAny <CourseAttachment>()))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <CourseAttachment> addCourseAttachmentTask =
                this.courseAttachmentService.AddCourseAttachmentAsync(someCourseAttachment);

            // then
            await Assert.ThrowsAsync <CourseAttachmentDependencyException>(() =>
                                                                           addCourseAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Пример #2
0
        public void ShouldThrowDependencyExceptionOnRetrieveAllCourseAttachmentsWhenSqlExceptionOccursAndLogIt()
        {
            // given
            var sqlException = GetSqlException();

            var expectedCourseAttachmentDependencyException =
                new CourseAttachmentDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllCourseAttachments())
            .Throws(sqlException);

            // when . then
            Assert.Throws <CourseAttachmentDependencyException>(() =>
                                                                this.courseAttachmentService.RetrieveAllCourseAttachments());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        private CourseAttachmentDependencyException CreateAndLogDependencyException(Exception exception)
        {
            var courseAttachmentDependencyException = new CourseAttachmentDependencyException(exception);

            this.loggingBroker.LogError(courseAttachmentDependencyException);

            return(courseAttachmentDependencyException);
        }
Пример #4
0
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid someAttachmentId = Guid.NewGuid();
            Guid someCourseId     = Guid.NewGuid();
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedAttachmentException =
                new LockedCourseAttachmentException(databaseUpdateConcurrencyException);

            var expectedCourseAttachmentException =
                new CourseAttachmentDependencyException(lockedAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCourseAttachmentByIdAsync(someCourseId, someAttachmentId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <CourseAttachment> removeCourseAttachmentTask =
                this.courseAttachmentService.RemoveCourseAttachmentByIdAsync(someCourseId, someAttachmentId);

            // then
            await Assert.ThrowsAsync <CourseAttachmentDependencyException>(() =>
                                                                           removeCourseAttachmentTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCourseAttachmentByIdAsync(someCourseId, someAttachmentId),
                                          Times.Once);

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            Guid         someAttachmentId = Guid.NewGuid();
            Guid         someCourseId     = Guid.NewGuid();
            SqlException sqlException     = GetSqlException();

            var expectedCourseAttachmentDependencyException =
                new CourseAttachmentDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCourseAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <CourseAttachment> retrieveCourseAttachmentTask =
                this.courseAttachmentService.RetrieveCourseAttachmentByIdAsync(
                    someCourseId,
                    someAttachmentId);

            // then
            await Assert.ThrowsAsync <CourseAttachmentDependencyException>(() =>
                                                                           retrieveCourseAttachmentTask.AsTask());

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

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

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