Exemplo n.º 1
0
        public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            StudentContact randomStudentContact    = CreateRandomStudentContact();
            StudentContact inputStudentContact     = randomStudentContact;
            var            databaseUpdateException = new DbUpdateException();

            var expectedStudentContactDependencyException =
                new StudentContactDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentContactAsync(inputStudentContact))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <StudentContact> addStudentContactTask =
                this.studentContactService.AddStudentContactAsync(inputStudentContact);

            // then
            await Assert.ThrowsAsync <StudentContactDependencyException>(() =>
                                                                         addStudentContactTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemplo n.º 2
0
        public void ShouldThrowDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt()
        {
            // given
            SqlException sqlException = GetSqlException();

            var expectedStudentContactDependencyException =
                new StudentContactDependencyException(sqlException);

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

            //when. then
            Assert.Throws<StudentContactDependencyException>(() =>
                this.studentContactService.RetrieveAllStudentContacts());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemplo n.º 3
0
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            Guid someContactId           = Guid.NewGuid();
            Guid someStudentId           = Guid.NewGuid();
            var  databaseUpdateException = new DbUpdateException();

            var expectedStudentContactDependencyException =
                new StudentContactDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentContactByIdAsync(someStudentId, someContactId))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <StudentContact> retrieveStudentContactTask =
                this.studentContactService.RetrieveStudentContactByIdAsync
                    (someStudentId, someContactId);

            // then
            await Assert.ThrowsAsync <StudentContactDependencyException>(
                () => retrieveStudentContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentContactByIdAsync(someStudentId, someContactId),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemplo n.º 4
0
        private StudentContactDependencyException CreateAndLogDependencyException(Exception exception)
        {
            var StudentContactDependencyException = new StudentContactDependencyException(exception);

            this.loggingBroker.LogError(StudentContactDependencyException);

            return(StudentContactDependencyException);
        }
Exemplo n.º 5
0
        private StudentContactDependencyException CreateAndLogCriticalDependencyException(Exception exception)
        {
            var studentContactDependencyException = new StudentContactDependencyException(exception);

            this.loggingBroker.LogCritical(studentContactDependencyException);

            return(studentContactDependencyException);
        }
Exemplo n.º 6
0
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            var          randomContactId = Guid.NewGuid();
            var          randomStudentId = Guid.NewGuid();
            Guid         someContactId   = randomContactId;
            Guid         someStudentId   = randomStudentId;
            SqlException sqlException    = GetSqlException();

            var expectedStudentContactDependencyException
                = new StudentContactDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentContactByIdAsync(someStudentId, someContactId))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <StudentContact> removeStudentContactTask =
                this.studentContactService.RemoveStudentContactByIdAsync(
                    someStudentId,
                    someContactId);

            // then
            await Assert.ThrowsAsync <StudentContactDependencyException>(() =>
                                                                         removeStudentContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentContactByIdAsync(someStudentId, someContactId),
                                          Times.Once);

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

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