-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added user service and serialization tests
- Loading branch information
Showing
4 changed files
with
102 additions
and
27 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 18 additions & 2 deletions
20
src/main/scala/com/tboonx/github/brocounting/model/Model.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
package com.tboonx.github.brocounting.model | ||
|
||
import com.novus.salat.EnumStrategy | ||
import com.novus.salat.annotations._ | ||
/** | ||
* Created by dhaeb on 31.08.14. | ||
*/ | ||
|
||
case class User(user_name: String, user_password: String) | ||
case class Session(response: String) | ||
object ModelConstants { | ||
@EnumAs(strategy = EnumStrategy.BY_ID) | ||
object AccountKind extends Enumeration { | ||
type AccountKind = Value | ||
val PAYPAL, CASH, GIRO_ACOUNT = Value | ||
} | ||
} | ||
|
||
import ModelConstants.AccountKind._ | ||
import org.joda.time.DateTime | ||
|
||
case class User(@Key("_id") name: String, password: String, tags : List[Tag], session : Option[Session]) | ||
case class Transaction(agent : String, account : String, amount : BigDecimal, date : DateTime, tags : List[Tag], note : String) | ||
case class Account(@Key("_id") bic : String, name : String, balance : BigDecimal, kind : AccountKind, miscellaneous : Option[Map[String, String]]) | ||
case class Session(@Key("_id") hash: String) | ||
case class Tag(@Key("_id") name : String, icon : Array[Byte], enabled : Boolean) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.github.tboonx | ||
|
||
import com.mongodb.casbah.Imports | ||
import com.novus.salat._ | ||
import com.novus.salat.global._ | ||
import com.novus.salat.annotations._ | ||
import com.tboonx.github.brocounting.model._ | ||
import org.json4s._ | ||
import org.json4s.jackson.JsonMethods | ||
import org.scalatest.FunSpec | ||
|
||
/** | ||
* Created by dhaeb on 31.08.14. | ||
*/ | ||
class UserSpec extends FunSpec { | ||
|
||
protected implicit val jsonFormats: Formats = DefaultFormats | ||
|
||
describe("A user instance") { | ||
|
||
val parsedInput: JValue = JsonMethods.parse("{\"name\":\"dab\",\"password\":\"123456\", \"session\" : {\"hash\" : \"asdf\"}}", true) | ||
|
||
it("should be compilable to json without sepecifying an session hash"){ | ||
val fixtureWithoutHash = parsedInput.removeField( (value : JField) => { | ||
"session".equals(value._1) | ||
}) | ||
val user: User = Extraction.extract[User](fixtureWithoutHash) | ||
assert("dab" === user.name) | ||
assert("123456" === user.password) | ||
assert(List() === user.tags) | ||
assert(user.session.isInstanceOf[Option[Session]]) | ||
} | ||
|
||
it("should be serializable to a mongo object without session and tags"){ | ||
val user : User = new User("dab", "123456", List(), Option[Session](null)) | ||
val expected = "{ \"_id\" : \"dab\" , \"password\" : \"123456\" , \"tags\" : [ ]}" | ||
assert(expected === grater[User].asDBObject(user).toString) | ||
} | ||
|
||
it("should be compilable to json using scalatra-json") { | ||
val user: User = Extraction.extract[User](parsedInput) | ||
assert(user.name === "dab") | ||
assert(user.password === "123456") | ||
assert(user.tags === List()) | ||
assert(user.session.get === new Session("asdf")) | ||
} | ||
|
||
it("should be serializable to a mongo db object using salat"){ | ||
val user : User = new User("dan", "123456", List(new Tag("eat", Array(), true), new Tag("sleep", Array(), false), new Tag("code", Array(), false)), Option(new Session("myRandomlyGeneratedSessionHash"))) | ||
val userDbObject: Imports.DBObject = grater[User].asDBObject(user) | ||
val expected = "{ \"_id\" : \"dan\" , \"password\" : \"123456\" , \"tags\" : [ { \"_id\" : \"eat\" , \"icon\" : <Binary Data> , \"enabled\" : true} , { \"_id\" : \"sleep\" , \"icon\" : <Binary Data> , \"enabled\" : false} , { \"_id\" : \"code\" , \"icon\" : <Binary Data> , \"enabled\" : false}] , \"session\" : { \"_id\" : \"myRandomlyGeneratedSessionHash\"}}" | ||
assert(expected === userDbObject.toString) | ||
} | ||
|
||
} | ||
} | ||
|