public async Task CorrelationFilterProperties()
        {
            var topicName        = Guid.NewGuid().ToString("D").Substring(0, 8);
            var subscriptionName = Guid.NewGuid().ToString("D").Substring(0, 8);
            var client           = new ServiceBusManagementClient(TestEnvironment.ServiceBusConnectionString);

            await client.CreateTopicAsync(topicName);

            await client.CreateSubscriptionAsync(topicName, subscriptionName);

            var filter = new CorrelationRuleFilter();

            filter.Properties.Add("stringKey", "stringVal");
            filter.Properties.Add("intKey", 5);
            filter.Properties.Add("dateTimeKey", DateTime.UtcNow);

            RuleProperties rule = await client.CreateRuleAsync(
                topicName,
                subscriptionName,
                new CreateRuleOptions("rule1", filter));

            Assert.True(filter.Properties.Count == 3);
            Assert.AreEqual(filter, rule.Filter);

            await client.DeleteTopicAsync(topicName);
        }
        /// <summary>
        /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription.
        /// </summary>
        ///
        /// <param name="description">The rule description that provides the rule to add.</param>
        /// <param name="timeout">The per-try timeout specified in the RetryOptions.</param>
        ///
        /// <returns>A task instance that represents the asynchronous add rule operation.</returns>
        private async Task AddRuleInternalAsync(
            RuleProperties description,
            TimeSpan timeout)
        {
            // Create an AmqpRequest Message to add rule
            var amqpRequestMessage = AmqpRequestMessage.CreateRequest(
                ManagementConstants.Operations.AddRuleOperation,
                timeout,
                null);

            amqpRequestMessage.Map[ManagementConstants.Properties.RuleName]        = description.Name;
            amqpRequestMessage.Map[ManagementConstants.Properties.RuleDescription] = AmqpMessageConverter.GetRuleDescriptionMap(description);

            AmqpResponseMessage response = await ManagementUtilities.ExecuteRequestResponseAsync(
                _connectionScope,
                _managementLink,
                amqpRequestMessage,
                null,
                timeout).ConfigureAwait(false);

            if (response.StatusCode != AmqpResponseStatusCode.OK)
            {
                throw response.ToMessagingContractException();
            }
        }
        public async Task CorrelationFilterProperties()
        {
            var topicName        = Recording.Random.NewGuid().ToString("D").Substring(0, 8);
            var subscriptionName = Recording.Random.NewGuid().ToString("D").Substring(0, 8);
            var client           = CreateClient();

            await client.CreateTopicAsync(topicName);

            await client.CreateSubscriptionAsync(topicName, subscriptionName);

            var filter = new CorrelationRuleFilter();

            filter.ApplicationProperties.Add("stringKey", "stringVal");
            filter.ApplicationProperties.Add("intKey", 5);
            filter.ApplicationProperties.Add("dateTimeKey", Recording.Now.UtcDateTime);

            RuleProperties rule = await client.CreateRuleAsync(
                topicName,
                subscriptionName,
                new CreateRuleOptions("rule1", filter));

            Assert.True(filter.ApplicationProperties.Count == 3);
            Assert.AreEqual(filter, rule.Filter);

            await client.DeleteTopicAsync(topicName);
        }
        public static AmqpMap GetRuleDescriptionMap(RuleProperties description)
        {
            var ruleDescriptionMap = new AmqpMap();

            switch (description.Filter)
            {
            case SqlRuleFilter sqlRuleFilter:
                var filterMap = GetSqlRuleFilterMap(sqlRuleFilter);
                ruleDescriptionMap[ManagementConstants.Properties.SqlRuleFilter] = filterMap;
                break;

            case CorrelationRuleFilter correlationFilter:
                var correlationFilterMap = GetCorrelationRuleFilterMap(correlationFilter);
                ruleDescriptionMap[ManagementConstants.Properties.CorrelationRuleFilter] = correlationFilterMap;
                break;

            default:
                throw new NotSupportedException(
                          Resources.RuleFilterNotSupported.FormatForUser(
                              description.Filter.GetType(),
                              nameof(SqlRuleFilter),
                              nameof(CorrelationRuleFilter)));
            }

            var amqpAction = GetRuleActionMap(description.Action as SqlRuleAction);

            ruleDescriptionMap[ManagementConstants.Properties.SqlRuleAction] = amqpAction;
            ruleDescriptionMap[ManagementConstants.Properties.RuleName]      = description.Name;

            return(ruleDescriptionMap);
        }
        /// <summary>
        /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription.
        /// </summary>
        ///
        /// <param name="description">The rule description that provides the rule to add.</param>
        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
        ///
        /// <remarks>
        /// You can add rules to the subscription that decides which messages from the topic should reach the subscription.
        /// A default <see cref="TrueRuleFilter"/> rule named <see cref="RuleProperties.DefaultRuleName"/> is always added while creation of the Subscription.
        /// You can add multiple rules with distinct names to the same subscription.
        /// Multiple filters combine with each other using logical OR condition. i.e., If any filter succeeds, the message is passed on to the subscription.
        /// </remarks>
        ///
        /// <returns>A task instance that represents the asynchronous add rule operation.</returns>
        public virtual async Task AddRuleAsync(
            RuleProperties description,
            CancellationToken cancellationToken = default)
        {
            Argument.AssertNotDisposed(IsDisposed, nameof(ServiceBusRuleManager));
            Argument.AssertNotNull(description, nameof(description));
            cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>();
            EntityNameFormatter.CheckValidRuleName(description.Name);
            ServiceBusEventSource.Log.AddRuleStart(Identifier, description.Name);

            try
            {
                await InnerRuleManager.AddRuleAsync(
                    description,
                    cancellationToken).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                ServiceBusEventSource.Log.AddRuleException(Identifier, exception.ToString());
                throw;
            }

            cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>();
            ServiceBusEventSource.Log.AddRuleComplete(Identifier);
        }
 /// <summary>
 /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription.
 /// </summary>
 ///
 /// <param name="description">The rule description that provides the rule to add.</param>
 /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
 ///
 /// <remarks>
 /// You can add rules to the subscription that decides which messages from the topic should reach the subscription.
 /// A default <see cref="TrueRuleFilter"/> rule named <see cref="RuleProperties.DefaultRuleName"/> is always added while creation of the Subscription.
 /// You can add multiple rules with distinct names to the same subscription.
 /// Multiple filters combine with each other using logical OR condition. i.e., If any filter succeeds, the message is passed on to the subscription.
 /// </remarks>
 ///
 /// <returns>A task instance that represents the asynchronous add rule operation.</returns>
 public override async Task AddRuleAsync(
     RuleProperties description,
     CancellationToken cancellationToken = default) =>
 await _retryPolicy.RunOperation(
     async (timeout) =>
     await AddRuleInternalAsync(
         description,
         timeout).ConfigureAwait(false),
     _connectionScope,
     cancellationToken).ConfigureAwait(false);
        public void CanCreateRulePropertiesFromOptions()
        {
            var options = new CreateRuleOptions("rule")
            {
                Filter = new SqlRuleFilter("PROPERTY(@propertyName) = @stringPropertyValue"),
                Action = new SqlRuleAction("SET a='b'")
            };
            var properties = new RuleProperties(options);

            Assert.AreEqual(options, new CreateRuleOptions(properties));
        }
        public static RuleProperties GetRuleDescription(AmqpRuleDescriptionCodec amqpDescription)
        {
            var filter     = GetFilter(amqpDescription.Filter);
            var ruleAction = GetRuleAction(amqpDescription.Action);

            var ruleDescription = new RuleProperties(amqpDescription.RuleName, filter)
            {
                Action = ruleAction
            };

            return(ruleDescription);
        }
Esempio n. 9
0
        public void CanCreateRulePropertiesWithCorrelationFilterFromOptions()
        {
            var options = new CreateRuleOptions("rule")
            {
                Filter = new CorrelationRuleFilter
                {
                    ApplicationProperties =
                    {
                        { "propertyName", "value" }
                    }
                },
                Action = new SqlRuleAction("SET a='b'")
            };
            var properties = new RuleProperties(options);

            Assert.AreEqual(options, new CreateRuleOptions(properties));
        }
        protected override void AddRuleProperties()
        {
            RenderScale = Rule.Add(d => d.RenderScale);
            MsaaCount   = Rule.Add(d => d.MsaaCount);
            Vsync       = Rule.Add(d => d.Vsync);

            RenderScale.OnVerify += (ref float value) =>
            {
                value = math.clamp(value, 0.1f, 2);
                return(true);
            };
            MsaaCount.OnVerify += (ref int value) =>
            {
                value = math.clamp(value, 1, 4);
                return(true);
            };

            Rule.OnPropertyChanged += OnHandler;
        }
        public async Task SqlFilterParams()
        {
            var client           = CreateClient();
            var topicName        = Recording.Random.NewGuid().ToString("D").Substring(0, 8);
            var subscriptionName = Recording.Random.NewGuid().ToString("D").Substring(0, 8);

            await client.CreateTopicAsync(topicName);

            await client.CreateSubscriptionAsync(topicName, subscriptionName);

            SqlRuleFilter sqlFilter = new SqlRuleFilter(
                "PROPERTY(@propertyName) = @stringPropertyValue " +
                "AND PROPERTY(intProperty) = @intPropertyValue " +
                "AND PROPERTY(longProperty) = @longPropertyValue " +
                "AND PROPERTY(boolProperty) = @boolPropertyValue " +
                "AND PROPERTY(doubleProperty) = @doublePropertyValue ")
            {
                Parameters =
                {
                    { "@propertyName",        "MyProperty" },
                    { "@stringPropertyValue", "string"     },
                    { "@intPropertyValue",               3 },
                    { "@longPropertyValue",             3L },
                    { "@boolPropertyValue",   true         },
                    { "@doublePropertyValue",          3.0 },
                }
            };

            RuleProperties rule = await client.CreateRuleAsync(
                topicName,
                subscriptionName,
                new CreateRuleOptions("rule1", sqlFilter));

            Assert.AreEqual(sqlFilter, rule.Filter);

            await client.DeleteTopicAsync(topicName);
        }
        public async Task AddGetAndRemoveRules()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);

                ServiceBusRuleManager ruleManager = client.CreateRuleManager(scope.TopicName, scope.SubscriptionNames.First());
                var sqlRuleName         = "sqlRule";
                var correlationRuleName = "correlationRule";

                var rules = (await ruleManager.GetRulesAsync()).ToList();
                Assert.AreEqual(1, rules.Count());
                var firstRule = rules[0];
                Assert.AreEqual(RuleProperties.DefaultRuleName, firstRule.Name);
                Assert.Null(firstRule.Action);

                await ruleManager.AddRuleAsync(sqlRuleName, new SqlRuleFilter("price > 10"));

                var ruleDescription = new RuleProperties(correlationRuleName)
                {
                    Filter = new CorrelationRuleFilter
                    {
                        CorrelationId         = "correlationId",
                        Subject               = "label",
                        MessageId             = "messageId",
                        ApplicationProperties =
                        {
                            { "key1", "value1" }
                        },
                        ReplyTo          = "replyTo",
                        ReplyToSessionId = "replyToSessionId",
                        SessionId        = "sessionId",
                        To = "to"
                    },
                    Action = new SqlRuleAction("Set CorrelationId = 'newValue'")
                };
                await ruleManager.AddRuleAsync(ruleDescription);

                rules = (await ruleManager.GetRulesAsync()).ToList();
                Assert.AreEqual(3, rules.Count);

                var sqlRule = rules.FirstOrDefault(rule => rule.Name.Equals(sqlRuleName));
                Assert.NotNull(sqlRule);
                Assert.Null(sqlRule.Action);
                Assert.IsInstanceOf <SqlRuleFilter>(sqlRule.Filter);
                Assert.AreEqual("price > 10", ((SqlRuleFilter)sqlRule.Filter).SqlExpression);

                var correlationRule = rules.FirstOrDefault(rule => rule.Name.Equals(correlationRuleName));
                Assert.NotNull(correlationRule);
                Assert.IsInstanceOf <SqlRuleAction>(correlationRule.Action);
                var sqlRuleAction = correlationRule.Action as SqlRuleAction;
                Assert.NotNull(sqlRuleAction);
                Assert.AreEqual("Set CorrelationId = 'newValue'", sqlRuleAction.SqlExpression);
                Assert.IsInstanceOf <CorrelationRuleFilter>(correlationRule.Filter);
                var correlationRuleFilter = correlationRule.Filter as CorrelationRuleFilter;
                Assert.NotNull(correlationRuleFilter);
                Assert.AreEqual("correlationId", correlationRuleFilter.CorrelationId);
                Assert.AreEqual("label", correlationRuleFilter.Subject);
                Assert.AreEqual("messageId", correlationRuleFilter.MessageId);
                Assert.AreEqual("replyTo", correlationRuleFilter.ReplyTo);
                Assert.AreEqual("replyToSessionId", correlationRuleFilter.ReplyToSessionId);
                Assert.AreEqual("sessionId", correlationRuleFilter.SessionId);
                Assert.AreEqual("to", correlationRuleFilter.To);
                Assert.NotNull(correlationRuleFilter.ApplicationProperties);
                Assert.AreEqual("value1", correlationRuleFilter.ApplicationProperties["key1"]);

                await ruleManager.RemoveRuleAsync(RuleProperties.DefaultRuleName);

                await ruleManager.RemoveRuleAsync(sqlRuleName);

                await ruleManager.RemoveRuleAsync(correlationRuleName);

                rules = (await ruleManager.GetRulesAsync()).ToList();
                Assert.AreEqual(0, rules.Count());
            }
        }
Esempio n. 13
0
 /// <summary>
 /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription.
 /// </summary>
 ///
 /// <param name="properties">The rule properties for the rule to add.</param>
 /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
 ///
 /// <returns>A task instance that represents the asynchronous add rule operation.</returns>
 public abstract Task AddRuleAsync(
     RuleProperties properties,
     CancellationToken cancellationToken);
Esempio n. 14
0
 protected override void AddRuleProperties()
 {
     UnitNoTeamColor = Rule.Add(d => d.UnitNoTeamColor);
     UnitOwnedColor  = Rule.Add(d => d.UnitOwnedColor);
 }
 /// <summary>
 /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription.
 /// </summary>
 ///
 /// <param name="properties">The rule description that provides the rule to add.</param>
 /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
 ///
 /// <remarks>
 /// You can add rules to the subscription that decides which messages from the topic should reach the subscription.
 /// A default <see cref="TrueRuleFilter"/> rule named <see cref="RuleProperties.DefaultRuleName"/> is always added while creation of the Subscription.
 /// You can add multiple rules with distinct names to the same subscription.
 /// Multiple filters combine with each other using logical OR condition. i.e., If any filter succeeds, the message is passed on to the subscription.
 /// </remarks>
 ///
 /// <returns>A task instance that represents the asynchronous add rule operation.</returns>
 public override async Task AddRuleAsync(
     RuleProperties properties,
     CancellationToken cancellationToken) =>
 await _retryPolicy.RunOperation(
Esempio n. 16
0
 protected override void AddRuleProperties()
 {
     EnableDrumVoices = Rule.Add(null, d => d.EnableDrumVoices);
 }
Esempio n. 17
0
 private static bool ExistingRuleIsValid(List <CreateRuleOptions> newRules, RuleProperties existingRule) =>
 newRules.Any(r => RuleIsEqual(existingRule, r));
        public async Task BasicRuleCrudOperations()
        {
            var topicName        = Recording.Random.NewGuid().ToString("D").Substring(0, 8);
            var subscriptionName = Recording.Random.NewGuid().ToString("D").Substring(0, 8);
            var client           = CreateClient();
            await client.CreateTopicAsync(topicName);

            var rule1 = new CreateRuleOptions
            {
                Filter = new TrueRuleFilter(),
                Name   = "rule1"
            };
            await client.CreateSubscriptionAsync(
                new CreateSubscriptionOptions(topicName, subscriptionName),
                rule1);

            RuleProperties getRule1 = await client.GetRuleAsync(topicName, subscriptionName, "rule1");

            Assert.AreEqual(rule1, new CreateRuleOptions(getRule1));

            var sqlRuleFilter = new SqlRuleFilter("stringValue = @stringParam AND intValue = @intParam AND longValue = @longParam AND dateValue = @dateParam AND timeSpanValue = @timeSpanParam");

            sqlRuleFilter.Parameters.Add("@stringParam", "string");
            sqlRuleFilter.Parameters.Add("@intParam", 1);
            sqlRuleFilter.Parameters.Add("@longParam", (long)12);
            sqlRuleFilter.Parameters.Add("@dateParam", Recording.Now.UtcDateTime);
            sqlRuleFilter.Parameters.Add("@timeSpanParam", TimeSpan.FromDays(1));
            var rule2 = new CreateRuleOptions
            {
                Name   = "rule2",
                Filter = sqlRuleFilter,
                Action = new SqlRuleAction("SET a='b'")
            };
            await client.CreateRuleAsync(topicName, subscriptionName, rule2);

            RuleProperties getRule2 = await client.GetRuleAsync(topicName, subscriptionName, "rule2");

            Assert.AreEqual(rule2, new CreateRuleOptions(getRule2));

            var correlationRuleFilter = new CorrelationRuleFilter()
            {
                ContentType      = "contentType",
                CorrelationId    = "correlationId",
                Subject          = "label",
                MessageId        = "messageId",
                ReplyTo          = "replyTo",
                ReplyToSessionId = "replyToSessionId",
                SessionId        = "sessionId",
                To = "to"
            };

            correlationRuleFilter.ApplicationProperties.Add("customKey", "customValue");
            var rule3 = new CreateRuleOptions()
            {
                Name   = "rule3",
                Filter = correlationRuleFilter,
                Action = null
            };
            await client.CreateRuleAsync(topicName, subscriptionName, rule3);

            RuleProperties getRule3 = await client.GetRuleAsync(topicName, subscriptionName, "rule3");

            Assert.AreEqual(rule3, new CreateRuleOptions(getRule3));

            List <RuleProperties> ruleList = new List <RuleProperties>();

            await foreach (RuleProperties rule in client.GetRulesAsync(topicName, subscriptionName))
            {
                ruleList.Add(rule);
            }
            RuleProperties[] ruleArr = ruleList.ToArray();
            Assert.True(ruleArr.Length == 3);
            Assert.AreEqual(rule1, new CreateRuleOptions(ruleArr[0]));
            Assert.AreEqual(rule2, new CreateRuleOptions(ruleArr[1]));
            Assert.AreEqual(rule3, new CreateRuleOptions(ruleArr[2]));

            ((CorrelationRuleFilter)getRule3.Filter).CorrelationId = "correlationIdModified";
            SubscriptionProperties sub = await client.GetSubscriptionAsync(topicName, subscriptionName);

            RuleProperties updatedRule3 = await client.UpdateRuleAsync(topicName, subscriptionName, getRule3);

            Assert.AreEqual(getRule3, updatedRule3);

            bool exists = await client.RuleExistsAsync(topicName, subscriptionName, rule1.Name);

            Assert.True(exists);

            await client.DeleteRuleAsync(topicName, subscriptionName, "rule1");

            Assert.That(
                async() =>
                await client.GetRuleAsync(topicName, subscriptionName, "rule1"),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusFailureReason.MessagingEntityNotFound));

            exists = await client.RuleExistsAsync(topicName, subscriptionName, rule1.Name);

            Assert.False(exists);

            await client.DeleteTopicAsync(topicName);
        }
Esempio n. 19
0
 private static bool RuleIsEqual(RuleProperties ruleProperties, CreateRuleOptions ruleOptions)
 => ruleOptions.Name == ruleProperties.Name &&
 (CorrelationRuleFilter)ruleOptions.Filter == (CorrelationRuleFilter)ruleProperties.Filter;
 /// <summary>
 /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription.
 /// </summary>
 ///
 /// <param name="description">The rule description that provides the rule to add.</param>
 /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
 ///
 /// <returns>A task instance that represents the asynchronous add rule operation.</returns>
 public abstract Task AddRuleAsync(
     RuleProperties description,
     CancellationToken cancellationToken);
Esempio n. 21
0
        private static VendorNameCoolDebugger CreateVendorNameCoolDebuggerProperties(System.Threading.Tasks.Task <System.Collections.Immutable.IImmutableDictionary <string, Microsoft.VisualStudio.ProjectSystem.Properties.IPropertyPagesCatalog> > namedCatalogs, object state)
        {
            RuleProperties that = ((RuleProperties)(state));

            return(new VendorNameCoolDebugger(that.ConfiguredProject, namedCatalogs.Result, "Project", that.File, that.ItemType, that.ItemName));
        }