Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API-4485: Update Push-Pull service connector to use modified subscriber endpoint #80

Merged
merged 1 commit into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions app/uk/gov/hmrc/apisubscriptionfields/connector/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)))

Expand All @@ -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)))

Expand All @@ -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"))
)
}
}
Expand Down