/test/FileStoreSpec.scala

https://github.com/delving/dos · Scala · 87 lines · 60 code · 23 blank · 4 comment · 2 complexity · e2a86ac559d7081ed4ccca99404e1a27 MD5 · raw file

  1. import com.mongodb.casbah.commons.MongoDBObject
  2. import controllers.dos._
  3. import java.io.{ByteArrayInputStream, File}
  4. import controllers.dos.StoredFile
  5. import org.bson.types.ObjectId
  6. import org.scalatest.BeforeAndAfterAll
  7. import org.scalatest.matchers.ShouldMatchers
  8. import play.data.Upload
  9. import play.libs.{MimeTypes, IO}
  10. import play.mvc.results.{RenderBinary, Result}
  11. import play.Play
  12. import play.test.UnitFlatSpec
  13. /**
  14. *
  15. * @author Manuel Bernhardt <bernhardt.manuel@gmail.com>
  16. */
  17. class FileStoreSpec extends UnitFlatSpec with ShouldMatchers with BeforeAndAfterAll {
  18. override protected def afterAll() {
  19. fileStore.db.dropDatabase()
  20. }
  21. val testFile = if(Play.configuration.getProperty("application.name") == "dos")
  22. new File(play.Play.applicationPath, "public/dos/images/dummy-object.png")
  23. else
  24. new File(Play.modules.get("dos").getRealFile, "public/dos/images/dummy-object.png")
  25. val TEST_UID = "123456789"
  26. val TEST_OID = new ObjectId
  27. var uploaded_id: ObjectId = null
  28. it should "upload a file" in {
  29. val upload = new MockUpload(testFile)
  30. val uploads = List(upload)
  31. val res: Result = FileUpload.uploadFileInternal(TEST_UID, uploads)
  32. res.getClass should not equal (classOf[Error])
  33. }
  34. it should "find back files by upload UID" in {
  35. val fetched: Seq[StoredFile] = FileUpload.getFilesForUID(TEST_UID)
  36. fetched.length should equal(1)
  37. fetched.head.name should equal(testFile.getName)
  38. fetched.head.length should equal(testFile.length())
  39. }
  40. it should "attach uploaded files to an object, given an upload UID and an object ID" in {
  41. FileUpload.markFilesAttached(TEST_UID, TEST_OID)
  42. val file = fileStore.findOne(MongoDBObject(ITEM_POINTER_FIELD -> TEST_OID))
  43. file should not equal (None)
  44. FileUpload.getFilesForUID(TEST_UID).length should equal(0)
  45. uploaded_id = file.get.get("_id").get.asInstanceOf[ObjectId]
  46. }
  47. it should "mark an active thumbnail and an active image given a file pointer and object ID" in {
  48. FileUpload.activateThumbnails(uploaded_id, TEST_OID)
  49. val image = ImageDisplay.displayImage(TEST_OID.toString)
  50. val thumbnail = ImageDisplay.displayThumbnail(id = TEST_OID.toString, orgId = "", collectionId = "", browse = false)
  51. image.getClass should equal (classOf[RenderBinary])
  52. thumbnail.getClass should equal (classOf[RenderBinary])
  53. }
  54. }
  55. class MockUpload(file: File) extends Upload {
  56. def asBytes() = IO.readContent(file)
  57. def asStream() = new ByteArrayInputStream(asBytes())
  58. def getContentType = MimeTypes.getContentType(file.getName)
  59. def getFileName = file.getName
  60. def getFieldName = "mock"
  61. def getSize = file.length()
  62. def isInMemory = true
  63. def asFile() = file
  64. }