You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2017/11/10 08:11:52 UTC

[camel] 01/02: [CAMEL-11999] Cannot create queue/message for Azure

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 03586db503f65d5fd163eeee4240f9f7859d0d1f
Author: Thomas Diesler <td...@redhat.com>
AuthorDate: Thu Nov 9 06:55:27 2017 +0100

    [CAMEL-11999] Cannot create queue/message for Azure
---
 components/camel-azure/pom.xml                     |   5 +
 .../azure/queue/QueueServiceComponent.java         |  17 +--
 .../azure/queue/QueueServiceConfiguration.java     |   3 +-
 .../component/azure/queue/QueueServiceUtil.java    |  15 +--
 .../QueueServiceComponentConfigurationTest.java    |   4 +-
 .../azure/queue/QueueServiceProducerTest.java      | 116 +++++++++++++++++++++
 .../azure/queue/QueueServiceUtilTest.java          |  37 +++----
 .../src/test/resources/log4j.properties            |  36 +++++++
 8 files changed, 191 insertions(+), 42 deletions(-)

diff --git a/components/camel-azure/pom.xml b/components/camel-azure/pom.xml
index 9fb96d9..47d47bd 100644
--- a/components/camel-azure/pom.xml
+++ b/components/camel-azure/pom.xml
@@ -63,6 +63,11 @@
       <artifactId>camel-test-spring</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+        <groupId>org.slf4j</groupId>
+        <artifactId>slf4j-log4j12</artifactId>
+        <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
diff --git a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceComponent.java b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceComponent.java
index 42bd47d..572989c 100644
--- a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceComponent.java
+++ b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceComponent.java
@@ -42,15 +42,20 @@ public class QueueServiceComponent extends UriEndpointComponent {
         if (remaining != null) {
             parts = remaining.split("/"); 
         }
-        if (parts == null || parts.length < 2) {
-            throw new IllegalArgumentException("The account and queue names must be specified.");
-        }
-        if (parts.length > 2) {
+        if (parts == null || parts.length < 1) 
+            throw new IllegalArgumentException("The account name must be specified.");
+
+        QueueServiceOperations operation = configuration.getOperation();
+        if (operation != null && operation != QueueServiceOperations.listQueues && parts.length < 2) 
+            throw new IllegalArgumentException("The queue name must be specified.");
+
+        if (parts.length > 2) 
             throw new IllegalArgumentException("Only the account and queue names must be specified.");
-        }
         
         configuration.setAccountName(parts[0]);
-        configuration.setQueueName(parts[1]);
+        
+        if (parts.length > 1)
+            configuration.setQueueName(parts[1]);
         
         checkCredentials(configuration);
         
diff --git a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConfiguration.java b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConfiguration.java
index e85cd2c..699e013 100644
--- a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConfiguration.java
+++ b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceConfiguration.java
@@ -16,11 +16,12 @@
  */
 package org.apache.camel.component.azure.queue;
 
-import com.microsoft.azure.storage.queue.CloudQueue;
 import org.apache.camel.component.azure.common.AbstractConfiguration;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
 
+import com.microsoft.azure.storage.queue.CloudQueue;
+
 @UriParams
 public class QueueServiceConfiguration extends AbstractConfiguration {
 
diff --git a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceUtil.java b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceUtil.java
index 6f09418..9669dbe 100644
--- a/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceUtil.java
+++ b/components/camel-azure/src/main/java/org/apache/camel/component/azure/queue/QueueServiceUtil.java
@@ -31,19 +31,12 @@ public final class QueueServiceUtil {
     }
     
     public static URI prepareStorageQueueUri(QueueServiceConfiguration cfg) {
-        return prepareStorageQueueUri(cfg, true);
-    }
-
-    public static URI prepareStorageQueueUri(QueueServiceConfiguration cfg, boolean isForMessages) {
         StringBuilder uriBuilder = new StringBuilder();
         uriBuilder.append("https://")
             .append(cfg.getAccountName())
             .append(QueueServiceConstants.SERVICE_URI_SEGMENT)
-            .append("/")
-            .append(cfg.getQueueName());
-        if (isForMessages) {
-            uriBuilder.append("/messages");
-        }
+            .append("/" + cfg.getQueueName());
+        
         return URI.create(uriBuilder.toString());
     }
     
@@ -73,7 +66,9 @@ public final class QueueServiceUtil {
     public static void retrieveMessage(Exchange exchange, QueueServiceConfiguration cfg) throws Exception {
         CloudQueue client = createQueueClient(cfg);
         QueueServiceRequestOptions opts = getRequestOptions(exchange);  
-        CloudQueueMessage message = client.retrieveMessage(cfg.getMessageVisibilityDelay(),
+        int visibilityTimeout = cfg.getMessageVisibilityDelay();
+        visibilityTimeout = visibilityTimeout != 0 ? visibilityTimeout : 30;
+        CloudQueueMessage message = client.retrieveMessage(visibilityTimeout,
                                opts.getRequestOpts(), opts.getOpContext());
         ExchangeUtil.getMessageForResponse(exchange).setBody(message);
     }
diff --git a/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceComponentConfigurationTest.java b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceComponentConfigurationTest.java
index ac3e3c1..111812d 100644
--- a/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceComponentConfigurationTest.java
+++ b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceComponentConfigurationTest.java
@@ -136,10 +136,10 @@ public class QueueServiceComponentConfigurationTest extends CamelTestSupport {
     public void testTooFewPathSegments() throws Exception {
         QueueServiceComponent component = new QueueServiceComponent(context);
         try {
-            component.createEndpoint("azure-queue://camelazure");
+            component.createEndpoint("azure-queue://camelazure?operation=addMessage");
             fail();
         } catch (IllegalArgumentException ex) {
-            assertEquals("The account and queue names must be specified.", ex.getMessage());
+            assertEquals("The queue name must be specified.", ex.getMessage());
         }
     }
     
diff --git a/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceProducerTest.java b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceProducerTest.java
new file mode 100644
index 0000000..bc3107f
--- /dev/null
+++ b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceProducerTest.java
@@ -0,0 +1,116 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.azure.queue;
+
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.JndiRegistry;
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.Test;
+
+import com.microsoft.azure.storage.OperationContext;
+import com.microsoft.azure.storage.StorageCredentials;
+import com.microsoft.azure.storage.StorageCredentialsAccountAndKey;
+import com.microsoft.azure.storage.queue.CloudQueue;
+import com.microsoft.azure.storage.queue.CloudQueueMessage;
+
+public class QueueServiceProducerTest {
+
+    private static final String AZURE_STORAGE_QUEUE = "AZURE_STORAGE_QUEUE";
+
+    @Test
+    public void testAppendQueue() throws Exception {
+
+        StorageCredentials creds = getStorageCredentials("camelqueue", System.getenv(AZURE_STORAGE_QUEUE));
+        Assume.assumeNotNull("Credentials not null", creds);
+
+        OperationContext.setLoggingEnabledByDefault(true);
+        
+        CamelContext camelctx = createCamelContext(creds);
+        camelctx.addRoutes(new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:createQueue")
+                .to("azure-queue://camelqueue/queue1?credentials=#creds&operation=createQueue");
+
+                from("direct:listQueues")
+                .to("azure-queue://camelqueue?credentials=#creds&operation=listQueues");
+
+                from("direct:deleteQueue")
+                .to("azure-queue://camelqueue/queue1?credentials=#creds&operation=deleteQueue");
+
+                from("direct:addMessage")
+                .to("azure-queue://camelqueue/queue1?credentials=#creds&operation=addMessage");
+
+                from("direct:retrieveMessage")
+                .to("azure-queue://camelqueue/queue1?credentials=#creds&operation=retrieveMessage");
+            }
+        });
+
+        camelctx.start();
+        try {
+            ProducerTemplate producer = camelctx.createProducerTemplate();
+            
+            Iterator<?> it = producer.requestBody("direct:listQueues", null, Iterable.class).iterator();
+            Assert.assertFalse("No more queues", it.hasNext());
+
+            producer.sendBody("direct:addMessage", "SomeMsg");
+            
+            it = producer.requestBody("direct:listQueues", null, Iterable.class).iterator();
+            Assert.assertTrue("Has queues", it.hasNext());
+            CloudQueue queue = (CloudQueue) it.next();
+            Assert.assertEquals("queue1", queue.getName());
+            Assert.assertFalse("No more queues", it.hasNext());
+            
+            try {
+                CloudQueueMessage msg = producer.requestBody("direct:retrieveMessage", null, CloudQueueMessage.class);
+                Assert.assertNotNull("Retrieve a message", msg);
+                Assert.assertEquals("SomeMsg", msg.getMessageContentAsString());
+            } finally {
+                queue.delete();
+            }
+            
+        } finally {
+            camelctx.stop();
+        }
+    }
+
+    private StorageCredentials getStorageCredentials(String account, String key) {
+        return key != null ? new StorageCredentialsAccountAndKey(account, key) : null;
+    }
+
+    private CamelContext createCamelContext(StorageCredentials credentials) throws Exception {
+        JndiRegistry registry = new JndiRegistry(createJndiContext());
+        registry.bind("creds", credentials);
+        return new DefaultCamelContext(registry);
+    }
+
+    private Context createJndiContext() throws Exception {
+        Properties properties = new Properties();
+        properties.put("java.naming.factory.initial", "org.apache.camel.util.jndi.CamelInitialContextFactory");
+        return new InitialContext(new Hashtable<Object, Object>(properties));
+    }
+}
\ No newline at end of file
diff --git a/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java
index 40bf2a4..85a7216 100644
--- a/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java
+++ b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java
@@ -32,42 +32,34 @@ public class QueueServiceUtilTest extends CamelTestSupport {
     @Test
     public void testPrepareUri() throws Exception {
         registerCredentials();
-        
+
         QueueServiceComponent component = new QueueServiceComponent(context);
-        QueueServiceEndpoint endpoint = 
-            (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue?credentials=#creds");
-        URI uri = 
-            QueueServiceUtil.prepareStorageQueueUri(endpoint.getConfiguration());
-        assertEquals("https://camelazure.queue.core.windows.net/testqueue/messages", uri.toString());
+        QueueServiceEndpoint endpoint = (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue?credentials=#creds");
+        URI uri = QueueServiceUtil.prepareStorageQueueUri(endpoint.getConfiguration());
+        assertEquals("https://camelazure.queue.core.windows.net/testqueue", uri.toString());
     }
 
     @Test
     public void testGetConfiguredClient() throws Exception {
-        CloudQueue client = 
-            new CloudQueue(URI.create("https://camelazure.queue.core.windows.net/testqueue/messages"),
-                           newAccountKeyCredentials());
-        
+        CloudQueue client = new CloudQueue(URI.create("https://camelazure.queue.core.windows.net/testqueue"), newAccountKeyCredentials());
         JndiRegistry registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
         registry.bind("azureQueueClient", client);
-        
+
         QueueServiceComponent component = new QueueServiceComponent(context);
-        QueueServiceEndpoint endpoint = 
-            (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue?azureQueueClient=#azureQueueClient");
+        QueueServiceEndpoint endpoint = (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue?azureQueueClient=#azureQueueClient");
         assertSame(client, QueueServiceUtil.getConfiguredClient(endpoint.getConfiguration()));
     }
+
     @Test
     public void testGetConfiguredClientUriMismatch() throws Exception {
-        CloudQueue client = 
-            new CloudQueue(URI.create("https://camelazure.queue.core.windows.net/testqueue"),
-                           newAccountKeyCredentials());
-        
+        CloudQueue client = new CloudQueue(URI.create("https://camelazure.queue.core.windows.net/testqueue"), newAccountKeyCredentials());
+
         JndiRegistry registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
         registry.bind("azureQueueClient", client);
-        
+
         QueueServiceComponent component = new QueueServiceComponent(context);
-        QueueServiceEndpoint endpoint = 
-            (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue2?azureQueueClient=#azureQueueClient");
-        
+        QueueServiceEndpoint endpoint = (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue2?azureQueueClient=#azureQueueClient");
+
         try {
             QueueServiceUtil.getConfiguredClient(endpoint.getConfiguration());
             fail();
@@ -82,7 +74,6 @@ public class QueueServiceUtilTest extends CamelTestSupport {
     }
 
     private StorageCredentials newAccountKeyCredentials() {
-        return new StorageCredentialsAccountAndKey("camelazure", 
-                                                   Base64.encode("key".getBytes()));
+        return new StorageCredentialsAccountAndKey("camelazure", Base64.encode("key".getBytes()));
     }
 }
diff --git a/components/camel-azure/src/test/resources/log4j.properties b/components/camel-azure/src/test/resources/log4j.properties
new file mode 100644
index 0000000..6b2698a
--- /dev/null
+++ b/components/camel-azure/src/test/resources/log4j.properties
@@ -0,0 +1,36 @@
+###
+# #%L
+# Wildfly Camel :: Testsuite
+# %%
+# Copyright (C) 2013 - 2014 RedHat
+# %%
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+# 
+#      http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# #L%
+###
+
+# Root logger option
+log4j.rootLogger=DEBUG, file, console
+ 
+# Direct log messages to a log file
+log4j.appender.file=org.apache.log4j.FileAppender
+log4j.appender.file.file=target/test.log
+log4j.appender.file.layout=org.apache.log4j.PatternLayout
+log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c] (%t) - %m%n
+log4j.appender.file.threshold=DEBUG
+ 
+# Direct log messages to console
+log4j.appender.console=org.apache.log4j.ConsoleAppender
+log4j.appender.console.target=System.out
+log4j.appender.console.layout=org.apache.log4j.PatternLayout
+log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c] (%t) - %m%n
+log4j.appender.console.threshold=WARN

-- 
To stop receiving notification emails like this one, please contact
"commits@camel.apache.org" <co...@camel.apache.org>.