From f93a148eb1894acf4cabb532ee8fa8d71cd5a6e2 Mon Sep 17 00:00:00 2001 From: Chris Rowe Date: Wed, 15 Jul 2020 09:55:50 +0100 Subject: [PATCH] API-4485: Update Push-Pull service connector to use modified subscriber endpoint --- .../connector/JsonFormatters.scala | 6 ++--- .../connector/Model.scala | 6 ++--- ...PushPullNotificationServiceConnector.scala | 7 +++--- ...PullNotificationServiceConnectorSpec.scala | 22 ++++++++++--------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/app/uk/gov/hmrc/apisubscriptionfields/connector/JsonFormatters.scala b/app/uk/gov/hmrc/apisubscriptionfields/connector/JsonFormatters.scala index c1fb575..34c85db 100644 --- a/app/uk/gov/hmrc/apisubscriptionfields/connector/JsonFormatters.scala +++ b/app/uk/gov/hmrc/apisubscriptionfields/connector/JsonFormatters.scala @@ -26,9 +26,9 @@ trait JsonFormatters { implicit val createBoxRequestJF = Json.format[CreateBoxRequest] implicit val createBoxResponseJF = Json.format[CreateBoxResponse] - implicit val subscribersRequestJF = Json.format[SubscribersRequest] - implicit val updateSubscribersRequestJF = Json.format[UpdateSubscribersRequest] - implicit val updateSubscribersResponseJF = Json.format[UpdateSubscribersResponse] + implicit val subscriberRequestJF = Json.format[SubscriberRequest] + implicit val updateSubscriberRequestJF = Json.format[UpdateSubscriberRequest] + implicit val updateSubscriberResponseJF = Json.format[UpdateSubscriberResponse] } object JsonFormatters extends JsonFormatters diff --git a/app/uk/gov/hmrc/apisubscriptionfields/connector/Model.scala b/app/uk/gov/hmrc/apisubscriptionfields/connector/Model.scala index 51b0ea6..3acfdc6 100644 --- a/app/uk/gov/hmrc/apisubscriptionfields/connector/Model.scala +++ b/app/uk/gov/hmrc/apisubscriptionfields/connector/Model.scala @@ -22,8 +22,8 @@ private[connector] case class CreateBoxRequest(boxName: String, clientId: Client private[connector] case class CreateBoxResponse(boxId: BoxId) -private[connector] case class SubscribersRequest(callBackUrl: String, subscriberType: String, subscriberId: Option[SubscriptionFieldsId] = None) +private[connector] case class SubscriberRequest(callBackUrl: String, subscriberType: String, subscriberId: Option[SubscriptionFieldsId] = None) -private[connector] case class UpdateSubscribersRequest(subscribers: List[SubscribersRequest]) +private[connector] case class UpdateSubscriberRequest(subscriber: SubscriberRequest) -private[connector] case class UpdateSubscribersResponse(boxId: BoxId) +private[connector] case class UpdateSubscriberResponse(boxId: BoxId) diff --git a/app/uk/gov/hmrc/apisubscriptionfields/connector/PushPullNotificationServiceConnector.scala b/app/uk/gov/hmrc/apisubscriptionfields/connector/PushPullNotificationServiceConnector.scala index 7cb8a05..d50c781 100644 --- a/app/uk/gov/hmrc/apisubscriptionfields/connector/PushPullNotificationServiceConnector.scala +++ b/app/uk/gov/hmrc/apisubscriptionfields/connector/PushPullNotificationServiceConnector.scala @@ -28,7 +28,8 @@ import scala.util.control.NonFatal import uk.gov.hmrc.apisubscriptionfields.model.SubscriptionFieldsId @Singleton -class PushPullNotificationServiceConnector @Inject()(http: HttpClient, appConfig: ApplicationConfig, val apiMetrics: ApiMetrics)(implicit ec: ExecutionContext) extends RecordMetrics { +class PushPullNotificationServiceConnector @Inject()(http: HttpClient, appConfig: ApplicationConfig, val apiMetrics: ApiMetrics) + (implicit ec: ExecutionContext) extends RecordMetrics { import uk.gov.hmrc.apisubscriptionfields.connector.JsonFormatters._ val api = API("api-subscription-fields") @@ -46,9 +47,9 @@ class PushPullNotificationServiceConnector @Inject()(http: HttpClient, appConfig } def subscribe(subscriberFieldsId: SubscriptionFieldsId, boxId: BoxId, callbackUrl: String)(implicit hc: HeaderCarrier): Future[Unit] = { - val payload = UpdateSubscribersRequest(List(SubscribersRequest(callbackUrl, "API_PUSH_SUBSCRIBER", Some(subscriberFieldsId)))) + val payload = UpdateSubscriberRequest(SubscriberRequest(callbackUrl, "API_PUSH_SUBSCRIBER", Some(subscriberFieldsId))) - http.PUT[UpdateSubscribersRequest, UpdateSubscribersResponse](s"$externalServiceUri/box/${boxId.value.toString}/subscribers", payload) + http.PUT[UpdateSubscriberRequest, UpdateSubscriberResponse](s"$externalServiceUri/box/${boxId.value.toString}/subscriber", payload) .map(_ => ()) .recover { case NonFatal(e) => throw new RuntimeException(s"Unexpected response from $externalServiceUri: ${e.getMessage}") diff --git a/test/uk/gov/hmrc/apisubscriptionfields/connector/PushPullNotificationServiceConnectorSpec.scala b/test/uk/gov/hmrc/apisubscriptionfields/connector/PushPullNotificationServiceConnectorSpec.scala index e525040..30edb9c 100644 --- a/test/uk/gov/hmrc/apisubscriptionfields/connector/PushPullNotificationServiceConnectorSpec.scala +++ b/test/uk/gov/hmrc/apisubscriptionfields/connector/PushPullNotificationServiceConnectorSpec.scala @@ -17,6 +17,7 @@ package uk.gov.hmrc.apisubscriptionfields.connector import java.{util => ju} + import org.scalatest.BeforeAndAfterEach import org.scalatestplus.play.guice.GuiceOneAppPerSuite import play.api.Application @@ -28,9 +29,10 @@ import play.api.libs.json.Json import play.api.http.Status.OK import com.github.tomakehurst.wiremock.client.WireMock import org.scalatest.BeforeAndAfterAll +import play.api.http.HeaderNames.{CONTENT_TYPE, USER_AGENT} import uk.gov.hmrc.apisubscriptionfields.AsyncHmrcSpec import uk.gov.hmrc.http.HeaderCarrier -import uk.gov.hmrc.apisubscriptionfields.model.{SubscriptionFieldsId, BoxId} +import uk.gov.hmrc.apisubscriptionfields.model.{BoxId, SubscriptionFieldsId} import uk.gov.hmrc.apisubscriptionfields.model.ClientId class PushPullNotificationServiceConnectorSpec @@ -85,7 +87,7 @@ class PushPullNotificationServiceConnectorSpec wireMockServer.stubFor( put(path).withRequestBody(equalTo(requestBody)) .willReturn(aResponse() - .withHeader("Content-Type", "application/json") + .withHeader(CONTENT_TYPE, "application/json") .withBody(Json.stringify(Json.toJson(response))) .withStatus(OK))) @@ -96,22 +98,22 @@ class PushPullNotificationServiceConnectorSpec wireMockServer.verify( putRequestedFor(urlPathEqualTo(path)) - .withHeader("Content-Type", equalTo("application/json")) - .withHeader("User-Agent", equalTo("api-subscription-fields")) + .withHeader(CONTENT_TYPE, equalTo("application/json")) + .withHeader(USER_AGENT, equalTo("api-subscription-fields")) ) } "send proper request to subscribe" in new Setup { val callbackUrl = "my-callback" - val updateRequest: UpdateSubscribersRequest = UpdateSubscribersRequest(List(SubscribersRequest(callbackUrl, "API_PUSH_SUBSCRIBER", Some(subscriptionFieldsId)))) + val updateRequest: UpdateSubscriberRequest = UpdateSubscriberRequest(SubscriberRequest(callbackUrl, "API_PUSH_SUBSCRIBER", Some(subscriptionFieldsId))) val requestBody = Json.stringify(Json.toJson(updateRequest)) - val response: UpdateSubscribersResponse = UpdateSubscribersResponse(boxId) + val response: UpdateSubscriberResponse = UpdateSubscriberResponse(boxId) - val path = s"/box/${boxId.value}/subscribers" + val path = s"/box/${boxId.value}/subscriber" wireMockServer.stubFor( put(path).withRequestBody(equalTo(requestBody)) .willReturn(aResponse() - .withHeader("Content-Type", "application/json") + .withHeader(CONTENT_TYPE, "application/json") .withBody(Json.stringify(Json.toJson(response))) .withStatus(OK))) @@ -122,8 +124,8 @@ class PushPullNotificationServiceConnectorSpec wireMockServer.verify( putRequestedFor(urlPathEqualTo(path)) - .withHeader("Content-Type", equalTo("application/json")) - .withHeader("User-Agent", equalTo("api-subscription-fields")) + .withHeader(CONTENT_TYPE, equalTo("application/json")) + .withHeader(USER_AGENT, equalTo("api-subscription-fields")) ) } }