public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifierAndNoExistingContactWasReturnedFromCommand_AssertUpdateContactAsyncWasNotCalledOnMicrosoftGraphRepository()
        {
            CommandHandler sut = CreateSut();

            IUpdateContactCommand command = CreateUpdateContactCommandMock(hasExistingContact: false).Object;
            await sut.ExecuteAsync(command);

            _microsoftGraphRepositoryMock.Verify(m => m.UpdateContactAsync(It.IsAny <IRefreshableToken>(), It.IsAny <IContact>()), Times.Never());
        }
        public async Task ExecuteAsync_WhenCalled_AssertExternalIdentifierWasCalledOnCommand()
        {
            CommandHandler sut = CreateSut();

            Mock <IUpdateContactCommand> commandMock = CreateUpdateContactCommandMock();
            await sut.ExecuteAsync(commandMock.Object);

            commandMock.Verify(m => m.ExternalIdentifier, Times.Once);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandDoesNotHaveExternalIdentifier_AssertGetExistingContactAsyncWasNotCalledOnCommand()
        {
            CommandHandler sut = CreateSut();

            Mock <IUpdateContactCommand> commandMock = CreateUpdateContactCommandMock(false);
            await sut.ExecuteAsync(commandMock.Object);

            commandMock.Verify(m => m.GetExistingContactAsync(It.IsAny <IMicrosoftGraphRepository>(), It.IsAny <IContactRepository>()), Times.Never());
        }
        public async Task ExecuteAsync_WhenCalledAndCommandDoesNotHaveExternalIdentifier_AssertCreateOrUpdateContactSupplementAsyncWasNotCalledOnContactRepository()
        {
            CommandHandler sut = CreateSut();

            IUpdateContactCommand command = CreateUpdateContactCommandMock(false).Object;
            await sut.ExecuteAsync(command);

            _contactRepositoryMock.Verify(m => m.CreateOrUpdateContactSupplementAsync(It.IsAny <IContact>(), It.IsAny <string>()), Times.Never());
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifierAndExistingContactWasReturnedFromCommandAndNoUpdatedContactWasReturnedFromMicrosoftGraphRepository_AssertCreateOrUpdateContactSupplementAsyncWasNotCalledOnContactRepository()
        {
            CommandHandler sut = CreateSut(false);

            IUpdateContactCommand command = CreateUpdateContactCommandMock().Object;
            await sut.ExecuteAsync(command);

            _contactRepositoryMock.Verify(m => m.CreateOrUpdateContactSupplementAsync(It.IsAny <IContact>(), It.IsAny <string>()), Times.Never());
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifierAndExistingContactWasReturnedFromCommand_AssertInternalIdentifierWasCalledOnExistingContactFromCommand()
        {
            CommandHandler sut = CreateSut();

            Mock <IContact>       existingContactMock = _fixture.BuildContactMock();
            IUpdateContactCommand command             = CreateUpdateContactCommandMock(existingContact: existingContactMock.Object).Object;
            await sut.ExecuteAsync(command);

            existingContactMock.Verify(m => m.InternalIdentifier, Times.Once);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifierAndNoExistingContactWasReturnedFromCommand_AssertExternalIdentifierSetterWasNotCalledOnContactFromToDomainOnCommand()
        {
            CommandHandler sut = CreateSut();

            Mock <IContact>       toDomainContactMock = _fixture.BuildContactMock();
            IUpdateContactCommand command             = CreateUpdateContactCommandMock(toDomainContact: toDomainContactMock.Object, hasExistingContact: false).Object;
            await sut.ExecuteAsync(command);

            toDomainContactMock.VerifySet(m => m.ExternalIdentifier = It.IsAny <string>(), Times.Never);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifierAndExistingContactWasReturnedFromCommandAndNoUpdatedContactWasReturnedFromMicrosoftGraphRepository_AssertExternalIdentifierWasNotCalledOnExistingContactFromCommand()
        {
            CommandHandler sut = CreateSut(false);

            Mock <IContact>       existingContactMock = _fixture.BuildContactMock();
            IUpdateContactCommand command             = CreateUpdateContactCommandMock(existingContact: existingContactMock.Object).Object;
            await sut.ExecuteAsync(command);

            existingContactMock.Verify(m => m.ExternalIdentifier, Times.Never);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandDoesNotHaveExternalIdentifier_AssertInternalIdentifierWasNotCalledOnExistingContactFromCommand()
        {
            CommandHandler sut = CreateSut();

            Mock <IContact>       existingContactMock = _fixture.BuildContactMock();
            IUpdateContactCommand command             = CreateUpdateContactCommandMock(false, existingContact: existingContactMock.Object).Object;
            await sut.ExecuteAsync(command);

            existingContactMock.Verify(m => m.InternalIdentifier, Times.Never);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifierAndExistingContactWasReturnedFromCommandAndUpdatedContactWasReturnedFromMicrosoftGraphRepository_AssertExternalIdentifierWasCalledOnUpdatedContactFromMicrosoftGraphRepository()
        {
            Mock <IContact> updatedMicrosoftGraphContactMock = _fixture.BuildContactMock();
            CommandHandler  sut = CreateSut(updatedMicrosoftGraphContact: updatedMicrosoftGraphContactMock.Object);

            IUpdateContactCommand command = CreateUpdateContactCommandMock().Object;
            await sut.ExecuteAsync(command);

            updatedMicrosoftGraphContactMock.Verify(m => m.ExternalIdentifier, Times.Once);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandDoesNotHaveExternalIdentifier_AssertInternalIdentifierSetterWasNotCalledOnContactFromToDomainOnCommand()
        {
            CommandHandler sut = CreateSut();

            Mock <IContact>       toDomainContactMock = _fixture.BuildContactMock();
            IUpdateContactCommand command             = CreateUpdateContactCommandMock(false, toDomainContactMock.Object).Object;
            await sut.ExecuteAsync(command);

            toDomainContactMock.VerifySet(m => m.InternalIdentifier = It.IsAny <string>(), Times.Never);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandDoesNotHaveExternalIdentifier_AssertExternalIdentifierWasNotCalledOnUpdatedContactFromMicrosoftGraphRepository()
        {
            Mock <IContact> updatedMicrosoftGraphContactMock = _fixture.BuildContactMock();
            CommandHandler  sut = CreateSut(updatedMicrosoftGraphContact: updatedMicrosoftGraphContactMock.Object);

            IUpdateContactCommand command = CreateUpdateContactCommandMock(false).Object;
            await sut.ExecuteAsync(command);

            updatedMicrosoftGraphContactMock.Verify(m => m.ExternalIdentifier, Times.Never);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifierAndExistingContactWasReturnedFromCommandAndUpdatedContactWithoutExternalIdentifierWasReturnedFromMicrosoftGraphRepository_AssertExternalIdentifierSetterWasNotCalledOnContactFromToDomainOnCommand()
        {
            IContact       updatedMicrosoftGraphContact = _fixture.BuildContactMock(hasExternalIdentifier: false).Object;
            CommandHandler sut = CreateSut(updatedMicrosoftGraphContact: updatedMicrosoftGraphContact);

            Mock <IContact>       toDomainContactMock = _fixture.BuildContactMock();
            IUpdateContactCommand command             = CreateUpdateContactCommandMock(toDomainContact: toDomainContactMock.Object).Object;
            await sut.ExecuteAsync(command);

            toDomainContactMock.VerifySet(m => m.ExternalIdentifier = It.IsAny <string>(), Times.Never);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifier_AssertToDomainWasCalledOnCommand()
        {
            CommandHandler sut = CreateSut();

            Mock <IUpdateContactCommand> commandMock = CreateUpdateContactCommandMock();
            await sut.ExecuteAsync(commandMock.Object);

            commandMock.Verify(m => m.ToDomain(
                                   It.Is <IContactRepository>(value => value == _contactRepositoryMock.Object),
                                   It.Is <IAccountingRepository>(value => value == _accountingRepositoryMock.Object)),
                               Times.Once);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifier_AssertGetExistingContactAsyncWasCalledOnCommand()
        {
            CommandHandler sut = CreateSut();

            Mock <IUpdateContactCommand> commandMock = CreateUpdateContactCommandMock();
            await sut.ExecuteAsync(commandMock.Object);

            commandMock.Verify(m => m.GetExistingContactAsync(
                                   It.Is <IMicrosoftGraphRepository>(value => value == _microsoftGraphRepositoryMock.Object),
                                   It.Is <IContactRepository>(value => value == _contactRepositoryMock.Object)),
                               Times.Once());
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifierAndExistingContactWasReturnedFromCommandAndUpdatedContactWithExternalIdentifierWasReturnedFromMicrosoftGraphRepository_AssertExternalIdentifierSetterWasCalledOnContactFromToDomainOnCommandWithExternalIdentifierFromUpdatedContractFormMicrosoftGraphRepository()
        {
            string         externalIdentifier           = _fixture.Create <string>();
            IContact       updatedMicrosoftGraphContact = _fixture.BuildContactMock(externalIdentifier: externalIdentifier).Object;
            CommandHandler sut = CreateSut(updatedMicrosoftGraphContact: updatedMicrosoftGraphContact);

            Mock <IContact>       toDomainContactMock = _fixture.BuildContactMock();
            IUpdateContactCommand command             = CreateUpdateContactCommandMock(toDomainContact: toDomainContactMock.Object).Object;
            await sut.ExecuteAsync(command);

            toDomainContactMock.VerifySet(m => m.ExternalIdentifier = It.Is <string>(value => string.CompareOrdinal(value, externalIdentifier) == 0), Times.Once);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifierAndExistingContactWithInternalIdentifierWasReturnedFromCommand_AssertInternalIdentifierSetterWasCalledOnContactFromToDomainOnCommandWithInternalIdentifierFromExistingContactFromCommand()
        {
            CommandHandler sut = CreateSut();

            Mock <IContact>       toDomainContactMock = _fixture.BuildContactMock();
            string                internalIdentifier  = _fixture.Create <string>();
            IContact              existingContact     = _fixture.BuildContactMock(internalIdentifier: internalIdentifier).Object;
            IUpdateContactCommand command             = CreateUpdateContactCommandMock(toDomainContact: toDomainContactMock.Object, existingContact: existingContact).Object;
            await sut.ExecuteAsync(command);

            toDomainContactMock.VerifySet(m => m.InternalIdentifier = It.Is <string>(value => string.CompareOrdinal(value, internalIdentifier) == 0), Times.Once);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifierAndExistingContactWasReturnedFromCommand_AssertUpdateContactAsyncWasCalledOnMicrosoftGraphRepositoryWithContactFromToDomainOnCommand()
        {
            CommandHandler sut = CreateSut();

            IContact              toDomainContact  = _fixture.BuildContactMock().Object;
            IRefreshableToken     refreshableToken = _fixture.BuildRefreshableTokenMock().Object;
            IUpdateContactCommand command          = CreateUpdateContactCommandMock(toDomainContact: toDomainContact, refreshableToken: refreshableToken).Object;
            await sut.ExecuteAsync(command);

            _microsoftGraphRepositoryMock.Verify(m => m.UpdateContactAsync(
                                                     It.Is <IRefreshableToken>(value => value == refreshableToken),
                                                     It.Is <IContact>(value => value == toDomainContact)),
                                                 Times.Once);
        }
        public async Task ExecuteAsync_WhenCalledAndCommandHasExternalIdentifierAndExistingContactWasReturnedFromCommandAndUpdatedContactWithExternalIdentifierWasReturnedFromMicrosoftGraphRepository_AssertCreateOrUpdateContactSupplementAsyncWasCalledOnContactRepositoryWithContactFromToDomainOnCommandAndWithExternalIdentifierFromExistingContactFromCommand()
        {
            CommandHandler sut = CreateSut();

            IContact toDomainContact      = _fixture.BuildContactMock().Object;
            string   externalIdentifier   = _fixture.Create <string>();
            IContact existingContact      = _fixture.BuildContactMock(externalIdentifier: externalIdentifier).Object;
            IUpdateContactCommand command = CreateUpdateContactCommandMock(toDomainContact: toDomainContact, existingContact: existingContact).Object;
            await sut.ExecuteAsync(command);

            _contactRepositoryMock.Verify(m => m.CreateOrUpdateContactSupplementAsync(
                                              It.Is <IContact>(value => value == toDomainContact),
                                              It.Is <string>(value => string.CompareOrdinal(value, externalIdentifier) == 0)),
                                          Times.Once());
        }
        public void ExecuteAsync_WhenCommandIsNull_ThrowsArgumentNullException()
        {
            CommandHandler sut = CreateSut();

            ArgumentNullException result = Assert.ThrowsAsync <ArgumentNullException>(async() => await sut.ExecuteAsync(null));

            Assert.That(result.ParamName, Is.EqualTo("command"));
        }