Skip to content

Commit f33d9f9

Browse files
committed
update test
1 parent b66ece1 commit f33d9f9

1 file changed

Lines changed: 39 additions & 2 deletions

File tree

mobile-save-for-later/src/test/scala/com/gu/sfl/controller/SaveArticlesControllerSpec.scala

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package com.gu.sfl.controller
22

33
import com.gu.sfl.exception.SaveForLaterError
44
import com.gu.sfl.lambda.LambdaRequest
5-
import com.gu.sfl.model.SavedArticles
5+
import com.gu.sfl.lib.Jackson._
6+
import com.gu.sfl.model.{SavedArticle, SavedArticles, SavedArticlesResponse}
67
import com.gu.sfl.savedarticles.UpdateSavedArticles
78
import com.gu.sfl.util.StatusCodes
89
import org.specs2.mock.Mockito
910
import org.specs2.mutable.Specification
1011
import org.specs2.specification.Scope
1112

13+
import java.time.LocalDateTime
1214
import scala.concurrent.duration.Duration
1315
import scala.concurrent.{Await, ExecutionContext, Future}
1416

@@ -37,7 +39,11 @@ class SaveArticlesControllerSpec extends Specification with Mockito {
3739
response.statusCode mustEqual StatusCodes.badRequest
3840
}
3941

40-
/* TEMPORARY: the following test was added while we temporarily accept java-default-format fallback while Android fixes their bug in sending the incorrect format. Remove them once the ISO format is accepted again. This test exists only to document and pin down the temporary fallback behaviour, and MUST fail once that fallback is gone.
42+
/* ==================== TEMPORARY: start of java-default-format fallback tests ====================
43+
* The 3 tests below were added while we temporarily accept the java-default-format fallback
44+
* while Android fixes their bug in sending the incorrect format. These tests exist only to
45+
* document and pin down the temporary fallback behaviour, and MUST fail (or get removed) once
46+
* that fallback is gone.
4147
*/
4248
"save the articles when the date is in the java default format" in new Setup {
4349
val javaDefaultDateTimeString = "Fri Jan 01 00:00:01 GMT 2010"
@@ -48,6 +54,37 @@ class SaveArticlesControllerSpec extends Specification with Mockito {
4854

4955
there was one(updateSavedArticles).save(any[Map[String, String]](), any[SavedArticles]())
5056
}
57+
58+
"save the java default format date to the db as the correct parsed LocalDateTime" in new Setup {
59+
val javaDefaultDateTimeString = "Fri Jan 01 00:00:01 GMT 2010"
60+
val expectedDateTime = LocalDateTime.of(2010, 1, 1, 0, 0, 1)
61+
val json = s"""{"version":"1","articles":[{"id":"id/1","shortUrl":"p/1","date": "$javaDefaultDateTimeString","read":false}]}"""
62+
63+
val savedArticlesCaptor = capture[SavedArticles]
64+
updateSavedArticles.save(any[Map[String, String]](), savedArticlesCaptor) returns Future.successful(Left(mock[SaveForLaterError]))
65+
66+
Await.result(controller(LambdaRequest(Some(json))), Duration.Inf)
67+
68+
savedArticlesCaptor.value.articles.head.date mustEqual expectedDateTime
69+
}
70+
71+
"return the java default format date in the API response as an ISO SavedArticle" in new Setup {
72+
val javaDefaultDateTimeString = "Fri Jan 01 00:00:01 GMT 2010"
73+
val expectedArticle = SavedArticle("id/1", "p/1", LocalDateTime.of(2010, 1, 1, 0, 0, 1), read = false)
74+
val json = s"""{"version":"1","articles":[{"id":"id/1","shortUrl":"p/1","date": "$javaDefaultDateTimeString","read":false}]}"""
75+
76+
updateSavedArticles.save(any[Map[String, String]](), any[SavedArticles]()) answers {
77+
(_: Any) match {
78+
case Array(_, savedArticles: SavedArticles) => Future.successful(Right(savedArticles))
79+
}
80+
}
81+
82+
val response = Await.result(controller(LambdaRequest(Some(json))), Duration.Inf)
83+
84+
val parsedResponse = mapper.readValue[SavedArticlesResponse](response.maybeBody.get)
85+
parsedResponse.savedArticles.articles mustEqual List(expectedArticle)
86+
}
87+
/* ==================== TEMPORARY: end of java-default-format fallback tests ==================== */
5188
}
5289

5390
trait Setup extends Scope {

0 commit comments

Comments
 (0)