public void ShouldThrowDependencyExceptionOnRetrieveAllWhenDbExceptionOccursAndLogIt() { // given var databaseUpdateException = new DbUpdateException(); var expectedSemesterCourseDependencyException = new SemesterCourseDependencyException(databaseUpdateException); this.storageBrokerMock.Setup(broker => broker.SelectAllSemesterCourses()) .Throws(databaseUpdateException); // when . then Assert.Throws <SemesterCourseDependencyException>(() => this.semesterCourseService.RetrieveAllSemesterCourses()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedSemesterCourseDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllSemesterCourses(), 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 randomSemesterCourseId = Guid.NewGuid(); Guid inputSemesterCourseId = randomSemesterCourseId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedSemesterCourseException = new LockedSemesterCourseException(databaseUpdateConcurrencyException); var expectedSemesterCourseException = new SemesterCourseDependencyException(lockedSemesterCourseException); this.storageBrokerMock.Setup(broker => broker.SelectSemesterCourseByIdAsync(inputSemesterCourseId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <SemesterCourse> deleteSemesterCourseTask = this.semesterCourseService.RemoveSemesterCourseByIdAsync(inputSemesterCourseId); // then await Assert.ThrowsAsync <SemesterCourseDependencyException>(() => deleteSemesterCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedSemesterCourseException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectSemesterCourseByIdAsync(inputSemesterCourseId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private SemesterCourseDependencyException CreateAndLogDependencyException(Exception exception) { var semesterCourseDependencyException = new SemesterCourseDependencyException(exception); this.loggingBroker.LogError(semesterCourseDependencyException); return(semesterCourseDependencyException); }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); SemesterCourse randomSemesterCourse = CreateRandomSemesterCourse(randomDateTime); SemesterCourse someSemesterCourse = randomSemesterCourse; someSemesterCourse.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedSemesterCourseException = new LockedSemesterCourseException(databaseUpdateConcurrencyException); var expectedSemesterCourseDependencyException = new SemesterCourseDependencyException(lockedSemesterCourseException); this.storageBrokerMock.Setup(broker => broker.SelectSemesterCourseByIdAsync(someSemesterCourse.Id)) .ThrowsAsync(databaseUpdateConcurrencyException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <SemesterCourse> modifySemesterCourseTask = this.semesterCourseService.ModifySemesterCourseAsync(someSemesterCourse); // then await Assert.ThrowsAsync <SemesterCourseDependencyException>(() => modifySemesterCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectSemesterCourseByIdAsync(someSemesterCourse.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedSemesterCourseDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnCreateWhenDbExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); SemesterCourse randomSemesterCourse = CreateRandomSemesterCourse(dateTime); SemesterCourse inputSemesterCourse = randomSemesterCourse; inputSemesterCourse.UpdatedBy = inputSemesterCourse.CreatedBy; inputSemesterCourse.UpdatedDate = inputSemesterCourse.CreatedDate; var databaseUpdateException = new DbUpdateException(); var expectedSemesterCourseDependencyException = new SemesterCourseDependencyException(databaseUpdateException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertSemesterCourseAsync(inputSemesterCourse)) .ThrowsAsync(databaseUpdateException); // when ValueTask <SemesterCourse> createSemesterCourseTask = this.semesterCourseService.CreateSemesterCourseAsync(inputSemesterCourse); // then await Assert.ThrowsAsync <SemesterCourseDependencyException>(() => createSemesterCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedSemesterCourseDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertSemesterCourseAsync(inputSemesterCourse), 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 randomSemesterCourseId = Guid.NewGuid(); Guid inputSemesterCourseId = randomSemesterCourseId; var sqlException = GetSqlException(); var expectedSemesterCourseDependencyException = new SemesterCourseDependencyException(sqlException); this.storageBrokerMock.Setup(broker => broker.SelectSemesterCourseByIdAsync(inputSemesterCourseId)) .ThrowsAsync(sqlException); // when ValueTask <SemesterCourse> retrieveSemesterCourseByIdTask = this.semesterCourseService.RetrieveSemesterCourseByIdAsync(inputSemesterCourseId); // then await Assert.ThrowsAsync <SemesterCourseDependencyException>(() => retrieveSemesterCourseByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs(expectedSemesterCourseDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectSemesterCourseByIdAsync(inputSemesterCourseId), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt() { // given SqlException sqlException = GetSqlException(); var expectedSemesterCourseDependencyException = new SemesterCourseDependencyException(sqlException); this.storageBrokerMock.Setup(broker => broker.SelectAllSemesterCourses()) .Throws(sqlException); // when Action retrieveAllSemesterCoursesAction = () => this.semesterCourseService.RetrieveAllSemesterCourses(); // then Assert.Throws <SemesterCourseDependencyException>( retrieveAllSemesterCoursesAction); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs( expectedSemesterCourseDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllSemesterCourses(), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }