Advertisement
mitrakov

Unit Test with Injecting

Jul 4th, 2018
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.56 KB | None | 0 0
  1. package ru.mitrakov.test.controllers
  2.  
  3. import scala.concurrent.duration._
  4. import scala.concurrent.ExecutionContext.Implicits.global
  5. import scala.concurrent.Future
  6. import scala.language.postfixOps
  7.  
  8. import akka.util.Timeout
  9. import play.api.http.Status._
  10. import play.api.libs.ws.WSClient
  11. import play.api.mvc.{AnyContent, ControllerComponents, Headers}
  12. import play.api.test.{FakeRequest, Helpers, Injecting}
  13.  
  14. import org.mockito.Mockito._
  15. import org.scalatest.mockito.MockitoSugar
  16. import org.scalatestplus.play.guice.GuiceOneAppPerSuite
  17. import org.scalatestplus.play.PlaySpec
  18.  
  19. import co.tala.kycqueue.helpers.{KycAttemptHelper, KycAttempts}
  20. import co.tala.kycqueue.model.PersonInfo
  21. import co.tala.kycqueue.services.{Person, ServerApiClient, TokenAuthenticationService}
  22.  
  23.  
  24. /**
  25.   * Class for testing KycAjaxController
  26.   * @author Artem Mitrakov (amitrakov@tala.co)
  27.   */
  28. class KycAjaxControllerSpec extends PlaySpec with GuiceOneAppPerSuite with Injecting with MockitoSugar {
  29.   /** arbitrary person ID */
  30.   private val personId = 357
  31.  
  32.   /** implicit timeout for Helpers methods */
  33.   private implicit val timeout: Timeout = Timeout(1 second)
  34.  
  35.   // create a Controller, substituting KycAttemptHelper and ServerApiClient with corresponding Mocks
  36.   private val ws = inject[WSClient]
  37.   private val cc = inject[ControllerComponents]
  38.   private val authService = inject[TokenAuthenticationService]
  39.   private val kycHelper = mock[KycAttemptHelper]
  40.   private val serverHelper = mock[ServerApiClient]
  41.   private val controller = new KycAjaxController(ws, authService, kycHelper, serverHelper, cc)
  42.  
  43.   // set up our Mocks to return fake data instead of requesting actual DB
  44.   when(serverHelper.getPerson(personId))  thenReturn Future.successful{Some(Person("Tom Brown", "Tom", "1989-12-07"))}
  45.   when(kycHelper.getKycAttempt(personId)) thenReturn Future.successful{Some(KycAttempts(3, 9, Nil, Seq(3, 3, 4)))}
  46.   when(serverHelper.getSmsTemplates)      thenReturn Future.successful{Map("438" -> "Verified", "439" -> "Blur image")}
  47.   when(serverHelper.getPerson(0))         thenReturn Future.successful{None}
  48.   when(kycHelper.getKycAttempt(0))        thenReturn Future.successful{None}
  49.  
  50.   "KycAjaxController#getPersonInfo" when {
  51.     "request received with EXISTING personId" should {
  52.       "return an actual result" in {
  53.         val request = FakeRequest(Helpers.GET, s"/api/v1/person/$personId/info", Headers(), AnyContent())
  54.         val result = controller.getPersonInfo(personId).apply(request)
  55.  
  56.         val status = Helpers.status(result)
  57.         status mustBe OK
  58.  
  59.         val info = Helpers.contentAsJson(result).as[PersonInfo]
  60.         info.name mustBe "Tom Brown"
  61.         info.birthday mustBe "1989-12-07"
  62.         info.status mustBe 3
  63.         info.identificationTypeId mustBe 9
  64.         info.sms must have size 2
  65.         info.sms must contain key "438"
  66.         info.sms must contain key "439"
  67.       }
  68.     }
  69.  
  70.     "request received with NON-EXISTING personId" should {
  71.       "return result with N/A parameters" in {
  72.         val request = FakeRequest(Helpers.GET, s"/api/v1/person/0/info", Headers(), AnyContent())
  73.         val result = controller.getPersonInfo(0).apply(request)
  74.  
  75.         val status = Helpers.status(result)
  76.         status mustBe OK
  77.  
  78.         val info = Helpers.contentAsJson(result).as[PersonInfo]
  79.         info.name mustBe "N/A"
  80.         info.birthday mustBe "N/A"
  81.         info.status mustBe 0
  82.         info.identificationTypeId mustBe 0
  83.         info.sms must have size 2
  84.         info.sms must contain key "438"
  85.         info.sms must contain key "439"
  86.       }
  87.     }
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement