#218 - Polishing.
Simplify Customer type using Lombok. Move test code from GridFsApplication to GridFsTests. Split tests into methods. Remove superfluous files. Original pull request: #229.
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
<artifactId>spring-data-mongodb-gridfs</artifactId>
|
||||
|
||||
<name>Spring Data MongoDB - GridFs</name>
|
||||
<name>Spring Data MongoDB - GridFs Example</name>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
@@ -12,5 +12,4 @@
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
|
||||
</project>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
public Customer(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
#spring.data.mongodb.grid-fs-database=dbfs
|
||||
@@ -1 +0,0 @@
|
||||
Just a simple file to add to GridFS
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -14,32 +14,31 @@
|
||||
<name>Spring Data MongoDB - Examples</name>
|
||||
<description>Sample projects for Spring Data MongoDB</description>
|
||||
<url>http://projects.spring.io/spring-data-mongodb</url>
|
||||
<inceptionYear>2011-2015</inceptionYear>
|
||||
<inceptionYear>2011</inceptionYear>
|
||||
|
||||
<modules>
|
||||
<module>aggregation</module>
|
||||
<module>example</module>
|
||||
<module>text-search</module>
|
||||
<module>java8</module>
|
||||
<module>security</module>
|
||||
<module>fluent-api</module>
|
||||
<module>geo-json</module>
|
||||
<module>gridfs</module>
|
||||
<module>java8</module>
|
||||
<module>query-by-example</module>
|
||||
<module>reactive</module>
|
||||
<module>fluent-api</module>
|
||||
<module>gridfs</module>
|
||||
<module>security</module>
|
||||
<module>text-search</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.querydsl</groupId>
|
||||
<artifactId>querydsl-mongodb</artifactId>
|
||||
<version>${querydsl.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.mongodb</groupId>
|
||||
|
||||
Reference in New Issue
Block a user