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 3d509a7d0913c1ca0242cf9c95562527fe8435c5
Author: Andrea Cosentino <anco...@gmail.com>
AuthorDate: Thu May 2 12:29:55 2019 +0200

    CAMEL-13453 - Camel-Azure: add automatic injection of single instance bean, 
Azure-Blob
---
 .../component/azure/blob/BlobServiceComponent.java |  17 +-
 .../azure/blob/BlobServiceConfiguration.java       |  14 ++
 ...lobServiceComponentConfigurationClientTest.java | 260 +++++++++++++++++++++
 3 files changed, 289 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceComponent.java
 
b/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceComponent.java
index 4ed2c7f..14bd646 100644
--- 
a/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceComponent.java
+++ 
b/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceComponent.java
@@ -17,27 +17,33 @@
 package org.apache.camel.component.azure.blob;
 
 import java.util.Map;
+import java.util.Set;
 
 import com.microsoft.azure.storage.StorageCredentials;
 import com.microsoft.azure.storage.StorageCredentialsAnonymous;
 import com.microsoft.azure.storage.blob.CloudBlob;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
+import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.annotations.Component;
 import org.apache.camel.support.DefaultComponent;
 
 @Component("azure-blob")
 public class BlobServiceComponent extends DefaultComponent {
+       
+    @Metadata(label = "advanced")    
+    private BlobServiceConfiguration configuration;
     
     public BlobServiceComponent() {
     }
 
     public BlobServiceComponent(CamelContext context) {
         super(context);
+        this.configuration = new BlobServiceConfiguration();
     }
 
     protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
-        BlobServiceConfiguration configuration = new 
BlobServiceConfiguration();
+        final BlobServiceConfiguration configuration = 
this.configuration.copy();
         setProperties(configuration, parameters);
 
         String[] parts = null;
@@ -62,7 +68,7 @@ public class BlobServiceComponent extends DefaultComponent {
             }
             configuration.setBlobName(sb.toString());
         }
-        
+        checkAndSetRegistryClient(configuration);
         checkCredentials(configuration);
         
         BlobServiceEndpoint endpoint = new BlobServiceEndpoint(uri, this, 
configuration);
@@ -79,4 +85,11 @@ public class BlobServiceComponent extends DefaultComponent {
             throw new IllegalArgumentException("Credentials must be 
specified.");
         }
     }
+    
+    private void checkAndSetRegistryClient(BlobServiceConfiguration 
configuration) {
+        Set<CloudBlob> clients = 
getCamelContext().getRegistry().findByType(CloudBlob.class);
+        if (clients.size() == 1) {
+            
configuration.setAzureBlobClient(clients.stream().findFirst().get());
+        }
+    }
 }
diff --git 
a/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceConfiguration.java
 
b/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceConfiguration.java
index b89f664..457edfd 100644
--- 
a/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceConfiguration.java
+++ 
b/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceConfiguration.java
@@ -19,6 +19,8 @@ package org.apache.camel.component.azure.blob;
 import java.util.Map;
 
 import com.microsoft.azure.storage.blob.CloudBlob;
+
+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;
@@ -248,4 +250,16 @@ public class BlobServiceConfiguration extends 
AbstractConfiguration {
     public void setUseFlatListing(boolean useFlatListing) {
         this.useFlatListing = useFlatListing;
     }
+    
+    // *************************************************
+    //
+    // *************************************************
+
+    public BlobServiceConfiguration copy() {
+        try {
+            return (BlobServiceConfiguration)super.clone();
+        } catch (CloneNotSupportedException e) {
+            throw new RuntimeCamelException(e);
+        }
+    }
 }
\ No newline at end of file
diff --git 
a/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceComponentConfigurationClientTest.java
 
b/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceComponentConfigurationClientTest.java
new file mode 100644
index 0000000..560a00f
--- /dev/null
+++ 
b/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceComponentConfigurationClientTest.java
@@ -0,0 +1,260 @@
+/*
+ * 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.blob;
+
+import java.net.URI;
+import java.util.Collections;
+
+import com.microsoft.azure.storage.StorageCredentials;
+import com.microsoft.azure.storage.StorageCredentialsAccountAndKey;
+import com.microsoft.azure.storage.StorageCredentialsAnonymous;
+import com.microsoft.azure.storage.blob.CloudBlob;
+import com.microsoft.azure.storage.blob.CloudBlockBlob;
+import com.microsoft.azure.storage.core.Base64;
+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 BlobServiceComponentConfigurationClientTest extends 
CamelTestSupport {
+    
+    @Test
+    public void testCreateEndpointWithMinConfigForClientOnly() throws 
Exception {
+        CloudBlockBlob client = 
+            new 
CloudBlockBlob(URI.create("https://camelazure.blob.core.windows.net/container/blob";),
+                               newAccountKeyCredentials());
+        
+        context.getRegistry().bind("azureBlobClient", client);
+        
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpoint = 
+            (BlobServiceEndpoint) 
component.createEndpoint("azure-blob://camelazure/container/blob");
+        
+        doTestCreateEndpointWithMinConfig(endpoint, true);
+    }
+    
+    @Test
+    public void testCreateEndpointWithMinConfigForCredsOnly() throws Exception 
{
+        registerCredentials();
+        
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpoint = 
+            (BlobServiceEndpoint) 
component.createEndpoint("azure-blob://camelazure/container/blob?credentials=#creds");
+        
+        doTestCreateEndpointWithMinConfig(endpoint, false);
+    }
+    
+    private void doTestCreateEndpointWithMinConfig(BlobServiceEndpoint 
endpoint, boolean clientExpected)
+        throws Exception {
+        assertEquals("camelazure", 
endpoint.getConfiguration().getAccountName());
+        assertEquals("container", 
endpoint.getConfiguration().getContainerName());
+        assertEquals("blob", endpoint.getConfiguration().getBlobName());
+        if (clientExpected) {
+            assertNotNull(endpoint.getConfiguration().getAzureBlobClient());
+            assertNull(endpoint.getConfiguration().getCredentials());
+        } else {
+            assertNull(endpoint.getConfiguration().getAzureBlobClient());
+            assertNotNull(endpoint.getConfiguration().getCredentials());
+        }
+        
+        assertEquals(BlobType.blockblob, 
endpoint.getConfiguration().getBlobType());
+        assertNull(endpoint.getConfiguration().getBlobPrefix());
+        assertNull(endpoint.getConfiguration().getFileDir());
+        assertEquals(Long.valueOf(0L), 
endpoint.getConfiguration().getBlobOffset());
+        assertNull(endpoint.getConfiguration().getBlobMetadata());
+        assertNull(endpoint.getConfiguration().getDataLength());
+        assertEquals(BlobServiceOperations.listBlobs, 
endpoint.getConfiguration().getOperation());
+        assertEquals(0, endpoint.getConfiguration().getStreamWriteSize());
+        assertEquals(0, endpoint.getConfiguration().getStreamReadSize());
+        assertTrue(endpoint.getConfiguration().isCloseStreamAfterRead());
+        assertTrue(endpoint.getConfiguration().isCloseStreamAfterWrite());
+        assertFalse(endpoint.getConfiguration().isPublicForRead());
+        assertTrue(endpoint.getConfiguration().isUseFlatListing());
+        
+        createConsumer(endpoint);
+    }
+    
+    @Test
+    public void testCreateEndpointWithMaxConfig() throws Exception {
+        registerCredentials();
+        context.getRegistry().bind("metadata", Collections.emptyMap());
+        
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpoint = 
+            (BlobServiceEndpoint) component.createEndpoint(
+            
"azure-blob://camelazure/container/blob?credentials=#creds&blobType=pageblob"
+            + 
"&blobPrefix=blob1&fileDir=/tmp&blobOffset=512&operation=clearPageBlob&dataLength=1024"
+            + 
"&streamWriteSize=512&streamReadSize=1024&closeStreamAfterRead=false&closeStreamAfterWrite=false"
+            + 
"&publicForRead=true&useFlatListing=false&blobMetadata=#metadata");
+        
+        assertEquals("camelazure", 
endpoint.getConfiguration().getAccountName());
+        assertEquals("container", 
endpoint.getConfiguration().getContainerName());
+        assertEquals("blob", endpoint.getConfiguration().getBlobName());
+        assertNull(endpoint.getConfiguration().getAzureBlobClient());
+        assertNotNull(endpoint.getConfiguration().getCredentials());
+        
+        assertEquals(BlobType.pageblob, 
endpoint.getConfiguration().getBlobType());
+        assertEquals("blob1", endpoint.getConfiguration().getBlobPrefix());
+        assertEquals("/tmp", endpoint.getConfiguration().getFileDir());
+        assertEquals(Long.valueOf(512L), 
endpoint.getConfiguration().getBlobOffset());
+        assertNotNull(endpoint.getConfiguration().getBlobMetadata());
+        assertEquals(Long.valueOf(1024L), 
endpoint.getConfiguration().getDataLength());
+        assertEquals(BlobServiceOperations.clearPageBlob, 
endpoint.getConfiguration().getOperation());
+        assertEquals(512, endpoint.getConfiguration().getStreamWriteSize());
+        assertEquals(1024, endpoint.getConfiguration().getStreamReadSize());
+        assertFalse(endpoint.getConfiguration().isCloseStreamAfterRead());
+        assertFalse(endpoint.getConfiguration().isCloseStreamAfterWrite());
+        assertTrue(endpoint.getConfiguration().isPublicForRead());
+        assertFalse(endpoint.getConfiguration().isUseFlatListing());
+    }
+    
+    @Test
+    public void testNoClientAndCredentials() throws Exception {
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        try {
+            component.createEndpoint("azure-blob://camelazure/container/blob");
+            fail();
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Credentials must be specified.", ex.getMessage());
+        }
+    }
+    @Test
+    public void testNoClientAndCredentialsPublicForRead() throws Exception {
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpoint = 
+            (BlobServiceEndpoint) 
component.createEndpoint("azure-blob://camelazure/container/blob?publicForRead=true");
+        assertTrue(endpoint.getConfiguration().isPublicForRead());
+    }
+    
+    @Test
+    public void testClientWithoutCredentials() throws Exception {
+        CloudBlockBlob client = 
+            new 
CloudBlockBlob(URI.create("https://camelazure.blob.core.windows.net/container/blob";));
+        
+        doTestClientWithoutCredentials(client);
+    }
+    @Test
+    public void testClientWithoutAnonymousCredentials() throws Exception {
+        CloudBlockBlob client = 
+            new 
CloudBlockBlob(URI.create("https://camelazure.blob.core.windows.net/container/blob";),
+                               StorageCredentialsAnonymous.ANONYMOUS);
+        
+        doTestClientWithoutCredentials(client);
+    }
+    @Test
+    public void testClientWithoutCredentialsPublicRead() throws Exception {
+        CloudBlockBlob client = 
+            new 
CloudBlockBlob(URI.create("https://camelazure.blob.core.windows.net/container/blob";));
+        
+        context.getRegistry().bind("azureBlobClient", client);
+        
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpoint = 
+            (BlobServiceEndpoint) 
component.createEndpoint("azure-blob://camelazure/container/blob?azureBlobClient=#azureBlobClient&publicForRead=true");
+        assertTrue(endpoint.getConfiguration().isPublicForRead());
+    }
+    private void doTestClientWithoutCredentials(CloudBlob client) throws 
Exception {
+        context.getRegistry().bind("azureBlobClient", client);
+        
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        try {
+            
component.createEndpoint("azure-blob://camelazure/container/blob?azureBlobClient=#azureBlobClient");
+            fail();
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Credentials must be specified.", ex.getMessage());
+        }
+    }
+    
+    @Test
+    public void testNoBlobNameProducerWithOp() throws Exception {
+        registerCredentials();
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpointWithOp = 
+            (BlobServiceEndpoint) component.createEndpoint(
+            
"azure-blob://camelazure/container?operation=deleteBlob&credentials=#creds");
+        try {
+            endpointWithOp.createProducer();
+            fail();
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Blob name must be specified.", ex.getMessage());
+        }
+    }
+    @Test
+    public void testNoBlobNameProducerDefaultOp() throws Exception {
+        registerCredentials();
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpoint = 
+            (BlobServiceEndpoint) component.createEndpoint(
+            "azure-blob://camelazure/container?credentials=#creds");
+        endpoint.createProducer();
+        assertEquals(BlobServiceOperations.listBlobs, 
endpoint.getConfiguration().getOperation());
+    }
+    
+    @Test
+    public void testNoBlobNameConsumer() throws Exception {
+        registerCredentials();
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpoint = 
+            (BlobServiceEndpoint) component.createEndpoint(
+            "azure-blob://camelazure/container?credentials=#creds");
+        try {
+            createConsumer(endpoint);
+            fail();
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Blob name must be specified.", ex.getMessage());
+        }
+    }
+    
+    @Test
+    public void testTooFewPathSegments() throws Exception {
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        try {
+            component.createEndpoint("azure-blob://camelazure");
+            fail();
+        } catch (IllegalArgumentException ex) {
+            assertEquals("At least the account and container names must be 
specified.", ex.getMessage());
+        }
+    }
+    
+    @Test
+    public void testHierarchicalBlobName() throws Exception {
+        registerCredentials();
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpoint = 
+            
(BlobServiceEndpoint)component.createEndpoint("azure-blob://camelazure/component1/blob/sub?credentials=#creds");
+        assertEquals("blob/sub", endpoint.getConfiguration().getBlobName());
+    }
+    
+    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()));
+    }
+    
+}

Reply via email to