diff --git a/CHANGELOG.md b/CHANGELOG.md index 02aedec..964600b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/file-service-test-utils/src/main/java/uk/gov/justice/services/fileservice/utils/test/FileServiceTestClient.java b/file-service-test-utils/src/main/java/uk/gov/justice/services/fileservice/utils/test/FileServiceTestClient.java index 0719d08..1cec6cf 100644 --- a/file-service-test-utils/src/main/java/uk/gov/justice/services/fileservice/utils/test/FileServiceTestClient.java +++ b/file-service-test-utils/src/main/java/uk/gov/justice/services/fileservice/utils/test/FileServiceTestClient.java @@ -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); diff --git a/file-service-test-utils/src/test/java/uk/gov/justice/services/fileservice/utils/test/FileServiceTestClientTest.java b/file-service-test-utils/src/test/java/uk/gov/justice/services/fileservice/utils/test/FileServiceTestClientTest.java index 61490e9..e2759fb 100644 --- a/file-service-test-utils/src/test/java/uk/gov/justice/services/fileservice/utils/test/FileServiceTestClientTest.java +++ b/file-service-test-utils/src/test/java/uk/gov/justice/services/fileservice/utils/test/FileServiceTestClientTest.java @@ -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; @@ -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"; @@ -34,7 +35,6 @@ 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(); @@ -42,8 +42,21 @@ public class FileServiceTestClientTest { 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, @@ -51,12 +64,9 @@ public void createInMemoryDatabase() throws Exception { PASSWORD, DRIVER_CLASS ); - - liquibaseDatabaseBootstrapper.bootstrap( - LIQUIBASE_FILE_STORE_DB_CHANGELOG_XML, - connection); } + @SuppressWarnings("OptionalGetWithoutIsPresent") @Test public void shouldStoreAndGetAnImage() throws Exception { @@ -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 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())); + } } }