Skip to content

DATAMONGO-2392 - Fix handling of old GridFS files with custom type fo… #796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public Mono<ReactiveGridFsResource> 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));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
13 changes: 11 additions & 2 deletions spring-data-mongodb/src/test/resources/gridfs/reactive-gridfs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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>