コード例 #1
0
 private bool Send(
     EnumHelper.EntityTypeWrapper entityTypeWrapper,
     string dbConnectionString,
     int entityTypeId,
     int entityId,
     int databaseId,
     DateTime messageArrivedTime
     )
 {
     return(Send(entityTypeWrapper, dbConnectionString, entityTypeId, entityTypeId, string.Empty, databaseId, messageArrivedTime));
 }
コード例 #2
0
        public IList <KeyValuePair <Tuple <int, int>, bool> > Send(
            EnumHelper.EntityTypeWrapper entityTypeWrapper,
            string dbConnectionString,
            int entityTypeId,
            List <int> entityIds,
            string entityExternalId,
            int databaseId,
            DateTime messageArrivedTime
            )
        {
            var result = new List <KeyValuePair <Tuple <int, int>, bool> >();

            Parallel.ForEach(entityIds, entityId =>
            {
                var keyValuePaid = new KeyValuePair <Tuple <int, int>, bool>(Tuple.Create(entityId, entityTypeId), Send(entityTypeWrapper, dbConnectionString, entityTypeId, entityId, entityExternalId, databaseId, messageArrivedTime));
                result.Add(keyValuePaid);
            });

            return(result);
        }
コード例 #3
0
        public void Should_not_send_to_orchestration_if_not_configured_for_entityType(int entityTypeId)
        {
            // mock bus interface to verify Send method later
            var bus = new Mock <IMessageBus>();
            // mock the factory to ensure it returns the mocked version of the bus
            var messagingFactory = new Mock <IMessagingFactory>();

            messagingFactory.Setup(m => m.CreateMessageBus()).Returns(bus.Object);
            // use real factory to create an actual command
            var commandFactory  = new CommandFactory(new DeactivateAccountSetter());
            var databaseManager = new Mock <IDatabaseManager>();

            databaseManager.Setup(m => m.IsPublishEnabled(It.IsAny <string>(), It.IsAny <int>())).Returns(false);
            var sender = new StarChefMessageSender(messagingFactory.Object, databaseManager.Object, Mock.Of <IEventFactory>(), commandFactory);
            // the message which is received from MSMQ
            const int    ANY_INT  = 0;
            const string ANY_TEXT = "any";
            const EnumHelper.EntityTypeWrapper ANY_WRAPPER = EnumHelper.EntityTypeWrapper.Ingredient;

            sender.Send(ANY_WRAPPER, ANY_TEXT, entityTypeId, ANY_INT, ANY_TEXT, ANY_INT, DateTime.UtcNow);

            // verify that the command is passed to the Send method of the bus and it has correct values in properties
            bus.Verify(m => m.Send(It.IsAny <IMessage>()), Times.Never);
        }
コード例 #4
0
        public void Should_send_events_when_sso_related_commands_disabled(int entityTypeId, EnumHelper.EntityTypeWrapper entityTypeWrapper)
        {
            // mock bus interface to verify Send method later
            var bus = new Mock <IMessageBus>();
            // mock the factory to ensure it returns the mocked version of the bus
            var messagingFactory = new Mock <IMessagingFactory>();

            messagingFactory.Setup(m => m.CreateMessageBus()).Returns(bus.Object);
            // use real factory to create an actual command
            var commandFactory  = new CommandFactory(new DeactivateAccountSetter());
            var databaseManager = new Mock <IDatabaseManager>();

            databaseManager.Setup(m => m.IsSsoEnabled(It.IsAny <string>())).Returns(false);
            databaseManager.Setup(m => m.IsPublishEnabled(It.IsAny <string>(), It.IsAny <int>())).Returns(true);

            IEventFactory eventFactory = Mock.Of <IEventFactory>();

            switch (entityTypeWrapper)
            {
            case EnumHelper.EntityTypeWrapper.Recipe:
            case EnumHelper.EntityTypeWrapper.Ingredient:
                databaseManager.Setup(m => m.IsSetOrchestrationSentDate(It.IsAny <string>(), It.IsAny <int>())).Returns(true);
                break;
            }

            var sender = new StarChefMessageSender(messagingFactory.Object, databaseManager.Object, eventFactory, commandFactory);
            // the message which is received from MSMQ
            const int    ANY_INT  = 0;
            const string ANY_TEXT = "any";

            sender.Send(entityTypeWrapper, ANY_TEXT, entityTypeId, ANY_INT, ANY_TEXT, ANY_INT, DateTime.UtcNow);

            // verify that the command is passed to the Send method of the bus and it has correct values in properties
            bus.Verify(m => m.Send(It.IsAny <IMessage>()), Times.Never);
            bus.Verify(m => m.Publish(It.IsAny <IMessage>()), Times.Once);
        }
コード例 #5
0
        public bool Send(
            EnumHelper.EntityTypeWrapper entityTypeWrapper,
            string dbConnectionString,
            int entityTypeId,
            int entityId,
            string entityExternalId,
            int databaseId,
            DateTime messageArrivedTime
            )
        {
            var result = false;

            var logged = false;

            messageArrivedTime = TimeZoneInfo.ConvertTimeToUtc(messageArrivedTime);

            try
            {
                var isSsoEnabled = _databaseManager.IsSsoEnabled(dbConnectionString);

                if (_databaseManager.IsPublishEnabled(dbConnectionString, entityTypeId))
                {
                    using (IMessageBus bus = _messagingFactory.CreateMessageBus())
                    {
                        // Create an event payload
                        switch (entityTypeWrapper)
                        {
                        case EnumHelper.EntityTypeWrapper.Recipe:
                        {
                            _logger.Debug("enter createEventUpdate");
                            var payload = _eventFactory.CreateUpdateEvent <RecipeUpdated, RecipeUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            _logger.Debug("exit createEventUpdate");

                            var isSetOrchestrationSentDate = _databaseManager.IsSetOrchestrationSentDate(dbConnectionString, entityId);

                            if (isSetOrchestrationSentDate || payload.ChangeType == Events.ChangeType.UPDATE)
                            {
                                result = Publish(bus, payload);

                                _logger.Debug("exit publish recipe");

                                if (result)
                                {
                                    _databaseManager.UpdateOrchestrationSentDate(dbConnectionString, entityId);
                                }
                            }
                            else
                            {
                                result = true;

                                _logger.Warn("RecipeUpdated message was not published, because OrchestrationSentDate is not set or ChangeType is Archive");
                            }
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.MealPeriod:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <MealPeriodUpdated, MealPeriodUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.Group:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <GroupUpdated, GroupUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.User:
                        {
                            if (isSsoEnabled)
                            {
                                IMessage messagePayload;

                                if (string.IsNullOrEmpty(entityExternalId))
                                {
                                    messagePayload = CommandFactory.CreateAccountCommand(dbConnectionString, entityId, databaseId);
                                }
                                else
                                {
                                    messagePayload = CommandFactory.UpdateAccountCommand(dbConnectionString, entityId, databaseId);
                                }

                                result = Send(bus, messagePayload);
                            }
                            else
                            {
                                _logger.Warn("SSO not enabled");
                                result = true;
                            }
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.UserActivated:
                        {
                            if (isSsoEnabled)
                            {
                                var userCommandAccountActivated = CommandFactory.ActivateAccountCommand(entityId, databaseId);
                                result = Send(bus, userCommandAccountActivated);
                            }
                            else
                            {
                                _logger.Warn("SSO not enabled");
                                result = true;
                            }
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.SendUserUpdatedEvent:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <UserUpdated, UserUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.SendUserUpdatedEventAndCommand:
                        {
                            var userCreatedEventPayload = _eventFactory.CreateUpdateEvent <UserUpdated, UserUpdatedBuilder>(dbConnectionString, entityId, databaseId);

                            result = Publish(bus, userCreatedEventPayload);
                            if (isSsoEnabled)
                            {
                                IMessage messagePayload;

                                if (string.IsNullOrEmpty(entityExternalId))
                                {
                                    messagePayload = CommandFactory.CreateAccountCommand(dbConnectionString, entityId, databaseId);
                                }
                                else
                                {
                                    messagePayload = CommandFactory.UpdateAccountCommand(dbConnectionString, entityId, databaseId);
                                }

                                result = result && Send(bus, messagePayload);
                            }
                            break;
                        }

                        case EnumHelper.EntityTypeWrapper.UserGroup:
                        {
                            var userIds = _databaseManager.GetUsersInGroup(dbConnectionString, entityId);
                            if (userIds != null && userIds.Any())
                            {
                                foreach (var userId in userIds)
                                {
                                    var payload = _eventFactory.CreateUpdateEvent <UserUpdated, UserUpdatedBuilder>(dbConnectionString, userId, databaseId);
                                    result = Publish(bus, payload);
                                    LogDatabase(dbConnectionString, entityTypeId, entityId, messageArrivedTime, result);
                                    logged = true;
                                }
                            }
                            else
                            {
                                result = true;
                                _logger.Warn($"No users for usergroupId {entityId}");
                            }
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.Menu:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <MenuUpdated, MenuUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.Ingredient:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <IngredientUpdated, IngredientUpdatedBuilder>(dbConnectionString, entityId, databaseId);

                            var isSetOrchestrationSentDate = _databaseManager.IsSetOrchestrationSentDate(dbConnectionString, entityId);

                            if (isSetOrchestrationSentDate || payload.ChangeType == Events.ChangeType.UPDATE)
                            {
                                result = Publish(bus, payload);

                                if (result)
                                {
                                    _databaseManager.UpdateOrchestrationSentDate(dbConnectionString, entityId);
                                }
                            }
                            else
                            {
                                result = true;

                                _logger.Warn("IngredientUpdated message was not published, because OrchestrationSentDate is not set or ChangeType is Archive");
                            }
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.SendSupplierUpdatedEvent:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <SupplierUpdated, SupplierUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.ProductSet:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <SetUpdated, SetUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.ProductNutrition:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <RecepiNutritionUpdated, RecepiNutritionUpdateBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        default:
                            throw new NotSupportedException();
                        }
                    }
                }

                if (!logged)
                {
                    _logger.Debug("enter LogDatabase");
                    LogDatabase(dbConnectionString,
                                entityTypeId,
                                entityId,
                                messageArrivedTime,
                                result);
                    _logger.Debug("exit LogDatabase");
                }
            }
            catch (Exception ex)
            {
                _logger.Fatal("StarChef MSMQService Orchestrate failed to send to Orchestration in Send.", ex);
            }
            _logger.Debug("Finish Send");
            return(result);
        }