diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplate.java index 82af8c3f7..fcdf77df9 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplate.java @@ -49,6 +49,7 @@ import com.mongodb.reactivestreams.client.gridfs.GridFSFindPublisher; * {@link DefaultDataBufferFactory} to create {@link DataBuffer buffers}. * * @author Mark Paluch + * @author Nick Stolwijk * @since 2.2 */ public class ReactiveGridFsTemplate extends GridFsOperationsSupport implements ReactiveGridFsOperations { 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 48ee5f548..4ddef1418 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 @@ -112,11 +112,11 @@ public class GridFsTemplateIntegrationTests { Document metadata = new Document("key", "value"); ObjectId reference = operations.store(resource.getInputStream(), "foo.xml", metadata); - List files = new ArrayList(); + List files = new ArrayList<>(); GridFSFindIterable result = operations.find(query(whereMetaData("key").is("value"))); result.into(files); - assertThat(files.size()).isEqualTo(1); + assertThat(files).hasSize(1); assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference); } @@ -128,11 +128,11 @@ public class GridFsTemplateIntegrationTests { ObjectId reference = operations.store(resource.getInputStream(), "foo.xml", metadata); - List files = new ArrayList(); + List files = new ArrayList<>(); GridFSFindIterable result = operations.find(query(whereFilename().is("foo.xml"))); result.into(files); - assertThat(files.size()).isEqualTo(1); + assertThat(files).hasSize(1); assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference); } @@ -143,7 +143,7 @@ public class GridFsTemplateIntegrationTests { GridFsResource[] resources = operations.getResources("*.xml"); - assertThat(resources.length).isEqualTo(1); + assertThat(resources).hasSize(1); assertThat(((BsonObjectId) resources[0].getId()).getValue()).isEqualTo(reference); assertThat(resources[0].contentLength()).isEqualTo(resource.contentLength()); } @@ -154,7 +154,7 @@ public class GridFsTemplateIntegrationTests { ObjectId reference = operations.store(resource.getInputStream(), "foo.xml"); GridFsResource[] resources = operations.getResources("foo.xml"); - assertThat(resources.length).isEqualTo(1); + assertThat(resources).hasSize(1); assertThat(((BsonObjectId) resources[0].getId()).getValue()).isEqualTo(reference); assertThat(resources[0].contentLength()).isEqualTo(resource.contentLength()); } @@ -164,11 +164,11 @@ public class GridFsTemplateIntegrationTests { ObjectId reference = operations.store(resource.getInputStream(), "foo2.xml", "application/xml"); - List files = new ArrayList(); + List files = new ArrayList<>(); GridFSFindIterable result = operations.find(query(whereContentType().is("application/xml"))); result.into(files); - assertThat(files.size()).isEqualTo(1); + assertThat(files).hasSize(1); assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference); } @@ -181,7 +181,7 @@ public class GridFsTemplateIntegrationTests { Query query = new Query().with(Sort.by(Direction.ASC, "filename")); - List files = new ArrayList(); + List files = new ArrayList<>(); GridFSFindIterable result = operations.find(query); result.into(files); @@ -217,7 +217,7 @@ public class GridFsTemplateIntegrationTests { Document metadata = new Document("key", "value"); ObjectId reference = operations.store(resource.getInputStream(), "foobar", metadata); - List files = new ArrayList(); + List files = new ArrayList<>(); GridFSFindIterable result = operations.find(query(whereMetaData("key").is("value"))); result.into(files); @@ -231,7 +231,7 @@ public class GridFsTemplateIntegrationTests { metadata.version = "1.0"; ObjectId reference = operations.store(resource.getInputStream(), "foobar", metadata); - List files = new ArrayList(); + List files = new ArrayList<>(); GridFSFindIterable result = operations.find(query(whereMetaData("version").is("1.0"))); result.into(files); @@ -252,7 +252,7 @@ public class GridFsTemplateIntegrationTests { operations.store(resource.getInputStream(), "no-content-type", (String) null); GridFsResource result = operations.getResource("no-content-type"); - assertThatThrownBy(() -> result.getContentType()).isInstanceOf(MongoGridFSException.class); + assertThatThrownBy(result::getContentType).isInstanceOf(MongoGridFSException.class); } @Test // DATAMONGO-1813 diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateTests.java index 3825e729d..cc808339f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateTests.java @@ -24,6 +24,7 @@ import reactor.core.publisher.Flux; import reactor.test.StepVerifier; import java.io.IOException; +import java.util.UUID; import org.bson.BsonObjectId; import org.bson.Document; @@ -39,11 +40,14 @@ import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DefaultDataBuffer; import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.dao.IncorrectResultSizeDataAccessException; +import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.core.query.Query; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.StreamUtils; +import com.mongodb.gridfs.GridFS; +import com.mongodb.gridfs.GridFSInputFile; import com.mongodb.reactivestreams.client.gridfs.AsyncInputStream; import com.mongodb.reactivestreams.client.gridfs.helpers.AsyncStreamHelper; @@ -52,6 +56,7 @@ import com.mongodb.reactivestreams.client.gridfs.helpers.AsyncStreamHelper; * * @author Mark Paluch * @author Christoph Strobl + * @author Nick Stolwijk */ @RunWith(SpringRunner.class) @ContextConfiguration("classpath:gridfs/reactive-gridfs.xml") @@ -60,6 +65,7 @@ public class ReactiveGridFsTemplateTests { Resource resource = new ClassPathResource("gridfs/gridfs.xml"); @Autowired ReactiveGridFsOperations operations; + @Autowired SimpleMongoDbFactory mongoClient; @Before public void setUp() { @@ -86,6 +92,25 @@ public class ReactiveGridFsTemplateTests { .verifyComplete(); } + @Test // DATAMONGO-2392 + public void storesAndFindsByUUID() throws IOException { + + UUID uuid = UUID.randomUUID(); + + GridFS fs = new GridFS(mongoClient.getLegacyDb()); + GridFSInputFile in = fs.createFile(resource.getInputStream(), "gridfs.xml"); + + in.put("_id", uuid); + in.put("contentType", "application/octet-stream"); + in.save(); + + operations.findOne(query(where("_id").is(uuid))).flatMap(operations::getResource) + .flatMapMany(ReactiveGridFsResource::getDownloadStream) // + .transform(DataBufferUtils::join) // + .doOnNext(DataBufferUtils::release).as(StepVerifier::create) // + .expectNextCount(1).verifyComplete(); + } + @Test // DATAMONGO-1855 public void writesMetadataCorrectly() throws IOException { @@ -148,7 +173,8 @@ public class ReactiveGridFsTemplateTests { public void shouldEmitFirstEntryWhenFindFirstRetrievesMoreThanOneResult() throws IOException { AsyncInputStream upload1 = AsyncStreamHelper.toAsyncInputStream(resource.getInputStream()); - AsyncInputStream upload2 = AsyncStreamHelper.toAsyncInputStream(new ClassPathResource("gridfs/another-resource.xml").getInputStream()); + AsyncInputStream upload2 = AsyncStreamHelper + .toAsyncInputStream(new ClassPathResource("gridfs/another-resource.xml").getInputStream()); operations.store(upload1, "foo.xml", null, null).block(); operations.store(upload2, "foo2.xml", null, null).block(); @@ -159,8 +185,7 @@ public class ReactiveGridFsTemplateTests { .assertNext(actual -> { assertThat(actual.getGridFSFile()).isNotNull(); - }) - .verifyComplete(); + }).verifyComplete(); } @Test // DATAMONGO-2240 @@ -179,7 +204,8 @@ public class ReactiveGridFsTemplateTests { public void shouldEmitErrorWhenFindOneRetrievesMoreThanOneResult() throws IOException { AsyncInputStream upload1 = AsyncStreamHelper.toAsyncInputStream(resource.getInputStream()); - AsyncInputStream upload2 = AsyncStreamHelper.toAsyncInputStream(new ClassPathResource("gridfs/another-resource.xml").getInputStream()); + AsyncInputStream upload2 = AsyncStreamHelper + .toAsyncInputStream(new ClassPathResource("gridfs/another-resource.xml").getInputStream()); operations.store(upload1, "foo.xml", null, null).block(); operations.store(upload2, "foo2.xml", null, null).block(); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateWithOldGridFsTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateWithOldGridFsTests.java deleted file mode 100644 index 3ab035cca..000000000 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateWithOldGridFsTests.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.springframework.data.mongodb.gridfs; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.data.mongodb.core.query.Criteria.where; -import static org.springframework.data.mongodb.core.query.Query.query; - -import java.io.IOException; - -import com.mongodb.DB; -import com.mongodb.MongoClient; -import com.mongodb.gridfs.GridFS; -import com.mongodb.gridfs.GridFSInputFile; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; -import org.springframework.core.io.buffer.DataBufferUtils; -import org.springframework.data.mongodb.core.SimpleMongoDbFactory; -import org.springframework.data.mongodb.core.query.Query; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.util.StreamUtils; -import reactor.test.StepVerifier; - -/** - * Integration tests for {@link ReactiveGridFsTemplate} in combination with the deprecated GridFs. - * - * @author Nick Stolwijk - */ -@RunWith(SpringRunner.class) -@ContextConfiguration({"classpath:gridfs/reactive-gridfs.xml"}) -public class ReactiveGridFsTemplateWithOldGridFsTests { - - Resource resource = new ClassPathResource("gridfs/gridfs.xml"); - - @Autowired ReactiveGridFsOperations operations; - - @Autowired SimpleMongoDbFactory mongoClient; - @Before - public void setUp() { - - operations.delete(new Query()) // - .as(StepVerifier::create) // - .verifyComplete(); - } - - @Test // DATAMONGO-2392 - public void storeFileWithOldGridFsAndFindItWithReactiveGridFsOperations() throws IOException { - byte[] content = StreamUtils.copyToByteArray(resource.getInputStream()); - String reference = "1af52e25-76b7-4e34-8618-9baa183c9112"; - - GridFS fs = new GridFS(mongoClient.getLegacyDb()); - GridFSInputFile in = fs.createFile(resource.getInputStream(), "gridfs.xml"); - - in.put("_id", reference); - in.put("contentType", "application/octet-stream"); - in.save(); - - operations.findOne(query(where("_id").is(reference))).flatMap(operations::getResource) - .flatMapMany(ReactiveGridFsResource::getDownloadStream) // - .transform(DataBufferUtils::join) // - .as(StepVerifier::create) // - .consumeNextWith(dataBuffer -> { - - byte[] actual = new byte[dataBuffer.readableByteCount()]; - dataBuffer.read(actual); - - assertThat(actual).isEqualTo(content); - }) // - .verifyComplete(); - } -}