Commit 8dd9b390 authored by satish kumar nargani's avatar satish kumar nargani
Browse files

Implemented Teardown method to cleanup file generated

parent c436fe5f
Pipeline #7882 failed
......@@ -93,6 +93,23 @@
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.50.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
......
......@@ -6,6 +6,7 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import static org.hamcrest.CoreMatchers.containsString;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import org.opengroup.osdu.core.common.model.file.FileLocationResponse;
import org.opengroup.osdu.core.common.model.file.LocationResponse;
import org.opengroup.osdu.file.apitest.Config;
......@@ -27,13 +28,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestFile extends File {
protected static final DummyRecordsHelper RECORDS_HELPER = new DummyRecordsHelper();
private static String containerName = System.getProperty("DATA_PARTITION_ID", System.getenv("DATA_PARTITION_ID"));
@BeforeAll
public static void setUp() throws IOException {
client = new HttpClientAzure();
cloudStorageUtil = new StorageUtilAzure();
}
@Test
@Override
public void getLocationShouldReturnForbidden_whenGivenNoDataAccess() throws Exception {
......@@ -55,7 +57,6 @@ public class TestFile extends File {
"{}");
assertEquals(HttpStatus.SC_FORBIDDEN, getLocationResponse.getStatus());
}
@Test
@Override
public void shouldReturnUnauthorized_whenPartitionIdNotGiven() throws Exception {
......@@ -136,9 +137,10 @@ public class TestFile extends File {
String resp = "ConstraintViolationException: Invalid FileLocationRequest";
assertThat(responseObject.message, containsString(resp));
}
@Test
public void getFileListShouldReturnForbidden_whenGivenNoDataAccess() throws Exception {
ClientResponse getLocationResponse = client.send(
ClientResponse getLocationResponse = client.send(
getFileList,
"POST",
getHeaders(Config.getDataPartitionId(), client.getNoDataAccessToken()),
......@@ -146,7 +148,7 @@ public class TestFile extends File {
assertEquals(HttpStatus.SC_UNAUTHORIZED, getLocationResponse.getStatus());
}
@Test
@Test
public void getFileListShouldReturnUnauthorized_whenGivenAnonimus() throws Exception {
ClientResponse getLocationResponse = client.send(
getFileList,
......@@ -175,12 +177,13 @@ public class TestFile extends File {
"{}");
assertEquals(HttpStatus.SC_FORBIDDEN, getLocationResponse.getStatus());
}
@Test
public void getFileList_shouldReturnBadrequest_whenrecordnotFound() throws Exception {
LocalDateTime from = LocalDateTime.now(ZoneId.of(Config.getTimeZone()));
LocalDateTime to = LocalDateTime.now(ZoneId.of(Config.getTimeZone()));
String fileListRequestBody = FileUtilsAzure.generateFileListRequestBody(0,from, to, (short) 1);
String fileListRequestBody = FileUtilsAzure.generateFileListRequestBody(0, from, to, (short) 1);
ClientResponse fileListResponse = client.send(
getFileList,
......@@ -191,6 +194,7 @@ public class TestFile extends File {
assertNotNull(fileListResponse);
assertEquals(HttpStatus.SC_BAD_REQUEST, fileListResponse.getStatus());
}
@Test
public void getFileListShouldReturnBadrequest_whenRequestBodyEmpty() throws Exception {
ClientResponse getFileLocationResponse = client.send(
......@@ -205,8 +209,7 @@ public class TestFile extends File {
assertThat(responseObject.message, containsString(resp));
}
//To Do
// This tear down method should get refactored to clean up the signed file location when there is change in azure main implementation
@AfterAll
public static void tearDown() throws Exception {
if (!locationResponses.isEmpty()) {
......@@ -219,11 +222,14 @@ public class TestFile extends File {
FileLocationResponse fileLocationResponse = mapper
.readValue(getFileLocationResponse.getEntity(String.class), FileLocationResponse.class);
if(fileLocationResponse!=null && StringUtils.isNotBlank(fileLocationResponse.getLocation())) {
String fileLoc[] = fileLocationResponse.getLocation().split(containerName);
String fileName = fileLoc[1];
cloudStorageUtil.deleteCloudFile(containerName, fileName);
String filename = fileLocationResponse.getLocation();
cloudStorageUtil.deleteCloudFile("TestContainer", filename);
}
}
}
}
}
......@@ -14,10 +14,23 @@
package util;
public class AzureBlobService {
import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobContainerClientBuilder;
import com.azure.storage.blob.BlobUrlParts;
import com.azure.storage.blob.specialized.BlockBlobClient;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AzureBlobService {
private static String clientSecret = System.getProperty("TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("TESTER_SERVICEPRINCIPAL_SECRET"));
private static String clientId = System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER"));
private static String tenantId = System.getProperty("AZURE_AD_TENANT_ID", System.getenv("AZURE_AD_TENANT_ID"));
private static String storageAccount;
AzureBlobService(String storageAccount) {
......@@ -28,9 +41,49 @@ public class AzureBlobService {
return System.getProperty("AZURE_STORAGE_ACCOUNT", System.getenv("AZURE_STORAGE_ACCOUNT"));
}
private static String generateContainerPath(String accountName, String containerName) {
return String.format("https://%s.blob.core.windows.net/%s", accountName, containerName);
private BlobContainerClient getBlobContainerClient(String accountName, String containerName) {
ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientSecret(clientSecret)
.clientId(clientId)
.tenantId(tenantId)
.build();
BlobContainerClient blobContainerClient = new BlobContainerClientBuilder()
.endpoint(getBlobAccountUrl(accountName))
.credential(clientSecretCredential)
.containerName(containerName)
.buildClient();
return blobContainerClient;
}
private static String getBlobAccountUrl(String accountName) {
return String.format("https://%s.blob.core.windows.net", accountName);
}
private static String generateBlobName(String blobName) {
return blobName.replace(":","_");
}
private static String generateBlobPath(String accountName, String containerName, String blobName) {
return String.format("https://%s.blob.core.windows.net/%s%s", accountName, containerName, blobName);
}
public void deleteObject(String containerName, String blobName) {
String blobPath = generateBlobPath(storageAccount, containerName, generateBlobName(blobName));
BlobUrlParts parts = BlobUrlParts.parse(blobPath);
BlobContainerClient blobContainerClient = getBlobContainerClient(parts.getAccountName(), parts.getBlobContainerName());
BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(parts.getBlobName()).getBlockBlobClient();
if (blockBlobClient.exists()) {
blockBlobClient.delete();
}
}
}
......@@ -24,7 +24,7 @@ public class AzureBlobServiceConfig {
this.storageAccount = storageAccount;
}
public AzureBlobService azureBlobService() {
return new AzureBlobService(storageAccount);
}
public AzureBlobService azureBlobService() {
return new AzureBlobService(storageAccount);
}
}
......@@ -3,20 +3,20 @@ package util;
import org.opengroup.osdu.file.util.CloudStorageUtil;
public class StorageUtilAzure extends CloudStorageUtil {
private static String clientSecret = System.getProperty("TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("TESTER_SERVICEPRINCIPAL_SECRET"));
private static String clientId = System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER"));
private static String tenantId = System.getProperty("AZURE_AD_TENANT_ID", System.getenv("AZURE_AD_TENANT_ID"));
private static String storageAccount = AzureBlobService.getStorageAccount();
private AzureBlobService blobService;
public StorageUtilAzure() {
AzureBlobServiceConfig config = new AzureBlobServiceConfig(storageAccount);
blobService = config.azureBlobService();
}
private static String storageAccount = AzureBlobService.getStorageAccount();
@Override
public void deleteCloudFile(String bucketName, String fileName) {
blobService.deleteObject(bucketName,fileName);
}
}
  • @satishkumarnargani Can you change name of method from generateBlobName to decodeBlobName or convertBlobName, etc? or find out another mechanism to stash the blob name before the %2f (/) are created?

Markdown is supported
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