Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Merge 4be8e45 into a062b07
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Apr 9, 2020
2 parents a062b07 + 4be8e45 commit cd9fdf8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## [Unreleased]

## [1.17.13] - 2020-04-09
## Changed
- Added new create method to FileServiceTestClientTest that allows the file id to be passed in

## [1.17.12] - 2019-10-23
## Changed
- Update utilities to version 1.20.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,34 @@ public UUID create(
final InputStream contentStream,
final Connection connection) throws FileServiceException {

return create(randomUUID(), fileName, mediaType, contentStream, connection);
}

/**
* Puts a file in the database with the specified id.
*
* NB: the {@link Connection} is not closed by the client.
*
* @param fileName the name of the file. Will appear in the metadata json
* @param mediaType the media type of the file. Will appear in the metadata json
* @param contentStream an {@link InputStream} from the file to store
* @param connection a live connection to the database.
* @return the file id
* @throws FileServiceException if saving the file fails
*/
public UUID create(
final UUID fileId,
final String fileName,
final String mediaType,
final InputStream contentStream,
final Connection connection) throws FileServiceException {

final JsonObject metadata = createObjectBuilder()
.add("fileName", fileName)
.add("mediaType", mediaType)
.add("createdAt", ZonedDateTimes.toString(utcClock.now()))
.build();

final UUID fileId = randomUUID();
contentJdbcRepository.insert(fileId, contentStream, connection);
metadataJdbcRepository.insert(fileId, metadata, connection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static java.io.File.createTempFile;
import static java.nio.file.Files.copy;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.util.UUID.randomUUID;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertThat;
Expand All @@ -22,9 +23,9 @@
import java.util.UUID;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


public class FileServiceTestClientTest {

private static final String URL = "jdbc:h2:mem:test;MV_STORE=FALSE;MVCC=FALSE";
Expand All @@ -34,29 +35,38 @@ public class FileServiceTestClientTest {

private static final String LIQUIBASE_FILE_STORE_DB_CHANGELOG_XML = "liquibase/file-service-liquibase-db-changelog.xml";

private final LiquibaseDatabaseBootstrapper liquibaseDatabaseBootstrapper = new LiquibaseDatabaseBootstrapper();
private final JdbcConnectionProvider jdbcConnectionProvider = new JdbcConnectionProvider();
private final ClasspathFileResource classpathFileResource = new ClasspathFileResource();

private final FileServiceTestClient fileServiceTestClient = new FileServiceTestClient(ANSI_SQL);

private Connection connection;

@BeforeClass
public static void createInMemoryDatabase() throws Exception {

new LiquibaseDatabaseBootstrapper().bootstrap(
LIQUIBASE_FILE_STORE_DB_CHANGELOG_XML,
new JdbcConnectionProvider().getConnection(
URL,
USERNAME,
PASSWORD,
DRIVER_CLASS
));
}

@Before
public void createInMemoryDatabase() throws Exception {
public void createDatabaseConnection() throws Exception {

connection = jdbcConnectionProvider.getConnection(
URL,
USERNAME,
PASSWORD,
DRIVER_CLASS
);

liquibaseDatabaseBootstrapper.bootstrap(
LIQUIBASE_FILE_STORE_DB_CHANGELOG_XML,
connection);
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void shouldStoreAndGetAnImage() throws Exception {

Expand Down Expand Up @@ -98,6 +108,51 @@ public void shouldStoreAndGetAnImage() throws Exception {
assertThat(outputFile.length(), is(greaterThan(0L)));
assertThat(outputFile.length(), is(inputFile.length()));
}
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void shouldStoreAndGetAnImageWithTheSpecifiedId() throws Exception {

final String fileName = "some.jpg";
final String mediaType = "image/jpeg";
final File inputFile = classpathFileResource.getFileFromClasspath("/for-testing.jpg");

final InputStream contentStream = new FileInputStream(inputFile);

final UUID fileId = randomUUID();

assertThat(fileServiceTestClient.create(fileId, fileName, mediaType, contentStream, connection), is(fileId));

contentStream.close();

final Optional<FileReference> fileReferenceOptional = fileServiceTestClient.read(fileId, connection);

assertThat(fileReferenceOptional.isPresent(), is(true));

final FileReference fileReference = fileReferenceOptional.get();

assertThat(fileReference.getFileId(), is(fileId));
assertThat(fileReference.isDeleted(), is(false));


final String metadataJson = fileReference.getMetadata().toString();

with(metadataJson)
.assertThat("$.fileName", is(fileName))
.assertThat("$.mediaType", is(mediaType))
;


try(final InputStream inputStream = fileReference.getContentStream()) {
final File outputFile = createTempFile("for-testing-please.delete", "jpg");
outputFile.deleteOnExit();

copy(inputStream, outputFile.toPath(), REPLACE_EXISTING);

assertThat(outputFile.exists(), is(true));
assertThat(outputFile.length(), is(greaterThan(0L)));
assertThat(outputFile.length(), is(inputFile.length()));
}
}
}

0 comments on commit cd9fdf8

Please sign in to comment.