コード例 #1
0
        public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime           = GetRandomDateTime();
            Registration   randomRegistration = CreateRandomRegistration(dateTime);
            Registration   inputRegistration  = randomRegistration;

            inputRegistration.UpdatedBy = inputRegistration.CreatedBy;
            var databaseUpdateException = new DbUpdateException();

            var expectedRegistrationDependencyException =
                new RegistrationDependencyException(databaseUpdateException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertRegistrationAsync(inputRegistration))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <Registration> createRegistrationTask =
                this.registrationService.AddRegistrationAsync(inputRegistration);

            // then
            await Assert.ThrowsAsync <RegistrationDependencyException>(() =>
                                                                       createRegistrationTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
コード例 #2
0
        public async Task ShouldThrowDependencyExceptionOnModifyIfSqlExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset randomDateTime   = GetRandomDateTime();
            Registration   someRegistration = CreateRandomRegistration(dateTime: randomDateTime);
            SqlException   sqlException     = GetSqlException();
            var            expectedRegistrationDependencyException = new RegistrationDependencyException(sqlException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectRegistrationByIdAsync(It.IsAny <Guid>()))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <Registration> modifyRegistrationTask =
                this.registrationService.ModifyRegistrationAsync(someRegistration);

            // then
            await Assert.ThrowsAsync <RegistrationDependencyException>(() =>
                                                                       modifyRegistrationTask.AsTask());

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRemoveIfDbUpdateExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset randomDateTime      = GetRandomDateTime();
            Registration   someRegistration    = CreateRandomRegistration(dateTime: randomDateTime);
            Guid           someRegistrationId  = someRegistration.Id;
            Registration   storageRegistration = someRegistration;
            var            dbUpdateException   = new DbUpdateException();

            var expectedRegistrationDependencyException =
                new RegistrationDependencyException(dbUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectRegistrationByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(storageRegistration);

            this.storageBrokerMock.Setup(broker =>
                                         broker.DeleteRegistrationAsync(It.IsAny <Registration>()))
            .ThrowsAsync(dbUpdateException);

            // when
            ValueTask <Registration> deleteRegistrationTask =
                this.registrationService.RemoveRegistrationByIdAsync(someRegistrationId);

            // then
            await Assert.ThrowsAsync <RegistrationDependencyException>(() =>
                                                                       deleteRegistrationTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        ShouldThrowDependencyExceptionOnRetrieveByIdWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid someRegistrationId = Guid.NewGuid();
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedRegistrationException =
                new LockedRegistrationException(databaseUpdateConcurrencyException);

            var expectedRegistrationDependencyException =
                new RegistrationDependencyException(lockedRegistrationException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectRegistrationByIdAsync(It.IsAny <Guid>()))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <Registration> retrieveByIdRegistrationTask =
                this.registrationService.RetrieveRegistrationByIdAsync(someRegistrationId);

            // then
            await Assert.ThrowsAsync <RegistrationDependencyException>(() =>
                                                                       retrieveByIdRegistrationTask.AsTask());

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

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

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