Skip to content

Commit de4fae3

Browse files
committed
DATAMONGO-2392 - Polishing.
Add author tags. Move integration tests to existing test class. Apply more appropriate in existing tests assertions. Use diamond syntax. Original pull request: #796.
1 parent 2f1aff3 commit de4fae3

File tree

4 files changed

+43
-90
lines changed

4 files changed

+43
-90
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* {@link DefaultDataBufferFactory} to create {@link DataBuffer buffers}.
5050
*
5151
* @author Mark Paluch
52+
* @author Nick Stolwijk
5253
* @since 2.2
5354
*/
5455
public class ReactiveGridFsTemplate extends GridFsOperationsSupport implements ReactiveGridFsOperations {

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ public void writesMetadataCorrectly() throws IOException {
112112
Document metadata = new Document("key", "value");
113113
ObjectId reference = operations.store(resource.getInputStream(), "foo.xml", metadata);
114114

115-
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
115+
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<>();
116116
GridFSFindIterable result = operations.find(query(whereMetaData("key").is("value")));
117117
result.into(files);
118118

119-
assertThat(files.size()).isEqualTo(1);
119+
assertThat(files).hasSize(1);
120120
assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference);
121121
}
122122

@@ -128,11 +128,11 @@ public void marshalsComplexMetadata() throws IOException {
128128

129129
ObjectId reference = operations.store(resource.getInputStream(), "foo.xml", metadata);
130130

131-
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
131+
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<>();
132132
GridFSFindIterable result = operations.find(query(whereFilename().is("foo.xml")));
133133
result.into(files);
134134

135-
assertThat(files.size()).isEqualTo(1);
135+
assertThat(files).hasSize(1);
136136
assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference);
137137
}
138138

@@ -143,7 +143,7 @@ public void findsFilesByResourcePattern() throws IOException {
143143

144144
GridFsResource[] resources = operations.getResources("*.xml");
145145

146-
assertThat(resources.length).isEqualTo(1);
146+
assertThat(resources).hasSize(1);
147147
assertThat(((BsonObjectId) resources[0].getId()).getValue()).isEqualTo(reference);
148148
assertThat(resources[0].contentLength()).isEqualTo(resource.contentLength());
149149
}
@@ -154,7 +154,7 @@ public void findsFilesByResourceLocation() throws IOException {
154154
ObjectId reference = operations.store(resource.getInputStream(), "foo.xml");
155155

156156
GridFsResource[] resources = operations.getResources("foo.xml");
157-
assertThat(resources.length).isEqualTo(1);
157+
assertThat(resources).hasSize(1);
158158
assertThat(((BsonObjectId) resources[0].getId()).getValue()).isEqualTo(reference);
159159
assertThat(resources[0].contentLength()).isEqualTo(resource.contentLength());
160160
}
@@ -164,11 +164,11 @@ public void storesContentType() throws IOException {
164164

165165
ObjectId reference = operations.store(resource.getInputStream(), "foo2.xml", "application/xml");
166166

167-
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
167+
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<>();
168168
GridFSFindIterable result = operations.find(query(whereContentType().is("application/xml")));
169169
result.into(files);
170170

171-
assertThat(files.size()).isEqualTo(1);
171+
assertThat(files).hasSize(1);
172172
assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference);
173173
}
174174

@@ -181,7 +181,7 @@ public void considersSortWhenQueryingFiles() throws IOException {
181181

182182
Query query = new Query().with(Sort.by(Direction.ASC, "filename"));
183183

184-
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
184+
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<>();
185185
GridFSFindIterable result = operations.find(query);
186186
result.into(files);
187187

@@ -217,7 +217,7 @@ public void storesAndFindsSimpleDocumentWithMetadataDocument() throws IOExceptio
217217
Document metadata = new Document("key", "value");
218218
ObjectId reference = operations.store(resource.getInputStream(), "foobar", metadata);
219219

220-
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
220+
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<>();
221221
GridFSFindIterable result = operations.find(query(whereMetaData("key").is("value")));
222222
result.into(files);
223223

@@ -231,7 +231,7 @@ public void storesAndFindsSimpleDocumentWithMetadataObject() throws IOException
231231
metadata.version = "1.0";
232232
ObjectId reference = operations.store(resource.getInputStream(), "foobar", metadata);
233233

234-
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
234+
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<>();
235235
GridFSFindIterable result = operations.find(query(whereMetaData("version").is("1.0")));
236236
result.into(files);
237237

@@ -252,7 +252,7 @@ public void failsOnNonExistingContentTypeRetrieval() throws IOException {
252252
operations.store(resource.getInputStream(), "no-content-type", (String) null);
253253
GridFsResource result = operations.getResource("no-content-type");
254254

255-
assertThatThrownBy(() -> result.getContentType()).isInstanceOf(MongoGridFSException.class);
255+
assertThatThrownBy(result::getContentType).isInstanceOf(MongoGridFSException.class);
256256
}
257257

258258
@Test // DATAMONGO-1813

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import reactor.test.StepVerifier;
2525

2626
import java.io.IOException;
27+
import java.util.UUID;
2728

2829
import org.bson.BsonObjectId;
2930
import org.bson.Document;
@@ -39,11 +40,14 @@
3940
import org.springframework.core.io.buffer.DefaultDataBuffer;
4041
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
4142
import org.springframework.dao.IncorrectResultSizeDataAccessException;
43+
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
4244
import org.springframework.data.mongodb.core.query.Query;
4345
import org.springframework.test.context.ContextConfiguration;
4446
import org.springframework.test.context.junit4.SpringRunner;
4547
import org.springframework.util.StreamUtils;
4648

49+
import com.mongodb.gridfs.GridFS;
50+
import com.mongodb.gridfs.GridFSInputFile;
4751
import com.mongodb.reactivestreams.client.gridfs.AsyncInputStream;
4852
import com.mongodb.reactivestreams.client.gridfs.helpers.AsyncStreamHelper;
4953

@@ -52,6 +56,7 @@
5256
*
5357
* @author Mark Paluch
5458
* @author Christoph Strobl
59+
* @author Nick Stolwijk
5560
*/
5661
@RunWith(SpringRunner.class)
5762
@ContextConfiguration("classpath:gridfs/reactive-gridfs.xml")
@@ -60,6 +65,7 @@ public class ReactiveGridFsTemplateTests {
6065
Resource resource = new ClassPathResource("gridfs/gridfs.xml");
6166

6267
@Autowired ReactiveGridFsOperations operations;
68+
@Autowired SimpleMongoDbFactory mongoClient;
6369

6470
@Before
6571
public void setUp() {
@@ -86,6 +92,25 @@ public void storesAndFindsSimpleDocument() {
8692
.verifyComplete();
8793
}
8894

95+
@Test // DATAMONGO-2392
96+
public void storesAndFindsByUUID() throws IOException {
97+
98+
UUID uuid = UUID.randomUUID();
99+
100+
GridFS fs = new GridFS(mongoClient.getLegacyDb());
101+
GridFSInputFile in = fs.createFile(resource.getInputStream(), "gridfs.xml");
102+
103+
in.put("_id", uuid);
104+
in.put("contentType", "application/octet-stream");
105+
in.save();
106+
107+
operations.findOne(query(where("_id").is(uuid))).flatMap(operations::getResource)
108+
.flatMapMany(ReactiveGridFsResource::getDownloadStream) //
109+
.transform(DataBufferUtils::join) //
110+
.doOnNext(DataBufferUtils::release).as(StepVerifier::create) //
111+
.expectNextCount(1).verifyComplete();
112+
}
113+
89114
@Test // DATAMONGO-1855
90115
public void writesMetadataCorrectly() throws IOException {
91116

@@ -148,7 +173,8 @@ public void getResourceShouldRetrieveContentByIdentity() throws IOException {
148173
public void shouldEmitFirstEntryWhenFindFirstRetrievesMoreThanOneResult() throws IOException {
149174

150175
AsyncInputStream upload1 = AsyncStreamHelper.toAsyncInputStream(resource.getInputStream());
151-
AsyncInputStream upload2 = AsyncStreamHelper.toAsyncInputStream(new ClassPathResource("gridfs/another-resource.xml").getInputStream());
176+
AsyncInputStream upload2 = AsyncStreamHelper
177+
.toAsyncInputStream(new ClassPathResource("gridfs/another-resource.xml").getInputStream());
152178

153179
operations.store(upload1, "foo.xml", null, null).block();
154180
operations.store(upload2, "foo2.xml", null, null).block();
@@ -159,8 +185,7 @@ public void shouldEmitFirstEntryWhenFindFirstRetrievesMoreThanOneResult() throws
159185
.assertNext(actual -> {
160186

161187
assertThat(actual.getGridFSFile()).isNotNull();
162-
})
163-
.verifyComplete();
188+
}).verifyComplete();
164189
}
165190

166191
@Test // DATAMONGO-2240
@@ -179,7 +204,8 @@ public void shouldReturnNoGridFsFileWhenAbsent() {
179204
public void shouldEmitErrorWhenFindOneRetrievesMoreThanOneResult() throws IOException {
180205

181206
AsyncInputStream upload1 = AsyncStreamHelper.toAsyncInputStream(resource.getInputStream());
182-
AsyncInputStream upload2 = AsyncStreamHelper.toAsyncInputStream(new ClassPathResource("gridfs/another-resource.xml").getInputStream());
207+
AsyncInputStream upload2 = AsyncStreamHelper
208+
.toAsyncInputStream(new ClassPathResource("gridfs/another-resource.xml").getInputStream());
183209

184210
operations.store(upload1, "foo.xml", null, null).block();
185211
operations.store(upload2, "foo2.xml", null, null).block();

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

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)