From 92d53db4075547ff92f4571c44e5497c0d510160 Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Thu, 30 Jul 2020 16:18:05 +0530 Subject: [PATCH 01/13] adding new class to interat with azure blob storage --- .../org/opengroup/osdu/azure/BlobStore.java | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/main/java/org/opengroup/osdu/azure/BlobStore.java diff --git a/src/main/java/org/opengroup/osdu/azure/BlobStore.java b/src/main/java/org/opengroup/osdu/azure/BlobStore.java new file mode 100644 index 00000000..eafde2fa --- /dev/null +++ b/src/main/java/org/opengroup/osdu/azure/BlobStore.java @@ -0,0 +1,115 @@ +package org.opengroup.osdu.azure; + +// Copyright © Microsoft Corporation +// +// Licensed 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. + + +import com.azure.storage.blob.BlobContainerClient; +import com.azure.storage.blob.models.BlobStorageException; +import com.azure.storage.blob.specialized.BlockBlobClient; +import org.opengroup.osdu.core.common.model.http.AppException; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; + +import java.io.ByteArrayOutputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + */ + +@Component +@Lazy +public class BlobStore { + @Autowired + private DpsHeaders headers; + + @Autowired + private BlobContainerClient blobContainerClient; + + private static final Logger LOGGER = Logger.getLogger(BlobStore.class.getName()); + + /** + * + * @param filePath Path of file to be read. + * @return the content of file with provided file path. + */ + public String readFromBlob(final String filePath) { + String content = ""; + BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); + try (ByteArrayOutputStream downloadStream = new ByteArrayOutputStream()) { + blockBlobClient.download(downloadStream); + content = downloadStream.toString(StandardCharsets.UTF_8.name()); + return content; + } catch (Exception ex) { + LOGGER.log(Level.WARNING, ex.getMessage(), ex); + throw new AppException(500, String.valueOf(ex.getCause()), ex.getMessage(), ex); + } + } + + /** + * + * @param filePath Path of file to be deleted. + * @return boolean indicating whether the deletion of given file was successful or not. + */ + public boolean deleteFromBlob(final String filePath) { + BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); + try { + blockBlobClient.delete(); + return true; + } catch (Exception ex) { + LOGGER.log(Level.WARNING, ex.getMessage(), ex); + throw new AppException(500, String.valueOf(ex.getCause()), ex.getMessage(), ex); + } + } + + /** + * + * @param filePath Path of file to be written at. + * @param content Content to be written in the file. + * @return the name of blob where given file content was written. + */ + public String writeToBlob(final String filePath, final String content) { + byte[] bytes = content.getBytes(StandardCharsets.UTF_8); + int bytesSize = bytes.length; + BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); + + if (blockBlobClient.exists()) { + // we need to clean up the previous schema content. + LOGGER.info(String.format("Cleaning the previous blob with name=%s", blockBlobClient.getBlobName())); + this.deleteFromBlob(filePath); + } + + BlockBlobClient newBlockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); + try (ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes)) { + newBlockBlobClient.upload(dataStream, bytesSize); + } catch (BlobStorageException ex) { + LOGGER.log(Level.WARNING, ex.getMessage(), ex); + throw new AppException(500, String.valueOf(ex.getCause()), ex.getMessage(), ex); + } catch (IOException ex) { + LOGGER.log(Level.WARNING, ex.getMessage(), ex); + throw new AppException(500, String.valueOf(ex.getCause()), ex.getMessage(), ex); + } + + // Get the blobname + return newBlockBlobClient.getBlobName(); + } +} + -- GitLab From 160ef273e673aa8971301f15267857d624805256 Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Thu, 30 Jul 2020 16:30:08 +0530 Subject: [PATCH 02/13] adding sample code examples --- .../org/opengroup/osdu/azure/BlobStore.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/org/opengroup/osdu/azure/BlobStore.java b/src/main/java/org/opengroup/osdu/azure/BlobStore.java index eafde2fa..8afc4c15 100644 --- a/src/main/java/org/opengroup/osdu/azure/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/BlobStore.java @@ -32,7 +32,31 @@ import java.util.logging.Level; import java.util.logging.Logger; /** + * A simpler interface to interact with Azure blob storage. + * Usage examples: + *
+ * {@code
+ *      @Autowired
+ *      private BlobStore blobStore;
  *
+ *      String readFromBlobExample()
+ *      {
+ *          String content = blobStorage.readFromBlob("filePath");
+ *             if (content != null)
+ *                 return content;
+ *      }
+ *
+ *      void writeToBlobExample()
+ *      {
+ *          String blobName = blobStorage.writeToBlob("filePath", "content");
+ *      }
+ *
+ *      void deleteFromBlobExample()
+ *      {
+ *          Boolean success = blobStorage.deleteFromBlob("filePath");
+ *      }
+ * }
+ * 
*/ @Component -- GitLab From 945f2020f3fcaca3fee1186c454ba7ed907ae33c Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Fri, 31 Jul 2020 11:43:28 +0530 Subject: [PATCH 03/13] implementing the blob containeer client factory --- .../BlobContainerClientFactoryImpl.java | 42 +++++++++++++++++++ .../azure/{ => blobstorage}/BlobStore.java | 3 +- .../IBlobContainerClientFactory.java | 30 +++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/opengroup/osdu/azure/blobstorage/BlobContainerClientFactoryImpl.java rename src/main/java/org/opengroup/osdu/azure/{ => blobstorage}/BlobStore.java (99%) create mode 100644 src/main/java/org/opengroup/osdu/azure/blobstorage/IBlobContainerClientFactory.java diff --git a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobContainerClientFactoryImpl.java b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobContainerClientFactoryImpl.java new file mode 100644 index 00000000..cf461ea2 --- /dev/null +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobContainerClientFactoryImpl.java @@ -0,0 +1,42 @@ +// Copyright © Microsoft Corporation +// +// Licensed 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.opengroup.osdu.azure.blobstorage; + +import com.azure.storage.blob.BlobContainerClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; + +/** + * Implementation for IBlobContainerClientFactory. + */ +@Component +@Lazy +public class BlobContainerClientFactoryImpl implements IBlobContainerClientFactory{ + + @Lazy + @Autowired + private BlobContainerClient blobContainerClient; + + /** + * + * @param dataPartitionId Data partition id + * @return the blob container client instance. + */ + @Override + public BlobContainerClient getClient(final String dataPartitionId) { + return blobContainerClient; + } +} diff --git a/src/main/java/org/opengroup/osdu/azure/BlobStore.java b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java similarity index 99% rename from src/main/java/org/opengroup/osdu/azure/BlobStore.java rename to src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java index 8afc4c15..379ff242 100644 --- a/src/main/java/org/opengroup/osdu/azure/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java @@ -1,5 +1,3 @@ -package org.opengroup.osdu.azure; - // Copyright © Microsoft Corporation // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,6 +12,7 @@ package org.opengroup.osdu.azure; // See the License for the specific language governing permissions and // limitations under the License. +package org.opengroup.osdu.azure.blobstorage; import com.azure.storage.blob.BlobContainerClient; import com.azure.storage.blob.models.BlobStorageException; diff --git a/src/main/java/org/opengroup/osdu/azure/blobstorage/IBlobContainerClientFactory.java b/src/main/java/org/opengroup/osdu/azure/blobstorage/IBlobContainerClientFactory.java new file mode 100644 index 00000000..24a06be3 --- /dev/null +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/IBlobContainerClientFactory.java @@ -0,0 +1,30 @@ +// Copyright © Microsoft Corporation +// +// Licensed 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.opengroup.osdu.azure.blobstorage; + +import com.azure.storage.blob.BlobContainerClient; + +/** + * Interface for BlobContainer client factory to return appropriate + * blobContainerClient based on the data partition id. + */ +public interface IBlobContainerClientFactory { + /** + * + * @param dataPartitionId Data partition id + * @return blobContainerClient for given data partition id. + */ + BlobContainerClient getClient(String dataPartitionId); +} -- GitLab From 58f0b33b5255d7a0eef367c2d5c44f37022fc40c Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Mon, 3 Aug 2020 12:10:23 +0530 Subject: [PATCH 04/13] adding new tests --- .../BlobContainerClientFactoryImpl.java | 2 +- .../osdu/azure/blobstorage/BlobStore.java | 43 ++++++++--- .../osdu/azure/blobstorage/BlobStoreTest.java | 76 +++++++++++++++++++ 3 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java diff --git a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobContainerClientFactoryImpl.java b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobContainerClientFactoryImpl.java index cf461ea2..81e6c93c 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobContainerClientFactoryImpl.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobContainerClientFactoryImpl.java @@ -24,7 +24,7 @@ import org.springframework.stereotype.Component; */ @Component @Lazy -public class BlobContainerClientFactoryImpl implements IBlobContainerClientFactory{ +public class BlobContainerClientFactoryImpl implements IBlobContainerClientFactory { @Lazy @Autowired 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 379ff242..11b6b7bc 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java @@ -18,7 +18,6 @@ import com.azure.storage.blob.BlobContainerClient; import com.azure.storage.blob.models.BlobStorageException; import com.azure.storage.blob.specialized.BlockBlobClient; import org.opengroup.osdu.core.common.model.http.AppException; -import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @@ -61,21 +60,21 @@ import java.util.logging.Logger; @Component @Lazy public class BlobStore { - @Autowired - private DpsHeaders headers; @Autowired - private BlobContainerClient blobContainerClient; + private IBlobContainerClientFactory blobContainerClientFactory; private static final Logger LOGGER = Logger.getLogger(BlobStore.class.getName()); /** * - * @param filePath Path of file to be read. + * @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 filePath) { + public String readFromBlob(final String dataPartitionId, final String filePath) { String content = ""; + BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); try (ByteArrayOutputStream downloadStream = new ByteArrayOutputStream()) { blockBlobClient.download(downloadStream); @@ -89,10 +88,12 @@ public class BlobStore { /** * - * @param filePath Path of file to be deleted. + * @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 filePath) { + public boolean deleteFromBlob(final String dataPartitionId, final String filePath) { + BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); try { blockBlobClient.delete(); @@ -105,19 +106,23 @@ public class BlobStore { /** * - * @param filePath Path of file to be written at. - * @param content Content to be written in the file. + * @param filePath Path of file to be written at. + * @param content Content to be written in the file. + * @param dataPartitionId Data partition id * @return the name of blob where given file content was written. */ - public String writeToBlob(final String filePath, final String content) { + public String 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(); if (blockBlobClient.exists()) { // we need to clean up the previous schema content. LOGGER.info(String.format("Cleaning the previous blob with name=%s", blockBlobClient.getBlobName())); - this.deleteFromBlob(filePath); + this.deleteFromBlob(dataPartitionId, filePath); } BlockBlobClient newBlockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); @@ -134,5 +139,19 @@ public class BlobStore { // Get the blobname return newBlockBlobClient.getBlobName(); } + + /** + * + * @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) { + LOGGER.log(Level.WARNING, ex.getMessage(), ex); + throw new AppException(500, ex.getMessage(), String.valueOf(ex.getCause()), 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 new file mode 100644 index 00000000..c08c3bbb --- /dev/null +++ b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java @@ -0,0 +1,76 @@ +package org.opengroup.osdu.azure.blobstorage; + +import com.azure.storage.blob.BlobClient; +import com.azure.storage.blob.BlobContainerClient; +import com.azure.storage.blob.models.BlobStorageException; +import com.azure.storage.blob.specialized.BlockBlobClient; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opengroup.osdu.core.common.model.http.AppException; + +import java.io.ByteArrayOutputStream; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; +import static org.mockito.MockitoAnnotations.initMocks; + +@ExtendWith(MockitoExtension.class) +public class BlobStoreTest { + private static final String PARTITION_ID = "dataPartitionId"; + private static final String FILE_PATH = "filePath"; + private static final String CONTENT = "hello world"; + + @InjectMocks + BlobStore blobStore; + + @Mock + IBlobContainerClientFactory blobContainerClientFactory; + + @Mock + BlobContainerClient blobContainerClient; + + @Mock + BlobClient blobClient; + + @Mock + BlockBlobClient blockBlobClient; + + @BeforeEach + void init() { + initMocks(this); + doReturn(blobContainerClient).when(blobContainerClientFactory).getClient(PARTITION_ID); + doReturn(blobClient).when(blobContainerClient).getBlobClient(FILE_PATH); + doReturn(blockBlobClient).when(blobClient).getBlockBlobClient(); + //doReturn(CONTENT).when(blockBlobClient).download(any()); + } + + @Test + public void readFromBlob_Success() + { + String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH); + ArgumentCaptor outputStream = ArgumentCaptor.forClass(ByteArrayOutputStream.class); + verify(blockBlobClient).download(outputStream.capture()); + //assertEquals(CONTENT, content); + } + + @Test + public void readFromBlob_Failure() + { + doThrow(BlobStorageException.class).when(blockBlobClient).download(any()); + //ArgumentCaptor outputStream = ArgumentCaptor.forClass(ByteArrayOutputStream.class); + //verify(blockBlobClient).download(outputStream.capture()); + try { + String content = blobStore.readFromBlob(PARTITION_ID, FILE_PATH); + } catch (AppException ex) { + assertEquals("not found", ex.getMessage()); + } catch (Exception ex) { + fail("should not get different exception"); + } + } +} -- GitLab From 63f1e6775252f33c659cd5a293faba41c31b263a Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Tue, 4 Aug 2020 01:27:46 +0530 Subject: [PATCH 05/13] adding new tests --- .../osdu/azure/blobstorage/BlobStore.java | 60 ++++++---- .../osdu/azure/blobstorage/BlobStoreTest.java | 112 ++++++++++++++++-- 2 files changed, 140 insertions(+), 32 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 11b6b7bc..b7759ea3 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.models.BlobErrorCode; import com.azure.storage.blob.models.BlobStorageException; import com.azure.storage.blob.specialized.BlockBlobClient; import org.opengroup.osdu.core.common.model.http.AppException; @@ -25,6 +26,7 @@ import org.springframework.stereotype.Component; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.logging.Level; import java.util.logging.Logger; @@ -79,11 +81,26 @@ public class BlobStore { try (ByteArrayOutputStream downloadStream = new ByteArrayOutputStream()) { blockBlobClient.download(downloadStream); content = downloadStream.toString(StandardCharsets.UTF_8.name()); - return content; - } catch (Exception ex) { - LOGGER.log(Level.WARNING, ex.getMessage(), ex); - throw new AppException(500, String.valueOf(ex.getCause()), ex.getMessage(), ex); + } catch (BlobStorageException ex) { + if (ex.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND)) { + String errorMessage = "Specified blob was not found"; + LOGGER.log(Level.WARNING, errorMessage, ex); + throw new AppException(404, errorMessage, ex.getMessage(), ex); + } else { + String errorMessage = "Failed to read specified blob"; + LOGGER.log(Level.WARNING, errorMessage, ex); + 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.log(Level.WARNING, errorMessage, ex); + throw new AppException(400, errorMessage, ex.getMessage(), ex); + } catch (IOException ex) { + String errorMessage = String.format("Malformed document for item with name=%s", filePath); + LOGGER.log(Level.WARNING, errorMessage, ex); + throw new AppException(500, errorMessage, ex.getMessage(), ex); } + return content; } /** @@ -98,9 +115,16 @@ public class BlobStore { try { blockBlobClient.delete(); return true; - } catch (Exception ex) { - LOGGER.log(Level.WARNING, ex.getMessage(), ex); - throw new AppException(500, String.valueOf(ex.getCause()), ex.getMessage(), ex); + } catch (BlobStorageException ex) { + if (ex.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND)) { + String errorMessage = "Specified blob was not found"; + LOGGER.log(Level.WARNING, errorMessage, ex); + throw new AppException(404, errorMessage, ex.getMessage(), ex); + } else { + String errorMessage = "Failed to delete blob"; + LOGGER.log(Level.WARNING, errorMessage, ex); + throw new AppException(500, errorMessage, ex.getMessage(), ex); + } } } @@ -118,26 +142,20 @@ public class BlobStore { int bytesSize = bytes.length; BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); - - if (blockBlobClient.exists()) { - // we need to clean up the previous schema content. - LOGGER.info(String.format("Cleaning the previous blob with name=%s", blockBlobClient.getBlobName())); - this.deleteFromBlob(dataPartitionId, filePath); - } - - BlockBlobClient newBlockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); try (ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes)) { - newBlockBlobClient.upload(dataStream, bytesSize); + blockBlobClient.upload(dataStream, bytesSize, true); } catch (BlobStorageException ex) { - LOGGER.log(Level.WARNING, ex.getMessage(), ex); - throw new AppException(500, String.valueOf(ex.getCause()), ex.getMessage(), ex); + String errorMessage = "Failed to upload file content."; + LOGGER.log(Level.WARNING, errorMessage, ex); + throw new AppException(500, errorMessage, ex.getMessage(), ex); } catch (IOException ex) { - LOGGER.log(Level.WARNING, ex.getMessage(), ex); - throw new AppException(500, String.valueOf(ex.getCause()), ex.getMessage(), ex); + String errorMessage = String.format("Malformed document for item with name=%s", filePath); + LOGGER.log(Level.WARNING, errorMessage, ex); + throw new AppException(500, errorMessage, ex.getMessage(), ex); } // Get the blobname - return newBlockBlobClient.getBlobName(); + return blockBlobClient.getBlobName(); } /** 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 c08c3bbb..fd9d1953 100644 --- a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java +++ b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java @@ -2,7 +2,9 @@ package org.opengroup.osdu.azure.blobstorage; import com.azure.storage.blob.BlobClient; import com.azure.storage.blob.BlobContainerClient; +import com.azure.storage.blob.models.BlobErrorCode; import com.azure.storage.blob.models.BlobStorageException; +import com.azure.storage.blob.models.BlockBlobItem; import com.azure.storage.blob.specialized.BlockBlobClient; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -41,13 +43,15 @@ public class BlobStoreTest { @Mock BlockBlobClient blockBlobClient; + @Mock + BlockBlobItem blockBlobItem; + @BeforeEach void init() { initMocks(this); - doReturn(blobContainerClient).when(blobContainerClientFactory).getClient(PARTITION_ID); - doReturn(blobClient).when(blobContainerClient).getBlobClient(FILE_PATH); - doReturn(blockBlobClient).when(blobClient).getBlockBlobClient(); - //doReturn(CONTENT).when(blockBlobClient).download(any()); + lenient().doReturn(blobContainerClient).when(blobContainerClientFactory).getClient(PARTITION_ID); + lenient().doReturn(blobClient).when(blobContainerClient).getBlobClient(FILE_PATH); + lenient().doReturn(blockBlobClient).when(blobClient).getBlockBlobClient(); } @Test @@ -55,22 +59,108 @@ public class BlobStoreTest { { 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()); - //assertEquals(CONTENT, content); } @Test - public void readFromBlob_Failure() + public void readFromBlob_BlobNotFound() { - doThrow(BlobStorageException.class).when(blockBlobClient).download(any()); - //ArgumentCaptor outputStream = ArgumentCaptor.forClass(ByteArrayOutputStream.class); - //verify(blockBlobClient).download(outputStream.capture()); + 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("not found", ex.getMessage()); + 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_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_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 exception"); + fail("should not get different error code"); } } + + @Test + public void writeToBlob_Success() + { + doReturn(blockBlobItem).when(blockBlobClient).upload(any(), anyLong(), eq(true)); + doReturn(FILE_PATH).when(blockBlobClient).getBlobName(); + try { + String blobName = blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT); + assertEquals(FILE_PATH, blobName); + } catch (Exception ex) { + fail("should not get any exception."); + } + } + + private BlobStorageException mockStorageException(BlobErrorCode errorCode) { + BlobStorageException mockException = mock(BlobStorageException.class); + lenient().when(mockException.getErrorCode()).thenReturn(errorCode); + return mockException; + } } -- GitLab From ec896b87d7a502f86c3cb4934f8cdd5aef0db85f Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Tue, 4 Aug 2020 02:56:47 +0530 Subject: [PATCH 06/13] updating the artifact versiosn --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d2b2be31..4e8ae794 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ org.opengroup.osdu core-lib-azure jar - 0.0.12 + 0.0.13 core-lib-azure -- GitLab From 834a1b8f5dfc569a038025927d4d2ca31afcd84c Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Tue, 4 Aug 2020 03:11:16 +0530 Subject: [PATCH 07/13] adding tests for failure in client creation --- .../osdu/azure/blobstorage/BlobStore.java | 11 ++-- .../osdu/azure/blobstorage/BlobStoreTest.java | 53 +++++++++++++++++++ 2 files changed, 59 insertions(+), 5 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 b7759ea3..e9254fc2 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.logging.Logger; * * String readFromBlobExample() * { - * String content = blobStorage.readFromBlob("filePath"); + * String content = blobStorage.readFromBlob("dataPartitionId", "filePath"); * if (content != null) * return content; * } * * void writeToBlobExample() * { - * String blobName = blobStorage.writeToBlob("filePath", "content"); + * String blobName = blobStorage.writeToBlob("dataPartitionId", "filePath", "content"); * } * * void deleteFromBlobExample() * { - * Boolean success = blobStorage.deleteFromBlob("filePath"); + * Boolean success = blobStorage.deleteFromBlob("dataPartitionId", "filePath"); * } * } * @@ -167,8 +167,9 @@ public class BlobStore { try { return blobContainerClientFactory.getClient(dataPartitionId); } catch (Exception ex) { - LOGGER.log(Level.WARNING, ex.getMessage(), ex); - throw new AppException(500, ex.getMessage(), String.valueOf(ex.getCause()), ex); + String errorMessage = "Error creating creating blob container client Client"; + LOGGER.log(Level.WARNING, errorMessage, ex); + 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 fd9d1953..3fbc82bd 100644 --- a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java +++ b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java @@ -1,3 +1,17 @@ +// Copyright © Microsoft Corporation +// +// Licensed 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.opengroup.osdu.azure.blobstorage; import com.azure.storage.blob.BlobClient; @@ -54,6 +68,19 @@ public class BlobStoreTest { lenient().doReturn(blockBlobClient).when(blobClient).getBlockBlobClient(); } + @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() { @@ -92,6 +119,19 @@ public class BlobStoreTest { } } + @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() { @@ -131,6 +171,19 @@ public class BlobStoreTest { } } + @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() { -- GitLab From 06ae4ef02e3f42bd60cb118974d632c7ac2e38ad Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Tue, 4 Aug 2020 03:13:29 +0530 Subject: [PATCH 08/13] fixing the error message --- .../java/org/opengroup/osdu/azure/blobstorage/BlobStore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e9254fc2..3e0e5f25 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java @@ -167,7 +167,7 @@ public class BlobStore { try { return blobContainerClientFactory.getClient(dataPartitionId); } catch (Exception ex) { - String errorMessage = "Error creating creating blob container client Client"; + String errorMessage = "Error creating creating blob container client."; LOGGER.log(Level.WARNING, errorMessage, ex); throw new AppException(500, errorMessage, ex.getMessage(), ex); } -- GitLab From 6da4e5667a7f9fe624f183372381b737d0a19387 Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Tue, 4 Aug 2020 16:52:06 +0530 Subject: [PATCH 09/13] updating the method signature of deleteFromBlob method --- .../org/opengroup/osdu/azure/blobstorage/BlobStore.java | 6 +----- .../org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java | 4 +--- 2 files changed, 2 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 3e0e5f25..430afa80 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java @@ -133,9 +133,8 @@ 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 - * @return the name of blob where given file content was written. */ - public String writeToBlob(final String dataPartitionId, + public void writeToBlob(final String dataPartitionId, final String filePath, final String content) { byte[] bytes = content.getBytes(StandardCharsets.UTF_8); @@ -153,9 +152,6 @@ public class BlobStore { LOGGER.log(Level.WARNING, errorMessage, ex); throw new AppException(500, errorMessage, ex.getMessage(), ex); } - - // Get the blobname - return blockBlobClient.getBlobName(); } /** 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 3fbc82bd..ed247cf2 100644 --- a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java +++ b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java @@ -202,10 +202,8 @@ public class BlobStoreTest { public void writeToBlob_Success() { doReturn(blockBlobItem).when(blockBlobClient).upload(any(), anyLong(), eq(true)); - doReturn(FILE_PATH).when(blockBlobClient).getBlobName(); try { - String blobName = blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT); - assertEquals(FILE_PATH, blobName); + blobStore.writeToBlob(PARTITION_ID, FILE_PATH, CONTENT); } catch (Exception ex) { fail("should not get any exception."); } -- GitLab From 658e68f436c46435f04b51f2c499584230877dad Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Tue, 4 Aug 2020 17:49:57 +0530 Subject: [PATCH 10/13] addressing MR comments --- .../osdu/azure/blobstorage/BlobStore.java | 36 +++++++++++-------- .../osdu/azure/blobstorage/BlobStoreTest.java | 11 ++++++ 2 files changed, 32 insertions(+), 15 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 430afa80..56619ffb 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java @@ -18,7 +18,9 @@ import com.azure.storage.blob.BlobContainerClient; import com.azure.storage.blob.models.BlobErrorCode; import com.azure.storage.blob.models.BlobStorageException; import com.azure.storage.blob.specialized.BlockBlobClient; +import org.opengroup.osdu.core.common.logging.ILogger; import org.opengroup.osdu.core.common.model.http.AppException; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @@ -27,9 +29,7 @@ import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.nio.charset.StandardCharsets;; /** * A simpler interface to interact with Azure blob storage. @@ -48,7 +48,7 @@ import java.util.logging.Logger; * * void writeToBlobExample() * { - * String blobName = blobStorage.writeToBlob("dataPartitionId", "filePath", "content"); + * blobStorage.writeToBlob("dataPartitionId", "filePath", "content"); * } * * void deleteFromBlobExample() @@ -66,7 +66,13 @@ public class BlobStore { @Autowired private IBlobContainerClientFactory blobContainerClientFactory; - private static final Logger LOGGER = Logger.getLogger(BlobStore.class.getName()); + @Autowired + private ILogger logger; + + @Autowired + private DpsHeaders headers; + + private static final String LOG_PREFIX = "azure-core-lib"; /** * @@ -81,26 +87,26 @@ public class BlobStore { try (ByteArrayOutputStream downloadStream = new ByteArrayOutputStream()) { blockBlobClient.download(downloadStream); content = downloadStream.toString(StandardCharsets.UTF_8.name()); + return content; } catch (BlobStorageException ex) { if (ex.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND)) { String errorMessage = "Specified blob was not found"; - LOGGER.log(Level.WARNING, errorMessage, ex); + logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); throw new AppException(404, errorMessage, ex.getMessage(), ex); } else { String errorMessage = "Failed to read specified blob"; - LOGGER.log(Level.WARNING, errorMessage, ex); + logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); 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.log(Level.WARNING, errorMessage, ex); + logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); throw new AppException(400, errorMessage, ex.getMessage(), ex); } catch (IOException ex) { String errorMessage = String.format("Malformed document for item with name=%s", filePath); - LOGGER.log(Level.WARNING, errorMessage, ex); + logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); throw new AppException(500, errorMessage, ex.getMessage(), ex); } - return content; } /** @@ -118,11 +124,11 @@ public class BlobStore { } catch (BlobStorageException ex) { if (ex.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND)) { String errorMessage = "Specified blob was not found"; - LOGGER.log(Level.WARNING, errorMessage, ex); + logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); throw new AppException(404, errorMessage, ex.getMessage(), ex); } else { String errorMessage = "Failed to delete blob"; - LOGGER.log(Level.WARNING, errorMessage, ex); + logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); throw new AppException(500, errorMessage, ex.getMessage(), ex); } } @@ -145,11 +151,11 @@ public class BlobStore { blockBlobClient.upload(dataStream, bytesSize, true); } catch (BlobStorageException ex) { String errorMessage = "Failed to upload file content."; - LOGGER.log(Level.WARNING, errorMessage, ex); + logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); throw new AppException(500, errorMessage, ex.getMessage(), ex); } catch (IOException ex) { String errorMessage = String.format("Malformed document for item with name=%s", filePath); - LOGGER.log(Level.WARNING, errorMessage, ex); + logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); throw new AppException(500, errorMessage, ex.getMessage(), ex); } } @@ -164,7 +170,7 @@ public class BlobStore { return blobContainerClientFactory.getClient(dataPartitionId); } catch (Exception ex) { String errorMessage = "Error creating creating blob container client."; - LOGGER.log(Level.WARNING, errorMessage, ex); + logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); 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 ed247cf2..5a65802c 100644 --- a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java +++ b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java @@ -27,9 +27,12 @@ import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.opengroup.osdu.core.common.logging.ILogger; import org.opengroup.osdu.core.common.model.http.AppException; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; import java.io.ByteArrayOutputStream; +import java.util.HashMap; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; @@ -60,12 +63,20 @@ public class BlobStoreTest { @Mock BlockBlobItem blockBlobItem; + @Mock + ILogger logger; + + @Mock + DpsHeaders headers; + @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().doNothing().when(logger).warning(eq("azure-core-lib"), any(), anyMap()); + lenient().doReturn(new HashMap()).when(headers).getHeaders(); } @Test -- GitLab From fda7ab692c57363bcb385266dd0d93a2132ff955 Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Tue, 4 Aug 2020 17:52:13 +0530 Subject: [PATCH 11/13] removing unnecessary variable instantiation --- .../java/org/opengroup/osdu/azure/blobstorage/BlobStore.java | 4 +--- 1 file changed, 1 insertion(+), 3 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 56619ffb..9ae50704 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java @@ -81,13 +81,11 @@ public class BlobStore { * @return the content of file with provided file path. */ public String readFromBlob(final String dataPartitionId, final String filePath) { - String content = ""; BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId); BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient(); try (ByteArrayOutputStream downloadStream = new ByteArrayOutputStream()) { blockBlobClient.download(downloadStream); - content = downloadStream.toString(StandardCharsets.UTF_8.name()); - return content; + 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"; -- GitLab From 96569f6346c85c1179a26b56ce1ec531fbb7a2d6 Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Tue, 4 Aug 2020 18:00:40 +0530 Subject: [PATCH 12/13] updating the version to 0.0.14 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4e8ae794..ee957547 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ org.opengroup.osdu core-lib-azure jar - 0.0.13 + 0.0.14 core-lib-azure -- GitLab From 98e7e1be2dd4c91e5cd5c92f6f9e62a9fa2ca177 Mon Sep 17 00:00:00 2001 From: Aman Verma Date: Tue, 4 Aug 2020 19:43:32 +0530 Subject: [PATCH 13/13] removing DPSHeaders --- .../osdu/azure/blobstorage/BlobStore.java | 25 ++++++++----------- .../osdu/azure/blobstorage/BlobStoreTest.java | 4 --- 2 files changed, 11 insertions(+), 18 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 9ae50704..cf88d09a 100644 --- a/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java +++ b/src/main/java/org/opengroup/osdu/azure/blobstorage/BlobStore.java @@ -20,7 +20,6 @@ import com.azure.storage.blob.models.BlobStorageException; import com.azure.storage.blob.specialized.BlockBlobClient; import org.opengroup.osdu.core.common.logging.ILogger; import org.opengroup.osdu.core.common.model.http.AppException; -import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @@ -29,7 +28,8 @@ import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets;; +import java.nio.charset.StandardCharsets; +import java.util.Collections;; /** * A simpler interface to interact with Azure blob storage. @@ -69,9 +69,6 @@ public class BlobStore { @Autowired private ILogger logger; - @Autowired - private DpsHeaders headers; - private static final String LOG_PREFIX = "azure-core-lib"; /** @@ -89,20 +86,20 @@ public class BlobStore { } catch (BlobStorageException ex) { if (ex.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND)) { String errorMessage = "Specified blob was not found"; - logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); + 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, headers.getHeaders()); + 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, headers.getHeaders()); + 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, headers.getHeaders()); + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); throw new AppException(500, errorMessage, ex.getMessage(), ex); } } @@ -122,11 +119,11 @@ public class BlobStore { } catch (BlobStorageException ex) { if (ex.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND)) { String errorMessage = "Specified blob was not found"; - logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); + 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, headers.getHeaders()); + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); throw new AppException(500, errorMessage, ex.getMessage(), ex); } } @@ -149,11 +146,11 @@ public class BlobStore { blockBlobClient.upload(dataStream, bytesSize, true); } catch (BlobStorageException ex) { String errorMessage = "Failed to upload file content."; - logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); + 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, headers.getHeaders()); + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); throw new AppException(500, errorMessage, ex.getMessage(), ex); } } @@ -168,7 +165,7 @@ public class BlobStore { return blobContainerClientFactory.getClient(dataPartitionId); } catch (Exception ex) { String errorMessage = "Error creating creating blob container client."; - logger.warning(LOG_PREFIX, errorMessage, headers.getHeaders()); + logger.warning(LOG_PREFIX, errorMessage, Collections.emptyMap()); 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 5a65802c..453367ae 100644 --- a/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java +++ b/src/test/java/org/opengroup/osdu/azure/blobstorage/BlobStoreTest.java @@ -66,9 +66,6 @@ public class BlobStoreTest { @Mock ILogger logger; - @Mock - DpsHeaders headers; - @BeforeEach void init() { initMocks(this); @@ -76,7 +73,6 @@ public class BlobStoreTest { lenient().doReturn(blobClient).when(blobContainerClient).getBlobClient(FILE_PATH); lenient().doReturn(blockBlobClient).when(blobClient).getBlockBlobClient(); lenient().doNothing().when(logger).warning(eq("azure-core-lib"), any(), anyMap()); - lenient().doReturn(new HashMap()).when(headers).getHeaders(); } @Test -- GitLab