diff --git a/app/uk/gov/hmrc/helptosave/connectors/HelpToSaveProxyConnector.scala b/app/uk/gov/hmrc/helptosave/connectors/HelpToSaveProxyConnector.scala index ceb1e106..a4bee585 100644 --- a/app/uk/gov/hmrc/helptosave/connectors/HelpToSaveProxyConnector.scala +++ b/app/uk/gov/hmrc/helptosave/connectors/HelpToSaveProxyConnector.scala @@ -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} @@ -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 } ) } diff --git a/test/uk/gov/hmrc/helptosave/actors/UCThresholdConnectorProxyActorSpec.scala b/test/uk/gov/hmrc/helptosave/actors/UCThresholdConnectorProxyActorSpec.scala index 45a601a8..1c9b43de 100644 --- a/test/uk/gov/hmrc/helptosave/actors/UCThresholdConnectorProxyActorSpec.scala +++ b/test/uk/gov/hmrc/helptosave/actors/UCThresholdConnectorProxyActorSpec.scala @@ -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") diff --git a/test/uk/gov/hmrc/helptosave/connectors/HelpToSaveProxyConnectorSpec.scala b/test/uk/gov/hmrc/helptosave/connectors/HelpToSaveProxyConnectorSpec.scala index f0962ecb..67feefae 100644 --- a/test/uk/gov/hmrc/helptosave/connectors/HelpToSaveProxyConnectorSpec.scala +++ b/test/uk/gov/hmrc/helptosave/connectors/HelpToSaveProxyConnectorSpec.scala @@ -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 { @@ -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) } } @@ -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 } @@ -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) } } @@ -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") } } @@ -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() @@ -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