diff --git a/mongodb/gridfs/pom.xml b/mongodb/gridfs/pom.xml
index 5aaf8b42..8945d97a 100644
--- a/mongodb/gridfs/pom.xml
+++ b/mongodb/gridfs/pom.xml
@@ -4,7 +4,7 @@
spring-data-mongodb-gridfs
- Spring Data MongoDB - GridFs
+ Spring Data MongoDB - GridFs Example
org.springframework.data.examples
@@ -12,5 +12,4 @@
2.0.0.BUILD-SNAPSHOT
-
diff --git a/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/Customer.java b/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/Customer.java
index f702950e..a4f3278d 100644
--- a/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/Customer.java
+++ b/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/Customer.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 the original author or authors.
+ * Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,44 +13,27 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package example.springdata.mongodb.gridfs;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
import org.springframework.data.annotation.Id;
+/**
+ * @author Hartmut Lang
+ */
+@Data
+@NoArgsConstructor
public class Customer {
- @Id
- public String id;
+ @Id private String id;
- public String firstName;
- public String lastName;
+ private String firstName;
+ private String lastName;
- public Customer() {}
-
- public Customer(String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
-
-
- public String getId() {
- return id;
- }
-
- public String getFirstName() {
- return firstName;
- }
-
- public String getLastName() {
- return lastName;
- }
-
- @Override
- public String toString() {
- return String.format(
- "Customer[id=%s, firstName='%s', lastName='%s']",
- id, firstName, lastName);
- }
-
-}
\ No newline at end of file
+ public Customer(String firstName, String lastName) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ }
+}
diff --git a/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/GridFsApplication.java b/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/GridFsApplication.java
index cbf428ae..77c6062a 100644
--- a/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/GridFsApplication.java
+++ b/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/GridFsApplication.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 the original author or authors.
+ * Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,25 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package example.springdata.mongodb.gridfs;
-import com.mongodb.client.gridfs.model.GridFSFile;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.CommandLineRunner;
-import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.core.io.ClassPathResource;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
-import org.springframework.data.mongodb.gridfs.GridFsResource;
-import org.springframework.util.StreamUtils;
-
-import java.io.BufferedInputStream;
-import java.io.InputStream;
-
-import static org.springframework.data.mongodb.core.query.Query.query;
-import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereFilename;
-import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereMetaData;
/**
* Spring Boot sample application to show the usage of {@link GridFsOperations} with Spring Data MongoDB.
@@ -39,39 +24,4 @@ import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereMetaDa
* @author Hartmut Lang
*/
@SpringBootApplication
-public class GridFsApplication implements CommandLineRunner {
-
- @Autowired
- private GridFsOperations gridFsOperations;
-
- public static void main(String[] args) {
- SpringApplication.run(GridFsApplication.class, args);
- }
-
- @Override
- public void run(String... strings) throws Exception {
-
- // store file
- try (InputStream is = new BufferedInputStream(new ClassPathResource("./myFile1.txt").getInputStream())) {
- gridFsOperations.store(is, "myFile1.txt");
- }
-
- // get file or resource by filename
- GridFSFile gridFsFile1 = gridFsOperations.findOne(query(whereFilename().is("myFile1.txt")));
- System.out.println("Filename: " + gridFsFile1.getFilename() +", MD5: " + gridFsFile1.getMD5());
- GridFsResource gridRes1 = gridFsOperations.getResource("myFile1.txt");
- StreamUtils.copy(gridRes1.getInputStream(), System.out);
-
- // store file with metaData
- try (InputStream is = new BufferedInputStream(new ClassPathResource("./myCustomerFile.txt").getInputStream())) {
- Customer customerMetaData = new Customer("Hardy", "Lang");
- gridFsOperations.store(is, "myCustomerFile.txt", customerMetaData);
- }
-
- // search by metaData
- GridFSFile gridFsFile2 = gridFsOperations.findOne(query(whereMetaData("firstName").is("Hardy")));
- System.out.println("Filename: " + gridFsFile2.getFilename() +", MD5: " + gridFsFile2.getMD5());
- GridFsResource gridRes2 = gridFsOperations.getResource(gridFsFile2.getFilename());
- StreamUtils.copy(gridRes2.getInputStream(), System.out);
- }
-}
+public class GridFsApplication {}
diff --git a/mongodb/gridfs/src/main/resources/application.properties b/mongodb/gridfs/src/main/resources/application.properties
deleted file mode 100644
index 0d039a4a..00000000
--- a/mongodb/gridfs/src/main/resources/application.properties
+++ /dev/null
@@ -1 +0,0 @@
-#spring.data.mongodb.grid-fs-database=dbfs
diff --git a/mongodb/gridfs/src/main/resources/myCustomerFile.txt b/mongodb/gridfs/src/main/resources/example-file.txt
similarity index 100%
rename from mongodb/gridfs/src/main/resources/myCustomerFile.txt
rename to mongodb/gridfs/src/main/resources/example-file.txt
diff --git a/mongodb/gridfs/src/main/resources/myFile1.txt b/mongodb/gridfs/src/main/resources/myFile1.txt
deleted file mode 100644
index 0e380079..00000000
--- a/mongodb/gridfs/src/main/resources/myFile1.txt
+++ /dev/null
@@ -1 +0,0 @@
-Just a simple file to add to GridFS
diff --git a/mongodb/gridfs/src/test/java/example/springdata/mongodb/gridfs/GridFsTests.java b/mongodb/gridfs/src/test/java/example/springdata/mongodb/gridfs/GridFsTests.java
new file mode 100644
index 00000000..cae4ad37
--- /dev/null
+++ b/mongodb/gridfs/src/test/java/example/springdata/mongodb/gridfs/GridFsTests.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package example.springdata.mongodb.gridfs;
+
+import static org.assertj.core.api.Assertions.*;
+import static org.springframework.data.mongodb.core.query.Query.*;
+import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.data.mongodb.core.query.Query;
+import org.springframework.data.mongodb.gridfs.GridFsOperations;
+import org.springframework.data.mongodb.gridfs.GridFsResource;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.util.StreamUtils;
+
+import com.mongodb.client.gridfs.model.GridFSFile;
+
+/**
+ * Tests to show the usage of {@link GridFsOperations} with Spring Data MongoDB.
+ *
+ * @author Hartmut Lang
+ * @author Mark Paluch
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class GridFsTests {
+
+ @Autowired GridFsOperations gridFsOperations;
+
+ @Before
+ public void before() {
+ gridFsOperations.delete(new Query());
+ }
+
+ @Test
+ public void shouldStoreSimpleFile() throws IOException {
+
+ try (InputStream is = new BufferedInputStream(new ClassPathResource("./example-file.txt").getInputStream())) {
+ // store file
+ gridFsOperations.store(is, "example-file.txt");
+
+ }
+
+ // get file or resource by filename
+ GridFSFile file = gridFsOperations.findOne(query(whereFilename().is("example-file.txt")));
+
+ assertThat(file.getFilename()).isEqualTo("example-file.txt");
+ }
+
+ @Test
+ public void shouldStoreFileWithMetadata() throws IOException {
+
+ try (InputStream is = new BufferedInputStream(new ClassPathResource("./example-file.txt").getInputStream())) {
+
+ // store file with metaData
+ Customer customerMetaData = new Customer("Hardy", "Lang");
+ gridFsOperations.store(is, "example-file.txt", customerMetaData);
+ }
+
+ // search by metaData
+ GridFSFile file = gridFsOperations.findOne(query(whereMetaData("firstName").is("Hardy")));
+ assertThat(file.getFilename()).isEqualTo("example-file.txt");
+ }
+
+ @Test
+ public void shouldStoreAndReadFile() throws IOException {
+
+ byte[] bytes;
+ try (InputStream is = new BufferedInputStream(new ClassPathResource("./example-file.txt").getInputStream())) {
+ bytes = StreamUtils.copyToByteArray(is);
+ }
+
+ // store file
+ gridFsOperations.store(new ByteArrayInputStream(bytes), "example-file.txt");
+
+ GridFsResource resource = gridFsOperations.getResource("example-file.txt");
+
+ byte[] loaded;
+ try (InputStream is = resource.getInputStream()) {
+ loaded = StreamUtils.copyToByteArray(is);
+ }
+
+ assertThat(bytes).isEqualTo(loaded);
+ }
+}
diff --git a/mongodb/pom.xml b/mongodb/pom.xml
index 1991fec3..f1d4c974 100644
--- a/mongodb/pom.xml
+++ b/mongodb/pom.xml
@@ -14,32 +14,31 @@
Spring Data MongoDB - Examples
Sample projects for Spring Data MongoDB
http://projects.spring.io/spring-data-mongodb
- 2011-2015
+ 2011
aggregation
example
- text-search
- java8
- security
+ fluent-api
geo-json
+ gridfs
+ java8
query-by-example
reactive
- fluent-api
- gridfs
+ security
+ text-search
- org.springframework.boot
- spring-boot-starter-data-mongodb
-
+ org.springframework.boot
+ spring-boot-starter-data-mongodb
+
com.querydsl
querydsl-mongodb
- ${querydsl.version}
org.mongodb