Search in sources :

Example 1 with MessagingEntityAlreadyExistsException

use of com.microsoft.azure.servicebus.primitives.MessagingEntityAlreadyExistsException in project azure-service-bus-java by Azure.

the class SubscriptionClientTests method testGetAddRemoveRules.

@Test
public void testGetAddRemoveRules() throws InterruptedException, ServiceBusException {
    this.createClients(ReceiveMode.PEEKLOCK);
    SubscriptionClient subscriptionClient = (SubscriptionClient) this.receiveClient;
    subscriptionClient.removeRule(SubscriptionClient.DEFAULT_RULE_NAME);
    RuleDescription[] rules = subscriptionClient.getRules().toArray(new RuleDescription[0]);
    Assert.assertEquals(0, rules.length);
    // Simple rule
    RuleDescription trueFilterRule = new RuleDescription("customRule1", TrueFilter.DEFAULT);
    subscriptionClient.addRule(trueFilterRule);
    try {
        subscriptionClient.addRule(trueFilterRule);
        Assert.fail("A rule with duplicate name is added.");
    } catch (MessagingEntityAlreadyExistsException e) {
    // Expected
    }
    rules = subscriptionClient.getRules().toArray(new RuleDescription[0]);
    Assert.assertEquals("More than one rules are present", 1, rules.length);
    Assert.assertEquals("Returned rule name doesn't match", trueFilterRule.getName(), rules[0].getName());
    Assert.assertTrue(rules[0].getFilter() instanceof SqlFilter);
    subscriptionClient.removeRule(trueFilterRule.getName());
    // Custom SQL Filter rule
    SqlFilter sqlFilter = new SqlFilter("1=1");
    SqlRuleAction action = new SqlRuleAction("set FilterTag = 'true'");
    RuleDescription sqlRule = new RuleDescription("customRule2", sqlFilter);
    sqlRule.setAction(action);
    subscriptionClient.addRule(sqlRule);
    rules = subscriptionClient.getRules().toArray(new RuleDescription[0]);
    Assert.assertEquals("More than one rules are present", 1, rules.length);
    RuleDescription returnedRule = rules[0];
    Assert.assertEquals("Returned rule name doesn't match", sqlRule.getName(), returnedRule.getName());
    Assert.assertTrue(returnedRule.getFilter() instanceof SqlFilter);
    Assert.assertEquals(sqlFilter.getSqlExpression(), ((SqlFilter) returnedRule.getFilter()).getSqlExpression());
    Assert.assertTrue(returnedRule.getAction() instanceof SqlRuleAction);
    Assert.assertEquals(action.getSqlExpression(), ((SqlRuleAction) returnedRule.getAction()).getSqlExpression());
    subscriptionClient.removeRule(sqlRule.getName());
    // Correlation Filter rule
    CorrelationFilter correlationFilter = new CorrelationFilter();
    correlationFilter.setCorrelationId("TestCorrelationId");
    correlationFilter.setMessageId("TestMessageId");
    correlationFilter.setReplyTo("testReplyTo");
    correlationFilter.setLabel("testLabel");
    correlationFilter.setTo("testTo");
    correlationFilter.setReplyTo("testReplyTo");
    HashMap<String, Object> properties = new HashMap<>();
    properties.put("testKey1", "testValue1");
    properties.put("testKey2", null);
    correlationFilter.setProperties(properties);
    RuleDescription correlationRule = new RuleDescription("customRule3", correlationFilter);
    correlationRule.setAction(action);
    subscriptionClient.addRule(correlationRule);
    rules = subscriptionClient.getRules().toArray(new RuleDescription[0]);
    Assert.assertEquals("More than one rules are present", 1, rules.length);
    returnedRule = rules[0];
    Assert.assertEquals("Returned rule name doesn't match", correlationRule.getName(), returnedRule.getName());
    Assert.assertTrue(returnedRule.getAction() instanceof SqlRuleAction);
    Assert.assertEquals(action.getSqlExpression(), ((SqlRuleAction) returnedRule.getAction()).getSqlExpression());
    Assert.assertTrue(returnedRule.getFilter() instanceof CorrelationFilter);
    CorrelationFilter returnedFilter = (CorrelationFilter) returnedRule.getFilter();
    Assert.assertEquals(correlationFilter.getCorrelationId(), returnedFilter.getCorrelationId());
    Assert.assertEquals(correlationFilter.getMessageId(), returnedFilter.getMessageId());
    Assert.assertEquals(correlationFilter.getReplyTo(), returnedFilter.getReplyTo());
    Assert.assertEquals(correlationFilter.getLabel(), returnedFilter.getLabel());
    Assert.assertEquals(correlationFilter.getTo(), returnedFilter.getTo());
    Assert.assertEquals(correlationFilter.getReplyTo(), returnedFilter.getReplyTo());
    for (Map.Entry<String, Object> entry : properties.entrySet()) {
        Assert.assertTrue(returnedFilter.getProperties().containsKey(entry.getKey()));
        Assert.assertEquals(entry.getValue(), returnedFilter.getProperties().get(entry.getKey()));
    }
    subscriptionClient.removeRule(correlationRule.getName());
    subscriptionClient.addRule(SubscriptionClient.DEFAULT_RULE_NAME, TrueFilter.DEFAULT);
}
Also used : MessagingEntityAlreadyExistsException(com.microsoft.azure.servicebus.primitives.MessagingEntityAlreadyExistsException) RuleDescription(com.microsoft.azure.servicebus.rules.RuleDescription) SqlFilter(com.microsoft.azure.servicebus.rules.SqlFilter) HashMap(java.util.HashMap) CorrelationFilter(com.microsoft.azure.servicebus.rules.CorrelationFilter) SqlRuleAction(com.microsoft.azure.servicebus.rules.SqlRuleAction) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

MessagingEntityAlreadyExistsException (com.microsoft.azure.servicebus.primitives.MessagingEntityAlreadyExistsException)1 CorrelationFilter (com.microsoft.azure.servicebus.rules.CorrelationFilter)1 RuleDescription (com.microsoft.azure.servicebus.rules.RuleDescription)1 SqlFilter (com.microsoft.azure.servicebus.rules.SqlFilter)1 SqlRuleAction (com.microsoft.azure.servicebus.rules.SqlRuleAction)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1