Beispiel #1
0
        public async void ShouldThrowValidationExceptionOnCreateWhenAttendanceAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime                = GetRandomDateTime();
            Attendance     randomAttendance        = CreateRandomAttendance(dateTime);
            Attendance     alreadyExistsAttendance = randomAttendance;

            alreadyExistsAttendance.UpdatedBy = alreadyExistsAttendance.CreatedBy;
            string randomMessage         = GetRandomMessage();
            string exceptionMessage      = randomMessage;
            var    duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsAttendanceException =
                new AlreadyExistsAttendanceException(duplicateKeyException);

            var expectedAttendanceValidationException =
                new AttendanceValidationException(alreadyExistsAttendanceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(dateTime);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertAttendanceAsync(alreadyExistsAttendance))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <Attendance> createAttendanceTask =
                this.attendanceService.CreateAttendanceAsync(alreadyExistsAttendance);

            // then
            await Assert.ThrowsAsync <AttendanceValidationException>(() =>
                                                                     createAttendanceTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Beispiel #2
0
        private async ValueTask <Attendance> TryCatch(ReturningAttendanceFunction returningAttendanceFunction)
        {
            try
            {
                return(await returningAttendanceFunction());
            }
            catch (NullAttendanceException nullAttendanceException)
            {
                throw CreateAndLogValidationException(nullAttendanceException);
            }
            catch (InvalidAttendanceException invalidAttendanceException)
            {
                throw CreateAndLogValidationException(invalidAttendanceException);
            }
            catch (NotFoundAttendanceException notFoundAttendanceException)
            {
                throw CreateAndLogValidationException(notFoundAttendanceException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsAttendanceException =
                    new AlreadyExistsAttendanceException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsAttendanceException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedAttendanceException = new LockedAttendanceException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedAttendanceException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedAttendanceServiceException =
                    new FailedAttendanceServiceException(exception);

                throw CreateAndLogServiceException(failedAttendanceServiceException);
            }
        }