Example #1
0
        public void ShouldThrowDependencyExceptionOnRetrieveAllAttachmentsWhenSqlExceptionOccursAndLogIt()
        {
            // given
            var sqlException = GetSqlException();

            var expectedAttachmentDependencyException =
                new AttachmentDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllAttachments())
            .Throws(sqlException);

            // when . then
            Assert.Throws <AttachmentDependencyException>(() =>
                                                          this.attachmentService.RetrieveAllAttachments());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Example #2
0
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomAttachmentId = Guid.NewGuid();
            Guid inputAttachmentId  = randomAttachmentId;
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var  lockedAttachmentException          = new LockedAttachmentException(databaseUpdateConcurrencyException);

            var expectedAttachmentDependencyException =
                new AttachmentDependencyException(lockedAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttachmentByIdAsync(inputAttachmentId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <Attachment> deleteAttachmentTask =
                this.attachmentService.RemoveAttachmentByIdAsync(inputAttachmentId);

            // then
            await Assert.ThrowsAsync <AttachmentDependencyException>(() =>
                                                                     deleteAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAttachmentByIdAsync(inputAttachmentId),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #3
0
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            Guid         someAttachmentId = Guid.NewGuid();
            SqlException sqlException     = GetSqlException();

            var expectedAttachmentDependencyException =
                new AttachmentDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttachmentByIdAsync(someAttachmentId))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <Attachment> deleteAttachmentTask =
                this.attachmentService.RemoveAttachmentByIdAsync(someAttachmentId);

            // then
            await Assert.ThrowsAsync <AttachmentDependencyException>(() =>
                                                                     deleteAttachmentTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        private AttachmentDependencyException CreateAndLogCriticalDependencyException(Exception exception)
        {
            var attachmentDependencyException = new AttachmentDependencyException(exception);

            this.loggingBroker.LogCritical(attachmentDependencyException);

            return(attachmentDependencyException);
        }
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Attachment     randomAttachment     = CreateRandomAttachment(randomDateTime);
            Attachment     someAttachment       = randomAttachment;

            someAttachment.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedAttachmentException          = new LockedAttachmentException(databaseUpdateConcurrencyException);

            var expectedAttachmentDependencyException =
                new AttachmentDependencyException(lockedAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttachmentByIdAsync(someAttachment.Id))
            .ThrowsAsync(databaseUpdateConcurrencyException);

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

            // when
            ValueTask <Attachment> modifyAttachmentTask =
                this.attachmentService.ModifyAttachmentAsync(someAttachment);

            // then
            await Assert.ThrowsAsync <AttachmentDependencyException>(() =>
                                                                     modifyAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAttachmentByIdAsync(someAttachment.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Example #6
0
        public async Task ShouldThrowDependencyExceptionOnCreateWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime         = GetRandomDateTime();
            Attachment     randomAttachment = CreateRandomAttachment(dateTime);
            Attachment     inputAttachment  = randomAttachment;

            inputAttachment.UpdatedBy   = inputAttachment.CreatedBy;
            inputAttachment.UpdatedDate = inputAttachment.CreatedDate;
            var databaseUpdateException = new DbUpdateException();

            var expectedAttachmentDependencyException =
                new AttachmentDependencyException(databaseUpdateException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertAttachmentAsync(inputAttachment))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <Attachment> createAttachmentTask =
                this.attachmentService.AddAttachmentAsync(inputAttachment);

            // then
            await Assert.ThrowsAsync <AttachmentDependencyException>(() =>
                                                                     createAttachmentTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertAttachmentAsync(inputAttachment),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #7
0
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbExceptionOccursAndLogIt()
        {
            // given
            var databaseUpdateException = new DbUpdateException();

            var guid = Guid.NewGuid();

            var expectedAttachmentDependencyException =
                new AttachmentDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAttachmentByIdAsync(guid))
            .Throws(databaseUpdateException);

            // when
            ValueTask <Attachment> retrieveTask =
                this.attachmentService.RetrieveAttachmentByIdAsync(guid);

            // then
            await Assert.ThrowsAsync <AttachmentDependencyException>(() => retrieveTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAttachmentByIdAsync(guid),
                                          Times.Once);

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

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