Guest User

Untitled

a guest
Nov 19th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. package test
  2.  
  3. import com.mongodb.casbah.Imports._
  4. import com.novus.salat.dao.{ModelCompanion, SalatDAO}
  5. import java.io._
  6. import models._
  7. import models.util._
  8. import org.apache.commons.net.ftp._
  9. import org.scala_tools.time.Imports._
  10. import org.specs2.mutable._
  11. import play.api._
  12. import play.api.Play.current
  13. import play.api.test._
  14. import play.api.test.Helpers._
  15. import scala.sys.process._
  16. import scalaxb._
  17. import zpProtocol._
  18.  
  19. class XMLImportSpec extends Specification {
  20. override def is = args(sequential = true) ^ super.is
  21.  
  22. val log = play.api.Logger
  23.  
  24. "Database" should {
  25. "be filled with data from XML files" in new FakeApp() {
  26. "download" must beAWritablePath
  27.  
  28. val dnloads = tree(new File("download")).toArray
  29.  
  30. def imported = for {
  31. f <- dnloads if (f.isFile && f.getName.endsWith(".xml"))
  32. nodes = xml.XML.loadFile("download/" + f.getName)
  33. parsed = scalaxb.fromXML[ZpBaseType](nodes)
  34. } yield parsed
  35.  
  36. var cnt = 0
  37. for(rec <- imported) {
  38. rec.zpbasetypeoption must beAnInstanceOf[Seq[Any]]
  39. PublicOrder.insert(rec) must beSome
  40. MongoConnection()(play.api.Play.configuration.getString("mongodb.default.db").get).getLastError.get("err") must beNull
  41. cnt += 1
  42. }
  43. cnt aka "import iterations" must_== imported.length
  44. PublicOrder.collection aka "the imported collection" must haveSize(imported.length)
  45. }
  46. }
  47.  
  48. step (DeregisterXMLStandardTypesSerializer())
  49. }
  50.  
  51.  
  52. trait FakeApp extends Around {
  53. val db_name = "offers_db_test"
  54.  
  55. def mongoTestDatabase() = Map("mongodb.default.db" -> db_name,
  56. "source.ftp" -> "ftp.uzp.gov.pl")
  57. object FakeApp extends FakeApplication(additionalConfiguration = mongoTestDatabase())
  58.  
  59. val Some(ftpAddr) = FakeApp.configuration.getString("source.ftp")
  60.  
  61. def around[T <% org.specs2.execute.Result](test: =>T) = running(FakeApplication(additionalConfiguration = mongoTestDatabase())) {
  62. test // run tests inside a fake application
  63. }
  64.  
  65. def tree(root: File, skipHidden: Boolean = true): Stream[File] =
  66. if (!root.exists || (skipHidden && root.isHidden)) Stream.empty
  67. else root #:: (
  68. root.listFiles match {
  69. case null => Stream.empty
  70. case files => files.toStream.flatMap(tree(_, skipHidden))
  71. })
  72. }
Add Comment
Please, Sign In to add comment