From de4a89ffd30d23667d28e3c3b5b068e87a4f2d06 Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Wed, 26 Aug 2020 16:51:47 +0530 Subject: [PATCH 1/5] changing method signature to add container name --- .../osdu/azure/blobstorage/BlobStore.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java index cf88d09a..8f795771 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java @@ -41,19 +41,19 @@ import java.util.Collections;; * * String readFromBlobExample() * { - * String content = blobStorage.readFromBlob("dataPartitionId", "filePath"); + * String content = blobStorage.readFromBlob("dataPartitionId", "filePath", "containerName"); * if (content != null) * return content; * } * * void writeToBlobExample() * { - * blobStorage.writeToBlob("dataPartitionId", "filePath", "content"); + * blobStorage.writeToBlob("dataPartitionId", "filePath", "content", "containerName"); * } * * void deleteFromBlobExample() * { - * Boolean success = blobStorage.deleteFromBlob("dataPartitionId", "filePath"); + * Boolean success = blobStorage.deleteFromBlob("dataPartitionId", "filePath", "containerName"); * } * } * @@ -75,9 +75,13 @@ public class BlobStore { * * @param filePath Path of file to be read. * @param dataPartitionId Data partition id + * @param containerName Name of the storage container * @return the content of file with provided file path. */ - public String readFromBlob(final String dataPartitionId, final String filePath) { + public String readFromBlob( + final String dataPartitionId, + final String filePath, + final String containerName) { BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); try (ByteArrayOutputStream downloadStream = new ByteArrayOutputStream()) { @@ -108,9 +112,13 @@ public class BlobStore { * * @param filePath Path of file to be deleted. * @param dataPartitionId Data partition id + * @param containerName Name of the storage container * @return boolean indicating whether the deletion of given file was successful or not. */ - public boolean deleteFromBlob(final String dataPartitionId, final String filePath) { + public boolean deleteFromBlob( + final String dataPartitionId, + final String filePath, + final String containerName) { BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); try { @@ -134,10 +142,13 @@ public class BlobStore { * @param filePath Path of file to be written at. * @param content Content to be written in the file. * @param dataPartitionId Data partition id + * @param containerName Name of the storage container */ - public void writeToBlob(final String dataPartitionId, - final String filePath, - final String content) { + public void writeToBlob( + final String dataPartitionId, + final String filePath, + final String content, + final String containerName) { byte[] bytes = content.getBytes(StandardCharsets.UTF_8); int bytesSize = bytes.length; BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); -- GitLab From 1fb047420135ff15e503a3e60686e0902ea1df9e Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Wed, 26 Aug 2020 16:56:59 +0530 Subject: [PATCH 2/5] updating UTs --- .../osdu/azure/blobstorage/BlobStoreTest.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java index 453367ae..592cdf16 100644 --- a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java +++ b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java @@ -44,6 +44,7 @@ public class BlobStoreTest { private static final String PARTITION_ID = "dataPartitionId"; private static final String FILE_PATH = "filePath"; private static final String CONTENT = "hello world"; + private static final String STORAGE_CONTAINER_NAME = "containerName"; @InjectMocks BlobStore blobStore; @@ -80,7 +81,7 @@ public class BlobStoreTest { { doThrow(AppException.class).when(blobContainerClientFactory).getClient(eq(PARTITION_ID)); try { - String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH); + String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -91,7 +92,7 @@ public class BlobStoreTest { @Test public void readFromBlob_Success() { - String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH); + String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); ArgumentCaptor outputStream = ArgumentCaptor.forClass(ByteArrayOutputStream.class); // validate that the download method is being invoked appropriately. @@ -104,7 +105,7 @@ public class BlobStoreTest { BlobStorageException exception = mockStorageException(BlobErrorCode.BLOB_NOT_FOUND); doThrow(exception).when(blockBlobClient).download(any()); try { - String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH); + String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(404, ex.getError().getCode()); } catch (Exception ex) { @@ -118,7 +119,7 @@ public class BlobStoreTest { BlobStorageException exception = mockStorageException(BlobErrorCode.INTERNAL_ERROR); doThrow(exception).when(blockBlobClient).download(any()); try { - String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH); + String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -131,7 +132,7 @@ public class BlobStoreTest { { doThrow(AppException.class).when(blobContainerClientFactory).getClient(eq(PARTITION_ID)); try { - blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH); + blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -145,7 +146,7 @@ public class BlobStoreTest { BlobStorageException exception = mockStorageException(BlobErrorCode.BLOB_NOT_FOUND); doThrow(exception).when(blockBlobClient).delete(); try { - blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH); + blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(404, ex.getError().getCode()); } catch (Exception ex) { @@ -159,7 +160,7 @@ public class BlobStoreTest { BlobStorageException exception = mockStorageException(BlobErrorCode.INTERNAL_ERROR); doThrow(exception).when(blockBlobClient).delete(); try { - blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH); + blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -172,7 +173,7 @@ public class BlobStoreTest { { doNothing().when(blockBlobClient).delete(); try { - blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH); + blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (Exception ex) { fail("should not get any exception."); } @@ -183,7 +184,7 @@ public class BlobStoreTest { { doThrow(AppException.class).when(blobContainerClientFactory).getClient(eq(PARTITION_ID)); try { - blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT); + blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -197,7 +198,7 @@ public class BlobStoreTest { BlobStorageException exception = mockStorageException(BlobErrorCode.INTERNAL_ERROR); doThrow(exception).when(blockBlobClient).upload(any(), anyLong(), eq(true)); try { - blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT); + blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -210,7 +211,7 @@ public class BlobStoreTest { { doReturn(blockBlobItem).when(blockBlobClient).upload(any(), anyLong(), eq(true)); try { - blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT); + blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT, STORAGE_CONTAINER_NAME); } catch (Exception ex) { fail("should not get any exception."); } -- GitLab From e869a98c70f0ba5d30648c9d7954673dcd8e0dd2 Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Wed, 26 Aug 2020 18:01:58 +0530 Subject: [PATCH 3/5] updating UTs to use the blobServiceCleintFactory --- .../osdu/azure/blobstorage/BlobStore.java | 17 ++++++---- .../osdu/azure/blobstorage/BlobStoreTest.java | 32 +++++++++++++++---- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java index 8f795771..e9211f59 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java @@ -15,6 +15,7 @@ package org.opengroup.osdu.azure.blobstorage; import com.azure.storage.blob.BlobContainerClient; +import com.azure.storage.blob.BlobServiceClient; import com.azure.storage.blob.models.BlobErrorCode; import com.azure.storage.blob.models.BlobStorageException; import com.azure.storage.blob.specialized.BlockBlobClient; @@ -64,7 +65,7 @@ import java.util.Collections;; public class BlobStore { @Autowired - private IBlobContainerClientFactory blobContainerClientFactory; + private IBlobServiceClientFactory blobServiceClientFactory; @Autowired private ILogger logger; @@ -82,7 +83,7 @@ public class BlobStore { final String dataPartitionId, final String filePath, final String containerName) { - BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); + BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId, containerName); BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); try (ByteArrayOutputStream downloadStream = new ByteArrayOutputStream()) { blockBlobClient.download(downloadStream); @@ -119,7 +120,7 @@ public class BlobStore { final String dataPartitionId, final String filePath, final String containerName) { - BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); + BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId, containerName); BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); try { blockBlobClient.delete(); @@ -151,7 +152,7 @@ public class BlobStore { final String containerName) { byte[] bytes = content.getBytes(StandardCharsets.UTF_8); int bytesSize = bytes.length; - BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); + BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId, containerName); BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); try (ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes)) { blockBlobClient.upload(dataStream, bytesSize, true); @@ -168,12 +169,14 @@ public class BlobStore { /** * - * @param dataPartitionId Data partition id + * @param dataPartitionId Data partition id. + * @param containerName Name of storage container. * @return blob container client corresponding to the dataPartitionId. */ - private BlobContainerClient getBlobContainerClient(final String dataPartitionId) { + private BlobContainerClient getBlobContainerClient(final String dataPartitionId, final String containerName) { try { - return blobContainerClientFactory.getClient(dataPartitionId); + BlobServiceClient serviceClient = blobServiceClientFactory.getBlobServiceClient(dataPartitionId); + return serviceClient.getBlobContainerClient(containerName); } catch (Exception ex) { String errorMessage = "Error creating creating blob container client."; logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); diff --git a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java index 592cdf16..d15afc7d 100644 --- a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java +++ b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java @@ -16,6 +16,7 @@ package org.opengroup.osdu.azure.blobstorage; import com.azure.storage.blob.BlobClient; import com.azure.storage.blob.BlobContainerClient; +import com.azure.storage.blob.BlobServiceClient; import com.azure.storage.blob.models.BlobErrorCode; import com.azure.storage.blob.models.BlobStorageException; import com.azure.storage.blob.models.BlockBlobItem; @@ -50,7 +51,10 @@ public class BlobStoreTest { BlobStore blobStore; @Mock - IBlobContainerClientFactory blobContainerClientFactory; + IBlobServiceClientFactory blobServiceClientFactory; + + @Mock + BlobServiceClient blobServiceClient; @Mock BlobContainerClient blobContainerClient; @@ -70,16 +74,30 @@ public class BlobStoreTest { @BeforeEach void init() { initMocks(this); - lenient().doReturn(blobContainerClient).when(blobContainerClientFactory).getClient(PARTITION_ID); - lenient().doReturn(blobClient).when(blobContainerClient).getBlobClient(FILE_PATH); - lenient().doReturn(blockBlobClient).when(blobClient).getBlockBlobClient(); + lenient().when(blobClient.getBlockBlobClient()).thenReturn(blockBlobClient); + lenient().when(blobContainerClient.getBlobClient(FILE_PATH)).thenReturn(blobClient); + lenient().when(blobServiceClient.getBlobContainerClient(STORAGE_CONTAINER_NAME)).thenReturn(blobContainerClient); + lenient().when(blobServiceClientFactory.getBlobServiceClient(PARTITION_ID)).thenReturn(blobServiceClient); lenient().doNothing().when(logger).warning(eq("azure-core-lib"), any(), anyMap()); } @Test public void readFromBlob_ErrorCreatingBlobContainerClient() { - doThrow(AppException.class).when(blobContainerClientFactory).getClient(eq(PARTITION_ID)); + doThrow(AppException.class).when(blobServiceClientFactory).getBlobServiceClient(eq(PARTITION_ID)); + try { + String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); + } catch (AppException ex) { + assertEquals(500, ex.getError().getCode()); + } catch (Exception ex) { + fail("should not get different error code"); + } + } + + @Test + public void readFromBlob_ErrorCreatingBlobContainerClient_FromServiceClient() + { + doThrow(AppException.class).when(blobServiceClient).getBlobContainerClient(eq(STORAGE_CONTAINER_NAME)); try { String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { @@ -130,7 +148,7 @@ public class BlobStoreTest { @Test public void deleteFromBlob_ErrorCreatingBlobContainerClient() { - doThrow(AppException.class).when(blobContainerClientFactory).getClient(eq(PARTITION_ID)); + doThrow(AppException.class).when(blobServiceClientFactory).getBlobServiceClient(eq(PARTITION_ID)); try { blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { @@ -182,7 +200,7 @@ public class BlobStoreTest { @Test public void writeToBlob_ErrorCreatingBlobContainerClient() { - doThrow(AppException.class).when(blobContainerClientFactory).getClient(eq(PARTITION_ID)); + doThrow(AppException.class).when(blobServiceClientFactory).getBlobServiceClient(eq(PARTITION_ID)); try { blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT, STORAGE_CONTAINER_NAME); } catch (AppException ex) { -- GitLab From 0816c8268bdc8a9090adf51a442a92c7f9ea2c83 Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Wed, 26 Aug 2020 18:02:39 +0530 Subject: [PATCH 4/5] updating the artifact version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f91cd79f..955c93f1 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ org.opengroup.osdu core-lib-azure jar - 0.0.21 + 0.0.22 core-lib-azure -- GitLab From d18bb60a1bb06a6d6957af9dfe7afb66e435b56a Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Fri, 28 Aug 2020 13:14:49 +0530 Subject: [PATCH 5/5] adding equivalent methods instead of replacing existing ones --- .../osdu/azure/blobstorage/BlobStore.java | 134 +++++++++++- .../osdu/azure/blobstorage/BlobStoreTest.java | 191 +++++++++++++++--- 2 files changed, 295 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java index e9211f59..fb2748f3 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java @@ -42,19 +42,36 @@ import java.util.Collections;; * * String readFromBlobExample() * { - * String content = blobStorage.readFromBlob("dataPartitionId", "filePath", "containerName"); + * String content = blobStorage.readFromBlob("dataPartitionId", "filePath"); * if (content != null) * return content; * } * * void writeToBlobExample() * { - * blobStorage.writeToBlob("dataPartitionId", "filePath", "content", "containerName"); + * blobStorage.writeToBlob("dataPartitionId", "filePath", "content"); * } * * void deleteFromBlobExample() * { - * Boolean success = blobStorage.deleteFromBlob("dataPartitionId", "filePath", "containerName"); + * Boolean success = blobStorage.deleteFromBlob("dataPartitionId", "filePath"); + * } + * + * String readFromStorageContainerExamole() + * { + * String content = blobStorage.readFromStorageContainer("dataPartitionId", "filePath", "containerName"); + * if (content != null) + * return content; + * } + * + * void writeToStorageContainerExample() + * { + * blobStorage.writeToStorageContainer("dataPartitionId", "filePath", "content", "containerName"); + * } + * + * void deleteFromStorageContainerExample() + * { + * Boolean success = blobStorage.deleteFromStorageContainer("dataPartitionId", "filePath", "containerName"); * } * } * @@ -64,6 +81,9 @@ import java.util.Collections;; @Lazy public class BlobStore { + @Autowired + private IBlobContainerClientFactory blobContainerClientFactory; + @Autowired private IBlobServiceClientFactory blobServiceClientFactory; @@ -72,6 +92,90 @@ public class BlobStore { private static final String LOG_PREFIX = "azure-core-lib"; + /** + * + * @param filePath Path of file to be read. + * @param dataPartitionId Data partition id + * @return the content of file with provided file path. + */ + public String readFromBlob(final String dataPartitionId, final String filePath) { + BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); + BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); + try (ByteArrayOutputStream downloadStream = new ByteArrayOutputStream()) { + blockBlobClient.download(downloadStream); + return downloadStream.toString(StandardCharsets.UTF_8.name()); + } catch (BlobStorageException ex) { + if (ex.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND)) { + String errorMessage = "Specified blob was not found"; + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); + throw new AppException(404, errorMessage, ex.getMessage(), ex); + } else { + String errorMessage = "Failed to read specified blob"; + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); + throw new AppException(500, errorMessage, ex.getMessage(), ex); + } + } catch (UnsupportedEncodingException ex) { + String errorMessage = String.format("Encoding was not correct for item with name=%s", filePath); + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); + throw new AppException(400, errorMessage, ex.getMessage(), ex); + } catch (IOException ex) { + String errorMessage = String.format("Malformed document for item with name=%s", filePath); + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); + throw new AppException(500, errorMessage, ex.getMessage(), ex); + } + } + + /** + * + * @param filePath Path of file to be deleted. + * @param dataPartitionId Data partition id + * @return boolean indicating whether the deletion of given file was successful or not. + */ + public boolean deleteFromBlob(final String dataPartitionId, final String filePath) { + BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); + BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); + try { + blockBlobClient.delete(); + return true; + } catch (BlobStorageException ex) { + if (ex.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND)) { + String errorMessage = "Specified blob was not found"; + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); + throw new AppException(404, errorMessage, ex.getMessage(), ex); + } else { + String errorMessage = "Failed to delete blob"; + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); + throw new AppException(500, errorMessage, ex.getMessage(), ex); + } + } + } + + /** + * + * @param filePath Path of file to be written at. + * @param content Content to be written in the file. + * @param dataPartitionId Data partition id + */ + public void writeToBlob(final String dataPartitionId, + final String filePath, + final String content) { + byte[] bytes = content.getBytes(StandardCharsets.UTF_8); + int bytesSize = bytes.length; + BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); + BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); + try (ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes)) { + blockBlobClient.upload(dataStream, bytesSize, true); + } catch (BlobStorageException ex) { + String errorMessage = "Failed to upload file content."; + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); + throw new AppException(500, errorMessage, ex.getMessage(), ex); + } catch (IOException ex) { + String errorMessage = String.format("Malformed document for item with name=%s", filePath); + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); + throw new AppException(500, errorMessage, ex.getMessage(), ex); + } + } + /** * * @param filePath Path of file to be read. @@ -79,7 +183,7 @@ public class BlobStore { * @param containerName Name of the storage container * @return the content of file with provided file path. */ - public String readFromBlob( + public String readFromStorageContainer( final String dataPartitionId, final String filePath, final String containerName) { @@ -116,7 +220,7 @@ public class BlobStore { * @param containerName Name of the storage container * @return boolean indicating whether the deletion of given file was successful or not. */ - public boolean deleteFromBlob( + public boolean deleteFromStorageContainer( final String dataPartitionId, final String filePath, final String containerName) { @@ -145,7 +249,7 @@ public class BlobStore { * @param dataPartitionId Data partition id * @param containerName Name of the storage container */ - public void writeToBlob( + public void writeToStorageContainer( final String dataPartitionId, final String filePath, final String content, @@ -167,6 +271,22 @@ public class BlobStore { } } + + /** + * + * @param dataPartitionId Data partition id + * @return blob container client corresponding to the dataPartitionId. + */ + private BlobContainerClient getBlobContainerClient(final String dataPartitionId) { + try { + return blobContainerClientFactory.getClient(dataPartitionId); + } catch (Exception ex) { + String errorMessage = "Error creating creating blob container client."; + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); + throw new AppException(500, errorMessage, ex.getMessage(), ex); + } + } + /** * * @param dataPartitionId Data partition id. @@ -183,5 +303,5 @@ public class BlobStore { throw new AppException(500, errorMessage, ex.getMessage(), ex); } } -} +} diff --git a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java index d15afc7d..b7c1f53c 100644 --- a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java +++ b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java @@ -50,6 +50,9 @@ public class BlobStoreTest { @InjectMocks BlobStore blobStore; + @Mock + IBlobContainerClientFactory blobContainerClientFactory; + @Mock IBlobServiceClientFactory blobServiceClientFactory; @@ -78,15 +81,157 @@ public class BlobStoreTest { lenient().when(blobContainerClient.getBlobClient(FILE_PATH)).thenReturn(blobClient); lenient().when(blobServiceClient.getBlobContainerClient(STORAGE_CONTAINER_NAME)).thenReturn(blobContainerClient); lenient().when(blobServiceClientFactory.getBlobServiceClient(PARTITION_ID)).thenReturn(blobServiceClient); + lenient().when(blobContainerClientFactory.getClient(PARTITION_ID)).thenReturn(blobContainerClient); lenient().doNothing().when(logger).warning(eq("azure-core-lib"), any(), anyMap()); } @Test public void readFromBlob_ErrorCreatingBlobContainerClient() + { + doThrow(AppException.class).when(blobContainerClientFactory).getClient(eq(PARTITION_ID)); + try { + String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH); + } catch (AppException ex) { + assertEquals(500, ex.getError().getCode()); + } catch (Exception ex) { + fail("should not get different error code"); + } + } + + @Test + public void readFromBlob_Success() + { + String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH); + ArgumentCaptor outputStream = ArgumentCaptor.forClass(ByteArrayOutputStream.class); + + // validate that the download method is being invoked appropriately. + verify(blockBlobClient).download(outputStream.capture()); + } + + @Test + public void readFromBlob_BlobNotFound() + { + BlobStorageException exception = mockStorageException(BlobErrorCode.BLOB_NOT_FOUND); + doThrow(exception).when(blockBlobClient).download(any()); + try { + String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH); + } catch (AppException ex) { + assertEquals(404, ex.getError().getCode()); + } catch (Exception ex) { + fail("should not get different error code"); + } + } + + @Test + public void readFromBlob_InternalError() + { + BlobStorageException exception = mockStorageException(BlobErrorCode.INTERNAL_ERROR); + doThrow(exception).when(blockBlobClient).download(any()); + try { + String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH); + } catch (AppException ex) { + assertEquals(500, ex.getError().getCode()); + } catch (Exception ex) { + fail("should not get different error code"); + } + } + + @Test + public void deleteFromBlob_ErrorCreatingBlobContainerClient() + { + doThrow(AppException.class).when(blobContainerClientFactory).getClient(eq(PARTITION_ID)); + try { + blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH); + } catch (AppException ex) { + assertEquals(500, ex.getError().getCode()); + } catch (Exception ex) { + fail("should not get different error code"); + } + } + + @Test + public void deleteFromBlob_BlobNotFound() + { + BlobStorageException exception = mockStorageException(BlobErrorCode.BLOB_NOT_FOUND); + doThrow(exception).when(blockBlobClient).delete(); + try { + blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH); + } catch (AppException ex) { + assertEquals(404, ex.getError().getCode()); + } catch (Exception ex) { + fail("should not get different error code"); + } + } + + @Test + public void deleteFromBlob_InternalError() + { + BlobStorageException exception = mockStorageException(BlobErrorCode.INTERNAL_ERROR); + doThrow(exception).when(blockBlobClient).delete(); + try { + blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH); + } catch (AppException ex) { + assertEquals(500, ex.getError().getCode()); + } catch (Exception ex) { + fail("should not get different error code"); + } + } + + @Test + public void deleteFromBlob_Success() + { + doNothing().when(blockBlobClient).delete(); + try { + blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH); + } catch (Exception ex) { + fail("should not get any exception."); + } + } + + @Test + public void writeToBlob_ErrorCreatingBlobContainerClient() + { + doThrow(AppException.class).when(blobContainerClientFactory).getClient(eq(PARTITION_ID)); + try { + blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT); + } catch (AppException ex) { + assertEquals(500, ex.getError().getCode()); + } catch (Exception ex) { + fail("should not get different error code"); + } + } + + @Test + public void writeToBlob_InternalError() + { + BlobStorageException exception = mockStorageException(BlobErrorCode.INTERNAL_ERROR); + doThrow(exception).when(blockBlobClient).upload(any(), anyLong(), eq(true)); + try { + blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT); + } catch (AppException ex) { + assertEquals(500, ex.getError().getCode()); + } catch (Exception ex) { + fail("should not get different error code"); + } + } + + @Test + public void writeToBlob_Success() + { + doReturn(blockBlobItem).when(blockBlobClient).upload(any(), anyLong(), eq(true)); + try { + blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT); + } catch (Exception ex) { + fail("should not get any exception."); + } + } + + @Test + public void readFromStorageContainer_ErrorCreatingBlobContainerClient() { doThrow(AppException.class).when(blobServiceClientFactory).getBlobServiceClient(eq(PARTITION_ID)); try { - String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); + String content = blobStore.readFromStorageContainer(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -95,11 +240,11 @@ public class BlobStoreTest { } @Test - public void readFromBlob_ErrorCreatingBlobContainerClient_FromServiceClient() + public void readFromStorageContainer_ErrorCreatingBlobContainerClient_FromServiceClient() { doThrow(AppException.class).when(blobServiceClient).getBlobContainerClient(eq(STORAGE_CONTAINER_NAME)); try { - String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); + String content = blobStore.readFromStorageContainer(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -108,9 +253,9 @@ public class BlobStoreTest { } @Test - public void readFromBlob_Success() + public void readFromStorageContainer_Success() { - String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); + String content = blobStore.readFromStorageContainer(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); ArgumentCaptor outputStream = ArgumentCaptor.forClass(ByteArrayOutputStream.class); // validate that the download method is being invoked appropriately. @@ -118,12 +263,12 @@ public class BlobStoreTest { } @Test - public void readFromBlob_BlobNotFound() + public void readFromStorageContainer_BlobNotFound() { BlobStorageException exception = mockStorageException(BlobErrorCode.BLOB_NOT_FOUND); doThrow(exception).when(blockBlobClient).download(any()); try { - String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); + String content = blobStore.readFromStorageContainer(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(404, ex.getError().getCode()); } catch (Exception ex) { @@ -132,12 +277,12 @@ public class BlobStoreTest { } @Test - public void readFromBlob_InternalError() + public void readFromStorageContainer_InternalError() { BlobStorageException exception = mockStorageException(BlobErrorCode.INTERNAL_ERROR); doThrow(exception).when(blockBlobClient).download(any()); try { - String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); + String content = blobStore.readFromStorageContainer(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -146,11 +291,11 @@ public class BlobStoreTest { } @Test - public void deleteFromBlob_ErrorCreatingBlobContainerClient() + public void deleteFromStorageContainer_ErrorCreatingBlobContainerClient() { doThrow(AppException.class).when(blobServiceClientFactory).getBlobServiceClient(eq(PARTITION_ID)); try { - blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); + blobStore.deleteFromStorageContainer(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -159,12 +304,12 @@ public class BlobStoreTest { } @Test - public void deleteFromBlob_BlobNotFound() + public void deleteFromStorageContainer_BlobNotFound() { BlobStorageException exception = mockStorageException(BlobErrorCode.BLOB_NOT_FOUND); doThrow(exception).when(blockBlobClient).delete(); try { - blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); + blobStore.deleteFromStorageContainer(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(404, ex.getError().getCode()); } catch (Exception ex) { @@ -173,12 +318,12 @@ public class BlobStoreTest { } @Test - public void deleteFromBlob_InternalError() + public void deleteFromStorageContainer_InternalError() { BlobStorageException exception = mockStorageException(BlobErrorCode.INTERNAL_ERROR); doThrow(exception).when(blockBlobClient).delete(); try { - blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); + blobStore.deleteFromStorageContainer(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -187,22 +332,22 @@ public class BlobStoreTest { } @Test - public void deleteFromBlob_Success() + public void deleteFromStorageContainer_Success() { doNothing().when(blockBlobClient).delete(); try { - blobStore.deleteFromBlob(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); + blobStore.deleteFromStorageContainer(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME); } catch (Exception ex) { fail("should not get any exception."); } } @Test - public void writeToBlob_ErrorCreatingBlobContainerClient() + public void writeToStorageContainer_ErrorCreatingBlobContainerClient() { doThrow(AppException.class).when(blobServiceClientFactory).getBlobServiceClient(eq(PARTITION_ID)); try { - blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT, STORAGE_CONTAINER_NAME); + blobStore.writeToStorageContainer(PARTITION_ID, FILE_PATH, CONTENT, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -211,12 +356,12 @@ public class BlobStoreTest { } @Test - public void writeToBlob_InternalError() + public void writeToStorageContainer_InternalError() { BlobStorageException exception = mockStorageException(BlobErrorCode.INTERNAL_ERROR); doThrow(exception).when(blockBlobClient).upload(any(), anyLong(), eq(true)); try { - blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT, STORAGE_CONTAINER_NAME); + blobStore.writeToStorageContainer(PARTITION_ID, FILE_PATH, CONTENT, STORAGE_CONTAINER_NAME); } catch (AppException ex) { assertEquals(500, ex.getError().getCode()); } catch (Exception ex) { @@ -225,11 +370,11 @@ public class BlobStoreTest { } @Test - public void writeToBlob_Success() + public void writeToStorageContainer_Success() { doReturn(blockBlobItem).when(blockBlobClient).upload(any(), anyLong(), eq(true)); try { - blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT, STORAGE_CONTAINER_NAME); + blobStore.writeToStorageContainer(PARTITION_ID, FILE_PATH, CONTENT, STORAGE_CONTAINER_NAME); } catch (Exception ex) { fail("should not get any exception."); } -- GitLab