public async void ShouldThrowValidationExceptionOnRetrieveWhenIdIsInvalidAndLogItAsync()
        {
            //given
            Guid randomContactId = default;
            Guid inputContactId  = randomContactId;

            var invalidContactInputException = new InvalidContactException(
                parameterName: nameof(Contact.Id),
                parameterValue: inputContactId);

            var expectedContactValidationException = new ContactValidationException(invalidContactInputException);

            //when
            ValueTask <Contact> retrieveContactByIdTask =
                this.contactService.RetrieveContactByIdAsync(inputContactId);

            //then
            await Assert.ThrowsAsync <ContactValidationException>(() => retrieveContactByIdTask.AsTask());

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidationExceptionOnModifyWhenContactIsNullAndLogItAsync()
        {
            //given
            Contact invalidContact       = null;
            var     nullContactException = new NullContactException();

            var expectedContactValidationException =
                new ContactValidationException(nullContactException);

            //when
            ValueTask <Contact> modifyContactTask =
                this.contactService.ModifyContactAsync(invalidContact);

            //then
            await Assert.ThrowsAsync <ContactValidationException>(() =>
                                                                  modifyContactTask.AsTask());

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Exemple #3
0
        public async void ShouldThrowValidationExceptionOnAddWhenContactIsNullAndLogItAsync()
        {
            // given
            Contact randomContact = null;
            Contact nullContact   = randomContact;

            var nullContactException = new NullContactException();

            var expectedContactValidationException =
                new ContactValidationException(nullContactException);

            // when
            ValueTask <Contact> addContactTask =
                this.contactService.AddContactAsync(nullContact);

            // then
            await Assert.ThrowsAsync <ContactValidationException>(() =>
                                                                  addContactTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsSameAsCreatedDateAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime      = GetRandomDateTime();
            Contact        randomContact = CreateRandomContact(dateTime);
            Contact        inputContact  = randomContact;

            var invalidContactInputException = new InvalidContactException(
                parameterName: nameof(Contact.UpdatedDate),
                parameterValue: inputContact.UpdatedDate);

            var expectedContactValidationException =
                new ContactValidationException(invalidContactInputException);

            // when
            ValueTask <Contact> modifyContactTask =
                this.contactService.ModifyContactAsync(inputContact);

            // then
            await Assert.ThrowsAsync <ContactValidationException>(() =>
                                                                  modifyContactTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowValidationExceptionOnModifyWhenContactIdIsInvalidAndLogItAsync()
        {
            //given
            Guid           invalidContactId = Guid.Empty;
            DateTimeOffset dateTime         = GetRandomDateTime();
            Contact        randomContact    = CreateRandomContact(dateTime);
            Contact        invalidContact   = randomContact;

            invalidContact.Id = invalidContactId;

            var invalidContactException = new InvalidContactException(
                parameterName: nameof(Contact.Id),
                parameterValue: invalidContact.Id);

            var expectedContactValidationException =
                new ContactValidationException(invalidContactException);

            //when
            ValueTask <Contact> modifyContactTask =
                this.contactService.ModifyContactAsync(invalidContact);

            //then
            await Assert.ThrowsAsync <ContactValidationException>(() =>
                                                                  modifyContactTask.AsTask());

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        private ContactValidationException CreateAndLogValidationException(Exception exception)
        {
            var contactValidationException = new ContactValidationException(exception);

            this.loggingBroker.LogError(contactValidationException);

            return(contactValidationException);
        }
Exemple #7
0
        public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            Guid           differentId           = Guid.NewGuid();
            Guid           invalidCreatedBy      = differentId;
            DateTimeOffset randomDate            = GetRandomDateTime();
            Contact        randomContact         = CreateRandomContact(randomDate);
            Contact        invalidContact        = randomContact;

            invalidContact.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes);
            Contact storageContact = randomContact.DeepClone();
            Guid    contactId      = invalidContact.Id;

            invalidContact.CreatedBy = invalidCreatedBy;

            var invalidContactInputException = new InvalidContactException(
                parameterName: nameof(Contact.CreatedBy),
                parameterValue: invalidContact.CreatedBy);

            var expectedContactValidationException =
                new ContactValidationException(invalidContactInputException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectContactByIdAsync(contactId))
            .ReturnsAsync(storageContact);

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

            // when
            ValueTask <Contact> modifyContactTask =
                this.contactService.ModifyContactAsync(invalidContact);

            // then
            await Assert.ThrowsAsync <ContactValidationException>(() =>
                                                                  modifyContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectContactByIdAsync(invalidContact.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async void ShouldThrowValidationExceptionOnAddWhenContactAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime             = GetRandomDateTime();
            Contact        randomContact        = CreateRandomContact(dateTime);
            Contact        alreadyExistsContact = randomContact;

            alreadyExistsContact.UpdatedBy = alreadyExistsContact.CreatedBy;
            string randomMessage         = GetRandomMessage();
            string exceptionMessage      = randomMessage;
            var    duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsContactException =
                new AlreadyExistsContactException(duplicateKeyException);

            var expectedContactValidationException =
                new ContactValidationException(alreadyExistsContactException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertContactAsync(alreadyExistsContact))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <Contact> addContactTask =
                this.contactService.AddContactAsync(alreadyExistsContact);

            // then
            await Assert.ThrowsAsync <ContactValidationException>(() =>
                                                                  addContactTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemple #9
0
        public async void ShouldThrowValidationExceptionOnCreateWhenCreatedDateIsNotRecentAndLogItAsync(
            int minutes)
        {
            // given
            DateTimeOffset randomDate     = GetRandomDateTime();
            Contact        randomContact  = CreateRandomContact(randomDate);
            Contact        invalidContact = randomContact;

            invalidContact.CreatedDate = randomDate.AddMinutes(minutes);
            invalidContact.UpdatedDate = invalidContact.CreatedDate;
            var invalidContactException = new InvalidContactException();

            invalidContactException.AddData(
                key: nameof(Contact.CreatedDate),
                values: $"Date is not recent");

            var expectedContactValidationException =
                new ContactValidationException(invalidContactException);

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

            // when
            ValueTask <Contact> createContactTask =
                this.contactService.AddContactAsync(invalidContact);

            // then
            await Assert.ThrowsAsync <ContactValidationException>(() =>
                                                                  createContactTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #10
0
        public async Task ShouldThrowValidationExceptionOnModifyIfContactDoesntExistAndLogItAsync()
        {
            // given
            int            randomNegativeMinutes = GetNegativeRandomNumber();
            DateTimeOffset dateTime           = GetRandomDateTime();
            Contact        randomContact      = CreateRandomContact(dateTime);
            Contact        nonExistentContact = randomContact;

            nonExistentContact.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes);
            Contact noContact = null;
            var     notFoundContactException = new NotFoundContactException(nonExistentContact.Id);

            var expectedContactValidationException =
                new ContactValidationException(notFoundContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectContactByIdAsync(nonExistentContact.Id))
            .ReturnsAsync(noContact);

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

            // when
            ValueTask <Contact> modifyContactTask =
                this.contactService.ModifyContactAsync(nonExistentContact);

            // then
            await Assert.ThrowsAsync <ContactValidationException>(() =>
                                                                  modifyContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectContactByIdAsync(nonExistentContact.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Exemple #11
0
        public async Task ShouldThrowValidatonExceptionOnDeleteWhenStorageContactIsInvalidAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime           = GetRandomDateTime();
            Contact        randomContact      = CreateRandomContact(dateTime);
            Guid           inputContactId     = randomContact.Id;
            Contact        inputContact       = randomContact;
            Contact        nullStorageContact = null;

            var notFoundContactException = new NotFoundContactException(inputContactId);

            var expectedContactValidationException =
                new ContactValidationException(notFoundContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectContactByIdAsync(inputContactId))
            .ReturnsAsync(nullStorageContact);

            // when
            ValueTask <Contact> actualContactTask =
                this.contactService.RemoveContactByIdAsync(inputContactId);

            // then
            await Assert.ThrowsAsync <ContactValidationException>(() => actualContactTask.AsTask());

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

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Exemple #12
0
        public async void ShouldThrowValidationExceptionOnRetrieveWhenStorageContactIsNullAndLogItAsync()
        {
            //given
            Guid    randomContactId       = Guid.NewGuid();
            Guid    inputContactId        = randomContactId;
            Contact invalidStorageContact = null;

            var notFoundContactException = new NotFoundContactException(inputContactId);

            var expectedContactValidationException = new ContactValidationException(notFoundContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectContactByIdAsync(inputContactId))
            .ReturnsAsync(invalidStorageContact);

            //when
            ValueTask <Contact> retrieveContactByIdTask =
                this.contactService.RetrieveContactByIdAsync(inputContactId);

            //then
            await Assert.ThrowsAsync <ContactValidationException>(() =>
                                                                  retrieveContactByIdTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #13
0
        public async void ShouldThrowValidationExceptionOnCreateIfContactIsInvalidAndLogItAsync(
            string invalidText)
        {
            // given
            var invalidContact = new Contact
            {
                Information = invalidText,
                Notes       = invalidText
            };

            var invalidContactException = new InvalidContactException();

            invalidContactException.AddData(
                key: nameof(Contact.Id),
                values: "Id is required");

            invalidContactException.AddData(
                key: nameof(Contact.Information),
                values: "Text is required");

            invalidContactException.AddData(
                key: nameof(Contact.Notes),
                values: "Text is required");

            invalidContactException.AddData(
                key: nameof(Contact.CreatedDate),
                values: "Date is required");

            invalidContactException.AddData(
                key: nameof(Contact.UpdatedDate),
                values: "Date is required");

            invalidContactException.AddData(
                key: nameof(Contact.CreatedBy),
                values: "Id is required");

            invalidContactException.AddData(
                key: nameof(Contact.UpdatedBy),
                values: "Id is required");

            var expectedContactValidationException =
                new ContactValidationException(invalidContactException);

            // when
            ValueTask <Contact> createContactTask =
                this.contactService.AddContactAsync(invalidContact);

            // then
            await Assert.ThrowsAsync <ContactValidationException>(() =>
                                                                  createContactTask.AsTask());

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

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

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

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