From f2028571951984b7ce7881f9b367936230412d64 Mon Sep 17 00:00:00 2001 From: coach88 Date: Tue, 22 Sep 2015 12:42:27 +0300 Subject: [PATCH 01/13] Added tests with GridFSTemplate --- .../java/org/baeldung/config/MongoConfig.java | 6 + .../java/org/baeldung/gridfs/GridFSTest.java | 186 ++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSTest.java diff --git a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java index f0f6105dfa..0989088b41 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java @@ -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()); + } } diff --git a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSTest.java b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSTest.java new file mode 100644 index 0000000000..3d6d2ade27 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSTest.java @@ -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 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 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")); + } +} From c6be2af18f97acc34fc58157c1d928ca16312b16 Mon Sep 17 00:00:00 2001 From: coach88 Date: Tue, 22 Sep 2015 12:46:12 +0300 Subject: [PATCH 02/13] Added assertion in GridFSTest --- .../java/org/baeldung/gridfs/GridFSTest.java | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSTest.java b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSTest.java index 3d6d2ade27..0499f7fd5d 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSTest.java @@ -2,6 +2,7 @@ package org.baeldung.gridfs; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; @@ -32,16 +33,16 @@ 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 fileList = gridFsTemplate.find(null); - for(GridFSDBFile file: fileList) { + for (GridFSDBFile file : fileList) { gridFsTemplate.delete(new Query(Criteria.where("filename").is(file.getFilename()))); } } @@ -52,7 +53,6 @@ public class GridFSTest { 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(); @@ -77,7 +77,6 @@ public class GridFSTest { 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(); @@ -92,9 +91,9 @@ public class GridFSTest { } } } - + 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")); @@ -121,21 +120,19 @@ public class GridFSTest { } } } - + List 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(); @@ -150,19 +147,18 @@ public class GridFSTest { } } } - + 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(); @@ -177,10 +173,11 @@ public class GridFSTest { } } } - + GridFsResource[] gridFsResource = gridFsTemplate.getResources("test*"); - + assertNotNull(gridFsResource); + assertEquals(gridFsResource.length, 1); assertThat(gridFsResource[0].getFilename(), is("test.png")); } } From 7defd5d598f34c2e51d1dae9ba8644b13397199b Mon Sep 17 00:00:00 2001 From: coach88 Date: Sun, 27 Sep 2015 12:13:29 +0300 Subject: [PATCH 03/13] Added xml configuration for GridFS --- spring-data-mongodb/src/main/resources/mongoConfig.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spring-data-mongodb/src/main/resources/mongoConfig.xml b/spring-data-mongodb/src/main/resources/mongoConfig.xml index 1a9d161ad6..0c692cdd62 100644 --- a/spring-data-mongodb/src/main/resources/mongoConfig.xml +++ b/spring-data-mongodb/src/main/resources/mongoConfig.xml @@ -14,6 +14,11 @@ + + + + + From d4bf988851e5aa25bb895e7d617056d41b699a96 Mon Sep 17 00:00:00 2001 From: coach88 Date: Wed, 30 Sep 2015 10:52:26 +0300 Subject: [PATCH 04/13] Minor cleanup --- .../gridfs/{GridFSTest.java => GridFSIntegrationTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename spring-data-mongodb/src/test/java/org/baeldung/gridfs/{GridFSTest.java => GridFSIntegrationTest.java} (99%) diff --git a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSTest.java b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java similarity index 99% rename from spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSTest.java rename to spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java index 0499f7fd5d..c6a4855957 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java @@ -32,7 +32,7 @@ import com.mongodb.gridfs.GridFSDBFile; @ContextConfiguration(classes = MongoConfig.class) @RunWith(SpringJUnit4ClassRunner.class) -public class GridFSTest { +public class GridFSIntegrationTest { private final Logger logger = LoggerFactory.getLogger(getClass()); From 8442e3c496019bfe10773520b96718f7d0375342 Mon Sep 17 00:00:00 2001 From: coach88 Date: Wed, 30 Sep 2015 13:01:37 +0300 Subject: [PATCH 05/13] Minor cleanup --- .../gridfs/GridFSIntegrationTest.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java index c6a4855957..2a8e2e121b 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java @@ -57,13 +57,13 @@ public class GridFSIntegrationTest { 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); + logger.error("File not found", ex); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { - logger.error("", ex); + logger.error("Failed to close", ex); } } } @@ -81,13 +81,13 @@ public class GridFSIntegrationTest { 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); + logger.error("File not found", ex); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { - logger.error("", ex); + logger.error("Failed to close", ex); } } } @@ -110,13 +110,13 @@ public class GridFSIntegrationTest { gridFsTemplate.store(inputStream, "test.png", "image/png", metaData); gridFsTemplate.store(inputStream, "test.png", "image/png", metaData); } catch (FileNotFoundException ex) { - logger.error("", ex); + logger.error("File not found", ex); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { - logger.error("", ex); + logger.error("Failed to close", ex); } } } @@ -137,13 +137,13 @@ public class GridFSIntegrationTest { 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); + logger.error("File not found", ex); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { - logger.error("", ex); + logger.error("Failed to close", ex); } } } @@ -163,13 +163,13 @@ public class GridFSIntegrationTest { 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); + logger.error("File not found", ex); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { - logger.error("", ex); + logger.error("Failed to close", ex); } } } From 8c935d81d985697d4a2282a020f27a210ba2247e Mon Sep 17 00:00:00 2001 From: coach88 Date: Thu, 1 Oct 2015 15:19:59 +0300 Subject: [PATCH 06/13] Added more asserts for test --- .../org/baeldung/gridfs/GridFSIntegrationTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java index 2a8e2e121b..b12b19b894 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java @@ -4,6 +4,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import java.io.FileInputStream; @@ -95,6 +96,17 @@ public class GridFSIntegrationTest { GridFSDBFile gridFSDBFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id))); assertNotNull(gridFSDBFile); + assertNotNull(gridFSDBFile.getInputStream()); + assertThat(gridFSDBFile.numChunks(), is(1)); + assertThat(gridFSDBFile.containsField("filename"),is(true)); + assertThat(gridFSDBFile.get("filename"),is("test.png")); + assertThat(gridFSDBFile.getId(),is(id)); + assertThat(gridFSDBFile.keySet().size(),is(9)); + assertNotNull(gridFSDBFile.getMD5()); + assertNotNull(gridFSDBFile.getUploadDate()); + assertNull(gridFSDBFile.getAliases()); + assertNotNull(gridFSDBFile.getChunkSize()); + assertThat(gridFSDBFile.getContentType(), is("image/png")); assertThat(gridFSDBFile.getFilename(), is("test.png")); assertThat(gridFSDBFile.getMetaData().get("key"), is("value")); } From f0b6ada3cfd230051e337e779de3870bee4321c4 Mon Sep 17 00:00:00 2001 From: coach88 Date: Thu, 1 Oct 2015 15:42:36 +0300 Subject: [PATCH 07/13] Minor cleanup --- .../java/org/baeldung/gridfs/GridFSIntegrationTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java index b12b19b894..2416d6aaf8 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java @@ -98,10 +98,10 @@ public class GridFSIntegrationTest { assertNotNull(gridFSDBFile); assertNotNull(gridFSDBFile.getInputStream()); assertThat(gridFSDBFile.numChunks(), is(1)); - assertThat(gridFSDBFile.containsField("filename"),is(true)); - assertThat(gridFSDBFile.get("filename"),is("test.png")); - assertThat(gridFSDBFile.getId(),is(id)); - assertThat(gridFSDBFile.keySet().size(),is(9)); + assertThat(gridFSDBFile.containsField("filename"), is(true)); + assertThat(gridFSDBFile.get("filename"), is("test.png")); + assertThat(gridFSDBFile.getId(), is(id)); + assertThat(gridFSDBFile.keySet().size(), is(9)); assertNotNull(gridFSDBFile.getMD5()); assertNotNull(gridFSDBFile.getUploadDate()); assertNull(gridFSDBFile.getAliases()); From 41e2a1d3a2129d57756a3fc1cb5a178e9923beb4 Mon Sep 17 00:00:00 2001 From: coach88 Date: Thu, 1 Oct 2015 15:43:41 +0300 Subject: [PATCH 08/13] Minor formatting cleanup --- .../src/main/java/org/baeldung/config/MongoConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java index 823c185be9..6910245b2b 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java @@ -53,7 +53,7 @@ public class MongoConfig extends AbstractMongoConfiguration { converters.add(new UserWriterConverter()); return new CustomConversions(converters); } - + @Bean public GridFsTemplate gridFsTemplate() throws Exception { return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter()); From 27e649a6356a57be2afa15dad6925f96a0fe8f1f Mon Sep 17 00:00:00 2001 From: coach88 Date: Wed, 14 Oct 2015 12:24:02 +0300 Subject: [PATCH 09/13] Added test file for GridFS --- spring-data-mongodb/src/main/resources/test.png | Bin 0 -> 855 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 spring-data-mongodb/src/main/resources/test.png diff --git a/spring-data-mongodb/src/main/resources/test.png b/spring-data-mongodb/src/main/resources/test.png new file mode 100644 index 0000000000000000000000000000000000000000..c3b5e8027635ff4f0a0b10c0a9a6aa7a014e6086 GIT binary patch literal 855 zcmex=6kE^cmKJ|O{KK0!WiZUHd? zL17V5QBfX#aS1UI2_X?t5k_Vf7B*H^PBu0!em)*P5fKr7L0%pn0bT(feo-D?UVdI4 zpg0dNAHM*Q$HT+J$HNcg@q#%Zh5rvQ2y!sUFvu`73NkPWGBOJ?{y)MX0rV0OKpYGv zIoQ})S%5M+0t`%y%uFn-%&bsZMkb)c1=$oC*g1rh3>^~-jRG5mMVuBM1ge%|WME=O zF^8Fjk%^U!0Vt;gv=3+$3p2tNMkYaKCBqLwfr*X6ijMzpG4L=04G?4&WUyx___R78 z`bEZ3n*%y$c1p?=OzsY{vOIcDRID^@mH5k5n_VmCLN*tlQtc~BqmaI3acZm^A1dHBrChNRU-KfIG`3a9F1Z+JCT zaBkFRt*1+W!zihZ}s%F&cxn?h~EIRD%9;ni-#KyOjOZ3l7 z_6Ks(XSKVYPbdvPm!tpIJj%Ot(S2`WC0AaXj1Ds{Yc~e@JYj`ND^=I^G)#%-Q1qX7 z;Q8}@p=SNS>z3K2PrXivv`$ Date: Wed, 14 Oct 2015 15:10:10 +0200 Subject: [PATCH 10/13] Fixed xml mongoConfig --- .../src/main/resources/mongoConfig.xml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/spring-data-mongodb/src/main/resources/mongoConfig.xml b/spring-data-mongodb/src/main/resources/mongoConfig.xml index 0c692cdd62..2b32863fb6 100644 --- a/spring-data-mongodb/src/main/resources/mongoConfig.xml +++ b/spring-data-mongodb/src/main/resources/mongoConfig.xml @@ -10,10 +10,10 @@ http://www.springframework.org/schema/context/spring-context-3.2.xsd" > - - - - + + + + @@ -21,9 +21,8 @@ - + - From 78f441d1b9d5af878180dab5efbc807e70fec2cf Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Wed, 14 Oct 2015 16:29:38 +0200 Subject: [PATCH 11/13] Added test for find with Query --- .../gridfs/GridFSIntegrationTest.java | 52 +++++++++++++++---- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java index 2416d6aaf8..06ad21647b 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/gridfs/GridFSIntegrationTest.java @@ -31,7 +31,7 @@ import com.mongodb.BasicDBObject; 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 { @@ -51,7 +51,7 @@ public class GridFSIntegrationTest { @Test public void whenStoringFileWithMetadata_thenFileAndMetadataAreStored() { DBObject metaData = new BasicDBObject(); - metaData.put("key", "value"); + metaData.put("user", "alex"); InputStream inputStream = null; String id = ""; try { @@ -75,7 +75,7 @@ public class GridFSIntegrationTest { @Test public void givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataIsFound() { DBObject metaData = new BasicDBObject(); - metaData.put("key", "value"); + metaData.put("user", "alex"); InputStream inputStream = null; String id = ""; try { @@ -108,19 +108,21 @@ public class GridFSIntegrationTest { 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 { @@ -138,11 +140,41 @@ public class GridFSIntegrationTest { 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 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 { @@ -168,7 +200,7 @@ public class GridFSIntegrationTest { @Test public void givenFileWithMetadataExist_whenGettingFileByResource_thenFileWithMetadataIsGotten() { DBObject metaData = new BasicDBObject(); - metaData.put("key", "value"); + metaData.put("user", "alex"); InputStream inputStream = null; String id = ""; try { From b48806a68d039cb3be973d71d8688952f6c1e4df Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 22 Oct 2015 10:35:01 +0300 Subject: [PATCH 12/13] Minor cleanup --- spring-data-mongodb/.project | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 spring-data-mongodb/.project diff --git a/spring-data-mongodb/.project b/spring-data-mongodb/.project deleted file mode 100644 index bfe02478f5..0000000000 --- a/spring-data-mongodb/.project +++ /dev/null @@ -1,29 +0,0 @@ - - - spring-data-mongodb - NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse. - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.m2e.core.maven2Builder - - - - - org.springframework.ide.eclipse.core.springbuilder - - - - - - org.springframework.ide.eclipse.core.springnature - org.eclipse.jdt.core.javanature - org.eclipse.m2e.core.maven2Nature - - From f09de9b1a440901dcc314b7aae5e5cc9b2f3aabd Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 22 Oct 2015 10:35:23 +0300 Subject: [PATCH 13/13] Minor cleanup --- spring-data-mongodb/.classpath | 37 ---------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 spring-data-mongodb/.classpath diff --git a/spring-data-mongodb/.classpath b/spring-data-mongodb/.classpath deleted file mode 100644 index a86150fe21..0000000000 --- a/spring-data-mongodb/.classpath +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -