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);
        }
        internal static AmqpMap GetSqlRuleFilterMap(SqlRuleFilter sqlRuleFilter)
        {
            var amqpFilterMap = new AmqpMap
            {
                [ManagementConstants.Properties.Expression] = sqlRuleFilter.SqlExpression
            };

            return(amqpFilterMap);
        }
        public void CanCreateRulePropertiesFromFactory()
        {
            var filter     = new SqlRuleFilter("PROPERTY(@propertyName) = @stringPropertyValue");
            var action     = new SqlRuleAction("SET a='b'");
            var properties = ServiceBusModelFactory.RuleProperties(
                "ruleName",
                filter,
                action);

            Assert.AreEqual("ruleName", properties.Name);
            Assert.AreEqual(filter, properties.Filter);
            Assert.AreEqual(action, properties.Action);
        }
        public static RuleFilter GetFilter(AmqpRuleFilterCodec amqpFilter)
        {
            RuleFilter filter;

            switch (amqpFilter.DescriptorCode)
            {
            case AmqpSqlRuleFilterCodec.Code:
                var amqpSqlFilter = (AmqpSqlRuleFilterCodec)amqpFilter;
                filter = new SqlRuleFilter(amqpSqlFilter.Expression);
                break;

            case AmqpTrueRuleFilterCodec.Code:
                filter = new TrueRuleFilter();
                break;

            case AmqpFalseRuleFilterCodec.Code:
                filter = new FalseRuleFilter();
                break;

            case AmqpCorrelationRuleFilterCodec.Code:
                var amqpCorrelationFilter = (AmqpCorrelationRuleFilterCodec)amqpFilter;
                var correlationFilter     = new CorrelationRuleFilter
                {
                    CorrelationId    = amqpCorrelationFilter.CorrelationId,
                    MessageId        = amqpCorrelationFilter.MessageId,
                    To               = amqpCorrelationFilter.To,
                    ReplyTo          = amqpCorrelationFilter.ReplyTo,
                    Subject          = amqpCorrelationFilter.Subject,
                    SessionId        = amqpCorrelationFilter.SessionId,
                    ReplyToSessionId = amqpCorrelationFilter.ReplyToSessionId,
                    ContentType      = amqpCorrelationFilter.ContentType
                };

                foreach (var property in amqpCorrelationFilter.Properties)
                {
                    correlationFilter.ApplicationProperties.Add(property.Key.Key.ToString(), property.Value);
                }

                filter = correlationFilter;
                break;

            default:
                throw new NotSupportedException($"Unknown filter descriptor code: {amqpFilter.DescriptorCode}");
            }

            return(filter);
        }
Exemplo n.º 5
0
        public static async Task CreateSubscription(string connectionString,
                                                    string topic,
                                                    string subscription,
                                                    string subscriptionFilter)
        {
            CheckIsNotNullOrWhitespace(nameof(subscriptionFilter), subscriptionFilter);

            Debug.WriteLine($"CHECK: {subscriptionFilter}");

            string filterClause = $"{SUBSCRIPTION_FILTER} IN ('{subscriptionFilter.ToLower()}')";

            var client = new ServiceBusAdministrationClient(connectionString);

            await CreateSubscription(client, topic, subscription);

            await client.DeleteRuleAsync(topic, subscription, CreateRuleOptions.DefaultRuleName);

            var rule = new SqlRuleFilter(filterClause);

            await client.CreateRuleAsync(topic, subscription, new CreateRuleOptions(SUBSCRIPTION_FILTER, rule));
        }
        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 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);
        }