Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@
<Bug pattern="MS_EXPOSE_REP"/>
</Match>

<Match>
<Class name="uk.nhs.adaptors.pss.translator.storage.AzureStorageService"/>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>

<Match>
<Class name="uk.nhs.adaptors.pss.translator.storage.AWSStorageService"/>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>

</FindBugsFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ void setUp() {
.buildClient();
blobServiceClient.createBlobContainer(CONTAINER_NAME);

StorageServiceConfiguration config = new StorageServiceConfiguration();
config.setContainerName(CONTAINER_NAME);
var blobContainerClient = blobServiceClient.getBlobContainerClient(CONTAINER_NAME);

azureStorageService = new AzureStorageService(blobServiceClient, config);
azureStorageService = new AzureStorageService(blobContainerClient);
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.net.URL;
import java.time.Duration;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
Expand All @@ -23,7 +22,6 @@
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;

@Slf4j
@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "S3Client is immutable and thread-safe.")
public class AWSStorageService implements StorageService {

private static final long SIXTY_MINUTES = 60;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@
import java.io.InputStream;

import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.specialized.BlockBlobClient;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "BlobServiceClient is immutable and thread-safe.")
public class AzureStorageService implements StorageService {

private final BlobServiceClient blobServiceClient;
private final String containerName;
private final BlobContainerClient blobContainerClient;

public AzureStorageService(BlobServiceClient client, StorageServiceConfiguration config) {
blobServiceClient = client;
containerName = config.getContainerName();
public AzureStorageService(BlobContainerClient client) {
blobContainerClient = client;
}

public void uploadFile(String filename, byte[] fileAsString) throws StorageException {
Expand All @@ -35,23 +30,15 @@ public byte[] downloadFile(String filename) throws StorageException {
}

public void deleteFile(String filename) {
BlobContainerClient containerClient = createBlobContainerClient();
BlockBlobClient blobClient = containerClient.getBlobClient(filename).getBlockBlobClient();
blobClient.delete();
createBlobBlockClient(filename).delete();
}

public String getFileLocation(String filename) {
var blobClient = createBlobBlockClient(filename);
return blobClient.getBlobUrl();
}

private BlobContainerClient createBlobContainerClient() {
return blobServiceClient.getBlobContainerClient(containerName);
return createBlobBlockClient(filename).getBlobUrl();
}

private BlockBlobClient createBlobBlockClient(String filename) {
BlobContainerClient containerClient = createBlobContainerClient();
return containerClient.getBlobClient(filename).getBlockBlobClient();
return blobContainerClient.getBlobClient(filename).getBlockBlobClient();
}

private void addFileStringToMainContainer(String filename, byte[] fileAsString) throws StorageException, IOException {
Expand All @@ -75,5 +62,4 @@ private ByteArrayOutputStream downloadFileToStream(String filename) throws Stora
throw new StorageException("Failed to download blob from Azure Blob storage", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public class StorageServiceConfig {
.credential(credentials)
.buildClient();

return new AzureStorageService(blobServiceClient, configuration);
var blobContainerClient = blobServiceClient.getBlobContainerClient(configuration.getContainerName());

return new AzureStorageService(blobContainerClient);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package uk.nhs.adaptors.pss.translator.storage;

import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.models.BlobProperties;
import com.azure.storage.blob.specialized.BlockBlobClient;
import com.azure.storage.blob.BlobClient;
Expand All @@ -25,15 +24,9 @@

@ExtendWith(MockitoExtension.class)
public class AzureStorageServiceTest {

private static final String CONTAINER_NAME = "container";
private static final String FILE_NAME = "testfile.txt";
private static final byte[] FILE_CONTENT = "mock-content".getBytes(StandardCharsets.UTF_8);

@Mock
private BlobServiceClient blobServiceClient;
@Mock
private StorageServiceConfiguration configuration;
@Mock
private BlobContainerClient blobContainerClient;
@Mock
Expand All @@ -47,19 +40,17 @@ public class AzureStorageServiceTest {

@BeforeEach
void setUp() {
when(configuration.getContainerName()).thenReturn(CONTAINER_NAME);
azureStorageService = new AzureStorageService(blobServiceClient, configuration);
azureStorageService = new AzureStorageService(blobContainerClient);
}

private void mockBlobClientChain() {
when(blobServiceClient.getBlobContainerClient(CONTAINER_NAME)).thenReturn(blobContainerClient);
private void mockBlobContainerClientChain() {
when(blobContainerClient.getBlobClient(FILE_NAME)).thenReturn(blobClient);
when(blobClient.getBlockBlobClient()).thenReturn(blockBlobClient);
}

@Test
void When_UploadFile_Expect_SuccessfullyUploadsToAzure() throws StorageException {
mockBlobClientChain();
mockBlobContainerClientChain();

azureStorageService.uploadFile(FILE_NAME, FILE_CONTENT);

Expand All @@ -68,7 +59,7 @@ void When_UploadFile_Expect_SuccessfullyUploadsToAzure() throws StorageException

@Test
void When_DownloadFile_Expect_SuccessfullyDownloadsFromAzure() throws StorageException {
mockBlobClientChain();
mockBlobContainerClientChain();
when(blockBlobClient.getProperties()).thenReturn(blobProperties);
when(blobProperties.getBlobSize()).thenReturn((long) FILE_CONTENT.length);

Expand All @@ -85,7 +76,7 @@ void When_DownloadFile_Expect_SuccessfullyDownloadsFromAzure() throws StorageExc

@Test
void When_DeleteFile_Expect_SuccessfullyDeletesFromAzure() {
mockBlobClientChain();
mockBlobContainerClientChain();

azureStorageService.deleteFile(FILE_NAME);

Expand All @@ -94,7 +85,7 @@ void When_DeleteFile_Expect_SuccessfullyDeletesFromAzure() {

@Test
void When_GetFileLocation_Expect_ReturnsCorrectUrl() {
mockBlobClientChain();
mockBlobContainerClientChain();
String expectedUrl = "https://azuremock.blob.core.windows.net/test-container/testfile.txt";
when(blockBlobClient.getBlobUrl()).thenReturn(expectedUrl);

Expand Down
Loading