1717package connectors
1818
1919import base .{SpecBase , WireMockServerHandler }
20- import cats .syntax .either .*
21- import models .InternalIssueError
2220import models .btn .*
2321import org .scalacheck .Arbitrary .arbitrary
2422import org .scalacheck .Gen
@@ -29,7 +27,7 @@ import play.api.inject.guice.GuiceApplicationBuilder
2927import play .api .libs .json .*
3028import uk .gov .hmrc .http .test .WireMockSupport
3129
32- import java .time .{ LocalDate , ZonedDateTime }
30+ import java .time .LocalDate
3331
3432class BTNConnectorSpec extends SpecBase with WireMockSupport with WireMockServerHandler with EitherValues with ScalaCheckDrivenPropertyChecks {
3533 override lazy val app : Application = new GuiceApplicationBuilder ()
@@ -41,23 +39,22 @@ class BTNConnectorSpec extends SpecBase with WireMockSupport with WireMockServer
4139 accountingPeriodFrom = LocalDate .now.minusYears(1 ),
4240 accountingPeriodTo = LocalDate .now
4341 )
44- val rawProcessedDateTime : String = " 2022-01-31T09:26:17Z"
45- val stubProcessedDateTime : ZonedDateTime = ZonedDateTime .parse(rawProcessedDateTime)
46- val successfulBTNResponseBody : JsObject = Json .obj(" processingDate" -> rawProcessedDateTime)
47- val successfulBtnResponse : BtnResponse = BtnResponse (BtnSuccess (stubProcessedDateTime).asRight, CREATED )
48- val accountingPeriodFromDateMinus1Year : LocalDate = LocalDate .now.minusYears(1 )
49- val accountingPeriodToDateNow : LocalDate = LocalDate .now
50- val btnRequestDatesMinus1YearAndNow : BTNRequest = BTNRequest (accountingPeriodFromDateMinus1Year, accountingPeriodToDateNow)
42+ val rawProcessedDateTime : String = " 2022-01-31T09:26:17Z"
43+ val successfulBTNResponseBody : JsObject = Json .obj(" processingDate" -> rawProcessedDateTime)
44+ val accountingPeriodFromDateMinus1Year : LocalDate = LocalDate .now.minusYears(1 )
45+ val accountingPeriodToDateNow : LocalDate = LocalDate .now
46+ val btnRequestDatesMinus1YearAndNow : BTNRequest = BTNRequest (accountingPeriodFromDateMinus1Year, accountingPeriodToDateNow)
5147
5248 " submit BTN connector" should {
53- " return a BtnSuccess when the pillar-2 backend has returned status 201." in {
49+ " return the response when the pillar-2 backend has returned status 201." in {
5450 given pillar2Id : String = " XEPLR0000000000"
5551 stubResponse(submitBTNPath, CREATED , successfulBTNResponseBody.toString())
5652 val result = connector.submitBTN(btnRequestDatesMinus1YearAndNow).futureValue
57- result mustBe successfulBtnResponse
53+ result.status mustBe CREATED
54+ result.body mustBe successfulBTNResponseBody.toString()
5855 }
5956
60- " surface the failure details when dealing with a modelled 400 or 500 error" in forAll(
57+ " return the response when dealing with a 400 or 500 error" in forAll(
6158 Gen .oneOf(BAD_REQUEST , INTERNAL_SERVER_ERROR ),
6259 arbitrary[String ],
6360 arbitrary[String ]
@@ -69,13 +66,11 @@ class BTNConnectorSpec extends SpecBase with WireMockSupport with WireMockServer
6966 given pillar2Id : String = " XEPLR0000000000"
7067 stubResponse(submitBTNPath, httpStatusCode, jsonResponse.toString())
7168 val result = connector.submitBTN(btnRequestDatesMinus1YearAndNow).futureValue
72- result mustBe BtnResponse (
73- BtnError (errorCode, errorMessage).asLeft,
74- httpStatusCode
75- )
69+ result.status mustBe httpStatusCode
70+ result.body mustBe jsonResponse.toString()
7671 }
7772
78- " surface the failure details when dealing with a modelled 422 error" in forAll { (errorCode : String , errorMessage : String ) =>
73+ " return the response when dealing with a 422 error" in forAll { (errorCode : String , errorMessage : String ) =>
7974 val jsonResponse = Json .obj(
8075 " processingDate" -> rawProcessedDateTime,
8176 " code" -> errorCode,
@@ -84,26 +79,17 @@ class BTNConnectorSpec extends SpecBase with WireMockSupport with WireMockServer
8479 given pillar2Id : String = " XEPLR0000000000"
8580 stubResponse(submitBTNPath, UNPROCESSABLE_ENTITY , jsonResponse.toString())
8681 val result = connector.submitBTN(btnRequestDatesMinus1YearAndNow).futureValue
87- result mustBe BtnResponse (
88- BtnError (errorCode, errorMessage).asLeft,
89- UNPROCESSABLE_ENTITY
90- )
82+ result.status mustBe UNPROCESSABLE_ENTITY
83+ result.body mustBe jsonResponse.toString()
9184 }
9285
93- " raise an Exception when the expected response field-value-checking fails." in {
94- given pillar2Id : String = " XEPLR9999999999"
95- stubResponse(submitBTNPath, CREATED , Json .obj().toString())
96- val result = connector.submitBTN(btnRequestDatesMinus1YearAndNow).failed.futureValue
97- result mustBe JsResultException (Seq (((__ \ " processingDate" ), Seq (JsonValidationError (Seq (" error.path.missing" ))))))
98- }
99-
100- " return InternalIssueError when the pillar-2 backend returns any unsupported status." in forAll(
86+ " return the response even when the pillar-2 backend returns any unsupported status." in forAll(
10187 Gen .posNum[Int ].retryUntil(code => ! Seq (CREATED , BAD_REQUEST , UNPROCESSABLE_ENTITY , INTERNAL_SERVER_ERROR ).contains(code))
10288 ) { (httpStatus : Int ) =>
10389 given pillar2Id : String = " XEPLR4000000000"
10490 stubResponse(submitBTNPath, httpStatus, successfulBTNResponseBody.toString())
105- val result = connector.submitBTN(btnRequestDatesMinus1YearAndNow)
106- result.failed.futureValue mustBe InternalIssueError
91+ val result = connector.submitBTN(btnRequestDatesMinus1YearAndNow).futureValue
92+ result.status mustBe httpStatus
10793 }
10894 }
10995}
0 commit comments