|
| 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.assertEquals; |
| 6 | +import static org.junit.Assert.assertNotNull; |
| 7 | +import static org.junit.Assert.assertNull; |
| 8 | +import static org.junit.Assert.assertThat; |
| 9 | + |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.FileNotFoundException; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import org.baeldung.config.MongoConfig; |
| 17 | +import org.junit.After; |
| 18 | +import org.junit.Test; |
| 19 | +import org.junit.runner.RunWith; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.data.mongodb.core.query.Criteria; |
| 24 | +import org.springframework.data.mongodb.core.query.Query; |
| 25 | +import org.springframework.data.mongodb.gridfs.GridFsResource; |
| 26 | +import org.springframework.data.mongodb.gridfs.GridFsTemplate; |
| 27 | +import org.springframework.test.context.ContextConfiguration; |
| 28 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 29 | + |
| 30 | +import com.mongodb.BasicDBObject; |
| 31 | +import com.mongodb.DBObject; |
| 32 | +import com.mongodb.gridfs.GridFSDBFile; |
| 33 | + |
| 34 | +@ContextConfiguration("file:src/main/resources/mongoConfig.xml") |
| 35 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 36 | +public class GridFSIntegrationTest { |
| 37 | + |
| 38 | + private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private GridFsTemplate gridFsTemplate; |
| 42 | + |
| 43 | + @After |
| 44 | + public void tearDown() { |
| 45 | + List<GridFSDBFile> fileList = gridFsTemplate.find(null); |
| 46 | + for (GridFSDBFile file : fileList) { |
| 47 | + gridFsTemplate.delete(new Query(Criteria.where("filename").is(file.getFilename()))); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void whenStoringFileWithMetadata_thenFileAndMetadataAreStored() { |
| 53 | + DBObject metaData = new BasicDBObject(); |
| 54 | + metaData.put("user", "alex"); |
| 55 | + InputStream inputStream = null; |
| 56 | + String id = ""; |
| 57 | + try { |
| 58 | + inputStream = new FileInputStream("src/main/resources/test.png"); |
| 59 | + id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString(); |
| 60 | + } catch (FileNotFoundException ex) { |
| 61 | + logger.error("File not found", ex); |
| 62 | + } finally { |
| 63 | + if (inputStream != null) { |
| 64 | + try { |
| 65 | + inputStream.close(); |
| 66 | + } catch (IOException ex) { |
| 67 | + logger.error("Failed to close", ex); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + assertNotNull(id); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataIsFound() { |
| 77 | + DBObject metaData = new BasicDBObject(); |
| 78 | + metaData.put("user", "alex"); |
| 79 | + InputStream inputStream = null; |
| 80 | + String id = ""; |
| 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("File not found", ex); |
| 86 | + } finally { |
| 87 | + if (inputStream != null) { |
| 88 | + try { |
| 89 | + inputStream.close(); |
| 90 | + } catch (IOException ex) { |
| 91 | + logger.error("Failed to close", ex); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + GridFSDBFile gridFSDBFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id))); |
| 97 | + |
| 98 | + assertNotNull(gridFSDBFile); |
| 99 | + assertNotNull(gridFSDBFile.getInputStream()); |
| 100 | + assertThat(gridFSDBFile.numChunks(), is(1)); |
| 101 | + assertThat(gridFSDBFile.containsField("filename"), is(true)); |
| 102 | + assertThat(gridFSDBFile.get("filename"), is("test.png")); |
| 103 | + assertThat(gridFSDBFile.getId(), is(id)); |
| 104 | + assertThat(gridFSDBFile.keySet().size(), is(9)); |
| 105 | + assertNotNull(gridFSDBFile.getMD5()); |
| 106 | + assertNotNull(gridFSDBFile.getUploadDate()); |
| 107 | + assertNull(gridFSDBFile.getAliases()); |
| 108 | + assertNotNull(gridFSDBFile.getChunkSize()); |
| 109 | + assertThat(gridFSDBFile.getContentType(), is("image/png")); |
| 110 | + assertThat(gridFSDBFile.getFilename(), is("test.png")); |
| 111 | + assertThat(gridFSDBFile.getMetaData().get("user"), is("alex")); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void givenMetadataAndFilesExist_whenFindingAllFiles_thenFilesWithMetadataAreFound() { |
| 116 | + DBObject metaDataUser1 = new BasicDBObject(); |
| 117 | + metaDataUser1.put("user", "alex"); |
| 118 | + DBObject metaDataUser2 = new BasicDBObject(); |
| 119 | + metaDataUser2.put("user", "david"); |
| 120 | + InputStream inputStream = null; |
| 121 | + |
| 122 | + try { |
| 123 | + inputStream = new FileInputStream("src/main/resources/test.png"); |
| 124 | + gridFsTemplate.store(inputStream, "test.png", "image/png", metaDataUser1); |
| 125 | + gridFsTemplate.store(inputStream, "test.png", "image/png", metaDataUser2); |
| 126 | + } catch (FileNotFoundException ex) { |
| 127 | + logger.error("File not found", ex); |
| 128 | + } finally { |
| 129 | + if (inputStream != null) { |
| 130 | + try { |
| 131 | + inputStream.close(); |
| 132 | + } catch (IOException ex) { |
| 133 | + logger.error("Failed to close", ex); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + List<GridFSDBFile> gridFSDBFiles = gridFsTemplate.find(null); |
| 139 | + |
| 140 | + assertNotNull(gridFSDBFiles); |
| 141 | + assertThat(gridFSDBFiles.size(), is(2)); |
| 142 | + } |
| 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 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void givenFileWithMetadataExist_whenDeletingFileById_thenFileWithMetadataIsDeleted() { |
| 176 | + DBObject metaData = new BasicDBObject(); |
| 177 | + metaData.put("user", "alex"); |
| 178 | + InputStream inputStream = null; |
| 179 | + String id = ""; |
| 180 | + try { |
| 181 | + inputStream = new FileInputStream("src/main/resources/test.png"); |
| 182 | + id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString(); |
| 183 | + } catch (FileNotFoundException ex) { |
| 184 | + logger.error("File not found", ex); |
| 185 | + } finally { |
| 186 | + if (inputStream != null) { |
| 187 | + try { |
| 188 | + inputStream.close(); |
| 189 | + } catch (IOException ex) { |
| 190 | + logger.error("Failed to close", ex); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + gridFsTemplate.delete(new Query(Criteria.where("_id").is(id))); |
| 196 | + |
| 197 | + assertThat(gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id))), is(nullValue())); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + public void givenFileWithMetadataExist_whenGettingFileByResource_thenFileWithMetadataIsGotten() { |
| 202 | + DBObject metaData = new BasicDBObject(); |
| 203 | + metaData.put("user", "alex"); |
| 204 | + InputStream inputStream = null; |
| 205 | + String id = ""; |
| 206 | + try { |
| 207 | + inputStream = new FileInputStream("src/main/resources/test.png"); |
| 208 | + id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString(); |
| 209 | + } catch (FileNotFoundException ex) { |
| 210 | + logger.error("File not found", ex); |
| 211 | + } finally { |
| 212 | + if (inputStream != null) { |
| 213 | + try { |
| 214 | + inputStream.close(); |
| 215 | + } catch (IOException ex) { |
| 216 | + logger.error("Failed to close", ex); |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + GridFsResource[] gridFsResource = gridFsTemplate.getResources("test*"); |
| 222 | + |
| 223 | + assertNotNull(gridFsResource); |
| 224 | + assertEquals(gridFsResource.length, 1); |
| 225 | + assertThat(gridFsResource[0].getFilename(), is("test.png")); |
| 226 | + } |
| 227 | +} |
0 commit comments