Skip to content

Commit

Permalink
Added test for find with Query
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-semenyuk committed Oct 14, 2015
1 parent 919123b commit 78f441d
Showing 1 changed file with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.mongodb.DBObject;
import com.mongodb.gridfs.GridFSDBFile;

@ContextConfiguration(classes = MongoConfig.class)
@ContextConfiguration("file:src/main/resources/mongoConfig.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class GridFSIntegrationTest {

Expand All @@ -51,7 +51,7 @@ public void tearDown() {
@Test
public void whenStoringFileWithMetadata_thenFileAndMetadataAreStored() {
DBObject metaData = new BasicDBObject();
metaData.put("key", "value");
metaData.put("user", "alex");
InputStream inputStream = null;
String id = "";
try {
Expand All @@ -75,7 +75,7 @@ public void whenStoringFileWithMetadata_thenFileAndMetadataAreStored() {
@Test
public void givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataIsFound() {
DBObject metaData = new BasicDBObject();
metaData.put("key", "value");
metaData.put("user", "alex");
InputStream inputStream = null;
String id = "";
try {
Expand Down Expand Up @@ -108,19 +108,21 @@ public void givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataI
assertNotNull(gridFSDBFile.getChunkSize());
assertThat(gridFSDBFile.getContentType(), is("image/png"));
assertThat(gridFSDBFile.getFilename(), is("test.png"));
assertThat(gridFSDBFile.getMetaData().get("key"), is("value"));
assertThat(gridFSDBFile.getMetaData().get("user"), is("alex"));
}

@Test
public void givenMetadataAndFilesExist_whenFindingAllFiles_thenFilesWithMetadataAreFound() {
DBObject metaData = new BasicDBObject();
metaData.put("key", "value");
DBObject metaDataUser1 = new BasicDBObject();
metaDataUser1.put("user", "alex");
DBObject metaDataUser2 = new BasicDBObject();
metaDataUser2.put("user", "david");
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);
gridFsTemplate.store(inputStream, "test.png", "image/png", metaDataUser1);
gridFsTemplate.store(inputStream, "test.png", "image/png", metaDataUser2);
} catch (FileNotFoundException ex) {
logger.error("File not found", ex);
} finally {
Expand All @@ -138,11 +140,41 @@ public void givenMetadataAndFilesExist_whenFindingAllFiles_thenFilesWithMetadata
assertNotNull(gridFSDBFiles);
assertThat(gridFSDBFiles.size(), is(2));
}

@Test
public void givenMetadataAndFilesExist_whenFindingAllFilesOnQuery_thenFilesWithMetadataAreFoundOnQuery() {
DBObject metaDataUser1 = new BasicDBObject();
metaDataUser1.put("user", "alex");
DBObject metaDataUser2 = new BasicDBObject();
metaDataUser2.put("user", "david");
InputStream inputStream = null;

try {
inputStream = new FileInputStream("src/main/resources/test.png");
gridFsTemplate.store(inputStream, "test.png", "image/png", metaDataUser1);
gridFsTemplate.store(inputStream, "test.png", "image/png", metaDataUser2);
} catch (FileNotFoundException ex) {
logger.error("File not found", ex);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) {
logger.error("Failed to close", ex);
}
}
}

List<GridFSDBFile> gridFSDBFiles = gridFsTemplate.find(new Query(Criteria.where("metadata.user").is("alex")));

assertNotNull(gridFSDBFiles);
assertThat(gridFSDBFiles.size(), is(1));
}

@Test
public void givenFileWithMetadataExist_whenDeletingFileById_thenFileWithMetadataIsDeleted() {
DBObject metaData = new BasicDBObject();
metaData.put("key", "value");
metaData.put("user", "alex");
InputStream inputStream = null;
String id = "";
try {
Expand All @@ -168,7 +200,7 @@ public void givenFileWithMetadataExist_whenDeletingFileById_thenFileWithMetadata
@Test
public void givenFileWithMetadataExist_whenGettingFileByResource_thenFileWithMetadataIsGotten() {
DBObject metaData = new BasicDBObject();
metaData.put("key", "value");
metaData.put("user", "alex");
InputStream inputStream = null;
String id = "";
try {
Expand Down

0 comments on commit 78f441d

Please sign in to comment.