DATAMONGO-2240 - Expose GridFSFile through GridFsResource and ReactiveGridFsResource.

Original Pull Request: #741
This commit is contained in:
Mark Paluch
2019-04-16 13:16:07 +02:00
committed by Christoph Strobl
parent 29bf74c24c
commit 888054bb2a
4 changed files with 49 additions and 6 deletions

View File

@@ -48,7 +48,7 @@ public class GridFsResource extends InputStreamResource {
/**
* Creates a new, absent {@link GridFsResource}.
*
*
* @param filename filename of the absent resource.
* @since 2.1
*/
@@ -170,6 +170,15 @@ public class GridFsResource extends InputStreamResource {
return file.getId();
}
/**
* @return the underlying {@link GridFSFile}. Can be {@literal null} if absent.
* @since 2.2
*/
@Nullable
public GridFSFile getGridFSFile() {
return file;
}
/**
* Returns the {@link Resource}'s content type.
*

View File

@@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.InputStream;
import org.reactivestreams.Publisher;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer;
@@ -158,6 +159,15 @@ public class ReactiveGridFsResource extends AbstractResource {
return file.getId();
}
/**
* @return the underlying {@link GridFSFile}. Can be {@literal null} if absent.
* @since 2.2
*/
@Nullable
public GridFSFile getGridFSFile() {
return file;
}
/**
* Retrieve the download stream.
*

View File

@@ -45,6 +45,15 @@ public class GridFsResourceUnitTests {
assertThat(resource.getContentType()).isEqualTo("text/plain");
}
@Test // DATAMONGO-2240
public void shouldReturnGridFSFile() {
GridFSFile file = new GridFSFile(new BsonObjectId(), "foo", 0, 0, new Date(), "foo", new Document());
GridFsResource resource = new GridFsResource(file);
assertThat(resource.getGridFSFile()).isSameAs(file);
}
@Test // DATAMONGO-1850
public void shouldThrowExceptionOnEmptyContentType() {
@@ -89,6 +98,5 @@ public class GridFsResourceUnitTests {
assertThat(absent.exists()).isFalse();
assertThat(absent.getDescription()).contains("GridFs resource [foo]");
assertThat(absent.getFilename()).isEqualTo("foo");
}
}

View File

@@ -16,11 +16,10 @@
package org.springframework.data.mongodb.gridfs;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
@@ -32,12 +31,14 @@ import org.bson.types.ObjectId;
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.core.io.buffer.DefaultDataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@@ -143,7 +144,7 @@ public class ReactiveGridFsTemplateTests {
.verifyComplete();
}
@Test // DATAMONGO-1855
@Test // DATAMONGO-1855, DATAMONGO-2240
public void shouldEmitFirstEntryWhenFindFirstRetrievesMoreThanOneResult() throws IOException {
AsyncInputStream upload1 = AsyncStreamHelper.toAsyncInputStream(resource.getInputStream());
@@ -155,10 +156,25 @@ public class ReactiveGridFsTemplateTests {
operations.findFirst(query(where("filename").regex("foo*"))) //
.flatMap(operations::getResource) //
.as(StepVerifier::create) //
.expectNextCount(1) //
.assertNext(actual -> {
assertThat(actual.getGridFSFile()).isNotNull();
})
.verifyComplete();
}
@Test // DATAMONGO-2240
public void shouldReturnNoGridFsFileWhenAbsent() {
operations.getResource("absent") //
.as(StepVerifier::create) //
.assertNext(actual -> {
assertThat(actual.exists()).isFalse();
assertThat(actual.getGridFSFile()).isNull();
}).verifyComplete();
}
@Test // DATAMONGO-1855
public void shouldEmitErrorWhenFindOneRetrievesMoreThanOneResult() throws IOException {