/** * Acquires a lease on a blob. The lease ID is NULL initially. * @param leaseTimeInSec The time in seconds you want to acquire the lease for. * @param leaseId Proposed ID you want to acquire the lease with, null if not proposed. * @return String that represents lease ID. Null if acquireLease is unsuccessful because the blob is leased already. * @throws AzureException If a Azure storage service error occurred. This includes the case where the blob you're trying to lease does not exist. */ public String acquireLease(int leaseTimeInSec, String leaseId) { try { String id = leaseBlob.acquireLease(leaseTimeInSec, leaseId); LOG.info("Acquired lease with lease id = " + id); return id; } catch (StorageException storageException) { int httpStatusCode = storageException.getHttpStatusCode(); if (httpStatusCode == HttpStatus.CONFLICT_409) { LOG.info("The blob you're trying to acquire is leased already.", storageException.getMessage()); } else if (httpStatusCode == HttpStatus.NOT_FOUND_404) { LOG.error("The blob you're trying to lease does not exist.", storageException); throw new AzureException(storageException); } else { LOG.error("Error acquiring lease!", storageException); throw new AzureException(storageException); } } return null; }
private static void testMetadataFailures(CloudFileDirectory directory, String key, String value, boolean badKey) throws URISyntaxException { directory.getMetadata().put(key, value); try { directory.uploadMetadata(); fail(SR.METADATA_KEY_INVALID); } catch (StorageException e) { if (badKey) { assertEquals(SR.METADATA_KEY_INVALID, e.getMessage()); } else { assertEquals(SR.METADATA_VALUE_INVALID, e.getMessage()); } } directory.getMetadata().remove(key); }
private static void testMetadataFailures(CloudBlobContainer container, String key, String value, boolean badKey) { container.getMetadata().put(key, value); try { container.uploadMetadata(); fail(SR.METADATA_KEY_INVALID); } catch (StorageException e) { if (badKey) { assertEquals(SR.METADATA_KEY_INVALID, e.getMessage()); } else { assertEquals(SR.METADATA_VALUE_INVALID, e.getMessage()); } } container.getMetadata().remove(key); }
private static void testMetadataFailures(CloudFileShare share, String key, String value, boolean badKey) { share.getMetadata().put(key, value); try { share.uploadMetadata(); fail(SR.METADATA_KEY_INVALID); } catch (StorageException e) { if (badKey) { assertEquals(SR.METADATA_KEY_INVALID, e.getMessage()); } else { assertEquals(SR.METADATA_VALUE_INVALID, e.getMessage()); } } share.getMetadata().remove(key); }
@Test public void testContainerLeaseInvalidParams() throws StorageException, URISyntaxException { try { this.container.acquireLease(100, null); } catch(StorageException ex) { assertEquals("The value of the parameter 'leaseTimeInSeconds' should be between 15 and 60.", ex.getMessage()); } try { this.container.breakLease(100); } catch(StorageException ex) { assertEquals("The value of the parameter 'breakPeriodInSeconds' should be between 0 and 60.", ex.getMessage()); } }
private void testFileAccountSas(final boolean useHttps, final int bits) throws InvalidKeyException, StorageException, URISyntaxException, IOException { SharedAccessAccountPolicy policy = generatePolicy(bits, SharedAccessAccountService.FILE, null, null); this.fileShare = this.fileClient.getShareReference("filetest" + bits); try { testFileAccountSas(this.fileShare, useHttps, policy); } catch (StorageException ex) { if (bits < AccountSasTests.OBJECT_CODE || bits % AccountSasTests.OBJECT_CODE == AccountSasTests.EMPTY_PERMS_CODE) { // Expected failure if permissions or resource type is empty. assertEquals(AccountSasTests.QUERY_PARAM_MISSING_MESSAGE, ex.getMessage()); } else { throw ex; } } finally { this.fileShare.deleteIfExists(); } }
private void testBlobAccountSas(final boolean useHttps, final int bits) throws InvalidKeyException, StorageException, URISyntaxException, IOException { SharedAccessAccountPolicy policy = generatePolicy(bits, SharedAccessAccountService.BLOB, null, null); this.blobContainer = this.blobClient.getContainerReference("blobtest" + bits); try { testBlobAccountSas(this.blobContainer, useHttps, policy); } catch (StorageException ex) { if (bits < AccountSasTests.OBJECT_CODE || bits % AccountSasTests.OBJECT_CODE == AccountSasTests.EMPTY_PERMS_CODE) { // Expected failure if permissions or resource type is empty. assertEquals(AccountSasTests.QUERY_PARAM_MISSING_MESSAGE, ex.getMessage()); } else { throw ex; } } finally { this.blobContainer.deleteIfExists(); } }
private void testQueueAccountSas(final boolean useHttps, final int bits) throws InvalidKeyException, StorageException, URISyntaxException, IOException, InterruptedException { SharedAccessAccountPolicy policy = generatePolicy(bits, SharedAccessAccountService.QUEUE, null, null); this.queueQueue = this.queueClient.getQueueReference("queuetest" + bits); try { testQueueAccountSas(this.queueQueue, useHttps, policy); } catch (StorageException ex) { if (bits < AccountSasTests.OBJECT_CODE || bits % AccountSasTests.OBJECT_CODE == AccountSasTests.EMPTY_PERMS_CODE) { // Expected failure if permissions or resource type is empty. assertEquals(AccountSasTests.QUERY_PARAM_MISSING_MESSAGE, ex.getMessage()); } else { throw ex; } } finally { this.queueQueue.deleteIfExists(); } }
private void testTableAccountSas(final boolean useHttps, final int bits) throws InvalidKeyException, StorageException, URISyntaxException, IOException { SharedAccessAccountPolicy policy = generatePolicy(bits, SharedAccessAccountService.TABLE, null, null); this.tableTable = this.tableClient.getTableReference("tabletest" + bits); try { testTableAccountSas(this.tableTable, useHttps, policy); } catch (StorageException ex) { if (bits < AccountSasTests.OBJECT_CODE || bits % AccountSasTests.OBJECT_CODE == AccountSasTests.EMPTY_PERMS_CODE) { // Expected failure if permissions or resource type is empty. assertEquals(AccountSasTests.QUERY_PARAM_MISSING_MESSAGE, ex.getMessage()); } else { throw ex; } } finally { this.fileShare.deleteIfExists(); } }
/** * Test file creation with invalid sizes. * * @throws URISyntaxException * @throws StorageException */ public void testCloudFileCreateInvalidSize() throws StorageException, URISyntaxException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); try { file.create(-1); fail("Creating a file with size<0 should fail"); } catch (StorageException e) { assertEquals(e.getHttpStatusCode(), 400); assertEquals(e.getMessage(), "The value for one of the HTTP headers is not in the correct format."); } try { file.create(1L * 1024 * 1024 * 1024 * 1024 + 1); fail("Creating a file with size>1TB should fail"); } catch (StorageException e) { assertEquals(e.getHttpStatusCode(), 400); assertEquals(e.getMessage(), "The value for one of the HTTP headers is not in the correct format."); } }
@Test public void testTableOperationRetrieveJsonNoMetadataResolverFail() { // set custom property resolver which throws this.options.setPropertyResolver(new ThrowingPropertyResolver()); try { this.table.execute(TableOperation.retrieve(this.ent.getPartitionKey(), this.ent.getRowKey(), Class1.class), this.options, null); fail("Invalid property resolver should throw"); } catch (StorageException e) { assertEquals( "The custom property resolver delegate threw an exception. Check the inner exception for more details.", e.getMessage()); assertTrue(e.getCause().getClass() == IllegalArgumentException.class); } }
@Test public void testTableOperationRetrieveJsonNoMetadataFail() { // set custom property resolver this.options.setPropertyResolver(new CustomPropertyResolver()); try { this.table.execute(TableOperation.retrieve(this.ent.getPartitionKey(), this.ent.getRowKey(), Class1.class), this.options, null); fail("Invalid property resolver should throw"); } catch (StorageException e) { assertEquals("Failed to parse property 'fooint' with value '1234' as type 'Edm.Guid'", e.getMessage()); } }
/** * Create a container * * @throws StorageException * @throws URISyntaxException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testCloudBlobContainerCreate() throws StorageException { this.container.create(); try { this.container.create(); fail("Should not be able to create twice."); } catch (StorageException e) { assertEquals(e.getErrorCode(), "ContainerAlreadyExists"); assertEquals(e.getHttpStatusCode(), 409); assertEquals(e.getMessage(), "The specified container already exists."); } }
/** * Try to create a share after it is created * * @throws StorageException */ @Test public void testCloudFileShareCreate() throws StorageException { this.share.create(); assertTrue(this.share.exists()); try { this.share.create(); fail("Share already existed but was created anyway."); } catch (StorageException e) { assertEquals(e.getErrorCode(), "ShareAlreadyExists"); assertEquals(e.getHttpStatusCode(), 409); assertEquals(e.getMessage(), "The specified share already exists."); } }
@Test public void testRetryOn304() throws StorageException, IOException, URISyntaxException { OperationContext operationContext = new OperationContext(); operationContext.getRetryingEventHandler().addListener(new StorageEvent<RetryingEvent>() { @Override public void eventOccurred(RetryingEvent eventArg) { fail("Request should not be retried."); } }); CloudBlobContainer container = BlobTestHelper.getRandomContainerReference(); try { container.create(); CloudBlockBlob blockBlobRef = (CloudBlockBlob) BlobTestHelper.uploadNewBlob(container, BlobType.BLOCK_BLOB, "originalBlob", 1024, null); AccessCondition accessCondition = AccessCondition.generateIfNoneMatchCondition(blockBlobRef.getProperties().getEtag()); blockBlobRef.download(new ByteArrayOutputStream(), accessCondition, null, operationContext); fail("Download should fail with a 304."); } catch (StorageException ex) { assertEquals("The condition specified using HTTP conditional header(s) is not met.", ex.getMessage()); } finally { container.deleteIfExists(); } }
@Test public void testTableDeleteWhenExistAndNotExists() throws StorageException, URISyntaxException { CloudTableClient tClient = TableTestHelper.createCloudTableClient(); String tableName = TableTestHelper.generateRandomTableName(); CloudTable table = tClient.getTableReference(tableName); try { // Should fail as it doesnt already exists try { table.delete(); fail(); } catch (StorageException ex) { assertEquals(ex.getMessage(), "Not Found"); } table.create(); assertTrue(table.exists()); table.delete(); assertFalse(table.exists()); } finally { table.deleteIfExists(); } }
/** * Ensures we cannot create a directory without creating its parent directory first. * * @throws URISyntaxException * @throws StorageException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class, CloudTests.class }) public void testCloudFileDirectoryCreateDirectoryWithoutParent() throws URISyntaxException, StorageException { CloudFileDirectory dir1 = this.share.getRootDirectoryReference().getDirectoryReference("Dir1"); CloudFileDirectory dir2 = this.share.getRootDirectoryReference().getDirectoryReference("Dir1/Dir2"); try { dir2.create(); fail("Directory shouldn't be created when the parent directory wasn't created."); } catch (StorageException e) { assertEquals(e.getErrorCode(), "ParentNotFound"); assertEquals(e.getHttpStatusCode(), 404); assertEquals(e.getMessage(), "The specified parent path does not exist."); } dir1.create(); dir2.create(); }
/** * Ensures you cannot create a file without first creating the directory it is located within. * * @throws URISyntaxException * @throws StorageException */ public void testCloudFileDirectoryFileCreateWithoutDirectory() throws URISyntaxException, StorageException { CloudFileDirectory dir = this.share.getRootDirectoryReference().getDirectoryReference("Dir1"); CloudFile file = dir.getFileReference("file1"); try { file.create(0); fail("File shouldn't be created in a directory that wasn't created."); } catch (StorageException e) { assertEquals(e.getErrorCode(), "ParentNotFound"); assertEquals(e.getHttpStatusCode(), 404); assertEquals(e.getMessage(), "The specified parent path does not exist."); } // File creation directly in the share should pass. CloudFile file2 = this.share.getRootDirectoryReference().getFileReference("file2"); file2.create(0); dir.create(); file.create(0); }
@Test public void testOpenOutputStreamNoArgs() throws URISyntaxException, StorageException { String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); CloudPageBlob pageBlob = this.container.getPageBlobReference(blobName); try { pageBlob.openWriteExisting(); } catch (StorageException ex) { assertEquals("The specified blob does not exist.", ex.getMessage()); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode()); } pageBlob.openWriteNew(1024); pageBlob.openWriteExisting(); CloudPageBlob pageBlob2 = this.container.getPageBlobReference(blobName); pageBlob2.downloadAttributes(); assertEquals(1024, pageBlob2.getProperties().getLength()); assertEquals(BlobType.PAGE_BLOB, pageBlob2.getProperties().getBlobType()); }
@Test public void testOpenOutputStreamNoArgs() throws URISyntaxException, StorageException { String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); CloudAppendBlob appendBlob = this.container .getAppendBlobReference(blobName); try { appendBlob.openWriteExisting(); } catch (StorageException ex) { assertEquals("The specified blob does not exist.", ex.getMessage()); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode()); } appendBlob.openWriteNew(); appendBlob.openWriteExisting(); CloudAppendBlob appendBlob2 = this.container .getAppendBlobReference(blobName); appendBlob2.downloadAttributes(); assertEquals(0, appendBlob2.getProperties().getLength()); assertEquals(BlobType.APPEND_BLOB, appendBlob2.getProperties().getBlobType()); }