From 2756370224e5bdb8bc070a6742680543ed995a49 Mon Sep 17 00:00:00 2001 From: Nick Stolwijk Date: Wed, 16 Oct 2019 12:34:22 +0200 Subject: [PATCH] DATAMONGO-2392 - Fix handling of old GridFS files with custom type for _id field. --- .../gridfs/ReactiveGridFsTemplate.java | 2 +- ...ctiveGridFsTemplateWithOldGridFsTests.java | 74 +++++++++++++++++++ .../test/resources/gridfs/reactive-gridfs.xml | 13 +++- 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateWithOldGridFsTests.java 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 2770a4839f..82af8c3f77 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 @@ -222,7 +222,7 @@ public Mono getResource(GridFSFile file) { return Mono.fromSupplier(() -> { - GridFSDownloadStream stream = getGridFs().openDownloadStream(file.getObjectId()); + GridFSDownloadStream stream = getGridFs().openDownloadStream(file.getId()); return new ReactiveGridFsResource(file, BinaryStreamAdapters.toPublisher(stream, dataBufferFactory)); }); 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 new file mode 100644 index 0000000000..3ab035cca2 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateWithOldGridFsTests.java @@ -0,0 +1,74 @@ +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(); + } +} diff --git a/spring-data-mongodb/src/test/resources/gridfs/reactive-gridfs.xml b/spring-data-mongodb/src/test/resources/gridfs/reactive-gridfs.xml index a8df047609..91e90f010c 100644 --- a/spring-data-mongodb/src/test/resources/gridfs/reactive-gridfs.xml +++ b/spring-data-mongodb/src/test/resources/gridfs/reactive-gridfs.xml @@ -3,13 +3,13 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> - + - + @@ -27,4 +27,13 @@ + + + + + + + + +