From cc9a3ac8da752c1e74ead7ae908d3c39c648894e Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 22 Jan 2018 15:06:43 +0100 Subject: [PATCH] DATAMONGO-1850 - Polishing. Remove blank line, add tests and migrate to AssertJ. Original Pull Request: #527 --- .../data/mongodb/gridfs/GridFsResource.java | 1 - .../gridfs/GridFsResourceUnitTests.java | 10 +++ .../GridFsTemplateIntegrationTests.java | 66 +++++++++---------- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java index 085f54061..f2a6abcda 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java @@ -108,7 +108,6 @@ public class GridFsResource extends InputStreamResource { @SuppressWarnings("deprecation") public String getContentType() { - return Optionals .firstNonEmpty( () -> Optional.ofNullable(file.getMetadata()).map(it -> it.get(CONTENT_TYPE_FIELD, String.class)), diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsResourceUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsResourceUnitTests.java index 80c56fa6d..a159ca7da 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsResourceUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsResourceUnitTests.java @@ -30,6 +30,7 @@ import com.mongodb.client.gridfs.model.GridFSFile; * Unit tests for {@link GridFsResource}. * * @author Mark Paluch + * @auhtor Christoph Strobl */ public class GridFsResourceUnitTests { @@ -51,4 +52,13 @@ public class GridFsResourceUnitTests { assertThatThrownBy(resource::getContentType).isInstanceOf(MongoGridFSException.class); } + + @Test // DATAMONGO-1850 + public void shouldThrowExceptionOnEmptyContentTypeInMetadata() { + + GridFSFile file = new GridFSFile(new BsonObjectId(), "foo", 0, 0, new Date(), "foo", new Document()); + GridFsResource resource = new GridFsResource(file); + + assertThatThrownBy(resource::getContentType).isInstanceOf(MongoGridFSException.class); + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java index 184f67f74..59e081f62 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java @@ -15,11 +15,8 @@ */ package org.springframework.data.mongodb.gridfs; -import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.junit.Assert.assertThat; -import static org.springframework.data.mongodb.core.query.Criteria.*; +import static org.assertj.core.api.Assertions.*; +import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Query.*; import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*; @@ -42,6 +39,7 @@ import org.springframework.data.mongodb.core.query.Query; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import com.mongodb.MongoGridFSException; import com.mongodb.client.gridfs.GridFSFindIterable; /** @@ -73,8 +71,8 @@ public class GridFsTemplateIntegrationTests { List files = new ArrayList(); GridFSFindIterable result = operations.find(query(where("_id").is(reference))); result.into(files); - assertThat(files.size(), is(1)); - assertEquals(((BsonObjectId) files.get(0).getId()).getValue(), reference); + assertThat(files.size()).isEqualTo(1); + assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference); } @Test // DATAMONGO-6 @@ -87,8 +85,8 @@ public class GridFsTemplateIntegrationTests { GridFSFindIterable result = operations.find(query(whereMetaData("key").is("value"))); result.into(files); - assertThat(files.size(), is(1)); - assertEquals(((BsonObjectId) files.get(0).getId()).getValue(), reference); + assertThat(files.size()).isEqualTo(1); + assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference); } @Test // DATAMONGO-6 @@ -103,8 +101,8 @@ public class GridFsTemplateIntegrationTests { GridFSFindIterable result = operations.find(query(whereFilename().is("foo.xml"))); result.into(files); - assertThat(files.size(), is(1)); - assertEquals(((BsonObjectId) files.get(0).getId()).getValue(), reference); + assertThat(files.size()).isEqualTo(1); + assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference); } @Test // DATAMONGO-6 @@ -114,10 +112,9 @@ public class GridFsTemplateIntegrationTests { GridFsResource[] resources = operations.getResources("*.xml"); - assertThat(resources.length, is(1)); - assertThat(((BsonObjectId) resources[0].getId()).getValue(), is(reference)); - assertThat(resources[0].contentLength(), is(resource.contentLength())); - // assertThat(resources[0].getContentType(), is(resource.())); + assertThat(resources.length).isEqualTo(1); + assertThat(((BsonObjectId) resources[0].getId()).getValue()).isEqualTo(reference); + assertThat(resources[0].contentLength()).isEqualTo(resource.contentLength()); } @Test // DATAMONGO-6 @@ -126,10 +123,9 @@ public class GridFsTemplateIntegrationTests { ObjectId reference = operations.store(resource.getInputStream(), "foo.xml"); GridFsResource[] resources = operations.getResources("foo.xml"); - assertThat(resources.length, is(1)); - assertThat(((BsonObjectId) resources[0].getId()).getValue(), is(reference)); - assertThat(resources[0].contentLength(), is(resource.contentLength())); - // assertThat(resources[0].getContentType(), is(reference.getContentType())); + assertThat(resources.length).isEqualTo(1); + assertThat(((BsonObjectId) resources[0].getId()).getValue()).isEqualTo(reference); + assertThat(resources[0].contentLength()).isEqualTo(resource.contentLength()); } @Test // DATAMONGO-503 @@ -141,8 +137,8 @@ public class GridFsTemplateIntegrationTests { GridFSFindIterable result = operations.find(query(whereContentType().is("application/xml"))); result.into(files); - assertThat(files.size(), is(1)); - assertEquals(((BsonObjectId) files.get(0).getId()).getValue(), reference); + assertThat(files.size()).isEqualTo(1); + assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference); } @Test // DATAMONGO-534 @@ -158,10 +154,8 @@ public class GridFsTemplateIntegrationTests { GridFSFindIterable result = operations.find(query); result.into(files); - assertThat(files, hasSize(3)); - assertEquals(((BsonObjectId) files.get(0).getId()).getValue(), first); - assertEquals(((BsonObjectId) files.get(1).getId()).getValue(), second); - assertEquals(((BsonObjectId) files.get(2).getId()).getValue(), third); + assertThat(files).hasSize(3).extracting(it -> ((BsonObjectId) it.getId()).getValue()).containsExactly(first, second, + third); } @Test // DATAMONGO-534, DATAMONGO-1762 @@ -169,12 +163,11 @@ public class GridFsTemplateIntegrationTests { ObjectId reference = operations.store(resource.getInputStream(), "foo.xml"); - List files = new ArrayList(); + List files = new ArrayList<>(); GridFSFindIterable result = operations.find(new Query()); result.into(files); - assertThat(files, hasSize(1)); - assertEquals(((BsonObjectId) files.get(0).getId()).getValue(), reference); + assertThat(files).hasSize(1).extracting(it -> ((BsonObjectId) it.getId()).getValue()).containsExactly(reference); } @Test(expected = IllegalArgumentException.class) // DATAMONGO-1762 @@ -184,7 +177,7 @@ public class GridFsTemplateIntegrationTests { @Test // DATAMONGO-813 public void getResourceShouldReturnNullForNonExistingResource() { - assertThat(operations.getResource("doesnotexist"), is(nullValue())); + assertThat(operations.getResource("doesnotexist")).isNull(); } @Test // DATAMONGO-809 @@ -197,8 +190,7 @@ public class GridFsTemplateIntegrationTests { GridFSFindIterable result = operations.find(query(whereMetaData("key").is("value"))); result.into(files); - assertThat(files, hasSize(1)); - assertEquals(((BsonObjectId) files.get(0).getId()).getValue(), reference); + assertThat(files).hasSize(1).extracting(it -> ((BsonObjectId) it.getId()).getValue()).containsExactly(reference); } @Test // DATAMONGO-809 @@ -212,8 +204,7 @@ public class GridFsTemplateIntegrationTests { GridFSFindIterable result = operations.find(query(whereMetaData("version").is("1.0"))); result.into(files); - assertThat(files, hasSize(1)); - assertEquals(((BsonObjectId) files.get(0).getId()).getValue(), reference); + assertThat(files).hasSize(1).extracting(it -> ((BsonObjectId) it.getId()).getValue()).containsExactly(reference); } @Test // DATAMONGO-1695 @@ -224,6 +215,15 @@ public class GridFsTemplateIntegrationTests { assertThat(operations.getResource("someName").getContentType()).isEqualTo("contentType"); } + @Test // DATAMONGO-1850 + public void failsOnNonExistingContentTypeRetrieval() throws IOException { + + operations.store(resource.getInputStream(), "no-content-type", (String) null); + GridFsResource result = operations.getResource("no-content-type"); + + assertThatThrownBy(() -> result.getContentType()).isInstanceOf(MongoGridFSException.class); + } + class Metadata { String version; }