Example #1
0
        /// <exception cref="NotSupportedMessageException">Raised when message type is not supported</exception>
        public IMessageHandler CreateHandler <T>()
        {
            var configuration   = new AppConfigConfiguration();
            var csProvider      = new ConnectionStringProvider();
            var dbCommands      = new DatabaseCommands(csProvider, configuration);
            var messagingLogger = new MessagingLogger(dbCommands);

            if (typeof(T) == typeof(PriceBandUpdated))
            {
                var validator = new PriceBandUpdatedValidator(dbCommands);
                return(new PriceBandEventHandler(dbCommands, validator, messagingLogger, configuration));
            }

            if (typeof(T) == typeof(AccountCreated))
            {
                var validator = new AccountCreatedValidator(dbCommands);
                var handler   = new AccountCreatedEventHandler(dbCommands, validator, configuration, messagingLogger);
                handler.OnProcessed += SendMessage;
                return(handler);
            }

            if (typeof(T) == typeof(AccountCreateFailed))
            {
                var validator = new AccountCreateFailedValidator(dbCommands);
                return(new AccountCreateFailedEventHandler(dbCommands, validator, messagingLogger));
            }

            if (typeof(T) == typeof(AccountUpdated))
            {
                var validator = new AccountUpdatedValidator(dbCommands);
                return(new AccountUpdatedEventHandler(dbCommands, validator, messagingLogger));
            }

            if (typeof(T) == typeof(AccountUpdateFailed))
            {
                var validator = new AccountUpdateFailedValidator(dbCommands);
                return(new AccountUpdateFailedEventHandler(dbCommands, validator, messagingLogger));
            }
            if (typeof(T) == typeof(AccountStatusChanged))
            {
                var validator = new AccountStatusChangedValidator(dbCommands);
                return(new AccountStatusChangedEventHandler(dbCommands, validator, messagingLogger));
            }
            if (typeof(T) == typeof(AccountStatusChangeFailed))
            {
                var validator = new AccountStatusChangeFailedValidator(dbCommands);
                return(new AccountStatusChangeFailedEventHandler(dbCommands, validator, messagingLogger));
            }
            if (typeof(T) == typeof(FileUploadCompleted))
            {
                var validator = new FileUploadEventValidator(dbCommands);
                return(new FileUploadCompletedHandler(dbCommands, validator, messagingLogger));
            }

            throw new NotSupportedMessageException(string.Format("Message type {0} is not supported.", typeof(T).Name));
        }
Example #2
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);
        }
Example #4
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());
        }
Example #5
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 #6
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 #7
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());
        }