3131import com .mongodb .DBObject ;
3232import com .mongodb .gridfs .GridFSDBFile ;
3333
34- @ ContextConfiguration (classes = MongoConfig . class )
34+ @ ContextConfiguration ("file:src/main/resources/mongoConfig.xml" )
3535@ RunWith (SpringJUnit4ClassRunner .class )
3636public class GridFSIntegrationTest {
3737
@@ -51,7 +51,7 @@ public void tearDown() {
5151 @ Test
5252 public void whenStoringFileWithMetadata_thenFileAndMetadataAreStored () {
5353 DBObject metaData = new BasicDBObject ();
54- metaData .put ("key " , "value " );
54+ metaData .put ("user " , "alex " );
5555 InputStream inputStream = null ;
5656 String id = "" ;
5757 try {
@@ -75,7 +75,7 @@ public void whenStoringFileWithMetadata_thenFileAndMetadataAreStored() {
7575 @ Test
7676 public void givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataIsFound () {
7777 DBObject metaData = new BasicDBObject ();
78- metaData .put ("key " , "value " );
78+ metaData .put ("user " , "alex " );
7979 InputStream inputStream = null ;
8080 String id = "" ;
8181 try {
@@ -108,19 +108,21 @@ public void givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataI
108108 assertNotNull (gridFSDBFile .getChunkSize ());
109109 assertThat (gridFSDBFile .getContentType (), is ("image/png" ));
110110 assertThat (gridFSDBFile .getFilename (), is ("test.png" ));
111- assertThat (gridFSDBFile .getMetaData ().get ("key " ), is ("value " ));
111+ assertThat (gridFSDBFile .getMetaData ().get ("user " ), is ("alex " ));
112112 }
113113
114114 @ Test
115115 public void givenMetadataAndFilesExist_whenFindingAllFiles_thenFilesWithMetadataAreFound () {
116- DBObject metaData = new BasicDBObject ();
117- metaData .put ("key" , "value" );
116+ DBObject metaDataUser1 = new BasicDBObject ();
117+ metaDataUser1 .put ("user" , "alex" );
118+ DBObject metaDataUser2 = new BasicDBObject ();
119+ metaDataUser2 .put ("user" , "david" );
118120 InputStream inputStream = null ;
119121
120122 try {
121123 inputStream = new FileInputStream ("src/main/resources/test.png" );
122- gridFsTemplate .store (inputStream , "test.png" , "image/png" , metaData );
123- gridFsTemplate .store (inputStream , "test.png" , "image/png" , metaData );
124+ gridFsTemplate .store (inputStream , "test.png" , "image/png" , metaDataUser1 );
125+ gridFsTemplate .store (inputStream , "test.png" , "image/png" , metaDataUser2 );
124126 } catch (FileNotFoundException ex ) {
125127 logger .error ("File not found" , ex );
126128 } finally {
@@ -138,11 +140,41 @@ public void givenMetadataAndFilesExist_whenFindingAllFiles_thenFilesWithMetadata
138140 assertNotNull (gridFSDBFiles );
139141 assertThat (gridFSDBFiles .size (), is (2 ));
140142 }
143+
144+ @ Test
145+ public void givenMetadataAndFilesExist_whenFindingAllFilesOnQuery_thenFilesWithMetadataAreFoundOnQuery () {
146+ DBObject metaDataUser1 = new BasicDBObject ();
147+ metaDataUser1 .put ("user" , "alex" );
148+ DBObject metaDataUser2 = new BasicDBObject ();
149+ metaDataUser2 .put ("user" , "david" );
150+ InputStream inputStream = null ;
151+
152+ try {
153+ inputStream = new FileInputStream ("src/main/resources/test.png" );
154+ gridFsTemplate .store (inputStream , "test.png" , "image/png" , metaDataUser1 );
155+ gridFsTemplate .store (inputStream , "test.png" , "image/png" , metaDataUser2 );
156+ } catch (FileNotFoundException ex ) {
157+ logger .error ("File not found" , ex );
158+ } finally {
159+ if (inputStream != null ) {
160+ try {
161+ inputStream .close ();
162+ } catch (IOException ex ) {
163+ logger .error ("Failed to close" , ex );
164+ }
165+ }
166+ }
167+
168+ List <GridFSDBFile > gridFSDBFiles = gridFsTemplate .find (new Query (Criteria .where ("metadata.user" ).is ("alex" )));
169+
170+ assertNotNull (gridFSDBFiles );
171+ assertThat (gridFSDBFiles .size (), is (1 ));
172+ }
141173
142174 @ Test
143175 public void givenFileWithMetadataExist_whenDeletingFileById_thenFileWithMetadataIsDeleted () {
144176 DBObject metaData = new BasicDBObject ();
145- metaData .put ("key " , "value " );
177+ metaData .put ("user " , "alex " );
146178 InputStream inputStream = null ;
147179 String id = "" ;
148180 try {
@@ -168,7 +200,7 @@ public void givenFileWithMetadataExist_whenDeletingFileById_thenFileWithMetadata
168200 @ Test
169201 public void givenFileWithMetadataExist_whenGettingFileByResource_thenFileWithMetadataIsGotten () {
170202 DBObject metaData = new BasicDBObject ();
171- metaData .put ("key " , "value " );
203+ metaData .put ("user " , "alex " );
172204 InputStream inputStream = null ;
173205 String id = "" ;
174206 try {
0 commit comments