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);
        }
        public async Task SqlFilterParams()
        {
            var client           = new ServiceBusManagementClient(TestEnvironment.ServiceBusConnectionString);
            var topicName        = Guid.NewGuid().ToString("D").Substring(0, 8);
            var subscriptionName = Guid.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 },
                }
            };

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

            Assert.AreEqual(sqlFilter, rule.Filter);

            await client.DeleteTopicAsync(topicName);
        }
        public async Task BasicRuleCrudOperations()
        {
            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);

            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", DateTime.UtcNow);
            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",
                Label            = "label",
                MessageId        = "messageId",
                ReplyTo          = "replyTo",
                ReplyToSessionId = "replyToSessionId",
                SessionId        = "sessionId",
                To = "to"
            };

            correlationRuleFilter.Properties.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(ServiceBusException.FailureReason.MessagingEntityNotFound));

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

            Assert.False(exists);

            await client.DeleteTopicAsync(topicName);
        }