Skip to content

Commit

Permalink
PILLAR2-Stub: Simplify stub to provide static responses based on Pill…
Browse files Browse the repository at this point in the history
…ar2 reference headers
  • Loading branch information
JamesMMiller committed Dec 9, 2024
1 parent 93cbd78 commit 16b01fa
Show file tree
Hide file tree
Showing 15 changed files with 6,766 additions and 33 deletions.
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ HTTP 503 Request Could not be processed Error
}
}
}
```

> Response status: 503
Expand Down Expand Up @@ -346,6 +345,66 @@ HTTP 404 Record Not Found Error
---

```
POST /submit-uk-tax-return
```

Submit a UK Tax Return request. This endpoint requires an `X-Pillar2-Id` header.

#### Request Headers:
- `X-Pillar2-Id`: The Pillar 2 identifier (Required)
- `Content-Type`: application/json
- `Authorization`: Bearer token

#### Response Scenarios:

1. **Success Response (PLR Reference: XTC01234123412 or any other valid reference)**
```json
{
"success": {
"processingDate": "2024-12-09T09:03:58.100142Z",
"formBundleNumber": "119000004320",
"chargeReference": "XTC01234123412"
}
}
```
> Response status: 201 (Created)
2. **Invalid Request Response (PLR Reference: XEPLR1066196400)**
```json
{
"error": {
"code": "400",
"message": "Invalid JSON message content used; Message: \"Expected a ',' or '}' at character 93 of {...",
"logID": "C0000AB8190C86300000000200006836"
}
}
```
> Response status: 400 (Bad Request)
3. **Missing PLR Reference Response**
```json
{
"errors": {
"processingDate": "2024-12-09T09:03:58.094225Z",
"code": "002",
"text": "Pillar 2 ID missing or invalid"
}
}
```
> Response status: 400 (Bad Request)
#### Pillar2 Reference Patterns

| Pillar2 Reference | Response Status | Description |
|------------------|-----------------|-------------|
| XTC01234123412 | 201 (Created) | Returns a success response with matching charge reference |
| XEPLR1066196400 | 400 (Bad Request) | Returns invalid request error response |
| Missing Header | 400 (Bad Request) | Returns missing PLR error response |
| Any Other Value | 201 (Created) | Returns a success response with default values |

---

```
GET /pillar2/subscription/:plrReference
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2024 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.pillar2stubs.controllers

import play.api.Logging
import play.api.libs.json.Json
import play.api.mvc.{Action, ControllerComponents}
import uk.gov.hmrc.pillar2stubs.controllers.actions.AuthActionFilter
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 java.time.ZonedDateTime
import java.time.ZoneOffset
import play.api.mvc.Result

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

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

case Some(plrReference) => handleSubmission(plrReference, request.body)
}
}

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

private def returnSuccessResponse(filename: String): Future[Result] = {
val response = resourceAsString(s"/resources/$filename")
.map(replaceDate(_, getCurrentTimestamp))
.map(Json.parse)
.getOrElse(Json.obj("error" -> "Response not found"))

logger.info(s"Returning success response for $filename")
Future.successful(Created(response).as("application/json"))
}

private def returnErrorResponse(filename: String): Future[Result] = {
val response = resourceAsString(s"/resources/$filename")
.map(replaceDate(_, getCurrentTimestamp))
.map(Json.parse)
.getOrElse(Json.obj("error" -> "Error response not found"))

Future.successful(BadRequest(response))
}

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

private def replaceDate(response: String, newDate: String): String =
response.replace("2022-01-31T09:26:17Z", newDate)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.time.ZoneOffset
import java.time.ZonedDateTime


val now = ZonedDateTime.now(ZoneOffset.UTC)


2 changes: 1 addition & 1 deletion conf/app.routes
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ GET /get-obligation/:plrReference/:dateFrom/:dateTo

POST /RESTAdapter/plr/below-threshold-notification uk.gov.hmrc.pillar2stubs.controllers.BTNController.submitBTN(pillar2ID:String)

POST /submit-uk-tax-return uk.gov.hmrc.pillar2stubs.controllers.LiabilityController.submitUktr
POST /submit-uk-tax-return uk.gov.hmrc.pillar2stubs.controllers.UkTaxReturnController.submitUktr
8 changes: 0 additions & 8 deletions conf/resources/liabilities/InvalidJsonResponse.json

This file was deleted.

9 changes: 0 additions & 9 deletions conf/resources/liabilities/InvalidRequestResponse.json

This file was deleted.

8 changes: 0 additions & 8 deletions conf/resources/liabilities/LiabilitiesNotFoundResponse.json

This file was deleted.

6 changes: 0 additions & 6 deletions conf/resources/liabilities/NilReturnSuccessResponse.json

This file was deleted.

7 changes: 7 additions & 0 deletions conf/resources/uktaxreturn/InvalidRequestResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"error": {
"code": "400",
"message": "Invalid JSON message content used&#x3b; Message: "Expected a ',' or '}' at character 93 of {
	"idType": "EMPREF",
	"idValue": "864FZ00049",
	"regimeType": "PAYE",
	"lockReason": "8"
	"chargeReferences": [
		"XM123456789812",
		"XM123456781234",
		"XC123111781234"
	],	
	"createNote": true,
	"noteType": "TTP",
	"noteLines": [
		"paymentPlanType:       TTP",
		"arrangementAgreedDate: 2022-02-01",
		"arrangementChannel:    ePAYE",
		"firstPaymentAmount:    1234.56",
		"firstPaymentDate:      2022-03-01",
		"regularPaymentAmount:  1000",
		"paymentFrequency:      Monthly",
		"arrangementReviewDate: 2022-11-02",
		"ddiReference:          123456789012"
	]
}"",
"logID": "C0000AB8190C86300000000200006836"
}
}
7 changes: 7 additions & 0 deletions conf/resources/uktaxreturn/MissingPLRResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"errors": {
"processingDate": "2022-01-31T09:26:17Z",
"code": "002",
"text": "Pillar 2 ID missing or invalid"
}
}
Loading

0 comments on commit 16b01fa

Please sign in to comment.