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 6ea2d928cff426a8aafebe7fc6fd685b30cacf95 Author: Andrea Cosentino <[email protected]> AuthorDate: Thu May 2 13:00:19 2019 +0200 CAMEL-13453 - CAMEL-13453 - Camel-Azure: add automatic injection of single instance bean, Azure-Queue --- .../azure/queue/QueueServiceComponent.java | 29 +++- .../azure/queue/QueueServiceConfiguration.java | 13 ++ ...eueServiceComponentClientConfigurationTest.java | 161 +++++++++++++++++++++ 3 files changed, 202 insertions(+), 1 deletion(-) 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 770a6a1..e153b9f 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 @@ -17,27 +17,35 @@ package org.apache.camel.component.azure.queue; import java.util.Map; +import java.util.Set; import com.microsoft.azure.storage.StorageCredentials; +import com.microsoft.azure.storage.blob.CloudBlob; import com.microsoft.azure.storage.queue.CloudQueue; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; +import org.apache.camel.component.azure.blob.BlobServiceConfiguration; +import org.apache.camel.spi.Metadata; import org.apache.camel.spi.annotations.Component; import org.apache.camel.support.DefaultComponent; @Component("azure-queue") public class QueueServiceComponent extends DefaultComponent { + + @Metadata(label = "advanced") + private QueueServiceConfiguration configuration; public QueueServiceComponent() { } public QueueServiceComponent(CamelContext context) { super(context); + this.configuration = new QueueServiceConfiguration(); } protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - QueueServiceConfiguration configuration = new QueueServiceConfiguration(); + final QueueServiceConfiguration configuration = this.configuration.copy(); setProperties(configuration, parameters); String[] parts = null; @@ -63,6 +71,7 @@ public class QueueServiceComponent extends DefaultComponent { configuration.setQueueName(parts[1]); } + checkAndSetRegistryClient(configuration); checkCredentials(configuration); QueueServiceEndpoint endpoint = new QueueServiceEndpoint(uri, this, configuration); @@ -70,6 +79,17 @@ public class QueueServiceComponent extends DefaultComponent { return endpoint; } + public QueueServiceConfiguration getConfiguration() { + return configuration; + } + + /** + * The Queue Service configuration + */ + public void setConfiguration(QueueServiceConfiguration configuration) { + this.configuration = configuration; + } + private void checkCredentials(QueueServiceConfiguration cfg) { CloudQueue client = cfg.getAzureQueueClient(); StorageCredentials creds = client == null ? cfg.getCredentials() : client.getServiceClient().getCredentials(); @@ -77,4 +97,11 @@ public class QueueServiceComponent extends DefaultComponent { throw new IllegalArgumentException("Credentials must be specified."); } } + + private void checkAndSetRegistryClient(QueueServiceConfiguration configuration) { + Set<CloudQueue> clients = getCamelContext().getRegistry().findByType(CloudQueue.class); + if (clients.size() == 1) { + configuration.setAzureQueueClient(clients.stream().findFirst().get()); + } + } } 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 e08c000..1a5e52e 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 @@ -18,6 +18,7 @@ package org.apache.camel.component.azure.queue; import com.microsoft.azure.storage.queue.CloudQueue; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.azure.common.AbstractConfiguration; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParams; @@ -105,4 +106,16 @@ public class QueueServiceConfiguration extends AbstractConfiguration { public void setQueuePrefix(String queuePrefix) { this.queuePrefix = queuePrefix; } + + // ************************************************* + // + // ************************************************* + + public QueueServiceConfiguration copy() { + try { + return (QueueServiceConfiguration)super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeCamelException(e); + } + } } diff --git a/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceComponentClientConfigurationTest.java b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceComponentClientConfigurationTest.java new file mode 100644 index 0000000..cd837cc --- /dev/null +++ b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceComponentClientConfigurationTest.java @@ -0,0 +1,161 @@ +/* + * 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.net.URI; + +import com.microsoft.azure.storage.StorageCredentials; +import com.microsoft.azure.storage.StorageCredentialsAccountAndKey; +import com.microsoft.azure.storage.core.Base64; +import com.microsoft.azure.storage.queue.CloudQueue; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class QueueServiceComponentClientConfigurationTest extends CamelTestSupport { + + @Test + public void testCreateEndpointWithMinConfigForClientOnly() throws Exception { + CloudQueue client = + new CloudQueue(URI.create("https://camelazure.queue.core.windows.net/testqueue/messages"), + newAccountKeyCredentials()); + + context.getRegistry().bind("azureQueueClient", client); + + QueueServiceComponent component = new QueueServiceComponent(context); + QueueServiceEndpoint endpoint = + (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue"); + + doTestCreateEndpointWithMinConfig(endpoint, true); + } + + @Test + public void testCreateEndpointWithMinConfigForCredsOnly() throws Exception { + registerCredentials(); + + QueueServiceComponent component = new QueueServiceComponent(context); + QueueServiceEndpoint endpoint = + (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue?credentials=#creds"); + + doTestCreateEndpointWithMinConfig(endpoint, false); + } + + @Test + public void testCreateEndpointWithMaxConfig() throws Exception { + registerCredentials(); + + QueueServiceComponent component = new QueueServiceComponent(context); + QueueServiceEndpoint endpoint = + (QueueServiceEndpoint) component.createEndpoint("azure-queue://camelazure/testqueue?credentials=#creds" + + "&operation=addMessage&queuePrefix=prefix&messageTimeToLive=100&messageVisibilityDelay=10"); + + doTestCreateEndpointWithMaxConfig(endpoint, false); + } + + private void doTestCreateEndpointWithMinConfig(QueueServiceEndpoint endpoint, boolean clientExpected) + throws Exception { + assertEquals("camelazure", endpoint.getConfiguration().getAccountName()); + assertEquals("testqueue", endpoint.getConfiguration().getQueueName()); + if (clientExpected) { + assertNotNull(endpoint.getConfiguration().getAzureQueueClient()); + assertNull(endpoint.getConfiguration().getCredentials()); + } else { + assertNull(endpoint.getConfiguration().getAzureQueueClient()); + assertNotNull(endpoint.getConfiguration().getCredentials()); + } + assertEquals(QueueServiceOperations.listQueues, endpoint.getConfiguration().getOperation()); + + assertNull(endpoint.getConfiguration().getQueuePrefix()); + assertEquals(0, endpoint.getConfiguration().getMessageTimeToLive()); + assertEquals(0, endpoint.getConfiguration().getMessageVisibilityDelay()); + createConsumer(endpoint); + } + + private void doTestCreateEndpointWithMaxConfig(QueueServiceEndpoint endpoint, boolean clientExpected) + throws Exception { + assertEquals("camelazure", endpoint.getConfiguration().getAccountName()); + assertEquals("testqueue", endpoint.getConfiguration().getQueueName()); + if (clientExpected) { + assertNotNull(endpoint.getConfiguration().getAzureQueueClient()); + assertNull(endpoint.getConfiguration().getCredentials()); + } else { + assertNull(endpoint.getConfiguration().getAzureQueueClient()); + assertNotNull(endpoint.getConfiguration().getCredentials()); + } + assertEquals(QueueServiceOperations.addMessage, endpoint.getConfiguration().getOperation()); + + assertEquals("prefix", endpoint.getConfiguration().getQueuePrefix()); + assertEquals(100, endpoint.getConfiguration().getMessageTimeToLive()); + assertEquals(10, endpoint.getConfiguration().getMessageVisibilityDelay()); + + createConsumer(endpoint); + } + + @Test + public void testNoCredentials() throws Exception { + QueueServiceComponent component = new QueueServiceComponent(context); + try { + component.createEndpoint("azure-queue://camelazure/testqueue"); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Credentials must be specified.", ex.getMessage()); + } + } + + @Test + public void testTooManyPathSegments() throws Exception { + QueueServiceComponent component = new QueueServiceComponent(context); + try { + component.createEndpoint("azure-queue://camelazure/testqueue/1"); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("Only the account and queue names must be specified.", ex.getMessage()); + } + } + + @Test + public void testTooFewPathSegments() throws Exception { + QueueServiceComponent component = new QueueServiceComponent(context); + try { + component.createEndpoint("azure-queue://camelazure?operation=addMessage"); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("The queue name must be specified.", ex.getMessage()); + } + } + + + private static void createConsumer(Endpoint endpoint) throws Exception { + endpoint.createConsumer(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + // noop + } + }); + } + + private void registerCredentials() { + context.getRegistry().bind("creds", newAccountKeyCredentials()); + } + private StorageCredentials newAccountKeyCredentials() { + return new StorageCredentialsAccountAndKey("camelazure", + Base64.encode("key".getBytes())); + } + +}
