public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime      = GetRandomDateTime();
            ExamFee        randomExamFee = CreateRandomExamFee(dateTime);

            randomExamFee.UpdatedBy   = randomExamFee.CreatedBy;
            randomExamFee.UpdatedDate = randomExamFee.CreatedDate;
            ExamFee invalidExamFee   = randomExamFee;
            string  randomMessage    = GetRandomMessage();
            string  exceptionMessage = randomMessage;
            var     foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidExamFeeReferenceException =
                new InvalidExamFeeReferenceException(foreignKeyConstraintConflictException);

            var expectedExamFeeValidationException =
                new ExamFeeValidationException(invalidExamFeeReferenceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertExamFeeAsync(invalidExamFee))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <ExamFee> addExamFeeTask =
                this.examFeeService.AddExamFeeAsync(invalidExamFee);

            // then
            await Assert.ThrowsAsync <ExamFeeValidationException>(() =>
                                                                  addExamFeeTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddIfReferneceExceptionAndLogItAsync()
        {
            // given
            UserContact randomUserContact  = CreateRandomUserContact();
            UserContact invalidUserContact = randomUserContact;
            string      randomMessage      = GetRandomMessage();
            string      exceptionMessage   = randomMessage;
            var         foreignKeyConstraintConflictException =
                new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidUserContactReferenceException =
                new InvalidUserContactReferenceException(foreignKeyConstraintConflictException);

            var expectedUserContactValidationException =
                new UserContactValidationException(invalidUserContactReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertUserContactAsync(invalidUserContact))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <UserContact> addUserContactTask =
                this.userContactService.AddUserContactAsync(invalidUserContact);

            // then
            await Assert.ThrowsAsync <UserContactValidationException>(() =>
                                                                      addUserContactTask.AsTask());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 3
0
        public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            CourseAttachment randomCourseAttachment   = CreateRandomCourseAttachment();
            CourseAttachment someCourseAttachment     = randomCourseAttachment;
            string           randomMessage            = GetRandomMessage();
            string           exceptionMessage         = randomMessage;
            var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidCourseAttachmentReferenceException =
                new InvalidCourseAttachmentReferenceException(foreignKeyConstraintConflictException);

            var expectedCourseAttachmentValidationException =
                new CourseAttachmentValidationException(invalidCourseAttachmentReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCourseAttachmentAsync(someCourseAttachment))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <CourseAttachment> addCourseAttachmentTask =
                this.courseAttachmentService.AddCourseAttachmentAsync(someCourseAttachment);

            // then
            await Assert.ThrowsAsync <CourseAttachmentValidationException>(() =>
                                                                           addCourseAttachmentTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            StudentRegistration randomStudentRegistration = CreateRandomStudentRegistration();
            StudentRegistration someStudentRegistration   = randomStudentRegistration;
            string randomMessage    = GetRandomMessage();
            string exceptionMessage = randomMessage;
            var    foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidStudentRegistrationReferenceException =
                new InvalidStudentRegistrationReferenceException(foreignKeyConstraintConflictException);

            var expectedStudentRegistrationValidationException =
                new StudentRegistrationValidationException(invalidStudentRegistrationReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentRegistrationAsync(someStudentRegistration))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <StudentRegistration> addStudentRegistrationTask =
                this.studentRegistrationService.AddStudentRegistrationAsync(someStudentRegistration);

            // then
            await Assert.ThrowsAsync <StudentRegistrationValidationException>(() =>
                                                                              addStudentRegistrationTask.AsTask());

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidationOnModifyIfForeignKeyConstraintConflictExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset randomDateTime      = GetRandomDateTime();
            DateTimeOffset dateTime            = randomDateTime;
            Registration   randomRegistration  = CreateRandomRegistration(dateTime);
            Registration   storageRegistration = randomRegistration;
            Registration   foreignKeyConflictedRegistration = storageRegistration.DeepClone();

            storageRegistration.UpdatedDate = dateTime.AddMinutes(GetNegativeRandomNumber());
            string randomMessage    = GetRandomMessage();
            string exceptionMessage = randomMessage;

            var foreignKeyConstraintConflictException =
                new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidRegistrationReferenceException =
                new InvalidRegistrationReferenceException(foreignKeyConstraintConflictException);

            var registrationValidationException =
                new RegistrationValidationException(invalidRegistrationReferenceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectRegistrationByIdAsync(foreignKeyConflictedRegistration.Id))
            .ReturnsAsync(storageRegistration);

            this.storageBrokerMock.Setup(broker =>
                                         broker.UpdateRegistrationAsync(foreignKeyConflictedRegistration))
            .ThrowsAsync(foreignKeyConstraintConflictException);

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

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

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectRegistrationByIdAsync(foreignKeyConflictedRegistration.Id),
                                          Times.Once);

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

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

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