PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Dann/src-test/com/minosiants/dann/repository/MongoRepositoryTest.scala

http://tratata.googlecode.com/
Scala | 64 lines | 55 code | 8 blank | 1 comment | 3 complexity | d195731feb451b6533f50d9ed79c6d4b MD5 | raw file
  1. package com.minosiants.dann.repository
  2. import com.mongodb.casbah.commons.MongoDBObject
  3. import org.junit.Assert._
  4. import com.minosiants.dann.data._
  5. import com.google.gson.reflect.TypeToken;
  6. import org.junit.Test
  7. import scala.collection.JavaConverters._
  8. import com.mongodb.DBObject
  9. class MongoRepositoryTest {
  10. val gson=Gson();
  11. val mongoRepository =new MongoRepository
  12. //@Test
  13. def save(){
  14. val json="""[{"contributors":null,"retweeted":false,"text":"Introducing Starbucks Mobile Pour - http:\/\/goo.gl\/IQX3U \/vis @starbucks","in_reply_to_user_id_str":null,"retweet_count":0,"id_str":"53894976874102784","entities":{"hashtags":[],"urls":[{"indices":[37,56],"url":"http:\/\/goo.gl\/IQX3U","expanded_url":null}],"user_mentions":[{"indices":[62,72],"id_str":"30973","name":"Starbucks Coffee","screen_name":"Starbucks","id":30973}]},"place":null,"source":"web","created_at":"Fri Apr 01 19:02:18 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":null,"id":53894976874102784,"in_reply_to_status_id_str":null},{"contributors":null,"retweeted":false,"text":"@hseeberger Thanks. It's awesome :)","in_reply_to_user_id_str":"19757713","retweet_count":0,"id_str":"53890073032273920","entities":{"hashtags":[],"urls":[],"user_mentions":[{"indices":[0,11],"id_str":"19757713","name":"Heiko Seeberger","screen_name":"hseeberger","id":19757713}]},"place":null,"source":"web","created_at":"Fri Apr 01 18:42:49 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":19757713,"in_reply_to_status_id":53854478033629184,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":"hseeberger","id":53890073032273920,"in_reply_to_status_id_str":"53854478033629184"},{"contributors":null,"retweeted":false,"text":"LinkedIn greeting with #1April LOL #in http:\/\/twitpic.com\/4frllj","in_reply_to_user_id_str":null,"retweet_count":0,"id_str":"53887519632269313","entities":{"hashtags":[{"text":"1April","indices":[23,30]},{"text":"in","indices":[35,38]}],"urls":[{"indices":[40,65],"url":"http:\/\/twitpic.com\/4frllj","expanded_url":null}],"user_mentions":[]},"place":null,"source":"\u003Ca href=\"http:\/\/twitpic.com\" rel=\"nofollow\"\u003ETwitpic\u003C\/a\u003E","created_at":"Fri Apr 01 18:32:40 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":null,"id":53887519632269313,"in_reply_to_status_id_str":null},{"contributors":null,"retweeted":false,"text":"@hseeberger what's the best version of Scala Ide to use?I'm using http:\/\/goo.gl\/m54Xg and some times it behaves weird. Shows wrong errors.","in_reply_to_user_id_str":"19757713","retweet_count":0,"id_str":"53836731203600384","entities":{"hashtags":[],"urls":[{"indices":[66,85],"url":"http:\/\/goo.gl\/m54Xg","expanded_url":null}],"user_mentions":[{"indices":[0,11],"id_str":"19757713","name":"Heiko Seeberger","screen_name":"hseeberger","id":19757713}]},"place":null,"source":"web","created_at":"Fri Apr 01 15:10:51 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":19757713,"in_reply_to_status_id":53832095381987328,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":"hseeberger","id":53836731203600384,"in_reply_to_status_id_str":"53832095381987328"},{"contributors":null,"retweeted":false,"text":"#Scala Based #OSGi Development with the #ScalaModules Project | Eclipse Live - http:\/\/goo.gl\/wdHXg #webinar","in_reply_to_user_id_str":null,"retweet_count":0,"id_str":"53560695345778688","entities":{"hashtags":[{"text":"Scala","indices":[0,6]},{"text":"OSGi","indices":[13,18]},{"text":"ScalaModules","indices":[40,53]},{"text":"webinar","indices":[99,107]}],"urls":[{"indices":[79,98],"url":"http:\/\/goo.gl\/wdHXg","expanded_url":null}],"user_mentions":[]},"place":null,"source":"web","created_at":"Thu Mar 31 20:53:59 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":null,"id":53560695345778688,"in_reply_to_status_id_str":null},{"contributors":null,"retweeted":false,"text":"The Little MongoDB Book - http:\/\/goo.gl\/u5RrJ by @karlseguin","in_reply_to_user_id_str":null,"retweet_count":0,"id_str":"53119112523689984","entities":{"hashtags":[],"urls":[{"indices":[26,45],"url":"http:\/\/goo.gl\/u5RrJ","expanded_url":null}],"user_mentions":[{"indices":[49,60],"id_str":"15930954","name":"karlseguin","screen_name":"karlseguin","id":15930954}]},"place":null,"source":"web","created_at":"Wed Mar 30 15:39:17 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":null,"id":53119112523689984,"in_reply_to_status_id_str":null},{"contributors":null,"retweeted":false,"text":"Google College Basketball 2011 - http:\/\/goo.gl\/qkReB","in_reply_to_user_id_str":null,"retweet_count":0,"id_str":"52778276770430976","entities":{"hashtags":[],"urls":[{"indices":[33,52],"url":"http:\/\/goo.gl\/qkReB","expanded_url":null}],"user_mentions":[]},"place":null,"source":"web","created_at":"Tue Mar 29 17:04:56 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":null,"id":52778276770430976,"in_reply_to_status_id_str":null},{"contributors":null,"retweeted":false,"text":"@bornmw Quotes (more often philosophical) help mind to look at things differently. They're fuel for new ideas espcily if taken as metaphor","in_reply_to_user_id_str":"34664626","retweet_count":0,"id_str":"52737743167561728","entities":{"hashtags":[],"urls":[],"user_mentions":[{"indices":[0,7],"id_str":"34664626","name":"\u0298leg Mikh\u1d49\u2091v","screen_name":"bornmw","id":34664626}]},"place":null,"source":"web","created_at":"Tue Mar 29 14:23:52 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":34664626,"in_reply_to_status_id":52732286717796352,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":"bornmw","id":52737743167561728,"in_reply_to_status_id_str":"52732286717796352"},{"contributors":null,"retweeted":false,"text":"\"Everything we hear is an opinion, not a fact. Everything we see is a perspective, not the truth.\" --Marcus Aurelius #quote via @joshkaufman","in_reply_to_user_id_str":null,"retweet_count":0,"id_str":"52731277786361859","entities":{"hashtags":[{"text":"quote","indices":[117,123]}],"urls":[],"user_mentions":[{"indices":[128,140],"id_str":"1689391","name":"Josh Kaufman","screen_name":"joshkaufman","id":1689391}]},"place":null,"source":"web","created_at":"Tue Mar 29 13:58:10 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":null,"id":52731277786361859,"in_reply_to_status_id_str":null},{"contributors":null,"retweeted":false,"text":"\"When your work speaks for itself, don't interrupt\" -- Henry J. Kaiser \/via @joshkaufman \/cc #in","in_reply_to_user_id_str":null,"retweet_count":0,"id_str":"52730609684066306","entities":{"hashtags":[{"text":"in","indices":[94,97]}],"urls":[],"user_mentions":[{"indices":[77,89],"id_str":"1689391","name":"Josh Kaufman","screen_name":"joshkaufman","id":1689391}]},"place":null,"source":"web","created_at":"Tue Mar 29 13:55:31 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":null,"id":52730609684066306,"in_reply_to_status_id_str":null},{"contributors":null,"retweeted":false,"text":"RT #GlassFish is Hiring in Prague - Installer Framework & Tools http:\/\/cot.ag\/i7hAgF \/cc #in","in_reply_to_user_id_str":null,"retweet_count":0,"id_str":"52729633296224256","entities":{"hashtags":[{"text":"GlassFish","indices":[3,13]},{"text":"in","indices":[89,92]}],"urls":[{"indices":[64,84],"url":"http:\/\/cot.ag\/i7hAgF","expanded_url":null}],"user_mentions":[]},"place":null,"source":"web","created_at":"Tue Mar 29 13:51:38 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":null,"id":52729633296224256,"in_reply_to_status_id_str":null},{"contributors":null,"retweeted":false,"text":"The Basho Blog: Riak and Scala at Yammer - http:\/\/goo.gl\/Oqfnr","in_reply_to_user_id_str":null,"retweet_count":0,"id_str":"52483375457505280","entities":{"hashtags":[],"urls":[{"indices":[43,62],"url":"http:\/\/goo.gl\/Oqfnr","expanded_url":null}],"user_mentions":[]},"place":null,"source":"web","created_at":"Mon Mar 28 21:33:06 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":null,"id":52483375457505280,"in_reply_to_status_id_str":null},{"contributors":null,"retweeted":false,"text":"@kbolgarov \u0430 \u0447\u0435\u0433\u043e, \u0443\u0436\u0435 \u0434\u043e \u0411\u043e\u0441\u0442\u043e\u043d\u0430 \u0434\u043e\u0448\u043b\u043e ?","in_reply_to_user_id_str":"9262432","retweet_count":0,"id_str":"52398493221924864","entities":{"hashtags":[],"urls":[],"user_mentions":[{"indices":[0,10],"id_str":"9262432","name":"Kirill Bolgarov","screen_name":"kbolgarov","id":9262432}]},"place":null,"source":"web","created_at":"Mon Mar 28 15:55:48 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":9262432,"in_reply_to_status_id":52388931882323969,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":"kbolgarov","id":52398493221924864,"in_reply_to_status_id_str":"52388931882323969"},{"contributors":null,"retweeted":false,"text":"@ErikSorensen here are tests samples http:\/\/goo.gl\/OpwAs http:\/\/goo.gl\/0L19m","in_reply_to_user_id_str":"16952995","retweet_count":0,"id_str":"52382164880326656","entities":{"hashtags":[],"urls":[{"indices":[37,56],"url":"http:\/\/goo.gl\/OpwAs","expanded_url":null},{"indices":[57,76],"url":"http:\/\/goo.gl\/0L19m","expanded_url":null}],"user_mentions":[{"indices":[0,13],"id_str":"16952995","name":"Erik Sorensen","screen_name":"ErikSorensen","id":16952995}]},"place":null,"source":"web","created_at":"Mon Mar 28 14:50:55 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":16952995,"in_reply_to_status_id":52378712968142848,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":"ErikSorensen","id":52382164880326656,"in_reply_to_status_id_str":"52378712968142848"},{"contributors":null,"retweeted":false,"text":"@bornmw and you are the next one LOL","in_reply_to_user_id_str":"34664626","retweet_count":0,"id_str":"52379400909488128","entities":{"hashtags":[],"urls":[],"user_mentions":[{"indices":[0,7],"id_str":"34664626","name":"\u0298leg Mikh\u1d49\u2091v","screen_name":"bornmw","id":34664626}]},"place":null,"source":"web","created_at":"Mon Mar 28 14:39:56 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":34664626,"in_reply_to_status_id":52350614511947777,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":"bornmw","id":52379400909488128,"in_reply_to_status_id_str":"52350614511947777"},{"contributors":null,"retweeted":false,"text":"@cDima not good :P","in_reply_to_user_id_str":"2675041","retweet_count":0,"id_str":"52215140443111424","entities":{"hashtags":[],"urls":[],"user_mentions":[{"indices":[0,6],"id_str":"2675041","name":"Dmitry Sadakov","screen_name":"cDima","id":2675041}]},"place":null,"source":"web","created_at":"Mon Mar 28 03:47:14 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":2675041,"in_reply_to_status_id":52128130483232768,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":"cDima","id":52215140443111424,"in_reply_to_status_id_str":"52128130483232768"},{"contributors":null,"retweeted":false,"text":"@bornmw really ? 15 year ago I even didn't know that the internet exist LOL.","in_reply_to_user_id_str":"34664626","retweet_count":0,"id_str":"51445732854607872","entities":{"hashtags":[],"urls":[],"user_mentions":[{"indices":[0,7],"id_str":"34664626","name":"\u0298leg Mikh\u1d49\u2091v","screen_name":"bornmw","id":34664626}]},"place":null,"source":"web","created_at":"Sat Mar 26 00:49:53 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":34664626,"in_reply_to_status_id":51397629044719617,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":"bornmw","id":51445732854607872,"in_reply_to_status_id_str":"51397629044719617"},{"contributors":null,"retweeted":false,"text":"RT @LinkedIn alright, we'll be streaming @suzeormanshow, live from LinkedIn HQ in a half hour http:\/\/lnkd.in\/speakers #inspeaker \/cc #in","in_reply_to_user_id_str":null,"retweet_count":0,"id_str":"51388983321702400","entities":{"hashtags":[{"text":"inspeaker","indices":[118,128]},{"text":"in","indices":[133,136]}],"urls":[{"indices":[94,117],"url":"http:\/\/lnkd.in\/speakers","expanded_url":null}],"user_mentions":[{"indices":[3,12],"id_str":"13058772","name":"LinkedIn","screen_name":"LinkedIn","id":13058772},{"indices":[41,55],"id_str":"26551204","name":"Suze Orman","screen_name":"SuzeOrmanShow","id":26551204}]},"place":null,"source":"web","created_at":"Fri Mar 25 21:04:23 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":null,"id":51388983321702400,"in_reply_to_status_id_str":null}]""".trim
  15. val t=new TypeToken[java.util.List[UserTimeline]](){}.getType()
  16. val statuses=(gson.fromJson(json,t):java.util.List[UserTimeline]).asScala
  17. mongoRepository.drop(UserTimeline.collectionName)
  18. statuses.foreach(o=> {mongoRepository.save(o); println(o._id)})
  19. }
  20. @Test
  21. def find(){
  22. val status=createStatus();
  23. val result=doFind(Map("id_str"->status.idStr),status)
  24. assertTrue(result.size>0)
  25. }
  26. @Test
  27. def find_No_result(){
  28. val status=createStatus();
  29. val result=doFind(Map("id_str"->"lala"),status)
  30. assertTrue(result.size==0)
  31. }
  32. @Test
  33. def fineOne(){
  34. val status=createStatus();
  35. val result=mongoRepository.findOne(new Query[UserTimeline](UserTimeline.collectionName,Map("id_str"->status.idStr),classOf[UserTimeline] ))
  36. assertTrue(result.toList.size==1)
  37. }
  38. @Test
  39. def fineOne_No_result(){
  40. val status=createStatus();
  41. val result=mongoRepository.findOne(new Query[UserTimeline](UserTimeline.collectionName,Map("id_str"->"lala"),classOf[UserTimeline] ))
  42. assertTrue(result==None)
  43. }
  44. private def doFind(queryObject:Map[String,AnyRef],status:UserTimeline)={
  45. mongoRepository.find(new Query[UserTimeline](UserTimeline.collectionName,queryObject,classOf[UserTimeline] ))
  46. }
  47. private def createStatus()={
  48. val statusJson="""{"contributors":null,"retweeted":false,"text":"#Scala Based #OSGi Development with the #ScalaModules Project | Eclipse Live - http:\/\/goo.gl\/wdHXg #webinar","in_reply_to_user_id_str":null,"retweet_count":0,"id_str":"53560695345778688","entities":{"hashtags":[{"text":"Scala","indices":[0,6]},{"text":"OSGi","indices":[13,18]},{"text":"ScalaModules","indices":[40,53]},{"text":"webinar","indices":[99,107]}],"urls":[{"indices":[79,98],"url":"http:\/\/goo.gl\/wdHXg","expanded_url":null}],"user_mentions":[]},"place":null,"source":"web","created_at":"Thu Mar 31 20:53:59 +0000 2011","truncated":false,"coordinates":null,"favorited":false,"geo":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"user":{"id_str":"14264704","id":14264704},"in_reply_to_screen_name":null,"id":53560695345778688,"in_reply_to_status_id_str":null}"""
  49. val status=gson.fromJson(statusJson,classOf[UserTimeline])
  50. mongoRepository.drop(UserTimeline.collectionName)
  51. mongoRepository.save(status)
  52. status
  53. }
  54. private def createQO(queryObject:Map[String,AnyRef],status:UserTimeline)={
  55. }
  56. }