Skip to content

Commit 888054b

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-2240 - Expose GridFSFile through GridFsResource and ReactiveGridFsResource.
Original Pull Request: #741
1 parent 29bf74c commit 888054b

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class GridFsResource extends InputStreamResource {
4848

4949
/**
5050
* Creates a new, absent {@link GridFsResource}.
51-
*
51+
*
5252
* @param filename filename of the absent resource.
5353
* @since 2.1
5454
*/
@@ -170,6 +170,15 @@ public Object getId() {
170170
return file.getId();
171171
}
172172

173+
/**
174+
* @return the underlying {@link GridFSFile}. Can be {@literal null} if absent.
175+
* @since 2.2
176+
*/
177+
@Nullable
178+
public GridFSFile getGridFSFile() {
179+
return file;
180+
}
181+
173182
/**
174183
* Returns the {@link Resource}'s content type.
175184
*

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.InputStream;
2424

2525
import org.reactivestreams.Publisher;
26+
2627
import org.springframework.core.io.AbstractResource;
2728
import org.springframework.core.io.Resource;
2829
import org.springframework.core.io.buffer.DataBuffer;
@@ -158,6 +159,15 @@ public Object getId() {
158159
return file.getId();
159160
}
160161

162+
/**
163+
* @return the underlying {@link GridFSFile}. Can be {@literal null} if absent.
164+
* @since 2.2
165+
*/
166+
@Nullable
167+
public GridFSFile getGridFSFile() {
168+
return file;
169+
}
170+
161171
/**
162172
* Retrieve the download stream.
163173
*

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsResourceUnitTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public void shouldReadContentTypeCorrectly() {
4545
assertThat(resource.getContentType()).isEqualTo("text/plain");
4646
}
4747

48+
@Test // DATAMONGO-2240
49+
public void shouldReturnGridFSFile() {
50+
51+
GridFSFile file = new GridFSFile(new BsonObjectId(), "foo", 0, 0, new Date(), "foo", new Document());
52+
GridFsResource resource = new GridFsResource(file);
53+
54+
assertThat(resource.getGridFSFile()).isSameAs(file);
55+
}
56+
4857
@Test // DATAMONGO-1850
4958
public void shouldThrowExceptionOnEmptyContentType() {
5059

@@ -89,6 +98,5 @@ public void shouldReturnFilenameForAbsentResource() {
8998
assertThat(absent.exists()).isFalse();
9099
assertThat(absent.getDescription()).contains("GridFs resource [foo]");
91100
assertThat(absent.getFilename()).isEqualTo("foo");
92-
93101
}
94102
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateTests.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
package org.springframework.data.mongodb.gridfs;
1717

1818
import static org.assertj.core.api.Assertions.*;
19-
import static org.springframework.data.mongodb.core.query.Criteria.where;
19+
import static org.springframework.data.mongodb.core.query.Criteria.*;
2020
import static org.springframework.data.mongodb.core.query.Query.*;
2121
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;
2222

23-
import org.springframework.dao.IncorrectResultSizeDataAccessException;
2423
import reactor.core.publisher.Flux;
2524
import reactor.test.StepVerifier;
2625

@@ -32,12 +31,14 @@
3231
import org.junit.Before;
3332
import org.junit.Test;
3433
import org.junit.runner.RunWith;
34+
3535
import org.springframework.beans.factory.annotation.Autowired;
3636
import org.springframework.core.io.ClassPathResource;
3737
import org.springframework.core.io.Resource;
3838
import org.springframework.core.io.buffer.DataBufferUtils;
3939
import org.springframework.core.io.buffer.DefaultDataBuffer;
4040
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
41+
import org.springframework.dao.IncorrectResultSizeDataAccessException;
4142
import org.springframework.data.mongodb.core.query.Query;
4243
import org.springframework.test.context.ContextConfiguration;
4344
import org.springframework.test.context.junit4.SpringRunner;
@@ -143,7 +144,7 @@ public void getResourceShouldRetrieveContentByIdentity() throws IOException {
143144
.verifyComplete();
144145
}
145146

146-
@Test // DATAMONGO-1855
147+
@Test // DATAMONGO-1855, DATAMONGO-2240
147148
public void shouldEmitFirstEntryWhenFindFirstRetrievesMoreThanOneResult() throws IOException {
148149

149150
AsyncInputStream upload1 = AsyncStreamHelper.toAsyncInputStream(resource.getInputStream());
@@ -155,10 +156,25 @@ public void shouldEmitFirstEntryWhenFindFirstRetrievesMoreThanOneResult() throws
155156
operations.findFirst(query(where("filename").regex("foo*"))) //
156157
.flatMap(operations::getResource) //
157158
.as(StepVerifier::create) //
158-
.expectNextCount(1) //
159+
.assertNext(actual -> {
160+
161+
assertThat(actual.getGridFSFile()).isNotNull();
162+
})
159163
.verifyComplete();
160164
}
161165

166+
@Test // DATAMONGO-2240
167+
public void shouldReturnNoGridFsFileWhenAbsent() {
168+
169+
operations.getResource("absent") //
170+
.as(StepVerifier::create) //
171+
.assertNext(actual -> {
172+
173+
assertThat(actual.exists()).isFalse();
174+
assertThat(actual.getGridFSFile()).isNull();
175+
}).verifyComplete();
176+
}
177+
162178
@Test // DATAMONGO-1855
163179
public void shouldEmitErrorWhenFindOneRetrievesMoreThanOneResult() throws IOException {
164180

0 commit comments

Comments
 (0)