public void ShouldThrowDependencyExceptionOnRetrieveAllWhenDbExceptionOccursAndLogIt()
        {
            // given
            var databaseUpdateException = new DbUpdateException();

            var expectedTeacherContactDependencyException =
                new TeacherContactDependencyException(databaseUpdateException);

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

            // when . then
            Assert.Throws <TeacherContactDependencyException>(() =>
                                                              this.teacherContactService.RetrieveAllTeacherContacts());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public void ShouldThrowDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt()
        {
            // given
            SqlException sqlException = GetSqlException();

            var expectedTeacherContactDependencyException =
                new TeacherContactDependencyException(sqlException);

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

            // when
            Action retrieveAllTeacherContactsAction = () =>
                                                      this.teacherContactService.RetrieveAllTeacherContacts();

            // then
            Assert.Throws <TeacherContactDependencyException>(
                retrieveAllTeacherContactsAction);

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            TeacherContact randomTeacherContact    = CreateRandomTeacherContact();
            TeacherContact inputTeacherContact     = randomTeacherContact;
            var            databaseUpdateException = new DbUpdateException();

            var expectedTeacherContactDependencyException =
                new TeacherContactDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertTeacherContactAsync(It.IsAny <TeacherContact>()))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <TeacherContact> addTeacherContactTask =
                this.teacherContactService.AddTeacherContactAsync(inputTeacherContact);

            // then
            await Assert.ThrowsAsync <TeacherContactDependencyException>(() =>
                                                                         addTeacherContactTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbUpdateExceptionOccursAndLogItAsync()
        {
            // given
            Guid someContactId           = Guid.NewGuid();
            Guid someTeacherId           = Guid.NewGuid();
            var  databaseUpdateException = new DbUpdateException();

            var expectedTeacherContactDependencyException =
                new TeacherContactDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherContactByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <TeacherContact> retrieveTeacherContactTask =
                this.teacherContactService.RetrieveTeacherContactByIdAsync(someTeacherId, someContactId);

            // then
            await Assert.ThrowsAsync <TeacherContactDependencyException>(
                () => retrieveTeacherContactTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private TeacherContactDependencyException CreateAndLogDependencyException(Exception exception)
        {
            var TeacherContactDependencyException = new TeacherContactDependencyException(exception);

            this.loggingBroker.LogError(TeacherContactDependencyException);

            return(TeacherContactDependencyException);
        }
        private TeacherContactDependencyException CreateAndLogCriticalDependencyException(Exception exception)
        {
            var teacherContactDependencyException = new TeacherContactDependencyException(exception);

            this.loggingBroker.LogCritical(teacherContactDependencyException);

            return(teacherContactDependencyException);
        }
Ejemplo n.º 7
0
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            var          randomContactId = Guid.NewGuid();
            var          randomTeacherId = Guid.NewGuid();
            Guid         someContactId   = randomContactId;
            Guid         someTeacherId   = randomTeacherId;
            SqlException sqlException    = GetSqlException();

            var expectedTeacherContactDependencyException
                = new TeacherContactDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherContactByIdAsync(someTeacherId, someContactId))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <TeacherContact> removeTeacherContactTask =
                this.teacherContactService.RemoveTeacherContactByIdAsync(
                    someTeacherId,
                    someContactId);

            // then
            await Assert.ThrowsAsync <TeacherContactDependencyException>(() =>
                                                                         removeTeacherContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectTeacherContactByIdAsync(someTeacherId, someContactId),
                                          Times.Once);

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

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