Repository: camel
Updated Branches:
  refs/heads/master 8c9184b6f -> 533d67fd3


[CAMEL-10786] Adding more tests, simplifying the utils code


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/533d67fd
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/533d67fd
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/533d67fd

Branch: refs/heads/master
Commit: 533d67fd305dbf9f9819e7e622eb8b9727fa0ef8
Parents: 8c9184b
Author: Sergey Beryozkin <sberyoz...@gmail.com>
Authored: Tue Feb 14 13:47:25 2017 +0000
Committer: Sergey Beryozkin <sberyoz...@gmail.com>
Committed: Tue Feb 14 13:47:25 2017 +0000

----------------------------------------------------------------------
 .../azure/blob/BlobServiceComponent.java        | 13 +++--
 .../azure/blob/BlobServiceProducer.java         | 28 +++++-----
 .../component/azure/blob/BlobServiceUtil.java   | 49 +++++++----------
 .../BlobServiceComponentConfigurationTest.java  | 54 +++++++++++++++++--
 .../azure/blob/BlobServiceUtilTest.java         | 57 +++++++++++++++++++-
 5 files changed, 149 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/533d67fd/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceComponent.java
----------------------------------------------------------------------
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 33539a7..68925aa 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
@@ -18,6 +18,9 @@ package org.apache.camel.component.azure.blob;
 
 import java.util.Map;
 
+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.impl.UriEndpointComponent;
@@ -66,10 +69,12 @@ public class BlobServiceComponent extends 
UriEndpointComponent {
         return endpoint;
     }
     
-    private void checkCredentials(BlobServiceConfiguration configuration) {
-        if (configuration.getAzureBlobClient() == null
-            && configuration.getCredentials() == null
-            && !configuration.isPublicForRead()) {
+    private void checkCredentials(BlobServiceConfiguration cfg) {
+        CloudBlob client = cfg.getAzureBlobClient();
+        StorageCredentials creds = client == null ? cfg.getCredentials() 
+            : client.getServiceClient().getCredentials(); 
+        if ((creds == null || creds instanceof StorageCredentialsAnonymous)
+            && !cfg.isPublicForRead()) {
             throw new IllegalArgumentException("Credentials must be 
specified.");
         }
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/533d67fd/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceProducer.java
 
b/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceProducer.java
index b2faaf5..0f01588 100644
--- 
a/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceProducer.java
+++ 
b/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceProducer.java
@@ -130,7 +130,7 @@ public class BlobServiceProducer extends DefaultProducer {
     }
     
     private void updateBlockBlob(Exchange exchange) throws Exception {
-        CloudBlockBlob client = 
BlobServiceUtil.createBlockBlobClient(getConfiguration(), true);
+        CloudBlockBlob client = 
BlobServiceUtil.createBlockBlobClient(getConfiguration());
         configureCloudBlobForWrite(client);
         BlobServiceRequestOptions opts = 
BlobServiceUtil.getRequestOptions(exchange);
         
@@ -158,7 +158,7 @@ public class BlobServiceProducer extends DefaultProducer {
             throw new IllegalArgumentException("Illegal storageBlocks 
payload");
         }
         
-        CloudBlockBlob client = 
BlobServiceUtil.createBlockBlobClient(getConfiguration(), true);
+        CloudBlockBlob client = 
BlobServiceUtil.createBlockBlobClient(getConfiguration());
         configureCloudBlobForWrite(client);
         BlobServiceRequestOptions opts = 
BlobServiceUtil.getRequestOptions(exchange);
         
@@ -190,7 +190,7 @@ public class BlobServiceProducer extends DefaultProducer {
             throw new IllegalArgumentException("Illegal commit block list 
payload");
         }
         
-        CloudBlockBlob client = 
BlobServiceUtil.createBlockBlobClient(getConfiguration(), true);
+        CloudBlockBlob client = 
BlobServiceUtil.createBlockBlobClient(getConfiguration());
         BlobServiceRequestOptions opts = 
BlobServiceUtil.getRequestOptions(exchange);
         
         LOG.trace("Putting a blob [{}] block list from exchange [{}]...", 
getConfiguration().getBlobName(), exchange);
@@ -219,7 +219,7 @@ public class BlobServiceProducer extends DefaultProducer {
     }
     
     private void getBlobBlockList(Exchange exchange) throws Exception {
-        CloudBlockBlob client = 
BlobServiceUtil.createBlockBlobClient(getConfiguration(), false);
+        CloudBlockBlob client = 
BlobServiceUtil.createBlockBlobClient(getConfiguration());
         BlobServiceRequestOptions opts = 
BlobServiceUtil.getRequestOptions(exchange);
         LOG.trace("Getting the blob block list [{}] from exchange [{}]...", 
getConfiguration().getBlobName(), exchange);
         BlockListingFilter filter = 
exchange.getIn().getBody(BlockListingFilter.class);
@@ -232,12 +232,12 @@ public class BlobServiceProducer extends DefaultProducer {
     }
     
     private void deleteBlockBlob(Exchange exchange) throws Exception {
-        CloudBlockBlob client = 
BlobServiceUtil.createBlockBlobClient(getConfiguration(), true);
+        CloudBlockBlob client = 
BlobServiceUtil.createBlockBlobClient(getConfiguration());
         doDeleteBlock(client, exchange);
     }
     
     private void createAppendBlob(Exchange exchange) throws Exception {
-        CloudAppendBlob client = 
BlobServiceUtil.createAppendBlobClient(getConfiguration(), true);
+        CloudAppendBlob client = 
BlobServiceUtil.createAppendBlobClient(getConfiguration());
         BlobServiceRequestOptions opts = 
BlobServiceUtil.getRequestOptions(exchange);
         if (opts.getAccessCond() == null) {
             // Default: do not reset the blob content if the blob already 
exists
@@ -261,7 +261,7 @@ public class BlobServiceProducer extends DefaultProducer {
     }
 
     private void updateAppendBlob(Exchange exchange) throws Exception {
-        CloudAppendBlob client = 
BlobServiceUtil.createAppendBlobClient(getConfiguration(), true);
+        CloudAppendBlob client = 
BlobServiceUtil.createAppendBlobClient(getConfiguration());
         configureCloudBlobForWrite(client);
         BlobServiceRequestOptions opts = 
BlobServiceUtil.getRequestOptions(exchange);
         if (opts.getAccessCond() == null) {
@@ -285,13 +285,13 @@ public class BlobServiceProducer extends DefaultProducer {
     }    
     
     private void deleteAppendBlob(Exchange exchange) throws Exception {
-        CloudAppendBlob client = 
BlobServiceUtil.createAppendBlobClient(getConfiguration(), true);
+        CloudAppendBlob client = 
BlobServiceUtil.createAppendBlobClient(getConfiguration());
         doDeleteBlock(client, exchange);
     }
     
     
     private void createPageBlob(Exchange exchange) throws Exception {
-        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration(), true);
+        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration());
         BlobServiceRequestOptions opts = 
BlobServiceUtil.getRequestOptions(exchange);
         if (opts.getAccessCond() == null) {
             // Default: do not reset the blob content if the blob already 
exists
@@ -320,7 +320,7 @@ public class BlobServiceProducer extends DefaultProducer {
     private void uploadPageBlob(Exchange exchange) throws Exception {
         LOG.trace("Updating a page blob [{}] from exchange [{}]...", 
getConfiguration().getBlobName(), exchange);
         
-        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration(), true);
+        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration());
         configureCloudBlobForWrite(client);
         BlobServiceRequestOptions opts = 
BlobServiceUtil.getRequestOptions(exchange);
         if (opts.getAccessCond() == null) {
@@ -341,7 +341,7 @@ public class BlobServiceProducer extends DefaultProducer {
     private void resizePageBlob(Exchange exchange) throws Exception {
         LOG.trace("Resizing a page blob [{}] from exchange [{}]...", 
getConfiguration().getBlobName(), exchange);
         
-        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration(), true);
+        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration());
         BlobServiceRequestOptions opts = 
BlobServiceUtil.getRequestOptions(exchange);
         Long pageSize = getPageBlobSize(exchange);
         client.resize(pageSize, opts.getAccessCond(), opts.getRequestOpts(), 
opts.getOpContext());
@@ -350,7 +350,7 @@ public class BlobServiceProducer extends DefaultProducer {
     private void clearPageBlob(Exchange exchange) throws Exception {
         LOG.trace("Clearing a page blob [{}] from exchange [{}]...", 
getConfiguration().getBlobName(), exchange);
                 
-        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration(), true);
+        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration());
         BlobServiceRequestOptions opts = 
BlobServiceUtil.getRequestOptions(exchange);
         
         Long blobOffset = getConfiguration().getBlobOffset();
@@ -390,7 +390,7 @@ public class BlobServiceProducer extends DefaultProducer {
     }
     
     private void getPageBlobRanges(Exchange exchange) throws Exception {
-        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration(), false);
+        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration());
         BlobServiceUtil.configureCloudBlobForRead(client, getConfiguration());
         BlobServiceRequestOptions opts = 
BlobServiceUtil.getRequestOptions(exchange);
         LOG.trace("Getting the page blob ranges [{}] from exchange [{}]...", 
getConfiguration().getBlobName(), exchange);
@@ -400,7 +400,7 @@ public class BlobServiceProducer extends DefaultProducer {
     }
     
     private void deletePageBlob(Exchange exchange) throws Exception {
-        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration(), true);
+        CloudPageBlob client = 
BlobServiceUtil.createPageBlobClient(getConfiguration());
         doDeleteBlock(client, exchange);
     }
     

http://git-wip-us.apache.org/repos/asf/camel/blob/533d67fd/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceUtil.java
----------------------------------------------------------------------
diff --git 
a/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceUtil.java
 
b/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceUtil.java
index d4c647e..d00dd4d 100644
--- 
a/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceUtil.java
+++ 
b/components/camel-azure/src/main/java/org/apache/camel/component/azure/blob/BlobServiceUtil.java
@@ -62,17 +62,17 @@ public final class BlobServiceUtil {
 
     private static void getBlockBlob(Exchange exchange, 
BlobServiceConfiguration cfg)
         throws Exception {
-        CloudBlockBlob client = createBlockBlobClient(cfg, false);
+        CloudBlockBlob client = createBlockBlobClient(cfg);
         doGetBlob(client, exchange, cfg);
     }
 
     private static void getAppendBlob(Exchange exchange, 
BlobServiceConfiguration cfg) throws Exception {
-        CloudAppendBlob client = createAppendBlobClient(cfg, false);
+        CloudAppendBlob client = createAppendBlobClient(cfg);
         doGetBlob(client, exchange, cfg);
     }
 
     private static void getPageBlob(Exchange exchange, 
BlobServiceConfiguration cfg) throws Exception {
-        CloudPageBlob client = createPageBlobClient(cfg, false);
+        CloudPageBlob client = createPageBlobClient(cfg);
         doGetBlob(client, exchange, cfg);
     }
 
@@ -117,74 +117,65 @@ public final class BlobServiceUtil {
     public static CloudBlobContainer 
createBlobContainerClient(BlobServiceConfiguration cfg)
         throws Exception {
         URI uri = prepareStorageBlobUri(cfg, false);
-        StorageCredentials creds = getAccountCredentials(cfg, false);
+        StorageCredentials creds = getAccountCredentials(cfg);
         return new CloudBlobContainer(uri, creds);
     }
 
-    public static CloudBlockBlob 
createBlockBlobClient(BlobServiceConfiguration cfg, boolean isWrite)
+    public static CloudBlockBlob 
createBlockBlobClient(BlobServiceConfiguration cfg)
         throws Exception {
-        CloudBlockBlob client = (CloudBlockBlob) getConfiguredClient(cfg, 
BlobType.blockblob, isWrite);
+        CloudBlockBlob client = (CloudBlockBlob) getConfiguredClient(cfg);
         if (client == null) {
             URI uri = prepareStorageBlobUri(cfg);
-            StorageCredentials creds = getAccountCredentials(cfg, isWrite);
+            StorageCredentials creds = getAccountCredentials(cfg);
             client = new CloudBlockBlob(uri, creds);
         }
         return client;
     }
 
-    public static CloudAppendBlob 
createAppendBlobClient(BlobServiceConfiguration cfg, boolean isWrite)
+    public static CloudAppendBlob 
createAppendBlobClient(BlobServiceConfiguration cfg)
         throws Exception {
-        CloudAppendBlob client = (CloudAppendBlob) getConfiguredClient(cfg, 
BlobType.appendblob, isWrite);
+        CloudAppendBlob client = (CloudAppendBlob) getConfiguredClient(cfg);
         if (client == null) {
             URI uri = prepareStorageBlobUri(cfg);
-            StorageCredentials creds = getAccountCredentials(cfg, isWrite);
+            StorageCredentials creds = getAccountCredentials(cfg);
             client = new CloudAppendBlob(uri, creds);
         }
         return client;
     }
 
-    public static CloudPageBlob createPageBlobClient(BlobServiceConfiguration 
cfg, boolean isWrite)
+    public static CloudPageBlob createPageBlobClient(BlobServiceConfiguration 
cfg)
         throws Exception {
-        CloudPageBlob client = (CloudPageBlob) getConfiguredClient(cfg, 
BlobType.pageblob, isWrite);
+        CloudPageBlob client = (CloudPageBlob) getConfiguredClient(cfg);
         if (client == null) {
             URI uri = prepareStorageBlobUri(cfg);
-            StorageCredentials creds = getAccountCredentials(cfg, isWrite);
+            StorageCredentials creds = getAccountCredentials(cfg);
             client = new CloudPageBlob(uri, creds);
         }
         return client;
     }
 
-    public static CloudBlob getConfiguredClient(BlobServiceConfiguration cfg,
-                                                BlobType blobType,
-                                                boolean isWrite) {
+    public static CloudBlob getConfiguredClient(BlobServiceConfiguration cfg) {
         CloudBlob client = cfg.getAzureBlobClient();
         if (client != null) {
             Class<?> expectedCls = null;
-            if (blobType == BlobType.blockblob) {
+            if (cfg.getBlobType() == BlobType.blockblob) {
                 expectedCls = CloudBlockBlob.class;
-            } else if (blobType == BlobType.appendblob) {
+            } else if (cfg.getBlobType() == BlobType.appendblob) {
                 expectedCls = CloudAppendBlob.class;
-            } else if (blobType == BlobType.pageblob) {
+            } else if (cfg.getBlobType() == BlobType.pageblob) {
                 expectedCls = CloudPageBlob.class;
             }
             if (client.getClass() != expectedCls) {
-                throw new IllegalArgumentException("Invalid Blob Client Type");
+                throw new IllegalArgumentException("Invalid Client Type");
             }
             if (!client.getUri().equals(prepareStorageBlobUri(cfg))) {
-                throw new IllegalArgumentException("Invalid Client Uri");
-            }
-            if (client.getServiceClient().getCredentials() == null && (isWrite 
|| !cfg.isPublicForRead())) {
-                throw new IllegalArgumentException("Storage credentials must 
be specified");
+                throw new IllegalArgumentException("Invalid Client URI");
             }
         }
         return client;
     }
 
-    public static StorageCredentials 
getAccountCredentials(BlobServiceConfiguration cfg,
-                                                           boolean isWrite) {
-        if (cfg.getCredentials() == null && (isWrite || 
!cfg.isPublicForRead())) {
-            throw new IllegalArgumentException("Storage credentials must be 
specified");
-        }
+    public static StorageCredentials 
getAccountCredentials(BlobServiceConfiguration cfg) {
         return cfg.getCredentials();
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/533d67fd/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceComponentConfigurationTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceComponentConfigurationTest.java
 
b/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceComponentConfigurationTest.java
index 944de0b..326a4e8 100644
--- 
a/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceComponentConfigurationTest.java
+++ 
b/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceComponentConfigurationTest.java
@@ -21,6 +21,8 @@ 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;
@@ -36,7 +38,8 @@ public class BlobServiceComponentConfigurationTest extends 
CamelTestSupport {
     @Test
     public void testCreateEndpointWithMinConfigForClientOnly() throws 
Exception {
         CloudBlockBlob client = 
-            new 
CloudBlockBlob(URI.create("https://camelazure.blob.core.windows.net/container/blob";));
+            new 
CloudBlockBlob(URI.create("https://camelazure.blob.core.windows.net/container/blob";),
+                               newAccountKeyCredentials());
         
         JndiRegistry registry = (JndiRegistry) 
((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
         registry.bind("azureBlobClient", client);
@@ -142,6 +145,46 @@ public class BlobServiceComponentConfigurationTest extends 
CamelTestSupport {
         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";));
+        
+        JndiRegistry registry = (JndiRegistry) 
((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
+        registry.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 {
+        JndiRegistry registry = (JndiRegistry) 
((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
+        registry.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 {
@@ -193,9 +236,12 @@ public class BlobServiceComponentConfigurationTest extends 
CamelTestSupport {
     }
     
     private void registerCredentials() {
-        StorageCredentials creds = new 
StorageCredentialsAccountAndKey("camelazure", 
-                                                                       
Base64.encode("key".getBytes()));
         JndiRegistry registry = (JndiRegistry) 
((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
-        registry.bind("creds", creds);
+        registry.bind("creds", newAccountKeyCredentials());
     }
+    private StorageCredentials newAccountKeyCredentials() {
+        return new StorageCredentialsAccountAndKey("camelazure", 
+                                                   
Base64.encode("key".getBytes()));
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/533d67fd/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceUtilTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceUtilTest.java
 
b/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceUtilTest.java
index 26bba46..81a9f80 100644
--- 
a/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceUtilTest.java
+++ 
b/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceUtilTest.java
@@ -20,6 +20,8 @@ import java.net.URI;
 
 import com.microsoft.azure.storage.StorageCredentials;
 import com.microsoft.azure.storage.StorageCredentialsAccountAndKey;
+import com.microsoft.azure.storage.blob.CloudAppendBlob;
+import com.microsoft.azure.storage.blob.CloudBlockBlob;
 import com.microsoft.azure.storage.core.Base64;
 import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.impl.PropertyPlaceholderDelegateRegistry;
@@ -40,10 +42,63 @@ public class BlobServiceUtilTest extends CamelTestSupport {
         
assertEquals("https://camelazure.blob.core.windows.net/container/blob";, 
uri.toString());
     }
 
+    @Test
+    public void testGetConfiguredClient() throws Exception {
+        CloudBlockBlob client = 
+            new 
CloudBlockBlob(URI.create("https://camelazure.blob.core.windows.net/container/blob";));
+        
+        JndiRegistry registry = (JndiRegistry) 
((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
+        registry.bind("azureBlobClient", client);
+        
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpoint = 
+            (BlobServiceEndpoint) 
component.createEndpoint("azure-blob://camelazure/container/blob?azureBlobClient=#azureBlobClient&publicForRead=true");
+        assertSame(client, 
BlobServiceUtil.getConfiguredClient(endpoint.getConfiguration()));
+    }
+    @Test
+    public void testGetConfiguredClientTypeMismatch() throws Exception {
+        CloudBlockBlob client = 
+            new 
CloudBlockBlob(URI.create("https://camelazure.blob.core.windows.net/container/blob";));
+        
+        JndiRegistry registry = (JndiRegistry) 
((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
+        registry.bind("azureBlobClient", client);
+        
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpoint = 
+            (BlobServiceEndpoint) 
component.createEndpoint("azure-blob://camelazure/container/blob?azureBlobClient=#azureBlobClient&publicForRead=true"
+                                                           + 
"&blobType=appendBlob");
+        try {
+            BlobServiceUtil.getConfiguredClient(endpoint.getConfiguration());
+            fail();
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Invalid Client Type", ex.getMessage());
+        }
+    }
+    @Test
+    public void testGetConfiguredClientUriMismatch() throws Exception {
+        CloudAppendBlob client = 
+            new 
CloudAppendBlob(URI.create("https://camelazure.blob.core.windows.net/container/blob";));
+        
+        JndiRegistry registry = (JndiRegistry) 
((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
+        registry.bind("azureBlobClient", client);
+        
+        BlobServiceComponent component = new BlobServiceComponent(context);
+        BlobServiceEndpoint endpoint = 
+            (BlobServiceEndpoint) 
component.createEndpoint("azure-blob://camelazure/container/blob2?azureBlobClient=#azureBlobClient&publicForRead=true"
+                                                           + 
"&blobType=appendBlob");
+        try {
+            BlobServiceUtil.getConfiguredClient(endpoint.getConfiguration());
+            fail();
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Invalid Client URI", ex.getMessage());
+        }
+    }
+
     private void registerCredentials() {
         StorageCredentials creds = new 
StorageCredentialsAccountAndKey("camelazure", 
                                                                        
Base64.encode("key".getBytes()));
         JndiRegistry registry = (JndiRegistry) 
((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
         registry.bind("creds", creds);
     }
-}
\ No newline at end of file
+
+}

Reply via email to