Skip to content

Commit 6970f93

Browse files
Nick Stolwijkmp911de
Nick Stolwijk
authored andcommitted
DATAMONGO-2392 - Fix handling in ReactiveGridFsTemplate of GridFS files with custom id type.
Original pull request: #796.
1 parent 6b5168e commit 6970f93

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public Mono<ReactiveGridFsResource> getResource(GridFSFile file) {
222222

223223
return Mono.fromSupplier(() -> {
224224

225-
GridFSDownloadStream stream = getGridFs().openDownloadStream(file.getObjectId());
225+
GridFSDownloadStream stream = getGridFs().openDownloadStream(file.getId());
226226

227227
return new ReactiveGridFsResource(file, BinaryStreamAdapters.toPublisher(stream, dataBufferFactory));
228228
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.springframework.data.mongodb.gridfs;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.springframework.data.mongodb.core.query.Criteria.where;
5+
import static org.springframework.data.mongodb.core.query.Query.query;
6+
7+
import java.io.IOException;
8+
9+
import com.mongodb.DB;
10+
import com.mongodb.MongoClient;
11+
import com.mongodb.gridfs.GridFS;
12+
import com.mongodb.gridfs.GridFSInputFile;
13+
import org.junit.Before;
14+
import org.junit.Test;
15+
import org.junit.runner.RunWith;
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.core.io.ClassPathResource;
18+
import org.springframework.core.io.Resource;
19+
import org.springframework.core.io.buffer.DataBufferUtils;
20+
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
21+
import org.springframework.data.mongodb.core.query.Query;
22+
import org.springframework.test.context.ContextConfiguration;
23+
import org.springframework.test.context.junit4.SpringRunner;
24+
import org.springframework.util.StreamUtils;
25+
import reactor.test.StepVerifier;
26+
27+
/**
28+
* Integration tests for {@link ReactiveGridFsTemplate} in combination with the deprecated GridFs.
29+
*
30+
* @author Nick Stolwijk
31+
*/
32+
@RunWith(SpringRunner.class)
33+
@ContextConfiguration({"classpath:gridfs/reactive-gridfs.xml"})
34+
public class ReactiveGridFsTemplateWithOldGridFsTests {
35+
36+
Resource resource = new ClassPathResource("gridfs/gridfs.xml");
37+
38+
@Autowired ReactiveGridFsOperations operations;
39+
40+
@Autowired SimpleMongoDbFactory mongoClient;
41+
@Before
42+
public void setUp() {
43+
44+
operations.delete(new Query()) //
45+
.as(StepVerifier::create) //
46+
.verifyComplete();
47+
}
48+
49+
@Test // DATAMONGO-2392
50+
public void storeFileWithOldGridFsAndFindItWithReactiveGridFsOperations() throws IOException {
51+
byte[] content = StreamUtils.copyToByteArray(resource.getInputStream());
52+
String reference = "1af52e25-76b7-4e34-8618-9baa183c9112";
53+
54+
GridFS fs = new GridFS(mongoClient.getLegacyDb());
55+
GridFSInputFile in = fs.createFile(resource.getInputStream(), "gridfs.xml");
56+
57+
in.put("_id", reference);
58+
in.put("contentType", "application/octet-stream");
59+
in.save();
60+
61+
operations.findOne(query(where("_id").is(reference))).flatMap(operations::getResource)
62+
.flatMapMany(ReactiveGridFsResource::getDownloadStream) //
63+
.transform(DataBufferUtils::join) //
64+
.as(StepVerifier::create) //
65+
.consumeNextWith(dataBuffer -> {
66+
67+
byte[] actual = new byte[dataBuffer.readableByteCount()];
68+
dataBuffer.read(actual);
69+
70+
assertThat(actual).isEqualTo(content);
71+
}) //
72+
.verifyComplete();
73+
}
74+
}

spring-data-mongodb/src/test/resources/gridfs/reactive-gridfs.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
44
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">
55

6-
<bean id="mongoClient" class="org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean">
6+
<bean id="reactiveMongoClient" class="org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean">
77
<property name="host" value="127.0.0.1"/>
88
<property name="port" value="27017"/>
99
</bean>
1010

1111
<bean id="reactiveMongoDbFactory" class="org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory">
12-
<constructor-arg name="mongoClient" ref="mongoClient"/>
12+
<constructor-arg name="mongoClient" ref="reactiveMongoClient" type="com.mongodb.reactivestreams.client.MongoClient"/>
1313
<constructor-arg name="databaseName" value="reactive_gridfs"/>
1414
</bean>
1515

@@ -27,4 +27,13 @@
2727
<constructor-arg ref="converter"/>
2828
</bean>
2929

30+
<bean id="mongoClient" class="org.springframework.data.mongodb.core.MongoClientFactoryBean">
31+
<property name="host" value="127.0.0.1"/>
32+
<property name="port" value="27017"/>
33+
</bean>
34+
35+
<bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
36+
<constructor-arg name="mongoClient" ref="mongoClient"/>
37+
<constructor-arg name="databaseName" value="reactive_gridfs"/>
38+
</bean>
3039
</beans>

0 commit comments

Comments
 (0)