Commit 16939659 authored by Aman Verma's avatar Aman Verma Committed by Kishore Battula
Browse files

adding method to copy files in `blobStore` class

parent 1505e5f4
......@@ -14,8 +14,10 @@
package org.opengroup.osdu.azure.blobstorage;
import com.azure.core.util.polling.SyncPoller;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.models.BlobCopyInfo;
import com.azure.storage.blob.models.BlobErrorCode;
import com.azure.storage.blob.models.BlobStorageException;
import com.azure.storage.blob.sas.BlobSasPermission;
......@@ -32,6 +34,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.Collections;
......@@ -60,13 +63,19 @@ import java.util.Collections;
* Boolean success = blobStorage.deleteFromStorageContainer("dataPartitionId", "filePath", "containerName");
* }
*
* void getSasToken()
* void getSasTokenExample()
* {
* int expiryDays = 7;
* OffsetDateTime expiryTime = OffsetDateTime.now().plusDays(expiryDays);
* BlobSasPermission permissions = (new BlobSasPermission()).setReadPermission(true).setCreatePermission(true);
* String sasToken = blobStorage.getSasToken("dataPartitionId", "filePath", "containerName", expiryTime, permissions);
* }
*
* void copyFileExample()
* {
* BlobCopyInfo copyInfo = blobStore.copyFile("dataPartitionId", "filePath", "containerName", "sourceFilePath");
* System.out.println("copy info " + copyInfo.getCopyStatus());
* }
* }
* </pre>
*/
......@@ -186,6 +195,22 @@ public class BlobStore {
return blockBlobClient.getBlobUrl() + "?" + generateSASToken(blockBlobClient, expiryTime, permissions);
}
/**
* Method is used to copy a file specified at Source URL to the provided destination.
* @param dataPartitionId Data partition id
* @param filePath Path of file (blob) to which the file has to be copied
* @param containerName Name of the storage container
* @param sourceUrl URL of the file from where the file contents have to be copied
* @return Blob Copy Final Result.
*/
public BlobCopyInfo copyFile(final String dataPartitionId, final String filePath, final String containerName,
final String sourceUrl) {
BlobContainerClient blobContainerClient = getBlobContainerClient(dataPartitionId, containerName);
BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(filePath).getBlockBlobClient();
SyncPoller<BlobCopyInfo, Void> result = blockBlobClient.beginCopy(sourceUrl, Duration.ofSeconds(1));
return result.waitForCompletion().getValue();
}
/**
*
* @param blockBlobClient Blob client
......
......@@ -14,9 +14,12 @@
package org.opengroup.osdu.azure.blobstorage;
import com.azure.core.util.polling.PollResponse;
import com.azure.core.util.polling.SyncPoller;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.models.BlobCopyInfo;
import com.azure.storage.blob.models.BlobErrorCode;
import com.azure.storage.blob.models.BlobStorageException;
import com.azure.storage.blob.models.BlockBlobItem;
......@@ -32,6 +35,7 @@ import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.http.AppException;
import java.io.ByteArrayOutputStream;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
......@@ -45,6 +49,7 @@ public class BlobStoreTest {
private static final String FILE_PATH = "filePath";
private static final String CONTENT = "hello world";
private static final String STORAGE_CONTAINER_NAME = "containerName";
private static final String SOURCE_FILE_URL = "http://someURL";
@InjectMocks
private BlobStore blobStore;
......@@ -70,6 +75,15 @@ public class BlobStoreTest {
@Mock
private ILogger logger;
@Mock
private BlobCopyInfo blobCopyInfo;
@Mock
private SyncPoller<BlobCopyInfo, Void> syncPoller;
@Mock
private PollResponse<BlobCopyInfo> pollResponse;
@BeforeEach
void init() {
initMocks(this);
......@@ -222,6 +236,28 @@ public class BlobStoreTest {
}
}
@Test
public void copyFile_Success() {
String copyId = "copyId";
doReturn(copyId).when(blobCopyInfo).getCopyId();
doReturn(blobCopyInfo).when(pollResponse).getValue();
doReturn(pollResponse).when(syncPoller).waitForCompletion();
doReturn(syncPoller).when(blockBlobClient).beginCopy(SOURCE_FILE_URL, Duration.ofSeconds(1));
BlobCopyInfo copyInfo = blobStore.copyFile(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME, SOURCE_FILE_URL);
assertEquals(copyInfo.getCopyId(), copyId);
}
@Test
public void copyFile_Failure() {
doReturn(null).when(pollResponse).getValue();
doReturn(pollResponse).when(syncPoller).waitForCompletion();
doReturn(syncPoller).when(blockBlobClient).beginCopy(SOURCE_FILE_URL, Duration.ofSeconds(1));
BlobCopyInfo copyInfo = blobStore.copyFile(PARTITION_ID, FILE_PATH, STORAGE_CONTAINER_NAME, SOURCE_FILE_URL);
assertEquals(copyInfo, null);
}
private BlobStorageException mockStorageException(BlobErrorCode errorCode) {
BlobStorageException mockException = mock(BlobStorageException.class);
lenient().when(mockException.getErrorCode()).thenReturn(errorCode);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment