Skip to content

Commit

Permalink
Add decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMMiller committed Dec 9, 2024
1 parent 16b01fa commit 0df1fe8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,36 @@ import uk.gov.hmrc.pillar2stubs.utils.ResourceHelper.resourceAsString
import uk.gov.hmrc.play.bootstrap.backend.controller.BackendController

import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.Future
import java.time.ZonedDateTime
import java.time.ZoneOffset
import play.api.mvc.Result
import uk.gov.hmrc.pillar2stubs.models.UKTRSubmissionRequest

@Singleton
class UkTaxReturnController @Inject() (
cc: ControllerComponents,
authFilter: AuthActionFilter
)(implicit ec: ExecutionContext)
extends BackendController(cc)
cc: ControllerComponents,
authFilter: AuthActionFilter
) extends BackendController(cc)
with Logging {

def submitUktr: Action[String] = (Action(parse.tolerantText) andThen authFilter).async { implicit request =>
def submitUktr: Action[UKTRSubmissionRequest] = (Action(parse.json[UKTRSubmissionRequest]) andThen authFilter).async { implicit request =>
request.headers.get("X-Pillar2-Id") match {
case None =>
case None =>
logger.warn("No PLR Reference provided in headers")
returnErrorResponse("uktaxreturn/MissingPLRResponse.json")
case Some(plrReference) => handleSubmission(plrReference, request.body)

case Some(plrReference) => handleSubmission(plrReference)
}
}

private def handleSubmission(plrReference: String, body: String): Future[Result] = {
private def handleSubmission(plrReference: String): Future[Result] =
plrReference match {
case "XTC01234123412" => returnSuccessResponse("uktaxreturn/SuccessResponse.json")
case "XTC01234123412" => returnSuccessResponse("uktaxreturn/SuccessResponse.json")
case "XEPLR1066196400" => returnErrorResponse("uktaxreturn/InvalidRequestResponse.json")
case _ =>
case _ =>
returnSuccessResponse("uktaxreturn/SuccessResponse.json")
}
}

private def returnSuccessResponse(filename: String): Future[Result] = {
val response = resourceAsString(s"/resources/$filename")
Expand All @@ -75,7 +74,7 @@ class UkTaxReturnController @Inject() (
Future.successful(BadRequest(response))
}

private def getCurrentTimestamp: String =
private def getCurrentTimestamp: String =
ZonedDateTime.now(ZoneOffset.UTC).toString

private def replaceDate(response: String, newDate: String): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ import org.scalatest.matchers.should.Matchers
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.inject.bind
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.Json
import play.api.test.FakeRequest
import play.api.test.Helpers._
import uk.gov.hmrc.http.HeaderNames
import play.api.libs.json.Json

class UkTaxReturnControllerSpec extends AnyFreeSpec with Matchers with GuiceOneAppPerSuite with OptionValues {

private val stubResourceLoader: String => Option[String] = {
case "/resources/uktaxreturn/SuccessResponse.json" =>
Some("""{"success":{"processingDate":"2022-01-31T09:26:17Z","formBundleNumber":"119000004320","chargeReference":"XTC01234123412"}}""")
case "/resources/uktaxreturn/InvalidRequestResponse.json" =>
Some("""{"error":{"code":"400","message":"Invalid JSON message content used; Message: \\"Expected a ',' or '}' at character 93 of {...","logID":"C0000AB8190C86300000000200006836"}}""")
Some(
"""{"error":{"code":"400","message":"Invalid JSON message content used; Message: \\"Expected a ',' or '}' at character 93 of {...","logID":"C0000AB8190C86300000000200006836"}}"""
)
case "/resources/uktaxreturn/MissingPLRResponse.json" =>
Some("""{"errors":{"processingDate":"2022-01-31T09:26:17Z","code":"002","text":"Pillar 2 ID missing or invalid"}}""")
case _ => None
Expand All @@ -48,12 +50,39 @@ class UkTaxReturnControllerSpec extends AnyFreeSpec with Matchers with GuiceOneA

val authHeader: (String, String) = HeaderNames.authorisation -> "Bearer valid_token"

private val validRequestBody = Json.obj(
"accountingPeriodFrom" -> "2024-01-01",
"accountingPeriodTo" -> "2024-12-31",
"obligationMTT" -> true,
"electionUKGAAP" -> true,
"liabilities" -> Json.obj(
"totalLiability" -> Some(BigDecimal("10000.99")),
"totalLiabilityDTT" -> Some(BigDecimal("5000.99")),
"totalLiabilityIIR" -> Some(BigDecimal("4000")),
"totalLiabilityUTPR" -> Some(BigDecimal("10000.99")),
"liableEntities" -> Some(
Seq(
Json.obj(
"ukChargeableEntityName" -> "Newco PLC",
"idType" -> "CRN",
"idValue" -> "12345678",
"amountOwedDTT" -> BigDecimal("5000"),
"electedDTT" -> true,
"amountOwedIIR" -> BigDecimal("3400"),
"amountOwedUTPR" -> BigDecimal("6000.5"),
"electedUTPR" -> true
)
)
)
)
)

"UkTaxReturnController POST" - {

"return CREATED with success response for a valid submission with XTC01234123412" in {
val request = FakeRequest(POST, "/submit-uk-tax-return")
.withHeaders("X-Pillar2-Id" -> "XTC01234123412", "Content-Type" -> "application/json", authHeader)
.withBody("{}")
.withBody(validRequestBody)

val result = route(app, request).value
status(result) mustBe CREATED
Expand All @@ -63,7 +92,7 @@ class UkTaxReturnControllerSpec extends AnyFreeSpec with Matchers with GuiceOneA
"return BAD_REQUEST with invalid request response for XEPLR1066196400" in {
val request = FakeRequest(POST, "/submit-uk-tax-return")
.withHeaders("X-Pillar2-Id" -> "XEPLR1066196400", "Content-Type" -> "application/json", authHeader)
.withBody("{}")
.withBody(validRequestBody)

val result = route(app, request).value
status(result) mustBe BAD_REQUEST
Expand All @@ -73,7 +102,7 @@ class UkTaxReturnControllerSpec extends AnyFreeSpec with Matchers with GuiceOneA
"return BAD_REQUEST when X-Pillar2-Id header is missing" in {
val request = FakeRequest(POST, "/submit-uk-tax-return")
.withHeaders("Content-Type" -> "application/json", authHeader)
.withBody("{}")
.withBody(validRequestBody)

val result = route(app, request).value
status(result) mustBe BAD_REQUEST
Expand All @@ -83,7 +112,7 @@ class UkTaxReturnControllerSpec extends AnyFreeSpec with Matchers with GuiceOneA
"return CREATED with success response for any other PLR reference" in {
val request = FakeRequest(POST, "/submit-uk-tax-return")
.withHeaders("X-Pillar2-Id" -> "OTHER_PLR", "Content-Type" -> "application/json", authHeader)
.withBody("{}")
.withBody(validRequestBody)

val result = route(app, request).value
status(result) mustBe CREATED
Expand Down

0 comments on commit 0df1fe8

Please sign in to comment.