|
| 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 | +} |
0 commit comments