public static IEnumerable <object[]> AccountEventsWithNonStarChefSourceSystem()
        {
            var acb = AccountCreated.CreateBuilder();

            acb.SetExternalId("1").SetSource(SourceSystem.ADACO);
            yield return(new object[] { acb.Build(), new AccountCreatedValidator(Mock.Of <IDatabaseCommands>()) });

            var aub = AccountUpdated.CreateBuilder();

            aub.SetExternalId("1").SetSource(SourceSystem.ADACO);
            yield return(new object[] { aub.Build(), new AccountUpdatedValidator(Mock.Of <IDatabaseCommands>()) });

            var ascb = AccountStatusChanged.CreateBuilder();

            ascb.SetExternalId("1").SetStatus(AccountStatus.ACTIVE).SetSource(SourceSystem.ADACO);
            yield return(new object[] { ascb.Build(), new AccountStatusChangedValidator(Mock.Of <IDatabaseCommands>()) });

            var acfb = AccountCreateFailed.CreateBuilder();

            acfb.SetInternalId("1").SetReason(AccountCreateFailedReason.INVALID_CREATE_DATA).SetSource(SourceSystem.ADACO);
            yield return(new object[] { acfb.Build(), new AccountCreateFailedValidator(Mock.Of <IDatabaseCommands>()) });

            var aufb = AccountUpdateFailed.CreateBuilder();

            aufb.SetExternalId("1").SetCommandId("1").SetReason(AccountUpdateFailedReason.INVALID_UPDATE_DATA).SetSource(SourceSystem.ADACO);
            yield return(new object[] { aufb.Build(), new AccountUpdateFailedValidator(Mock.Of <IDatabaseCommands>()) });

            var ascfb = AccountStatusChangeFailed.CreateBuilder();

            ascfb.SetExternalId("1").SetSource(SourceSystem.ADACO);
            yield return(new object[] { ascfb.Build(), new AccountStatusChangeFailedValidator(Mock.Of <IDatabaseCommands>()) });
        }
        public void Should_not_have_log_for_non_starchef_events()
        {
            // arrange
            var builder = AccountCreateFailed.CreateBuilder();

            builder
            .SetInternalId("1")
            .SetReason(AccountCreateFailedReason.INVALID_CREATE_DATA)
            .SetSource(SourceSystem.ADACO);
            var payload = builder.Build();

            var dbCommands = new Mock <IDatabaseCommands>();
            var validator  = new Mock <IEventValidator>(MockBehavior.Strict);

            validator.Setup(m => m.IsAllowedEvent(payload)).Returns(false);

            var messagingLogger = new Mock <IMessagingLogger>();
            var logChecker      = new LogChecker(typeof(AccountCreateFailedEventHandler), Level.All);
            var handler         = new AccountCreateFailedEventHandler(dbCommands.Object, validator.Object, messagingLogger.Object, logChecker.GetLogger());

            // act
            var result = handler.HandleAsync(payload, "1").Result;

            var messageList = logChecker.LoggingEvents;

            logChecker.Dispose();

            // assert
            Assert.Empty(messageList);

            Assert.Null(ThreadContext.Properties[AccountCreateFailedEventHandler.INTERNAL_ID]);
        }
        public void Should_return_true_for_valid_model()
        {
            var builder = AccountCreateFailed.CreateBuilder();

            builder
            .SetInternalId("1")
            .SetReason(AccountCreateFailedReason.INVALID_CREATE_DATA);
            var accountCreated = builder.Build();

            var validator = new AccountCreateFailedValidator(Mock.Of <IDatabaseCommands>());
            var actual    = validator.IsValidPayload(accountCreated);

            Assert.True(actual);
        }
        public void Should_correctly_convert_AccountCreateFailed_to_json()
        {
            var builder = AccountCreateFailed.CreateBuilder();

            builder
            .SetInternalId("1")
            .SetReason(AccountCreateFailedReason.INVALID_CREATE_DATA)
            .SetSource(SourceSystem.STARCHEF);
            var payload = builder.Build();

            var          actual   = MessageEventsExtensions.ToJson(payload);
            const string expected = @"{""InternalId"":""1"",""Reason"":""INVALID_CREATE_DATA"",""Source"":""STARCHEF""}";

            Assert.Equal(expected, actual);
        }
        public void Should_catch_nonInt_IntenalId()
        {
            var        builder       = AccountCreateFailed.CreateBuilder();
            const long MORE_THAN_INT = (((long)int.MaxValue) + 1);

            builder.SetReason(AccountCreateFailedReason.INVALID_CREATE_DATA)
            .SetInternalId(MORE_THAN_INT.ToString());
            var payload = builder.Build();

            var validator = new AccountCreateFailedValidator(Mock.Of <IDatabaseCommands>());
            var result    = validator.IsValidPayload(payload);

            Assert.False(result);
            Assert.Equal("InternalId is not Int32: " + MORE_THAN_INT, validator.GetErrors());
        }
        public void Should_updated_valid_data_and_log_to_messaging_events()
        {
            var builder = AccountCreateFailed.CreateBuilder();

            builder
            .SetInternalId("1")
            .SetReason(AccountCreateFailedReason.INVALID_CREATE_DATA)
            .SetSource(SourceSystem.STARCHEF);
            var payload = builder.Build();

            var dbCommands      = new Mock <IDatabaseCommands>();
            var validator       = new AccountCreateFailedValidator(dbCommands.Object);
            var messagingLogger = new Mock <IMessagingLogger>();
            var handler         = new AccountCreateFailedEventHandler(dbCommands.Object, validator, messagingLogger.Object);

            var result = handler.HandleAsync(payload, "1").Result;

            // assertions
            Assert.Equal(MessageHandlerResult.Success, result);
            messagingLogger.Verify(m => m.ReceivedFailedMessage(It.IsAny <FailedTransferObject>(), It.IsAny <string>()), Times.Once);
        }
        public void All_logs_should_have_correct_internal_id_invalid_payload()
        {
            // arrange
            var builder = AccountCreateFailed.CreateBuilder();

            builder
            .SetInternalId("1")
            .SetReason(AccountCreateFailedReason.INVALID_CREATE_DATA)
            .SetSource(SourceSystem.STARCHEF);
            var payload = builder.Build();

            var dbCommands = new Mock <IDatabaseCommands>();
            var validator  = new Mock <IEventValidator>(MockBehavior.Strict);

            validator.Setup(m => m.IsAllowedEvent(payload)).Returns(true);

            validator.Setup(m => m.IsValidPayload(It.IsAny <object>())).Returns(false);
            validator.Setup(m => m.GetErrors()).Returns(string.Empty);
            var messagingLogger = new Mock <IMessagingLogger>();
            var logChecker      = new LogChecker(typeof(AccountCreateFailedEventHandler), Level.All);
            var handler         = new AccountCreateFailedEventHandler(dbCommands.Object, validator.Object, messagingLogger.Object, logChecker.GetLogger());

            // act
            var result = handler.HandleAsync(payload, "1").Result;

            var messageList = logChecker.LoggingEvents;

            logChecker.Dispose();

            // assert
            Assert.All(messageList, item =>
            {
                Assert.Equal("1", item.Properties[AccountCreateFailedEventHandler.INTERNAL_ID]);
            });

            Assert.Null(ThreadContext.Properties[AccountCreateFailedEventHandler.INTERNAL_ID]);
        }