public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomAttendanceId = Guid.NewGuid(); Guid inputAttendanceId = randomAttendanceId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedAttendanceException = new LockedAttendanceException(databaseUpdateConcurrencyException); var expectedAttendanceDependencyException = new AttendanceDependencyException(lockedAttendanceException); this.storageBrokerMock.Setup(broker => broker.SelectAttendanceByIdAsync(inputAttendanceId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <Attendance> deleteAttendanceTask = this.attendanceService.RemoveAttendanceByIdAsync(inputAttendanceId); // then await Assert.ThrowsAsync <AttendanceDependencyException>(() => deleteAttendanceTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedAttendanceDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttendanceByIdAsync(inputAttendanceId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt() { // given var sqlException = GetSqlException(); var expectedAttendanceDependencyException = new AttendanceDependencyException(sqlException); this.storageBrokerMock.Setup(broker => broker.SelectAllAttendances()) .Throws(sqlException); // when . then Assert.Throws <AttendanceDependencyException>(() => this.attendanceService.RetrieveAllAttendances()); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs(expectedAttendanceDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllAttendances(), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private AttendanceDependencyException CreateAndLogCriticalDependencyException(Exception exception) { var attendanceDependencyException = new AttendanceDependencyException(exception); this.loggingBroker.LogCritical(attendanceDependencyException); return(attendanceDependencyException); }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Attendance randomAttendance = CreateRandomAttendance(randomDateTime); Attendance someAttendance = randomAttendance; someAttendance.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedAttendanceException = new LockedAttendanceException(databaseUpdateConcurrencyException); var expectedAttendanceDependencyException = new AttendanceDependencyException(lockedAttendanceException); this.storageBrokerMock.Setup(broker => broker.SelectAttendanceByIdAsync(someAttendance.Id)) .ThrowsAsync(databaseUpdateConcurrencyException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <Attendance> modifyAttendanceTask = this.attendanceService.ModifyAttendanceAsync(someAttendance); // then await Assert.ThrowsAsync <AttendanceDependencyException>(() => modifyAttendanceTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttendanceByIdAsync(someAttendance.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAttendanceDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnCreateWhenDbExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Attendance randomAttendance = CreateRandomAttendance(dateTime); Attendance inputAttendance = randomAttendance; inputAttendance.UpdatedBy = inputAttendance.CreatedBy; var databaseUpdateException = new DbUpdateException(); var expectedAttendanceDependencyException = new AttendanceDependencyException(databaseUpdateException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertAttendanceAsync(inputAttendance)) .ThrowsAsync(databaseUpdateException); // when ValueTask <Attendance> createAttendanceTask = this.attendanceService.CreateAttendanceAsync(inputAttendance); // then await Assert.ThrowsAsync <AttendanceDependencyException>(() => createAttendanceTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAttendanceDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertAttendanceAsync(inputAttendance), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.AtLeastOnce); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnRetrieveWhenSqlExceptionOccursAndLogItAsync() { // given Guid randomAttendanceId = Guid.NewGuid(); Guid inputAttendanceId = randomAttendanceId; var sqlException = GetSqlException(); var expectedDependencyException = new AttendanceDependencyException(sqlException); this.storageBrokerMock.Setup(broker => broker.SelectAttendanceByIdAsync(inputAttendanceId)) .ThrowsAsync(sqlException); // when ValueTask <Attendance> retrieveAttendanceTask = this.attendanceService.RetrieveAttendanceByIdAsync(inputAttendanceId); // then await Assert.ThrowsAsync <AttendanceDependencyException>(() => retrieveAttendanceTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs(expectedDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttendanceByIdAsync(inputAttendanceId), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }