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_create_a_new_user()
        {
            const int    LOGIN_ID      = 123;
            const string USERNAME      = "******";
            const string FIRST_NAME    = "first_name";
            const string LAST_NAME     = "last_name";
            const string EMAIL_ADDRESS = "email";
            var          externalId    = Guid.NewGuid().ToString();
            var          builder       = AccountCreated.CreateBuilder();

            builder
            .SetInternalId(LOGIN_ID.ToString())
            .SetUsername(USERNAME)
            .SetFirstName(FIRST_NAME)
            .SetLastName(LAST_NAME)
            .SetEmailAddress(EMAIL_ADDRESS)
            .SetSource(SourceSystem.STARCHEF)
            .SetExternalId(externalId);
            var payload = builder.Build();

            var validator = new Mock <IEventValidator>();

            validator.Setup(m => m.IsAllowedEvent(It.IsAny <object>())).Returns(true);
            validator.Setup(m => m.IsValidPayload(It.IsAny <object>())).Returns(true);

            var messagingLogger = new Mock <IMessagingLogger>();

            var dbCommands = new Mock <IDatabaseCommands>();

            dbCommands.Setup(m => m.IsUserExists(It.IsAny <int?>(), It.IsAny <string>(), It.IsAny <string>())).Returns(Task.FromResult(false)); // user is not exists

            var config  = new Mock <IConfiguration>();
            var handler = new AccountCreatedEventHandler(dbCommands.Object, validator.Object, config.Object, messagingLogger.Object);

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

            // assertions
            Assert.Equal(MessageHandlerResult.Success, result);
            // while check loginId AND loginName are used
            dbCommands.Verify(m => m.IsUserExists(It.Is <int?>(p => p.Value == LOGIN_ID), It.Is <string>(p => p == null), It.Is <string>(p => p == USERNAME)), Times.Once);
            dbCommands.Verify(m => m.AddUser(It.Is <AccountCreatedTransferObject>(p =>
                                                                                  p.EmailAddress == EMAIL_ADDRESS &&
                                                                                  p.Username == USERNAME &&
                                                                                  p.FirstName == FIRST_NAME &&
                                                                                  p.LastName == LAST_NAME &&
                                                                                  p.ExternalLoginId == externalId
                                                                                  )), Times.Once);
        }
        public void Should_log_listener_exceptions_and_have_correct_internal_id()
        {
            // arrange
            var builder = AccountCreated.CreateBuilder();

            builder
            .SetInternalId("1")
            .SetFirstName("1")
            .SetLastName("1")
            .SetEmailAddress("1")
            .SetSource(SourceSystem.STARCHEF)
            .SetExternalId(Guid.Empty.ToString());
            var payload = builder.Build();

            var dbCommands = new Mock <IDatabaseCommands>();

            dbCommands.Setup(d => d.IsUserExists(It.IsAny <int?>(), It.IsAny <string>(), It.IsAny <string>())).Throws(new ListenerException());
            var validator = new Mock <IEventValidator>(MockBehavior.Strict);

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

            validator.Setup(m => m.IsValidPayload(It.IsAny <object>())).Returns(true);
            var messagingLogger = new Mock <IMessagingLogger>();
            var config          = new Mock <IConfiguration>();
            var logChecker      = new LogChecker(typeof(AccountCreatedEventHandler), Level.All);
            var handler         = new AccountCreatedEventHandler(dbCommands.Object, validator.Object, config.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[AccountCreatedEventHandler.INTERNAL_ID]);
            });

            Assert.NotNull(
                messageList.Where(
                    item =>
                    item.ExceptionObject != null &&
                    item.ExceptionObject.GetType() == typeof(ListenerException)).FirstOrDefault()
                );

            Assert.Null(ThreadContext.Properties[AccountCreatedEventHandler.INTERNAL_ID]);
        }
Example #4
0
        public void Should_return_false_when_InternalId_missing()
        {
            var builder = AccountCreated.CreateBuilder();

            builder
            .SetFirstName("1")
            .SetLastName("1")
            .SetEmailAddress("1")
            .SetExternalId("1");
            builder.AddPermissionSets("Star_Chef");
            var accountCreated = builder.Build();

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

            Assert.False(actual);
        }
        public void Should_updated_valid_data_and_log_to_messaging_events2()
        {
            string internalId     = Guid.NewGuid().ToString();
            int    creted_loginId = 15;
            string USERNAME       = "******";
            string FIRST_NAME     = "first_name";
            string LAST_NAME      = "last_name";
            string EMAIL_ADDRESS  = "email";
            var    externalId     = Guid.NewGuid().ToString();
            var    builder        = AccountCreated.CreateBuilder();

            builder
            .SetInternalId(internalId)
            .SetUsername(USERNAME)
            .SetFirstName(FIRST_NAME)
            .SetLastName(LAST_NAME)
            .SetEmailAddress(EMAIL_ADDRESS)
            .SetSource(SourceSystem.USER_MANAGEMENT)
            .SetExternalId(externalId);

            builder.AddPermissionSets("Star_Chef");

            var payload = builder.Build();

            var dbCommands = new Mock <IDatabaseCommands>();

            dbCommands.Setup(m => m.IsUserExists(It.IsAny <int?>(), It.IsAny <string>(), It.IsAny <string>())).Returns(Task.FromResult(false));
            dbCommands.Setup(m => m.AddUser(It.IsAny <AccountCreatedTransferObject>())).Returns(Task.FromResult(creted_loginId));

            var validator       = new AccountCreatedValidator(dbCommands.Object);
            var messagingLogger = new Mock <IMessagingLogger>();
            var config          = new Mock <IConfiguration>();
            var handler         = new AccountCreatedEventHandler(dbCommands.Object, validator, config.Object, messagingLogger.Object);

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

            // assertions
            Assert.Equal(MessageHandlerResult.Success, result);
            dbCommands.Verify(m => m.AddUser(It.Is <AccountCreatedTransferObject>(p =>
                                                                                  p.InternalLoginId == creted_loginId &&
                                                                                  p.InternalId.Equals(internalId, StringComparison.CurrentCultureIgnoreCase) &&
                                                                                  p.ExternalLoginId == externalId
                                                                                  )), Times.Once);
            messagingLogger.Verify(m => m.MessageProcessedSuccessfully(It.Is <object>(p => ReferenceEquals(p, payload)), It.IsAny <string>()), Times.Once);
        }
        public void Should_correctly_convert_AccountCreated_to_json()
        {
            var builder = AccountCreated.CreateBuilder();

            builder
            .SetInternalId("1")
            .SetFirstName("1")
            .SetLastName("1")
            .SetEmailAddress("1")
            .SetSource(SourceSystem.STARCHEF)
            .SetExternalId(Guid.Empty.ToString());
            var payload = builder.Build();

            var          actual   = MessageEventsExtensions.ToJson(payload);
            const string expected = @"{""ExternalId"":""00000000-0000-0000-0000-000000000000"",""InternalId"":""1"",""EmailAddress"":""1"",""FirstName"":""1"",""LastName"":""1"",""Source"":""STARCHEF""}";

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

            builder.SetExternalId("mandatory")
            .SetInternalId(MORE_THAN_INT.ToString());

            builder.AddPermissionSets("Star_Chef");
            builder.SetSource(Fourth.Orchestration.Model.Common.SourceSystemId.STARCHEF);
            var payload = builder.Build();

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

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

            builder
            .SetInternalId("1")
            .SetFirstName("1")
            .SetLastName("1")
            .SetEmailAddress("1")
            .SetSource(SourceSystem.STARCHEF)
            .SetExternalId(Guid.Empty.ToString());
            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 config          = new Mock <IConfiguration>();
            var logChecker      = new LogChecker(typeof(AccountCreatedEventHandler), Level.All);
            var handler         = new AccountCreatedEventHandler(dbCommands.Object, validator.Object, config.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[AccountCreatedEventHandler.INTERNAL_ID]);
            });

            Assert.Null(ThreadContext.Properties[AccountCreatedEventHandler.INTERNAL_ID]);
        }
Example #9
0
        public void Should_catch_nonInt_IntenalId2()
        {
            var builder = AccountCreated.CreateBuilder();

            builder
            .SetInternalId("1sdsdsd")
            .SetUsername("1")
            .SetFirstName("1")
            .SetLastName("1")
            .SetEmailAddress("1")
            .SetExternalId("1");
            builder.AddPermissionSets("Star_Chef");
            builder.SetSource(Fourth.Orchestration.Model.Common.SourceSystemId.USER_MANAGEMENT);
            var payload = builder.Build();

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

            Assert.True(result);
            //Assert.Equal("InternalId is not Int32: " + MORE_THAN_INT, validator.GetErrors());
        }
Example #10
0
        public void Should_return_false_when_PermissionSets_donot_contain_StarchefOrMenuCycle()
        {
            var builder = AccountCreated.CreateBuilder();

            builder
            .SetUsername("test")
            .SetInternalId("1")
            .SetFirstName("any")
            .SetLastName("any")
            .SetEmailAddress("any")
            .SetExternalId("any");

            builder.AddPermissionSets("Star_Char");

            var accountCreated = builder.Build();

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

            Assert.False(actual);
            Assert.Equal("PermissionSets don't contain Star_Chef or Menu_Cycles set.", validator.GetErrors());
        }
Example #11
0
        public void Should_return_false_when_Username_too_long()
        {
            var       builder    = AccountCreated.CreateBuilder();
            const int MAX_LENGTH = 50;
            var       value      = Enumerable.Repeat("a", MAX_LENGTH + 1).Aggregate((a, b) => a + b);

            builder
            .SetUsername(value)
            .SetInternalId("1")
            .SetFirstName("any")
            .SetLastName("any")
            .SetEmailAddress("any")
            .SetExternalId("any");
            builder.AddPermissionSets("Star_Chef");
            var accountCreated = builder.Build();

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

            Assert.False(actual);
            Assert.Equal("Username exceeds the maximum length of 50 characters.", validator.GetErrors());
        }
        public void Should_not_have_log_for_non_starchef_events()
        {
            // arrange
            var builder = AccountCreated.CreateBuilder();

            builder
            .SetInternalId("1")
            .SetFirstName("1")
            .SetLastName("1")
            .SetEmailAddress("1")
            .SetSource(SourceSystem.ADACO)
            .SetExternalId(Guid.Empty.ToString());
            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 config          = new Mock <IConfiguration>();
            var logChecker      = new LogChecker(typeof(AccountCreatedEventHandler), Level.All);
            var handler         = new AccountCreatedEventHandler(dbCommands.Object, validator.Object, config.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[AccountCreatedEventHandler.INTERNAL_ID]);
        }