Skip to content

Commit f202857

Browse files
committed
Added tests with GridFSTemplate
1 parent c9d5aff commit f202857

2 files changed

Lines changed: 192 additions & 0 deletions

File tree

‎spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.context.annotation.Configuration;
1111
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
1212
import org.springframework.data.mongodb.core.convert.CustomConversions;
13+
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
1314
import org.springframework.core.convert.converter.Converter;
1415
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
1516

@@ -52,4 +53,9 @@ public CustomConversions customConversions() {
5253
converters.add(new UserWriterConverter());
5354
return new CustomConversions(converters);
5455
}
56+
57+
@Bean
58+
public GridFsTemplate gridFsTemplate() throws Exception {
59+
return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
60+
}
5561
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package org.baeldung.gridfs;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.hamcrest.Matchers.nullValue;
5+
import static org.junit.Assert.assertNotNull;
6+
import static org.junit.Assert.assertThat;
7+
8+
import java.io.FileInputStream;
9+
import java.io.FileNotFoundException;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.util.List;
13+
14+
import org.baeldung.config.MongoConfig;
15+
import org.junit.After;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.data.mongodb.core.query.Criteria;
22+
import org.springframework.data.mongodb.core.query.Query;
23+
import org.springframework.data.mongodb.gridfs.GridFsResource;
24+
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
25+
import org.springframework.test.context.ContextConfiguration;
26+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27+
28+
import com.mongodb.BasicDBObject;
29+
import com.mongodb.DBObject;
30+
import com.mongodb.gridfs.GridFSDBFile;
31+
32+
@ContextConfiguration(classes = MongoConfig.class)
33+
@RunWith(SpringJUnit4ClassRunner.class)
34+
public class GridFSTest {
35+
36+
private final Logger logger = LoggerFactory.getLogger(getClass());
37+
38+
@Autowired
39+
private GridFsTemplate gridFsTemplate;
40+
41+
@After
42+
public void tearDown() {
43+
List<GridFSDBFile> fileList = gridFsTemplate.find(null);
44+
for(GridFSDBFile file: fileList) {
45+
gridFsTemplate.delete(new Query(Criteria.where("filename").is(file.getFilename())));
46+
}
47+
}
48+
49+
@Test
50+
public void whenStoringFileWithMetadata_thenFileAndMetadataAreStored() {
51+
DBObject metaData = new BasicDBObject();
52+
metaData.put("key", "value");
53+
InputStream inputStream = null;
54+
String id = "";
55+
56+
try {
57+
inputStream = new FileInputStream("src/main/resources/test.png");
58+
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
59+
} catch (FileNotFoundException ex) {
60+
logger.error("", ex);
61+
} finally {
62+
if (inputStream != null) {
63+
try {
64+
inputStream.close();
65+
} catch (IOException ex) {
66+
logger.error("", ex);
67+
}
68+
}
69+
}
70+
71+
assertNotNull(id);
72+
}
73+
74+
@Test
75+
public void givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataIsFound() {
76+
DBObject metaData = new BasicDBObject();
77+
metaData.put("key", "value");
78+
InputStream inputStream = null;
79+
String id = "";
80+
81+
try {
82+
inputStream = new FileInputStream("src/main/resources/test.png");
83+
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
84+
} catch (FileNotFoundException ex) {
85+
logger.error("", ex);
86+
} finally {
87+
if (inputStream != null) {
88+
try {
89+
inputStream.close();
90+
} catch (IOException ex) {
91+
logger.error("", ex);
92+
}
93+
}
94+
}
95+
96+
GridFSDBFile gridFSDBFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
97+
98+
assertNotNull(gridFSDBFile);
99+
assertThat(gridFSDBFile.getFilename(), is("test.png"));
100+
assertThat(gridFSDBFile.getMetaData().get("key"), is("value"));
101+
}
102+
103+
@Test
104+
public void givenMetadataAndFilesExist_whenFindingAllFiles_thenFilesWithMetadataAreFound() {
105+
DBObject metaData = new BasicDBObject();
106+
metaData.put("key", "value");
107+
InputStream inputStream = null;
108+
109+
try {
110+
inputStream = new FileInputStream("src/main/resources/test.png");
111+
gridFsTemplate.store(inputStream, "test.png", "image/png", metaData);
112+
gridFsTemplate.store(inputStream, "test.png", "image/png", metaData);
113+
} catch (FileNotFoundException ex) {
114+
logger.error("", ex);
115+
} finally {
116+
if (inputStream != null) {
117+
try {
118+
inputStream.close();
119+
} catch (IOException ex) {
120+
logger.error("", ex);
121+
}
122+
}
123+
}
124+
125+
List<GridFSDBFile> gridFSDBFiles = gridFsTemplate.find(null);
126+
127+
assertNotNull(gridFSDBFiles);
128+
assertThat(gridFSDBFiles.size(), is(2));
129+
}
130+
131+
132+
@Test
133+
public void givenFileWithMetadataExist_whenDeletingFileById_thenFileWithMetadataIsDeleted() {
134+
DBObject metaData = new BasicDBObject();
135+
metaData.put("key", "value");
136+
InputStream inputStream = null;
137+
String id = "";
138+
139+
try {
140+
inputStream = new FileInputStream("src/main/resources/test.png");
141+
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
142+
} catch (FileNotFoundException ex) {
143+
logger.error("", ex);
144+
} finally {
145+
if (inputStream != null) {
146+
try {
147+
inputStream.close();
148+
} catch (IOException ex) {
149+
logger.error("", ex);
150+
}
151+
}
152+
}
153+
154+
gridFsTemplate.delete(new Query(Criteria.where("_id").is(id)));
155+
156+
assertThat(gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id))), is(nullValue()));
157+
}
158+
159+
@Test
160+
public void givenFileWithMetadataExist_whenGettingFileByResource_thenFileWithMetadataIsGotten() {
161+
DBObject metaData = new BasicDBObject();
162+
metaData.put("key", "value");
163+
InputStream inputStream = null;
164+
String id = "";
165+
166+
try {
167+
inputStream = new FileInputStream("src/main/resources/test.png");
168+
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
169+
} catch (FileNotFoundException ex) {
170+
logger.error("", ex);
171+
} finally {
172+
if (inputStream != null) {
173+
try {
174+
inputStream.close();
175+
} catch (IOException ex) {
176+
logger.error("", ex);
177+
}
178+
}
179+
}
180+
181+
GridFsResource[] gridFsResource = gridFsTemplate.getResources("test*");
182+
183+
assertNotNull(gridFsResource);
184+
assertThat(gridFsResource[0].getFilename(), is("test.png"));
185+
}
186+
}

0 commit comments

Comments
 (0)