-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PILLAR2-Stub: Simplify stub to provide static responses based on Pill…
…ar2 reference headers
- Loading branch information
1 parent
93cbd78
commit 16b01fa
Showing
15 changed files
with
6,766 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
app/uk/gov/hmrc/pillar2stubs/controllers/UkTaxReturnController.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
7 changes: 7 additions & 0 deletions
7
app/uk/gov/hmrc/pillar2stubs/controllers/controllers.worksheet.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"error": { | ||
"code": "400", | ||
"message": "Invalid JSON message content used; 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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.