public void ShouldThrowDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt()
        {
            // given
            SqlException sqlException = GetSqlException();

            var expectedStudentGuardianDependencyException =
                new StudentGuardianDependencyException(sqlException);

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

            // when . then
            Assert.Throws <StudentGuardianDependencyException>(() =>
                                                               this.studentGuardianService.RetrieveAllStudentGuardians());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #2
0
        public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentGuardianId = Guid.NewGuid();
            Guid randomStudentId         = Guid.NewGuid();
            Guid inputStudentGuardianId  = randomStudentGuardianId;
            Guid inputStudentId          = randomStudentId;
            var  databaseUpdateException = new DbUpdateException();

            var expectedStudentGuardianDependencyException =
                new StudentGuardianDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentGuardianByIdAsync(inputStudentGuardianId, inputStudentId))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <StudentGuardian> deleteStudentGuardianTask =
                this.studentGuardianService.DeleteStudentGuardianAsync(inputStudentGuardianId, inputStudentId);

            // then
            await Assert.ThrowsAsync <StudentGuardianDependencyException>(() => deleteStudentGuardianTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentGuardianByIdAsync(inputStudentGuardianId, inputStudentId),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #3
0
        public async Task ShouldThrowDependencyExceptionOnDeleteWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            Guid         someStudentGuardianId = Guid.NewGuid();
            Guid         someStudentId         = Guid.NewGuid();
            SqlException sqlException          = GetSqlException();

            var expectedStudentGuardianDependencyException =
                new StudentGuardianDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentGuardianByIdAsync(someStudentGuardianId, someStudentId))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <StudentGuardian> deleteStudentGuardianTask =
                this.studentGuardianService.RemoveStudentGuardianByIdsAsync(someStudentGuardianId, someStudentId);

            // then
            await Assert.ThrowsAsync <StudentGuardianDependencyException>(() =>
                                                                          deleteStudentGuardianTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentGuardianByIdAsync(someStudentGuardianId, someStudentId),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private StudentGuardianDependencyException CreateAndLogCriticalDependencyException(Exception exception)
        {
            var StudentGuardianDependencyException = new StudentGuardianDependencyException(exception);

            this.loggingBroker.LogCritical(StudentGuardianDependencyException);

            return(StudentGuardianDependencyException);
        }
        private StudentGuardianDependencyException CreateAndLogDependencyException(Exception exception)
        {
            var studentGuardianDependencyException = new StudentGuardianDependencyException(exception);

            this.loggingBroker.LogError(studentGuardianDependencyException);

            return(studentGuardianDependencyException);
        }
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int             randomNegativeNumber  = GetNegativeRandomNumber();
            DateTimeOffset  randomDateTime        = GetRandomDateTime();
            StudentGuardian randomStudentGuardian = CreateRandomStudentGuardian(randomDateTime);
            StudentGuardian someStudentGuardian   = randomStudentGuardian;

            someStudentGuardian.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedStudentGuardianException     = new LockedStudentGuardianException(databaseUpdateConcurrencyException);

            var expectedStudentGuardianDependencyException =
                new StudentGuardianDependencyException(lockedStudentGuardianException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentGuardianByIdAsync
                                             (someStudentGuardian.StudentId, someStudentGuardian.GuardianId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

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

            // when
            ValueTask <StudentGuardian> modifyStudentGuardianTask =
                this.studentGuardianService.ModifyStudentGuardianAsync(someStudentGuardian);

            // then
            await Assert.ThrowsAsync <StudentGuardianDependencyException>(() =>
                                                                          modifyStudentGuardianTask.AsTask());

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset  dateTime = GetRandomDateTime();
            StudentGuardian randomStudentGuardian = CreateRandomStudentGuardian(dateTime);
            StudentGuardian inputStudentGuardian  = randomStudentGuardian;

            inputStudentGuardian.UpdatedBy = inputStudentGuardian.CreatedBy;
            var databaseUpdateException = new DbUpdateException();

            var expectedStudentGuardianDependencyException =
                new StudentGuardianDependencyException(databaseUpdateException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentGuardianAsync(inputStudentGuardian))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <StudentGuardian> addStudentGuardianTask =
                this.studentGuardianService.AddStudentGuardianAsync(inputStudentGuardian);

            // then
            await Assert.ThrowsAsync <StudentGuardianDependencyException>(() =>
                                                                          addStudentGuardianTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #8
0
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentId  = Guid.NewGuid();
            Guid randomGuardianId = Guid.NewGuid();
            Guid inputGuardianId  = randomGuardianId;
            Guid inputStudentId   = randomStudentId;
            var  sqlException     = GetSqlException();

            var expectedStudentGuardianDependencyException =
                new StudentGuardianDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentGuardianByIdAsync(inputStudentId, inputGuardianId))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <StudentGuardian> retrieveStudentGuardianByIdTask =
                this.studentGuardianService.RetrieveStudentGuardianByIdAsync(inputStudentId, inputGuardianId);

            // then
            await Assert.ThrowsAsync <StudentGuardianDependencyException>(() =>
                                                                          retrieveStudentGuardianByIdTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentGuardianByIdAsync(inputStudentId, inputGuardianId),
                                          Times.Once);

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

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