package com.eddsteel.posts.leastpower
package endpoints
import model._
import services._
import webapp._
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito.{when, verify, RETURNS_SMART_NULLS}
import scala.concurrent.{Future, ExecutionContext}
import java.time.{ZoneOffset, LocalDate, ZonedDateTime, ZoneId}
class EndpointTestsV1 extends FlatSpec with Matchers with MockitoSugar with ScalaFutures {
import ExecutionContext.Implicits.global
val mockUserService = mock[UserService](RETURNS_SMART_NULLS)
val mockGiftService = mock[GiftService](RETURNS_SMART_NULLS)
val controller = new Controller with EndpointsV1 with UserServiceComponent with GiftServiceComponent {
override val userService = mockUserService
override val giftService = mockGiftService
override def route(method: Method, path: String): Action = postGift
}
"POST /users/:id/gift" should "order a gift for a user on their birthday" in {
DateTimeUtils.setDateTime(ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC))
when(mockUserService.getById(1L)).thenReturn(
Future.successful(Some(
User(id = 1, birthday = LocalDate.of(1980, 1, 1), favoriteThing = "booze",
timezone = ZoneOffset.UTC, pets = Set.empty, whichFriendAreYou = Friends.Phoebe))))
when(mockGiftService.order(1L, "booze")).thenReturn(
Future.successful(
("booze ordered for user 1", Some(1L))))
val response = WebApp.postFakeRequest(controller, POST, "/users/1/gift", "")
response.futureValue.shouldEqual("(booze ordered for user 1,Some(1))")
verify(mockUserService).getById(1L)
verify(mockGiftService).order(1L, "booze") }
}