public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt() { // given var serviceException = new Exception(); var failedStudentSemesterCourseServiceException = new FailedStudentSemesterCourseServiceException(serviceException); var expectedStudentSemesterCourseServiceException = new StudentSemesterCourseServiceException(failedStudentSemesterCourseServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAllStudentSemesterCourses()) .Throws(serviceException); // when Action retrieveAllStudentSemesterCourseAction = () => this.studentSemesterCourseService.RetrieveAllStudentSemesterCourses(); // then Assert.Throws <StudentSemesterCourseServiceException>( retrieveAllStudentSemesterCourseAction); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentSemesterCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllStudentSemesterCourses(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); StudentSemesterCourse randomStudentSemesterCourse = CreateRandomStudentSemesterCourse(randomDateTime); StudentSemesterCourse someStudentSemesterCourse = randomStudentSemesterCourse; someStudentSemesterCourse.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var serviceException = new Exception(); var failedStudentSemesterCourseServiceException = new FailedStudentSemesterCourseServiceException(serviceException); var expectedStudentSemesterCourseServiceException = new StudentSemesterCourseServiceException( failedStudentSemesterCourseServiceException); this.storageBrokerMock.Setup(broker => broker.SelectStudentSemesterCourseByIdAsync (It.IsAny <Guid>(), It.IsAny <Guid>())) .ThrowsAsync(serviceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <StudentSemesterCourse> modifyStudentSemesterCourseTask = this.studentSemesterCourseService.ModifyStudentSemesterCourseAsync(someStudentSemesterCourse); // then await Assert.ThrowsAsync <StudentSemesterCourseServiceException>(() => modifyStudentSemesterCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentSemesterCourseByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentSemesterCourseServiceException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); StudentSemesterCourse randomStudentSemesterCourse = CreateRandomStudentSemesterCourse(dateTime); StudentSemesterCourse inputStudentSemesterCourse = randomStudentSemesterCourse; inputStudentSemesterCourse.UpdatedBy = inputStudentSemesterCourse.CreatedBy; inputStudentSemesterCourse.UpdatedDate = inputStudentSemesterCourse.CreatedDate; var serviceException = new Exception(); var failedStudentSemesterCourseServiceException = new FailedStudentSemesterCourseServiceException(serviceException); var expectedStudentSemesterCourseServiceException = new StudentSemesterCourseServiceException(failedStudentSemesterCourseServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertStudentSemesterCourseAsync(It.IsAny <StudentSemesterCourse>())) .ThrowsAsync(serviceException); // when ValueTask <StudentSemesterCourse> createStudentSemesterCourseTask = this.studentSemesterCourseService.CreateStudentSemesterCourseAsync(inputStudentSemesterCourse); // then await Assert.ThrowsAsync <StudentSemesterCourseServiceException>(() => createStudentSemesterCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentSemesterCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertStudentSemesterCourseAsync(It.IsAny <StudentSemesterCourse>()), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <StudentSemesterCourse> TryCatch( ReturningStudentSemesterCourseFunction returningStudentSemesterCourseFunction) { try { return(await returningStudentSemesterCourseFunction()); } catch (NullStudentSemesterCourseException nullStudentSemesterCourseException) { throw CreateAndLogValidationException(nullStudentSemesterCourseException); } catch (NotFoundStudentSemesterCourseException notFoundStudentSemesterCourseException) { throw CreateAndLogValidationException(notFoundStudentSemesterCourseException); } catch (InvalidStudentSemesterCourseInputException invalidStudentSemesterCourseInputException) { throw CreateAndLogValidationException(invalidStudentSemesterCourseInputException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsStudentSemesterCourseException = new AlreadyExistsStudentSemesterCourseException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsStudentSemesterCourseException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedSemesterCourseException = new LockedStudentSemesterCourseException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedSemesterCourseException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedStudentSemesterCourseServiceException = new FailedStudentSemesterCourseServiceException(exception); throw CreateAndLogServiceException(failedStudentSemesterCourseServiceException); } }
private IQueryable <StudentSemesterCourse> TryCatch( ReturningStudentSemesterCoursesFunction returningStudentSemesterCoursesFunction) { try { return(returningStudentSemesterCoursesFunction()); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (Exception exception) { var failedStudentSemesterCourseServiceException = new FailedStudentSemesterCourseServiceException(exception); throw CreateAndLogServiceException(failedStudentSemesterCourseServiceException); } }
public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync() { // given Guid someSemesterCourseId = Guid.NewGuid(); Guid someStudentId = Guid.NewGuid(); var serviceException = new Exception(); var failedStudentSemesterCourseServiceException = new FailedStudentSemesterCourseServiceException(serviceException); var expectedStudentSemesterCourseServiceException = new StudentSemesterCourseServiceException( failedStudentSemesterCourseServiceException); this.storageBrokerMock.Setup(broker => broker.SelectStudentSemesterCourseByIdAsync(someStudentId, someSemesterCourseId)) .ThrowsAsync(serviceException); // when ValueTask <StudentSemesterCourse> deleteStudentSemesterCourseTask = this.studentSemesterCourseService.RetrieveStudentSemesterCourseByIdAsync (someStudentId, someSemesterCourseId); // then await Assert.ThrowsAsync <StudentSemesterCourseServiceException>(() => deleteStudentSemesterCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentSemesterCourseServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentSemesterCourseByIdAsync(someStudentId, someSemesterCourseId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }