-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOfferAcceptanceSpec.scala
106 lines (86 loc) · 3.55 KB
/
OfferAcceptanceSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package acceptance
import java.time.LocalDate
import java.util.UUID
import com.github.stevee.offers.api.{CreateOfferResponse, OfferResponse}
import org.scalatestplus.play._
import org.scalatestplus.play.guice._
import play.api.libs.json.Json
import play.api.test.Helpers._
import play.api.test._
class OfferAcceptanceSpec extends PlaySpec with GuiceOneAppPerTest with Injecting {
val requestBody =
"""
|{
|"price":"9.99",
|"currency": "GBP" ,
|"description":"iPhone",
|"start":"2020-02-20T00:00:00",
|"duration":"10 days"
|}
|""".stripMargin.trim
val createRequest = Json.parse(requestBody)
"OfferController POST" should {
"create an offer" in new WithApplication() {
val eventualResult = route(app, FakeRequest(POST, "/v1/offers")
.withJsonBody(createRequest)
.withHeaders("Content-Type" -> "application/json")
).get
contentAsString(eventualResult) must include(""""id":""")
status(eventualResult) mustBe CREATED
contentType(eventualResult) mustBe Some("application/json")
}
}
"OfferController GET" should {
"fails with 404 if the id is not a valid uuid" in new WithApplication() {
val eventualResult = route(app, FakeRequest(GET, "/v1/offers/notauuid")).get
status(eventualResult) mustBe NOT_FOUND
}
"fails with 404 if the id does not exist" in new WithApplication() {
val eventualResult = route(app, FakeRequest(GET, s"/v1/offers/${UUID.randomUUID()}")).get
status(eventualResult) mustBe NOT_FOUND
}
"finds a valid offer" in new WithApplication() {
val createResult = route(app, FakeRequest(POST, "/v1/offers")
.withJsonBody(createRequest)
.withHeaders("Content-Type" -> "application/json")
).get
val id = Json.fromJson[CreateOfferResponse](contentAsJson(createResult))
.getOrElse(fail("Could not parse response")).id
val eventualResult = route(app, FakeRequest(GET, s"/v1/offers/$id")).get
status(eventualResult) mustBe OK
val offer = contentAsJson(eventualResult).as[OfferResponse]
offer.id.toString mustBe id
offer.price mustBe BigDecimal("9.99")
offer.description mustBe "iPhone"
val expectedStartDate = LocalDate.of(2020, 2, 20)
offer.start.toLocalDate mustBe expectedStartDate
offer.end.toLocalDate mustBe expectedStartDate.plusDays(10)
}
"get an expired offer" in new WithApplication() {
val createResult = route(app, FakeRequest(POST, "/v1/offers")
.withJsonBody(createRequest)
.withHeaders("Content-Type" -> "application/json")
).get
val id = (contentAsJson(createResult) \ "id").get.as[String]
val cancelResult = route(app, FakeRequest(DELETE, s"/v1/offers/$id")).get
status(cancelResult) mustBe OK
val eventualResult = route(app, FakeRequest(GET, s"/v1/offers/$id")).get
status(eventualResult) mustBe GONE
}
}
"OfferController DELETE" should {
"fails with 404 if the id does not exist" in new WithApplication() {
val eventualResult = route(app, FakeRequest(DELETE, "/v1/offers/foo")).get
status(eventualResult) mustBe NOT_FOUND
}
"cancels an offer" in new WithApplication() {
val createResult = route(app, FakeRequest(POST, "/v1/offers")
.withJsonBody(createRequest)
.withHeaders("Content-Type" -> "application/json")
).get
val id = (contentAsJson(createResult) \ "id").get.as[String]
val cancelResult = route(app, FakeRequest(DELETE, s"/v1/offers/$id")).get
status(cancelResult) mustBe OK
}
}
}