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

            var expectedStudentRegistrationDependencyException =
                new StudentRegistrationDependencyException(sqlException);

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

            // when . // then
            Assert.Throws <StudentRegistrationDependencyException>(() =>
                                                                   this.studentRegistrationService.RetrieveAllStudentRegistrations());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #2
0
        public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            StudentRegistration someStudentRegistration = CreateRandomStudentRegistration();
            var databaseUpdateException = new DbUpdateException();

            var expectedStudentRegistrationDependencyException =
                new StudentRegistrationDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentRegistrationAsync(It.IsAny <StudentRegistration>()))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <StudentRegistration> addStudentRegistrationTask =
                this.studentRegistrationService.AddStudentRegistrationAsync(someStudentRegistration);

            // then
            await Assert.ThrowsAsync <StudentRegistrationDependencyException>(() =>
                                                                              addStudentRegistrationTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemple #3
0
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            Guid someStudentId           = Guid.NewGuid();
            Guid someRegistrationId      = Guid.NewGuid();
            var  databaseUpdateException = new DbUpdateException();

            var expectedStudentRegistrationDependencyException =
                new StudentRegistrationDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentRegistrationByIdAsync(someStudentId, someRegistrationId))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <StudentRegistration> retrieveStudentRegistrationByIdTask =
                this.studentRegistrationService.RetrieveStudentRegistrationByIdAsync(someStudentId, someRegistrationId);

            // then
            await Assert.ThrowsAsync <StudentRegistrationDependencyException>(() =>
                                                                              retrieveStudentRegistrationByIdTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentRegistrationByIdAsync(someStudentId, someRegistrationId),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private StudentRegistrationDependencyException CreateAndLogDependencyException(Exception exception)
        {
            var studentRegistrationDependencyException = new StudentRegistrationDependencyException(exception);

            this.loggingBroker.LogError(studentRegistrationDependencyException);

            return(studentRegistrationDependencyException);
        }
Exemple #5
0
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentId      = Guid.NewGuid();
            Guid inputStudentId       = randomStudentId;
            Guid randomRegistrationId = Guid.NewGuid();
            Guid inputRegistrationId  = randomRegistrationId;
            StudentRegistration someStudentRegistration = CreateRandomStudentRegistration();

            var databaseUpdateConcurrencyException =
                new DbUpdateConcurrencyException();

            var lockedStudentRegistrationException =
                new LockedStudentRegistrationException(databaseUpdateConcurrencyException);

            var expectedStudentRegistrationDependencyException =
                new StudentRegistrationDependencyException(lockedStudentRegistrationException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentRegistrationByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .ReturnsAsync(someStudentRegistration);

            this.storageBrokerMock.Setup(broker =>
                                         broker.DeleteStudentRegistrationAsync(It.IsAny <StudentRegistration>()))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <StudentRegistration> deleteStudentRegistrationTask =
                this.studentRegistrationService.RemoveStudentRegistrationByIdsAsync(
                    inputStudentId,
                    inputRegistrationId);

            // then
            await Assert.ThrowsAsync <StudentRegistrationDependencyException>(() =>
                                                                              deleteStudentRegistrationTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentId      = Guid.NewGuid();
            Guid inputStudentId       = randomStudentId;
            Guid randomRegistrationId = Guid.NewGuid();
            Guid inputRegistrationId  = randomRegistrationId;
            var  sqlException         = GetSqlException();

            var expectedStudentRegistrationDependencyException =
                new StudentRegistrationDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentRegistrationByIdAsync(inputStudentId, inputRegistrationId))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <StudentRegistration> retrieveStudentRegistrationByIdTask =
                this.studentRegistrationService.RetrieveStudentRegistrationByIdAsync(inputStudentId, inputRegistrationId);

            // then
            await Assert.ThrowsAsync <StudentRegistrationDependencyException>(() =>
                                                                              retrieveStudentRegistrationByIdTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentRegistrationByIdAsync(inputStudentId, inputRegistrationId),
                                          Times.Once);

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

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