PageRenderTime 2874ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/scala/ApplicationProcessorSpec.scala

https://gitlab.com/zhaosheng/snagajob
Scala | 111 lines | 93 code | 17 blank | 1 comment | 0 complexity | b9b042054d7c542c2cecfb896c9e650e MD5 | raw file
  1. import com.google.inject.{AbstractModule, Guice}
  2. import com.google.inject.name.Names
  3. import main.scala._
  4. import net.codingwell.scalaguice.ScalaModule
  5. import org.scalatest._
  6. class ApplicationProcessorSpec extends FlatSpec with Matchers {
  7. val injector = Guice.createInjector(new ApplicationProcessorTestModule())
  8. import net.codingwell.scalaguice.InjectorExtensions._
  9. val jqp = injector.instance[JsonQuestionProvider]
  10. val ap = injector.instance[ApplicationProcessor]
  11. "Empty application list" should "return empty list application" in {
  12. val applications = JsonApplicationProvider(
  13. """ [ ]"""
  14. ).getApplications
  15. val result = ap.acceptList(applications)
  16. result.get.size should be (0)
  17. }
  18. "List of one valid application" should "return one valid application" in {
  19. val applications = JsonApplicationProvider(
  20. """ [ { "aid" : "ABC", "answeredQuestions": [{ "qid" : 1, "answer": "world" }, { "qid" : 2, "answer": "lazy" } ] } ]"""
  21. ).getApplications
  22. val result = ap.acceptList(applications)
  23. result.get.size should be (1)
  24. result.get.head.aid should be ("ABC")
  25. }
  26. "List of one invalid application" should "return zero application" in {
  27. val applications = JsonApplicationProvider(
  28. """ [ { "aid" : "ABC", "answeredQuestions": [{ "qid" : 1, "answer": "world" }, { "qid" : 2, "answer": "lazzy" } ] } ]"""
  29. ).getApplications
  30. val result = ap.acceptList(applications)
  31. result.get.size should be (0)
  32. }
  33. "List of all valid applications" should "return all applications" in {
  34. val applications = JsonApplicationProvider(
  35. """ [
  36. | { "aid" : "ABC1", "answeredQuestions": [{ "qid" : 1, "answer": "world" }, { "qid" : 2, "answer": "lazy" } ] },
  37. | { "aid" : "ABC2", "answeredQuestions": [{ "qid" : 1, "answer": "world" }, { "qid" : 2, "answer": "lazy" } ] }
  38. | { "aid" : "ABC3", "answeredQuestions": [{ "qid" : 1, "answer": "world" }, { "qid" : 2, "answer": "lazy" } ] }
  39. | { "aid" : "ABC4", "answeredQuestions": [{ "qid" : 1, "answer": "world" }, { "qid" : 2, "answer": "lazy" } ] }
  40. |]""".stripMargin
  41. ).getApplications
  42. val result = ap.acceptList(applications)
  43. result.get.size should be (4)
  44. }
  45. "List of all invalid applications" should "return zero application" in {
  46. val applications = JsonApplicationProvider(
  47. """ [
  48. | { "aid" : "ABC1", "answeredQuestions": [{ "qid" : 1, "answer": "world1" }, { "qid" : 2, "answer": "lazy" } ] },
  49. | { "aid" : "ABC2", "answeredQuestions": [{ "qid" : 1, "answer": "world1" }, { "qid" : 2, "answer": "lazy" } ] }
  50. | { "aid" : "ABC3", "answeredQuestions": [{ "qid" : 1, "answer": "world1" }, { "qid" : 2, "answer": "lazy" } ] }
  51. | { "aid" : "ABC4", "answeredQuestions": [{ "qid" : 1, "answer": "world1" }, { "qid" : 2, "answer": "lazy" } ] }
  52. |]""".stripMargin
  53. ).getApplications
  54. val result = ap.acceptList(applications)
  55. result.get.size should be (0)
  56. }
  57. "List of some invalid appliations and one valid application" should "return one application" in {
  58. val applications = JsonApplicationProvider(
  59. """ [
  60. | { "aid" : "ABC1", "answeredQuestions": [{ "qid" : 1, "answer": "world" }, { "qid" : 2, "answer": "lazzy" } ] },
  61. | { "aid" : "ABC2", "answeredQuestions": [{ "qid" : 1, "answer": "world1" }, { "qid" : 2, "answer": "lazzy" } ] }
  62. | { "aid" : "ABC3", "answeredQuestions": [{ "qid" : 1, "answer": "world" }, { "qid" : 2, "answer": "lazy" } ] }
  63. | { "aid" : "ABC4", "answeredQuestions": [{ "qid" : 1, "answer": "1world" }, { "qid" : 2, "answer": "lazy" } ] }
  64. |]""".stripMargin
  65. ).getApplications
  66. val result = ap.acceptList(applications)
  67. result.get.size should be (1)
  68. }
  69. "Error application input " should "return a Failure" in {
  70. val applications = JsonApplicationProvider(
  71. """ [
  72. | { "aid" : 1"ABC1", "answeredQuestions": [{ "qid" : 1, "answer": "world" }, { "qid" : 2, "answer": "lazzy" } ] }
  73. |]""".stripMargin
  74. ).getApplications
  75. val result = ap.acceptList(applications)
  76. result.getClass.getSimpleName should be ("Failure")
  77. }
  78. }
  79. class ApplicationProcessorTestModule extends AbstractModule with ScalaModule{
  80. def configure(): Unit = {
  81. bind[ApplicationProcessor].to[ApplicationProcessorImpl]
  82. bind[QuestionProvider].to[JsonQuestionProvider]
  83. // One example of instance binding
  84. bind[String].annotatedWith(Names.named("Question JSON String"))
  85. .toInstance(
  86. """ [
  87. |{ "qid" : 1, "question": "hello", "answer": ["world"] },
  88. |{ "qid" : 2, "question": "The quick brown fox", "answer": ["jumps", "over", "the", "lazy", "dog"] },
  89. |{ "qid" : 3, "question": "foo", "answer": ["bar"] },
  90. |{ "qid" : 4, "question": "Do you have a car?", "answer": ["Yes"] }
  91. |] """.stripMargin
  92. )
  93. }
  94. }