public async Task ShouldThrowValidatonExceptionOnRetrieveWhenContactIdIsInvalidAndLogItAsync()
        {
            // given
            Guid randomContactId = default;
            Guid randomStudentId = Guid.NewGuid();
            Guid inputContactId  = randomContactId;
            Guid inputStudentId  = randomStudentId;

            var invalidStudentContactException = new InvalidStudentContactException(
                parameterName: nameof(StudentContact.ContactId),
                parameterValue: inputContactId);

            var expectedStudentContactValidationException =
                new StudentContactValidationException(invalidStudentContactException);

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

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenContactIdIsInvalidAndLogItAsync()
        {
            // given
            StudentContact randomStudentContact = CreateRandomStudentContact();
            StudentContact inputStudentContact  = randomStudentContact;

            inputStudentContact.ContactId = default;

            var invalidStudentContactInputException = new InvalidStudentContactInputException(
                parameterName: nameof(StudentContact.ContactId),
                parameterValue: inputStudentContact.ContactId);

            var expectedStudentContactValidationException =
                new StudentContactValidationException(invalidStudentContactInputException);

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

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenStudentContactIsNullAndLogItAsync()
        {
            // given
            StudentContact randomStudentContact        = default;
            StudentContact nullStudentContact          = randomStudentContact;
            var            nullStudentContactException = new NullStudentContactException();

            var expectedStudentContactValidationException =
                new StudentContactValidationException(nullStudentContactException);

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

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 4
0
        private StudentContactValidationException CreateAndLogValidationException(Exception exception)
        {
            var StudentContactValidationException = new StudentContactValidationException(exception);

            this.loggingBroker.LogError(StudentContactValidationException);

            return(StudentContactValidationException);
        }
        public async Task ShouldThrowValidationExceptionOnRemoveWhenStorageStudentContactIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset randomDateTime            = GetRandomDateTime();
            StudentContact randomStudentContact      = CreateRandomStudentContact(randomDateTime);
            Guid           inputContactId            = randomStudentContact.ContactId;
            Guid           inputStudentId            = randomStudentContact.StudentId;
            StudentContact nullStorageStudentContact = null;

            var notFoundStudentContactException =
                new NotFoundStudentContactException(inputStudentId, inputContactId);

            var expectedSemesterCourseValidationException =
                new StudentContactValidationException(notFoundStudentContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentContactByIdAsync(inputStudentId, inputContactId))
            .ReturnsAsync(nullStorageStudentContact);

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

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

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            StudentContact randomStudentContact  = CreateRandomStudentContact();
            StudentContact invalidStudentContact = randomStudentContact;
            string         randomMessage         = GetRandomMessage();
            string         exceptionMessage      = randomMessage;
            var            foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidStudentContactReferenceException =
                new InvalidStudentContactReferenceException(foreignKeyConstraintConflictException);

            var expectedStudentContactValidationException =
                new StudentContactValidationException(invalidStudentContactReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentContactAsync(invalidStudentContact))
            .ThrowsAsync(foreignKeyConstraintConflictException);

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

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenStudentContactAlreadyExistsAndLogItAsync()
        {
            // given
            StudentContact randomStudentContact        = CreateRandomStudentContact();
            StudentContact alreadyExistsStudentContact = randomStudentContact;
            string         randomMessage         = GetRandomMessage();
            string         exceptionMessage      = randomMessage;
            var            duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsStudentContactException =
                new AlreadyExistsStudentContactException(duplicateKeyException);

            var expectedStudentContactValidationException =
                new StudentContactValidationException(alreadyExistsStudentContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentContactAsync(alreadyExistsStudentContact))
            .ThrowsAsync(duplicateKeyException);

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

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

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

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

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