DATAMONGO-2392 - Fix handling in ReactiveGridFsTemplate of GridFS files with custom id type.
Original pull request: #796.
This commit is contained in:
committed by
Mark Paluch
parent
77aafc597b
commit
ebfcfb3834
@@ -222,7 +222,7 @@ public class ReactiveGridFsTemplate extends GridFsOperationsSupport implements R
|
||||
|
||||
return Mono.fromSupplier(() -> {
|
||||
|
||||
GridFSDownloadStream stream = getGridFs().openDownloadStream(file.getObjectId());
|
||||
GridFSDownloadStream stream = getGridFs().openDownloadStream(file.getId());
|
||||
|
||||
return new ReactiveGridFsResource(file, BinaryStreamAdapters.toPublisher(stream, dataBufferFactory));
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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">
|
||||
|
||||
<bean id="mongoClient" class="org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean">
|
||||
<bean id="reactiveMongoClient" class="org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean">
|
||||
<property name="host" value="127.0.0.1"/>
|
||||
<property name="port" value="27017"/>
|
||||
</bean>
|
||||
|
||||
<bean id="reactiveMongoDbFactory" class="org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory">
|
||||
<constructor-arg name="mongoClient" ref="mongoClient"/>
|
||||
<constructor-arg name="mongoClient" ref="reactiveMongoClient" type="com.mongodb.reactivestreams.client.MongoClient"/>
|
||||
<constructor-arg name="databaseName" value="reactive_gridfs"/>
|
||||
</bean>
|
||||
|
||||
@@ -27,4 +27,13 @@
|
||||
<constructor-arg ref="converter"/>
|
||||
</bean>
|
||||
|
||||
<bean id="mongoClient" class="org.springframework.data.mongodb.core.MongoClientFactoryBean">
|
||||
<property name="host" value="127.0.0.1"/>
|
||||
<property name="port" value="27017"/>
|
||||
</bean>
|
||||
|
||||
<bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
|
||||
<constructor-arg name="mongoClient" ref="mongoClient"/>
|
||||
<constructor-arg name="databaseName" value="reactive_gridfs"/>
|
||||
</bean>
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user