Skip to content

Commit

Permalink
[DLS-10533] Applied the patch and fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ruthdas03 committed Oct 21, 2024
1 parent 59a7c66 commit f8ca6bf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import uk.gov.hmrc.helptosave.util.{LogMessageTransformer, Logging, PagerDutyAle
import uk.gov.hmrc.http.client.HttpClientV2
import uk.gov.hmrc.http.{BadRequestException, HeaderCarrier, HttpResponse, StringContextOps, UpstreamErrorResponse}
import uk.gov.hmrc.play.bootstrap.config.ServicesConfig
import uk.gov.hmrc.http.HttpReads.Implicits._

import java.util.UUID
import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -139,38 +140,31 @@ class HelpToSaveProxyConnectorImpl @Inject()(
http
.get(url).transform(_
.withQueryStringParameters("nino" -> nino , "transactionId" -> txnId.toString , "threshold" -> threshold.toString))
.execute[HttpResponse]
.map { response =>
.execute[Either[UpstreamErrorResponse, HttpResponse]]
.map { maybeResponse =>
val correlationId = "apiCorrelationId" -> getApiCorrelationId()

response.status match {
case Status.OK =>
val result = response.parseJson[UCResponse]
result.fold(
e =>
logger.warn(
s"Could not parse UniversalCredit response, received 200 (OK), error=$e",
nino,
correlationId),
_ =>
logger
.info(s"Call to check UniversalCredit check is successful, received 200 (OK)", nino, correlationId)
)
result

case other =>
logger.warn(
s"Call to check UniversalCredit check unsuccessful. Received unexpected status $other",
nino,
correlationId)
Left(s"Received unexpected status($other) from UniversalCredit check")
}
}
.recover {
case e:BadRequestException =>
Left("Received unexpected status(400) from UniversalCredit check")
case e =>
Left(s"Call to UniversalCredit check unsuccessful: ${e.getMessage}")
maybeResponse.map(response => {
val result = response.parseJson[UCResponse]
result.fold(
e =>
logger.warn(
s"Could not parse UniversalCredit response, received 200 (OK), error=$e",
nino,
correlationId),
_ =>
logger
.info(s"Call to check UniversalCredit check is successful, received 200 (OK)", nino, correlationId)
)
result

}).left.flatMap(error => {
logger.warn(
s"Call to check UniversalCredit check unsuccessful. Received unexpected status ${error.statusCode}",
nino,
correlationId)
Left(s"Received unexpected status(${error.statusCode}) from UniversalCredit check")
}).flatten
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ class UCThresholdConnectorProxyActorSpec

"ask for and return the value from the threshold connector" in {

mockConnectorGetValue(HttpResponse(200, Json.parse("""{"thresholdAmount" : 100.0}"""), returnHeaders))

actor ! UCThresholdConnectorProxyActor.GetThresholdValue
expectMsg(UCThresholdConnectorProxyActor.GetThresholdValueResponse(Right(100.0)))
connector.getThreshold()(*, *).returns(toFuture(Right(HttpResponse(200, Json.parse("""{"thresholdAmount" : 100.0}"""), returnHeaders))))
}

"ask for and return an error from the threshold connector if an error occurs" in {

mockConnectorGetValue(HttpResponse(500, Json.toJson("error occurred"), returnHeaders))
connector.getThreshold()(*, *).returns(toFuture(Left(UpstreamErrorResponse("error occurred", 500))))

mockPagerDuty
.alert("Received unexpected http status in response to get UC threshold from DES")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class HelpToSaveProxyConnectorSpec
mockPagerDutyAlert("Received unexpected http status in response to getAccount")

val result = await(proxyConnector.getAccount(nino, systemId, correlationId, path).value)
result shouldBe Left("Call to getNsiAccount unsuccessful: GET of 'http://localhost:6001/help-to-save-proxy/nsi-services/account' returned 400 (Bad Request). Response body ''")
result shouldBe Left("Received unexpected status(400) from getNsiAccount call")
}

"handle unexpected server errors" in {
Expand Down Expand Up @@ -272,7 +272,7 @@ class HelpToSaveProxyConnectorSpec
when(GET, getAccountUrl, queryParameters).thenReturn(Status.BAD_REQUEST, errorResponse.toString())

val result = await(proxyConnector.getAccount(nino, systemId, correlationId, path).value)
result shouldBe Left("Call to getNsiAccount unsuccessful: GET of 'http://localhost:6001/help-to-save-proxy/nsi-services/account' returned 400 (Bad Request). Response body '"+errorResponse+"'")
result shouldBe Right(None)
}

}
Expand Down Expand Up @@ -463,7 +463,7 @@ class HelpToSaveProxyConnectorSpec

val (result, timerMetricChange, errorMetricChange) =
transactionMetricChanges(await(proxyConnector.getTransactions(nino, systemId, correlationId).value))
result shouldBe Left("Call to get transactions unsuccessful: GET of 'http://localhost:6001/help-to-save-proxy/nsi-services/transactions' returned 400 (Bad Request). Response body ''")
result shouldBe Left("Received unexpected status(400) from get transactions call")
timerMetricChange shouldBe 0
errorMetricChange shouldBe 1
}
Expand All @@ -490,7 +490,7 @@ class HelpToSaveProxyConnectorSpec
when(GET, getTransactionsUrl, queryParameters).thenReturn(Status.BAD_REQUEST, errorResponse)

val result = await(proxyConnector.getTransactions(nino, systemId, correlationId).value)
result shouldBe Left("Call to get transactions unsuccessful: GET of 'http://localhost:6001/help-to-save-proxy/nsi-services/transactions' returned 400 (Bad Request). Response body '"+errorResponse+"'")
result shouldBe Right(None)
}

}
Expand All @@ -507,16 +507,11 @@ class HelpToSaveProxyConnectorSpec
val queryParams = Map("nino" -> nino, "transactionId" -> txnId.toString, "threshold" -> threshold.toString)

"handle unexpected errors" in {
wireMockServer.stop()
when(GET, url, queryParams)
val expectedStatusCode = 500
when(GET, url, queryParams).thenReturn(expectedStatusCode)

val result = await(proxyConnector.ucClaimantCheck(nino, txnId, threshold).value)
result.isLeft shouldBe true
result.left.value should include(
s"Call to UniversalCredit check unsuccessful: " +
s"${connectorCallFailureMessage(GET, s"$url?nino=$nino&transactionId=$txnId&threshold=$threshold")}"
)
wireMockServer.start()
result shouldBe Left(s"Received unexpected status($expectedStatusCode) from UniversalCredit check")
}
}

Expand Down Expand Up @@ -548,7 +543,6 @@ class HelpToSaveProxyConnectorSpec
"systemId" -> systemId
)
val getTransactionsUrl: String = "/help-to-save-proxy/nsi-services/transactions"
val urlWithParams: String = s"$getTransactionsUrl?nino=$nino&correlationId=$correlationId&version=$version&systemId=$systemId"

"handle unexpected server errors" in {
wireMockServer.stop()
Expand All @@ -559,7 +553,7 @@ class HelpToSaveProxyConnectorSpec
transactionMetricChanges(await(proxyConnector.getTransactions(nino, systemId, correlationId).value))
result.isLeft shouldBe true
result.left.value should include(
s"Call to get transactions unsuccessful: ${connectorCallFailureMessage(GET, urlWithParams)}"
s"Call to get transactions unsuccessful: ${connectorCallFailureMessage(GET, getTransactionsUrl)}"
)
timerMetricChange shouldBe 0
errorMetricChange shouldBe 1
Expand Down

0 comments on commit f8ca6bf

Please sign in to comment.