package com.aperture_software.glados_wiki.tests; import com.aperture_software.glados_wiki.junit.MyTestcase; import com.google.common.io.Closer; import com.mongodb.MongoClient; import com.mongodb.gridfs.GridFS; import com.mongodb.gridfs.GridFSDBFile; import com.mongodb.gridfs.GridFSInputFile; import org.apache.commons.io.IOUtils; import org.bson.types.ObjectId; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; /** * Created with IntelliJ IDEA. * User: jhyun * Date: 13. Nov. 15. * Time: 22:24 */ public class GridFsTests extends MyTestcase { @Autowired private ResourceLoader resourceLoader; @Autowired private MongoClient mongoClient; @Test public void t_01() throws IOException { GridFS gf = new GridFS(mongoClient.getDB("test"), "glados-wiki"); Resource r = resourceLoader.getResource("classpath:com/aperture_software/glados_wiki/tests/GridFsTests/test.jpg"); assertNotNull(r); File f = r.getFile(); assertNotNull(f); assertTrue(f.exists()); GridFSInputFile gif = gf.createFile(f); gif.setFilename(f.getName()); //gif.setContentType(); gif.save(); GridFSDBFile gif2 = gf.find((ObjectId) gif.getId()); assertNotNull(gif2); Closer closer = Closer.create(); try { InputStream fileIn = new FileInputStream(f); assertNotNull(fileIn); closer.register(fileIn); InputStream gif2_in = gif2.getInputStream(); assertNotNull(gif2_in); closer.register(gif2_in); assertTrue(IOUtils.contentEquals(fileIn, gif2_in)); } finally { closer.close(); } gf.remove((ObjectId) gif.getId()); } // TODO: metadata - public/private? }