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 730a3d899133aba2a26e2bf6d3c8c8aba98805b4..53acb2e99f62b46066aedda506fc51e4f34dfea1 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java @@ -200,6 +200,44 @@ public class BlobStore { } } + /** + * @param dataPartitionId Data partition id + * @param containerName Name of the storage container + * @return boolean indicating whether the creation of the given container was successful or not. + * Throws exception in case of failure. + */ + public boolean createBlobContainer( + final String dataPartitionId, + final String containerName) { + BlobServiceClient blobServiceClient = blobServiceClientFactory.getBlobServiceClient(dataPartitionId); + try { + blobServiceClient.createBlobContainer(containerName); + CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).info("{}", MessageFormatter.format("Done creating container with name {}", containerName).getMessage()); + return true; + } catch (BlobStorageException ex) { + throw handleBlobStoreException(500, "Failed to create blob container", ex); + } + } + + /** + * @param dataPartitionId Data partition id + * @param containerName Name of the storage container + * @return boolean indicating whether the deletion of the given container was successful or not. + * Throws exception in case of failure. + */ + public boolean deleteBlobContainer( + final String dataPartitionId, + final String containerName) { + BlobServiceClient blobServiceClient = blobServiceClientFactory.getBlobServiceClient(dataPartitionId); + try { + blobServiceClient.deleteBlobContainer(containerName); + CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).info("{}", MessageFormatter.format("Done deleting container with name {}", containerName).getMessage()); + return true; + } catch (BlobStorageException ex) { + throw handleBlobStoreException(500, "Failed to delete blob container", ex); + } + } + /** * @param dataPartitionId Data partition id * @param filePath Path of file (blob) for which SAS token needs to be generated 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 d7998f01e5b4007f9f2d1e305368594ad86291d3..b6485c14e59833d8550f46e2e96624bb7be9e84f 100644 --- a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java +++ b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java @@ -312,6 +312,58 @@ public class BlobStoreTest { assertEquals(copyInfo, null); } + @Test + public void createBlobContainer_Failure() { + BlobStorageException exception = mockStorageException(BlobErrorCode.INTERNAL_ERROR); + doThrow(exception).when(blobServiceClient).createBlobContainer(anyString()); + String containerName = "containerName"; + try { + blobStore.createBlobContainer(PARTITION_ID, containerName); + } catch (AppException ex) { + assertEquals(500, ex.getError().getCode()); + } catch (Exception ex) { + fail("should not get different error code"); + } + } + + @Test + public void createBlobContainer_Success() { + String containerName = "containerName"; + boolean status = blobStore.createBlobContainer(PARTITION_ID, containerName); + + ArgumentCaptor containerNameCaptor = ArgumentCaptor.forClass(String.class); + verify(blobServiceClient).createBlobContainer(containerNameCaptor.capture()); + + assertEquals(containerNameCaptor.getValue(), containerName); + assertEquals(status, true); + } + + @Test + public void deleteBlobContainer_Failure() { + BlobStorageException exception = mockStorageException(BlobErrorCode.INTERNAL_ERROR); + doThrow(exception).when(blobServiceClient).deleteBlobContainer(anyString()); + String containerName = "containerName"; + try { + blobStore.deleteBlobContainer(PARTITION_ID, containerName); + } catch (AppException ex) { + assertEquals(500, ex.getError().getCode()); + } catch (Exception ex) { + fail("should not get different error code"); + } + } + + @Test + public void deleteBlobContainer_Success() { + String containerName = "containerName"; + boolean status = blobStore.deleteBlobContainer(PARTITION_ID, containerName); + + ArgumentCaptor containerNameCaptor = ArgumentCaptor.forClass(String.class); + verify(blobServiceClient).deleteBlobContainer(containerNameCaptor.capture()); + + assertEquals(containerNameCaptor.getValue(), containerName); + assertEquals(status, true); + } + @Test public void getSasToken_NullSasTokenObtained() { int expiryDays = 1;