コード例 #1
0
        public async void ShouldThrowValidationExceptionOnAddWhenUserContactIsNullAndLogItAsync()
        {
            // given
            UserContact randomUserContact        = default;
            UserContact nullUserContact          = randomUserContact;
            var         nullUserContactException = new NullUserContactException();

            var expectedUserContactValidationException =
                new UserContactValidationException(nullUserContactException);

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

            // 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(It.IsAny <UserContact>()),
                                          Times.Never);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
コード例 #2
0
        public async void ShouldThrowValidationExceptionOnAddWhenContactIdIsInvalidAndLogItAsync()
        {
            // given
            UserContact randomUserContact = CreateRandomUserContact();
            UserContact inputUserContact  = randomUserContact;

            inputUserContact.ContactId = default;

            var invalidUserContactInputException = new InvalidUserContactInputException(
                parameterName: nameof(UserContact.ContactId),
                parameterValue: inputUserContact.ContactId);

            var expectedUserContactValidationException =
                new UserContactValidationException(invalidUserContactInputException);

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

            // 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(It.IsAny <UserContact>()),
                                          Times.Never);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
コード例 #3
0
        public async Task ShouldThrowValidatonExceptionOnRetrieveWhenContactIdIsInvalidAndLogItAsync()
        {
            // given
            Guid randomContactId = default;
            Guid randomUserId    = Guid.NewGuid();
            Guid inputContactId  = randomContactId;
            Guid inputUserId     = randomUserId;

            var invalidUserContactInputException = new InvalidUserContactInputException(
                parameterName: nameof(UserContact.ContactId),
                parameterValue: inputContactId);

            var expectedUserContactValidationException =
                new UserContactValidationException(invalidUserContactInputException);

            // when
            ValueTask <UserContact> retrieveUserContactTask =
                this.userContactService.RetrieveUserContactByIdAsync(inputUserId, inputContactId);

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
コード例 #4
0
        private UserContactValidationException CreateAndLogValidationException(Exception exception)
        {
            var UserContactValidationException = new UserContactValidationException(exception);

            this.loggingBroker.LogError(UserContactValidationException);

            return(UserContactValidationException);
        }
コード例 #5
0
        public async Task ShouldThrowValidationExceptionOnRemoveWhenStorageUserContactIsInvalidAndLogItAsync()
        {
            // given
            UserContact randomUserContact      = CreateRandomUserContact();
            Guid        inputContactId         = randomUserContact.ContactId;
            Guid        inputUserId            = randomUserContact.UserId;
            UserContact nullStorageUserContact = null;

            var notFoundUserContactException =
                new NotFoundUserContactException(inputUserId, inputContactId);

            var expectedSemesterCourseValidationException =
                new UserContactValidationException(notFoundUserContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectUserContactByIdAsync(inputUserId, inputContactId))
            .ReturnsAsync(nullStorageUserContact);

            // when
            ValueTask <UserContact> removeUserContactTask =
                this.userContactService.RemoveUserContactByIdAsync(inputUserId, inputContactId);

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

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
コード例 #6
0
        public async Task ShouldThrowValidatonExceptionOnRemoveWhenUserIdOrContactIdIsInvalidAndLogItAsync()
        {
            // given
            Guid inputContactId = Guid.Empty;
            Guid inputUserId    = Guid.Empty;

            var invalidUserContactException = new InvalidUserContactException();

            invalidUserContactException.AddData(
                key: nameof(UserContact.ContactId),
                values: "Id is required");

            invalidUserContactException.AddData(
                key: nameof(UserContact.UserId),
                values: "Id is required");

            var expectedUserContactValidationException =
                new UserContactValidationException(invalidUserContactException);

            // when
            ValueTask <UserContact> removeUserContactTask =
                this.userContactService.RemoveUserContactByIdAsync(inputUserId, inputContactId);

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

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
コード例 #7
0
        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();
        }
コード例 #8
0
        public async void ShouldThrowValidationExceptionOnAddIfUserContactAlreadyExistsAndLogItAsync()
        {
            // given
            UserContact randomUserContact        = CreateRandomUserContact();
            UserContact alreadyExistsUserContact = randomUserContact;
            string      randomMessage            = GetRandomMessage();
            string      exceptionMessage         = randomMessage;
            var         duplicateKeyException    = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsUserContactException =
                new AlreadyExistsUserContactException(duplicateKeyException);

            var expectedUserContactValidationException =
                new UserContactValidationException(alreadyExistsUserContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertUserContactAsync(alreadyExistsUserContact))
            .ThrowsAsync(duplicateKeyException);

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

            // 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(alreadyExistsUserContact),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
コード例 #9
0
        public async void ShouldThrowValidationExceptionOnAddIfUserIdOrContactIdInvalidAndLogItAsync()
        {
            // given
            UserContact randomUserContact = CreateRandomUserContact();
            UserContact inputUserContact  = new UserContact();

            var invalidUserContactException = new InvalidUserContactException();

            invalidUserContactException.AddData(
                key: nameof(UserContact.UserId),
                values: "Id is required");

            invalidUserContactException.AddData(
                key: nameof(UserContact.ContactId),
                values: "Id is required");

            var expectedUserContactValidationException =
                new UserContactValidationException(invalidUserContactException);

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

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

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

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

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