Example #1
0
        public async Task ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogItAsync()
        {
            // given
            var serviceException = new Exception();

            var failedStudentServiceExcption =
                new FailedStudentServiceException(serviceException);

            var expectedStudentServiceException =
                new StudentServiceException(failedStudentServiceExcption);

            this.apiBrokerMock.Setup(broker =>
                                     broker.GetAllStudentsAsync())
            .ThrowsAsync(serviceException);

            // when
            ValueTask <List <Student> > retrievedStudentTask =
                this.studentService.RetrieveAllStudentsAsync();

            // then
            await Assert.ThrowsAsync <StudentServiceException>(() =>
                                                               retrievedStudentTask.AsTask());

            this.apiBrokerMock.Verify(broker =>
                                      broker.GetAllStudentsAsync(),
                                      Times.Once);

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

            this.apiBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Example #2
0
        public void ShouldThrowServiceExceptionOnRetrieveAllIfExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedStudentServiceException =
                new FailedStudentServiceException(serviceException);

            var expectedStudentServiceException =
                new StudentServiceException(failedStudentServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllStudents())
            .Throws(serviceException);

            // when . then
            Assert.Throws <StudentServiceException>(() =>
                                                    this.studentService.RetrieveAllStudents());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        private async ValueTask <Student> TryCatch(ReturningStudentFunction returningStudentFunction)
        {
            try
            {
                return(await returningStudentFunction());
            }
            catch (NullStudentException nullStudentException)
            {
                throw CreateAndLogValidationException(nullStudentException);
            }
            catch (InvalidStudentException invalidStudentException)
            {
                throw CreateAndLogValidationException(invalidStudentException);
            }
            catch (NotFoundStudentException nullStudentException)
            {
                throw CreateAndLogValidationException(nullStudentException);
            }
            catch (SqlException sqlException)
            {
                var failedStudentStorageException =
                    new FailedStudentStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedStudentStorageException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsStudentException =
                    new AlreadyExistsStudentException(duplicateKeyException);

                throw CreateAndLogDependencyValidationException(alreadyExistsStudentException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedStudentException = new LockedStudentException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedStudentException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                var failedStudentStorageException =
                    new FailedStudentStorageException(dbUpdateException);

                throw CreateAndLogDependencyException(failedStudentStorageException);
            }
            catch (Exception exception)
            {
                var failedStudentServiceException =
                    new FailedStudentServiceException(exception);

                throw CreateAndLogServiceException(failedStudentServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            Student someStudent = CreateRandomStudent();
            int     randomDays  = GetRandomNumber();

            someStudent.UpdatedDate =
                someStudent.CreatedDate.AddDays(randomDays);

            var serviceException = new Exception();

            var failedStudentServiceException =
                new FailedStudentServiceException(serviceException);

            var expectedStudentServiceException =
                new StudentServiceException(failedStudentServiceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Throws(serviceException);

            // when
            ValueTask <Student> modifyStudentTask =
                this.studentService.ModifyStudentAsync(someStudent);

            // then
            await Assert.ThrowsAsync <StudentServiceException>(() =>
                                                               modifyStudentTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private IQueryable <Student> TryCatch(ReturningStudentsFunction returningStudentsFunction)
        {
            try
            {
                return(returningStudentsFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedStudentServiceException =
                    new FailedStudentServiceException(exception);

                throw CreateAndLogServiceException(failedStudentServiceException);
            }
        }
        private async ValueTask <List <Student> > TryCatch(ReturningStudentsFunction returningStudentsFunction)
        {
            try
            {
                return(await returningStudentsFunction());
            }
            catch (HttpRequestException httpRequestException)
            {
                var failedStudentDependencyException =
                    new FailedStudentDependencyException(httpRequestException);

                throw CreateAndLogCriticalDependencyException(failedStudentDependencyException);
            }
            catch (HttpResponseUrlNotFoundException httpResponseUrlNotFoundException)
            {
                var failedStudentDependencyException =
                    new FailedStudentDependencyException(httpResponseUrlNotFoundException);

                throw CreateAndLogCriticalDependencyException(failedStudentDependencyException);
            }
            catch (HttpResponseUnauthorizedException httpResponseUnauthorizedException)
            {
                var failedStudentDependencyException =
                    new FailedStudentDependencyException(httpResponseUnauthorizedException);

                throw CreateAndLogCriticalDependencyException(failedStudentDependencyException);
            }
            catch (HttpResponseException httpResponseException)
            {
                var failedStudentDependencyException =
                    new FailedStudentDependencyException(httpResponseException);

                throw CreateAndLogDependencyException(failedStudentDependencyException);
            }
            catch (Exception serviceException)
            {
                var failedStudentDependencyException =
                    new FailedStudentServiceException(serviceException);

                throw CreateAndLogServiceException(failedStudentDependencyException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnRegisterIfExceptionOccursAndLogItAsync()
        {
            // given
            Student someStudent = CreateRandomStudent();
            var     exception   = new Exception();

            var failedStudentServiceException =
                new FailedStudentServiceException(exception);

            var expectedStudentServiceException =
                new StudentServiceException(failedStudentServiceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Throws(exception);

            // when
            ValueTask <Student> registerStudentTask =
                this.studentService.RegisterStudentAsync(someStudent);

            // then
            await Assert.ThrowsAsync <StudentServiceException>(() =>
                                                               registerStudentTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnRetrieveIfExceptionOccursAndLogItAsync()
        {
            // given
            Guid someStudentId    = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedStudentServiceException =
                new FailedStudentServiceException(serviceException);

            var expectedStudentServiceException =
                new StudentServiceException(failedStudentServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentByIdAsync(It.IsAny <Guid>()))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Student> retrieveStudentByIdTask =
                this.studentService.RetrieveStudentByIdAsync(someStudentId);

            // then
            await Assert.ThrowsAsync <StudentServiceException>(() =>
                                                               retrieveStudentByIdTask.AsTask());

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

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

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