public void ShouldThrowDependencyExceptionOnRetrieveAllStudentExamFeesWhenSqlExceptionOccursAndLogIt()
        {
            // given
            var sqlException = GetSqlException();

            var expectedStudentExamFeeDependencyException =
                new StudentExamFeeDependencyException(sqlException);

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

            // when
            Action retrieveAllStudentExamFeesAction = () =>
                                                      this.studentExamFeeService.RetrieveAllStudentExamFees();

            // then
            Assert.Throws <StudentExamFeeDependencyException>(
                retrieveAllStudentExamFeesAction);

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
예제 #2
0
        private StudentExamFeeDependencyException CreateAndLogDependencyException(Exception exception)
        {
            var studentExamFeeDependencyException = new StudentExamFeeDependencyException(exception);

            this.loggingBroker.LogError(studentExamFeeDependencyException);

            return(studentExamFeeDependencyException);
        }
        private StudentExamFeeDependencyException CreateAndLogCriticalDependencyException(Exception exception)
        {
            var StudentExamFeeDependencyException = new StudentExamFeeDependencyException(exception);

            this.loggingBroker.LogCritical(StudentExamFeeDependencyException);

            return(StudentExamFeeDependencyException);
        }
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            StudentExamFee someStudentExamFee   = CreateRandomStudentExamFee(randomDateTime);

            someStudentExamFee.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedStudentExamFeeException =
                new LockedStudentExamFeeException(databaseUpdateConcurrencyException);

            var expectedStudentExamFeeDependencyException =
                new StudentExamFeeDependencyException(lockedStudentExamFeeException);

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

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

            // when
            ValueTask <StudentExamFee> modifyStudentExamFeeTask =
                this.studentExamFeeService.ModifyStudentExamFeeAsync(someStudentExamFee);

            // then
            await Assert.ThrowsAsync <StudentExamFeeDependencyException>(() =>
                                                                         modifyStudentExamFeeTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnCreateWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime           = GetRandomDateTime();
            StudentExamFee someStudentExamFee = CreateRandomStudentExamFee(dateTime);

            someStudentExamFee.UpdatedBy   = someStudentExamFee.CreatedBy;
            someStudentExamFee.UpdatedDate = someStudentExamFee.CreatedDate;
            var databaseUpdateException = new DbUpdateException();

            var expectedStudentExamFeeDependencyException =
                new StudentExamFeeDependencyException(databaseUpdateException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentExamFeeAsync(It.IsAny <StudentExamFee>()))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <StudentExamFee> createStudentExamFeeTask =
                this.studentExamFeeService.AddStudentExamFeeAsync(someStudentExamFee);

            // then
            await Assert.ThrowsAsync <StudentExamFeeDependencyException>(() =>
                                                                         createStudentExamFeeTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid someStudentId = Guid.NewGuid();
            Guid someExamFeeId = Guid.NewGuid();
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedAttachmentException =
                new LockedStudentExamFeeException(databaseUpdateConcurrencyException);

            var expectedStudentExamFeeException =
                new StudentExamFeeDependencyException(lockedAttachmentException);

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

            // when
            ValueTask <StudentExamFee> removeStudentExamFeeTask =
                this.studentExamFeeService.RemoveStudentExamFeeByIdAsync(someStudentId,
                                                                         someExamFeeId);

            // then
            await Assert.ThrowsAsync <StudentExamFeeDependencyException>(() =>
                                                                         removeStudentExamFeeTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            Guid someStudentId = Guid.NewGuid();
            Guid someExamFeeId = Guid.NewGuid();
            var  sqlException  = GetSqlException();

            var expectedStudentExamFeeDependencyException =
                new StudentExamFeeDependencyException(sqlException);

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

            // when
            ValueTask <StudentExamFee> retrieveStudentExamFeeByIdTask =
                this.studentExamFeeService.RetrieveStudentExamFeeByIdsAsync(
                    someStudentId, someExamFeeId);

            // then
            await Assert.ThrowsAsync <StudentExamFeeDependencyException>(() =>
                                                                         retrieveStudentExamFeeByIdTask.AsTask());

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

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

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

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