Skip to content

Commit

Permalink
Add exist method on BlobStoreRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
vpaturet committed Dec 3, 2024
1 parent 9ecea77 commit 045a451
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<hazelcast-kubernetes.version>2.2.3</hazelcast-kubernetes.version>
<httpclient5.version>5.4.1</httpclient5.version>
<hamcrest.version>3.0</hamcrest.version>

<jakarta.annotation-api.version>3.0.0</jakarta.annotation-api.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.rutebanken.helper.storage.BlobStoreException;
import org.rutebanken.helper.storage.model.BlobDescriptor;
import org.rutebanken.helper.storage.repository.BlobStoreRepository;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.services.s3.S3Client;
Expand Down Expand Up @@ -49,10 +51,14 @@ public S3BlobStoreRepository(S3Client s3Client) {

@Override
public InputStream getBlob(String objectName) {
return s3Client.getObject(
GetObjectRequest.builder().bucket(containerName).key(objectName).build(),
ResponseTransformer.toInputStream()
);
try {
return s3Client.getObject(
GetObjectRequest.builder().bucket(containerName).key(objectName).build(),
ResponseTransformer.toInputStream()
);
} catch (NoSuchKeyException e) {
return null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public void setContainerName(String containerName) {
this.containerName = containerName;
}

@Override
public boolean exist(String objectName) {
return BlobStoreHelper.existBlob(storage, containerName, objectName);
}

@Override
public InputStream getBlob(String name) {
return BlobStoreHelper.getBlob(storage, containerName, name);
Expand Down
5 changes: 5 additions & 0 deletions storage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ Inspired by: https://github.com/fabric8io/ipaas-quickstarts/
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>${jakarta.annotation-api.version}</version>
</dependency>

<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.rutebanken.helper.storage.repository;

import jakarta.annotation.Nullable;
import org.rutebanken.helper.storage.BlobAlreadyExistsException;
import org.rutebanken.helper.storage.model.BlobDescriptor;

Expand All @@ -31,12 +32,22 @@
public interface BlobStoreRepository {


/**
* Return true if the given blob exists in the repository.
* The default implementation retrieves the object and test for nullity.
* Specific implementations can provide an optimized algorithm.
*/
default boolean exist(String objectName) {
return getBlob(objectName) != null;
}

/**
* Download a blob from storage.
*
* @param objectName the name of the blob
* @return an InputStream on the file content.
* @return an InputStream on the file content or null if the object does not exist.
*/
@Nullable
InputStream getBlob(String objectName);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.rutebanken.helper.storage.repository;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.*;
class InMemoryBlobStoreRepositoryTest {

public static final String BLOB_NAME = "blobName";
public static final byte[] BLOB_CONTENT = "content".getBytes();

@Test
void testUploadExistAndGet() throws IOException {
InMemoryBlobStoreRepository repository = new InMemoryBlobStoreRepository(new HashMap<>());
repository.uploadBlob(BLOB_NAME, new ByteArrayInputStream(BLOB_CONTENT));
assertTrue(repository.exist(BLOB_NAME));
InputStream blob = repository.getBlob(BLOB_NAME);
assertNotNull(blob);
assertArrayEquals(BLOB_CONTENT, blob.readAllBytes());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.rutebanken.helper.storage.repository;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import static org.junit.jupiter.api.Assertions.*;

class LocalDiskBlobStoreRepositoryTest {

public static final String BLOB_NAME = "blobName";
public static final byte[] BLOB_CONTENT = "content".getBytes();

@TempDir
File tempDirectory;

@Test
void testUploadExistAndGet() throws IOException {
LocalDiskBlobStoreRepository repository = new LocalDiskBlobStoreRepository(tempDirectory.getAbsolutePath());
repository.uploadBlob(BLOB_NAME, new ByteArrayInputStream(BLOB_CONTENT));
assertTrue(repository.exist(BLOB_NAME));
InputStream blob = repository.getBlob(BLOB_NAME);
assertNotNull(blob);
assertArrayEquals(BLOB_CONTENT, blob.readAllBytes());
}

}

0 comments on commit 045a451

Please sign in to comment.