Added tests with GridFSTemplate
This commit is contained in:
parent
c9d5aff1ff
commit
f202857195
|
@ -10,6 +10,7 @@ import org.springframework.context.annotation.Bean;
|
|||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
|
||||
import org.springframework.data.mongodb.core.convert.CustomConversions;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
|
@ -52,4 +53,9 @@ public class MongoConfig extends AbstractMongoConfiguration {
|
|||
converters.add(new UserWriterConverter());
|
||||
return new CustomConversions(converters);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GridFsTemplate gridFsTemplate() throws Exception {
|
||||
return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,186 @@
|
|||
package org.baeldung.gridfs;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.config.MongoConfig;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsResource;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.gridfs.GridFSDBFile;
|
||||
|
||||
@ContextConfiguration(classes = MongoConfig.class)
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class GridFSTest {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private GridFsTemplate gridFsTemplate;
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
List<GridFSDBFile> fileList = gridFsTemplate.find(null);
|
||||
for(GridFSDBFile file: fileList) {
|
||||
gridFsTemplate.delete(new Query(Criteria.where("filename").is(file.getFilename())));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStoringFileWithMetadata_thenFileAndMetadataAreStored() {
|
||||
DBObject metaData = new BasicDBObject();
|
||||
metaData.put("key", "value");
|
||||
InputStream inputStream = null;
|
||||
String id = "";
|
||||
|
||||
try {
|
||||
inputStream = new FileInputStream("src/main/resources/test.png");
|
||||
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
|
||||
} catch (FileNotFoundException ex) {
|
||||
logger.error("", ex);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException ex) {
|
||||
logger.error("", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertNotNull(id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataIsFound() {
|
||||
DBObject metaData = new BasicDBObject();
|
||||
metaData.put("key", "value");
|
||||
InputStream inputStream = null;
|
||||
String id = "";
|
||||
|
||||
try {
|
||||
inputStream = new FileInputStream("src/main/resources/test.png");
|
||||
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
|
||||
} catch (FileNotFoundException ex) {
|
||||
logger.error("", ex);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException ex) {
|
||||
logger.error("", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GridFSDBFile gridFSDBFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
|
||||
|
||||
assertNotNull(gridFSDBFile);
|
||||
assertThat(gridFSDBFile.getFilename(), is("test.png"));
|
||||
assertThat(gridFSDBFile.getMetaData().get("key"), is("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMetadataAndFilesExist_whenFindingAllFiles_thenFilesWithMetadataAreFound() {
|
||||
DBObject metaData = new BasicDBObject();
|
||||
metaData.put("key", "value");
|
||||
InputStream inputStream = null;
|
||||
|
||||
try {
|
||||
inputStream = new FileInputStream("src/main/resources/test.png");
|
||||
gridFsTemplate.store(inputStream, "test.png", "image/png", metaData);
|
||||
gridFsTemplate.store(inputStream, "test.png", "image/png", metaData);
|
||||
} catch (FileNotFoundException ex) {
|
||||
logger.error("", ex);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException ex) {
|
||||
logger.error("", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<GridFSDBFile> gridFSDBFiles = gridFsTemplate.find(null);
|
||||
|
||||
assertNotNull(gridFSDBFiles);
|
||||
assertThat(gridFSDBFiles.size(), is(2));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenFileWithMetadataExist_whenDeletingFileById_thenFileWithMetadataIsDeleted() {
|
||||
DBObject metaData = new BasicDBObject();
|
||||
metaData.put("key", "value");
|
||||
InputStream inputStream = null;
|
||||
String id = "";
|
||||
|
||||
try {
|
||||
inputStream = new FileInputStream("src/main/resources/test.png");
|
||||
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
|
||||
} catch (FileNotFoundException ex) {
|
||||
logger.error("", ex);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException ex) {
|
||||
logger.error("", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gridFsTemplate.delete(new Query(Criteria.where("_id").is(id)));
|
||||
|
||||
assertThat(gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id))), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFileWithMetadataExist_whenGettingFileByResource_thenFileWithMetadataIsGotten() {
|
||||
DBObject metaData = new BasicDBObject();
|
||||
metaData.put("key", "value");
|
||||
InputStream inputStream = null;
|
||||
String id = "";
|
||||
|
||||
try {
|
||||
inputStream = new FileInputStream("src/main/resources/test.png");
|
||||
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
|
||||
} catch (FileNotFoundException ex) {
|
||||
logger.error("", ex);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException ex) {
|
||||
logger.error("", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GridFsResource[] gridFsResource = gridFsTemplate.getResources("test*");
|
||||
|
||||
assertNotNull(gridFsResource);
|
||||
assertThat(gridFsResource[0].getFilename(), is("test.png"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue