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

            var expectedStudentExamDependencyException =
                new StudentExamDependencyException(sqlException);

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

            // when . then
            Assert.Throws <StudentExamDependencyException>(() =>
                                                           this.studentExamService.RetrieveAllStudentExams());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentExamId = Guid.NewGuid();
            Guid inputStudentExamId  = randomStudentExamId;
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedStudentExamException = new LockedStudentExamException(databaseUpdateConcurrencyException);

            var expectedStudentExamException = new StudentExamDependencyException(lockedStudentExamException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamByIdAsync(inputStudentExamId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <StudentExam> deleteStudentExamTask =
                this.studentExamService.DeleteStudentExamByIdAsync(inputStudentExamId);

            // then
            await Assert.ThrowsAsync <StudentExamDependencyException>(() => deleteStudentExamTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #3
0
        private StudentExamDependencyException CreateAndLogDependencyException(Exception exception)
        {
            var StudentExamDependencyException = new StudentExamDependencyException(exception);

            this.loggingBroker.LogError(StudentExamDependencyException);

            return(StudentExamDependencyException);
        }
        private StudentExamDependencyException CreateAndLogCriticalDependencyException(Exception exception)
        {
            var studentExamDependencyException = new StudentExamDependencyException(exception);

            this.loggingBroker.LogCritical(studentExamDependencyException);

            return(studentExamDependencyException);
        }
Example #5
0
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            StudentExam    randomStudentExam    = CreateRandomStudentExam(randomDateTime);
            StudentExam    someStudentExam      = randomStudentExam;

            someStudentExam.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedStudentExamException         = new LockedStudentExamException(databaseUpdateConcurrencyException);

            var expectedStudentExamDependencyException =
                new StudentExamDependencyException(lockedStudentExamException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamByIdAsync
                                             (someStudentExam.Id))
            .ThrowsAsync(databaseUpdateConcurrencyException);

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

            // when
            ValueTask <StudentExam> modifyStudentExamTask =
                this.studentExamService.ModifyStudentExamAsync(someStudentExam);

            // then
            await Assert.ThrowsAsync <StudentExamDependencyException>(() =>
                                                                      modifyStudentExamTask.AsTask());

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime          = GetRandomDateTime();
            StudentExam    randomStudentExam = CreateRandomStudentExam(dateTime);
            StudentExam    inputStudentExam  = randomStudentExam;

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

            var expectedStudentGuardianDependencyException =
                new StudentExamDependencyException(databaseUpdateException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentExamAsync(inputStudentExam))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <StudentExam> addStudentGuardianTask =
                this.studentExamService.AddStudentExamAsync(inputStudentExam);

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

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

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

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

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

            var expectedStudentExamDependencyException =
                new StudentExamDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamByIdAsync(inputStudentExamId))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <StudentExam> retrieveStudentExamByIdTask =
                this.studentExamService.RetrieveStudentExamByIdAsync(inputStudentExamId);

            // then
            await Assert.ThrowsAsync <StudentExamDependencyException>(() =>
                                                                      retrieveStudentExamByIdTask.AsTask());

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

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

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

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