From 6b2f185a1ebe37b48a8059961ce1a88ca8b94bbf Mon Sep 17 00:00:00 2001 From: Pete Slater <28756214+peteslater-ee@users.noreply.github.com> Date: Wed, 7 Aug 2024 10:47:41 +0100 Subject: [PATCH] API-7747 - Updating to use HttpClientV2 (#515) --- .../connectors/StrideAuthConnector.scala | 4 +-- .../ApiPlatformEventsConnector.scala | 34 +++++++++++-------- .../ApiSubscriptionFieldsConnector.scala | 8 +++-- .../connector/AwsApiGatewayConnector.scala | 29 +++++++++------- .../connector/EmailConnector.scala | 10 ++++-- ...hirdPartyDelegatedAuthorityConnector.scala | 10 ++++-- .../connector/TotpConnector.scala | 15 ++++---- build.sbt | 20 ++--------- conf/application.conf | 2 +- project/AppDependencies.scala | 9 ++--- project/plugins.sbt | 8 ++--- .../ApiPlatformEventsConnectorSpec.scala | 9 +++-- .../ApiSubscriptionFieldsConnectorSpec.scala | 9 +++-- .../AwsApiGatewayConnectorSpec.scala | 8 ++--- .../connector/EmailConnectorSpec.scala | 10 +++--- .../connector/TotpConnectorSpec.scala | 8 ++--- .../connector/WiremockSugar.scala | 6 ++-- 17 files changed, 102 insertions(+), 97 deletions(-) diff --git a/app/uk/gov/hmrc/apiplatform/modules/gkauth/connectors/StrideAuthConnector.scala b/app/uk/gov/hmrc/apiplatform/modules/gkauth/connectors/StrideAuthConnector.scala index bef422d70..10253e9e7 100644 --- a/app/uk/gov/hmrc/apiplatform/modules/gkauth/connectors/StrideAuthConnector.scala +++ b/app/uk/gov/hmrc/apiplatform/modules/gkauth/connectors/StrideAuthConnector.scala @@ -19,13 +19,13 @@ package uk.gov.hmrc.apiplatform.modules.gkauth.connectors import javax.inject.{Inject, Singleton} import uk.gov.hmrc.auth.core._ -import uk.gov.hmrc.http.HttpClient +import uk.gov.hmrc.http.client.HttpClientV2 object StrideAuthConnector { case class Config(strideAuthBaseUrl: String) } @Singleton -class StrideAuthConnector @Inject() (val http: HttpClient, config: StrideAuthConnector.Config) extends PlayAuthConnector { +class StrideAuthConnector @Inject() (val httpClientV2: HttpClientV2, config: StrideAuthConnector.Config) extends PlayAuthConnector { lazy val serviceUrl = config.strideAuthBaseUrl } diff --git a/app/uk/gov/hmrc/thirdpartyapplication/connector/ApiPlatformEventsConnector.scala b/app/uk/gov/hmrc/thirdpartyapplication/connector/ApiPlatformEventsConnector.scala index 6a84c198e..037f79882 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/connector/ApiPlatformEventsConnector.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/connector/ApiPlatformEventsConnector.scala @@ -16,11 +16,14 @@ package uk.gov.hmrc.thirdpartyapplication.connector +import java.net.URL import javax.inject.Inject import scala.concurrent.{ExecutionContext, Future} +import play.api.libs.json.Json import uk.gov.hmrc.http.HttpReads.Implicits._ -import uk.gov.hmrc.http.{HeaderCarrier, HttpClient} +import uk.gov.hmrc.http.client.HttpClientV2 +import uk.gov.hmrc.http.{HeaderCarrier, StringContextOps} import uk.gov.hmrc.apiplatform.modules.common.services.ApplicationLogger import uk.gov.hmrc.apiplatform.modules.events.applications.domain.models._ @@ -30,7 +33,7 @@ object ApiPlatformEventsConnector { case class Config(baseUrl: String, enabled: Boolean) } -class ApiPlatformEventsConnector @Inject() (http: HttpClient, config: ApiPlatformEventsConnector.Config)(implicit val ec: ExecutionContext) extends ResponseUtils +class ApiPlatformEventsConnector @Inject() (http: HttpClientV2, config: ApiPlatformEventsConnector.Config)(implicit val ec: ExecutionContext) extends ResponseUtils with ApplicationLogger { val serviceBaseUrl: String = s"${config.baseUrl}" @@ -42,24 +45,25 @@ class ApiPlatformEventsConnector @Inject() (http: HttpClient, config: ApiPlatfor implicit val headersWithoutAuthorization: HeaderCarrier = hc.copy(authorization = None) if (config.enabled) { - http.POST[ApplicationEvent, ErrorOr[Unit]]( - addEventURI(uri), - event - ).map { - case Right(_) => - logger.info(s"calling platform event service for application ${event.applicationId.value}") - true - case Left(e) => - logger.warn(s"calling platform event service failed for application ${event.applicationId.value} $e") - false - } + http.post(addEventURI(uri)) + .withBody(Json.toJson(event)) + .execute[ErrorOr[Unit]] + .map { + case Right(_) => + logger.info(s"calling platform event service for application ${event.applicationId.value}") + true + case Left(e) => + logger.warn(s"calling platform event service failed for application ${event.applicationId.value} $e") + false + } } else { logger.info("call to platform events disabled") Future.successful(true) } } - private def addEventURI(path: String): String = { - serviceBaseUrl + path + private def addEventURI(path: String): URL = { + val x = s"$serviceBaseUrl$path" + url"$x" } } diff --git a/app/uk/gov/hmrc/thirdpartyapplication/connector/ApiSubscriptionFieldsConnector.scala b/app/uk/gov/hmrc/thirdpartyapplication/connector/ApiSubscriptionFieldsConnector.scala index 43f0a8382..b94a7291e 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/connector/ApiSubscriptionFieldsConnector.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/connector/ApiSubscriptionFieldsConnector.scala @@ -21,7 +21,8 @@ import scala.concurrent.{ExecutionContext, Future} import play.api.http.Status._ import uk.gov.hmrc.http.HttpReads.Implicits._ -import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, UpstreamErrorResponse} +import uk.gov.hmrc.http.client.HttpClientV2 +import uk.gov.hmrc.http.{HeaderCarrier, StringContextOps, UpstreamErrorResponse} import uk.gov.hmrc.apiplatform.modules.common.domain.models.ClientId import uk.gov.hmrc.thirdpartyapplication.models.HasSucceeded @@ -31,10 +32,11 @@ object ApiSubscriptionFieldsConnector { } @Singleton -class ApiSubscriptionFieldsConnector @Inject() (httpClient: HttpClient, config: ApiSubscriptionFieldsConnector.Config)(implicit val ec: ExecutionContext) extends ResponseUtils { +class ApiSubscriptionFieldsConnector @Inject() (httpClient: HttpClientV2, config: ApiSubscriptionFieldsConnector.Config)(implicit val ec: ExecutionContext) extends ResponseUtils { def deleteSubscriptions(clientId: ClientId)(implicit hc: HeaderCarrier): Future[HasSucceeded] = { - httpClient.DELETE[ErrorOr[Unit]](s"${config.baseUrl}/field/application/${clientId.value}") + httpClient.delete(url"${config.baseUrl}/field/application/${clientId.value}") + .execute[ErrorOr[Unit]] .map { case Right(_) => HasSucceeded case Left(UpstreamErrorResponse(_, NOT_FOUND, _, _)) => HasSucceeded diff --git a/app/uk/gov/hmrc/thirdpartyapplication/connector/AwsApiGatewayConnector.scala b/app/uk/gov/hmrc/thirdpartyapplication/connector/AwsApiGatewayConnector.scala index 72981f294..e2fa1b695 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/connector/AwsApiGatewayConnector.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/connector/AwsApiGatewayConnector.scala @@ -16,14 +16,16 @@ package uk.gov.hmrc.thirdpartyapplication.connector +import java.net.URL import javax.inject.{Inject, Singleton} import scala.concurrent.{ExecutionContext, Future} import play.api.http.ContentTypes.JSON import play.api.http.HeaderNames.CONTENT_TYPE -import play.api.libs.json.{JsPath, OWrites, Reads} +import play.api.libs.json.{JsPath, Json, OWrites, Reads} import uk.gov.hmrc.http.HttpReads.Implicits._ -import uk.gov.hmrc.http.{HeaderCarrier, HttpClient} +import uk.gov.hmrc.http.client.HttpClientV2 +import uk.gov.hmrc.http.{HeaderCarrier, StringContextOps} import uk.gov.hmrc.apiplatform.modules.common.services.ApplicationLogger import uk.gov.hmrc.apiplatform.modules.applications.core.domain.models.RateLimitTier @@ -46,25 +48,24 @@ object AwsApiGatewayConnector extends ApplicationLogger { } @Singleton -class AwsApiGatewayConnector @Inject() (http: HttpClient, config: AwsApiGatewayConnector.Config)(implicit val ec: ExecutionContext) { +class AwsApiGatewayConnector @Inject() (http: HttpClientV2, config: AwsApiGatewayConnector.Config)(implicit val ec: ExecutionContext) { import AwsApiGatewayConnector._ val serviceBaseUrl: String = s"${config.baseUrl}/v1/application" val awsApiKey: String = config.awsApiKey val apiKeyHeaderName = "x-api-key" - private def updateUsagePlanURL(rateLimitTier: RateLimitTier): String = s"${config.baseUrl}/v1/usage-plans/$rateLimitTier/api-keys" - private def deleteAPIKeyURL(applicationName: String): String = s"${config.baseUrl}/v1/api-keys/$applicationName" + private def updateUsagePlanURL(rateLimitTier: RateLimitTier): URL = url"${config.baseUrl}/v1/usage-plans/$rateLimitTier/api-keys" + private def deleteAPIKeyURL(applicationName: String): URL = url"${config.baseUrl}/v1/api-keys/$applicationName" def createOrUpdateApplication(applicationName: String, serverToken: String, usagePlan: RateLimitTier)(hc: HeaderCarrier): Future[HasSucceeded] = { implicit val headersWithoutAuthorization: HeaderCarrier = hc .copy(authorization = None) .withExtraHeaders(apiKeyHeaderName -> awsApiKey, CONTENT_TYPE -> JSON) - http.POST[UpdateApplicationUsagePlanRequest, RequestId]( - updateUsagePlanURL(usagePlan), - UpdateApplicationUsagePlanRequest(applicationName, serverToken) - ) + http.post(updateUsagePlanURL(usagePlan)) + .withBody(Json.toJson(UpdateApplicationUsagePlanRequest(applicationName, serverToken))) + .execute[RequestId] .map { requestId => logger.info(s"Successfully created or updated application '$applicationName' in AWS API Gateway with request ID ${requestId.value}") HasSucceeded @@ -76,9 +77,11 @@ class AwsApiGatewayConnector @Inject() (http: HttpClient, config: AwsApiGatewayC .copy(authorization = None) .withExtraHeaders(apiKeyHeaderName -> awsApiKey) - http.DELETE[RequestId](deleteAPIKeyURL(applicationName)).map(requestId => { - logger.info(s"Successfully deleted application '$applicationName' from AWS API Gateway with request ID ${requestId.value}") - HasSucceeded - }) + http.delete(deleteAPIKeyURL(applicationName)) + .execute[RequestId] + .map(requestId => { + logger.info(s"Successfully deleted application '$applicationName' from AWS API Gateway with request ID ${requestId.value}") + HasSucceeded + }) } } diff --git a/app/uk/gov/hmrc/thirdpartyapplication/connector/EmailConnector.scala b/app/uk/gov/hmrc/thirdpartyapplication/connector/EmailConnector.scala index 06ae1f6a5..4d5beeb98 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/connector/EmailConnector.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/connector/EmailConnector.scala @@ -24,7 +24,8 @@ import scala.util.{Failure, Success, Try} import play.api.libs.json.{Json, OFormat} import play.mvc.Http.Status._ -import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpResponse} +import uk.gov.hmrc.http.client.HttpClientV2 +import uk.gov.hmrc.http.{HeaderCarrier, HttpResponse, StringContextOps} import uk.gov.hmrc.apiplatform.modules.common.domain.models.{ApplicationId, LaxEmailAddress} import uk.gov.hmrc.apiplatform.modules.common.services.ApplicationLogger @@ -49,7 +50,7 @@ object EmailConnector { } @Singleton -class EmailConnector @Inject() (httpClient: HttpClient, config: EmailConnector.Config)(implicit val ec: ExecutionContext) extends ApplicationLogger { +class EmailConnector @Inject() (httpClient: HttpClientV2, config: EmailConnector.Config)(implicit val ec: ExecutionContext) extends ApplicationLogger { import EmailConnector._ val serviceUrl = config.baseUrl @@ -475,7 +476,10 @@ class EmailConnector @Inject() (httpClient: HttpClient, config: EmailConnector.C def makeCall() = { import uk.gov.hmrc.http.HttpReads.Implicits._ - httpClient.POST[SendEmailRequest, HttpResponse](url, payload) + httpClient + .post(url"$url") + .withBody(Json.toJson(payload)) + .execute[HttpResponse] .map { response => logger.info(s"Sent '${payload.templateId}' with response: ${response.status}") response.status match { diff --git a/app/uk/gov/hmrc/thirdpartyapplication/connector/ThirdPartyDelegatedAuthorityConnector.scala b/app/uk/gov/hmrc/thirdpartyapplication/connector/ThirdPartyDelegatedAuthorityConnector.scala index 1a7b63398..85d97bc90 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/connector/ThirdPartyDelegatedAuthorityConnector.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/connector/ThirdPartyDelegatedAuthorityConnector.scala @@ -20,7 +20,8 @@ import javax.inject.{Inject, Singleton} import scala.concurrent.{ExecutionContext, Future} import uk.gov.hmrc.http.HttpReads.Implicits._ -import uk.gov.hmrc.http.{HeaderCarrier, HttpClient} +import uk.gov.hmrc.http.client.HttpClientV2 +import uk.gov.hmrc.http.{HeaderCarrier, StringContextOps} import uk.gov.hmrc.apiplatform.modules.common.domain.models.ClientId import uk.gov.hmrc.thirdpartyapplication.models.HasSucceeded @@ -30,9 +31,12 @@ object ThirdPartyDelegatedAuthorityConnector { } @Singleton -class ThirdPartyDelegatedAuthorityConnector @Inject() (httpClient: HttpClient, config: ThirdPartyDelegatedAuthorityConnector.Config)(implicit val ec: ExecutionContext) { +class ThirdPartyDelegatedAuthorityConnector @Inject() (httpClient: HttpClientV2, config: ThirdPartyDelegatedAuthorityConnector.Config)(implicit val ec: ExecutionContext) { def revokeApplicationAuthorities(clientId: ClientId)(implicit hc: HeaderCarrier): Future[HasSucceeded] = { - httpClient.DELETE[Option[Unit]](s"${config.baseUrl}/authority/${clientId.value}") map (_ => HasSucceeded) + httpClient + .delete(url"${config.baseUrl}/authority/${clientId.value}") + .execute[Option[Unit]] + .map(_ => HasSucceeded) } } diff --git a/app/uk/gov/hmrc/thirdpartyapplication/connector/TotpConnector.scala b/app/uk/gov/hmrc/thirdpartyapplication/connector/TotpConnector.scala index 7813baa54..0c6200f21 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/connector/TotpConnector.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/connector/TotpConnector.scala @@ -21,7 +21,8 @@ import scala.concurrent.{ExecutionContext, Future} import scala.util.control.NonFatal import uk.gov.hmrc.http.HttpReads.Implicits._ -import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, UpstreamErrorResponse} +import uk.gov.hmrc.http.client.HttpClientV2 +import uk.gov.hmrc.http.{HeaderCarrier, StringContextOps, UpstreamErrorResponse} import uk.gov.hmrc.thirdpartyapplication.models.Totp @@ -30,15 +31,17 @@ object TotpConnector { } @Singleton -class TotpConnector @Inject() (httpClient: HttpClient, config: TotpConnector.Config)(implicit val ec: ExecutionContext) { +class TotpConnector @Inject() (httpClient: HttpClientV2, config: TotpConnector.Config)(implicit val ec: ExecutionContext) { def generateTotp()(implicit hc: HeaderCarrier): Future[Totp] = { - val url = s"${config.baseUrl}/time-based-one-time-password/secret" + val postUrl = url"${config.baseUrl}/time-based-one-time-password/secret" - httpClient.POSTEmpty[Totp](url) + httpClient + .post(postUrl) + .execute[Totp] .recover { - case e: UpstreamErrorResponse => throw new RuntimeException(s"Unexpected response from $url: (${e.statusCode}, ${e.message})") - case NonFatal(e) => throw new RuntimeException(s"Error response from $url: ${e.getMessage}") + case e: UpstreamErrorResponse => throw new RuntimeException(s"Unexpected response from $postUrl: (${e.statusCode}, ${e.message})") + case NonFatal(e) => throw new RuntimeException(s"Error response from $postUrl: ${e.getMessage}") } } } diff --git a/build.sbt b/build.sbt index daf2329a4..af777ebbd 100644 --- a/build.sbt +++ b/build.sbt @@ -6,7 +6,8 @@ import uk.gov.hmrc.DefaultBuildSettings._ lazy val appName = "third-party-application" -lazy val playSettings: Seq[Setting[_]] = Seq.empty +Global / bloopAggregateSourceDependencies := true +Global / bloopExportJarClassifiers := Some(Set("sources")) ThisBuild / scalaVersion := "2.13.12" ThisBuild / majorVersion := 0 @@ -17,15 +18,10 @@ ThisBuild / semanticdbVersion := scalafixSemanticdb.revision lazy val microservice = Project(appName, file(".")) .enablePlugins(PlayScala, SbtDistributablesPlugin) .disablePlugins(JUnitXmlReportPlugin) - .settings(playSettings: _*) - .settings(scalaSettings: _*) - .settings(defaultSettings(): _*) .settings(ScoverageSettings()) .settings( - name := appName, libraryDependencies ++= AppDependencies(), retrieveManaged := true, - routesGenerator := InjectedRoutesGenerator, scalacOptions += "-Wconf:src=routes/.*:s", routesImport ++= Seq( "uk.gov.hmrc.apiplatform.modules.common.domain.models._", @@ -34,9 +30,6 @@ lazy val microservice = Project(appName, file(".")) "uk.gov.hmrc.apiplatform.modules.submissions.domain.models._" ) ) - .settings( - addCompilerPlugin("org.typelevel" % "kind-projector" % "0.13.2" cross CrossVersion.full) - ) .settings( Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-eT"), Test / fork := false, @@ -44,12 +37,7 @@ lazy val microservice = Project(appName, file(".")) Test / parallelExecution := false ) .settings( - scalacOptions ++= Seq( - "-Wconf:cat=unused&src=views/.*\\.scala:s", - "-Wconf:cat=unused&src=.*RoutesPrefix\\.scala:s", - "-Wconf:cat=unused&src=.*Routes\\.scala:s", - "-Wconf:cat=unused&src=.*ReverseRoutes\\.scala:s" - ) + addCompilerPlugin("org.typelevel" % "kind-projector" % "0.13.2" cross CrossVersion.full) ) lazy val it = (project in file("it")) @@ -61,8 +49,6 @@ lazy val it = (project in file("it")) DefaultBuildSettings.itSettings() ) -Global / bloopAggregateSourceDependencies := true - commands ++= Seq( Command.command("cleanAll") { state => "clean" :: "it/clean" :: state }, Command.command("fmtAll") { state => "scalafmtAll" :: "it/scalafmtAll" :: state }, diff --git a/conf/application.conf b/conf/application.conf index 9bf1d2868..2e5f3fb3a 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -21,7 +21,7 @@ play.http.errorHandler = "uk.gov.hmrc.play.bootstrap.backend.http.JsonErrorHandl # Define additional modules used here play.modules.enabled += "uk.gov.hmrc.mongo.play.PlayMongoModule" -play.modules.enabled += "uk.gov.hmrc.play.bootstrap.HttpClientModule" +play.modules.enabled += "uk.gov.hmrc.play.bootstrap.HttpClientV2Module" play.modules.enabled += "uk.gov.hmrc.thirdpartyapplication.config.ConfigurationModule" play.modules.enabled += "uk.gov.hmrc.apiplatform.modules.gkauth.config.StrideConfigurationModule" play.modules.enabled += "uk.gov.hmrc.thirdpartyapplication.config.SchedulerModule" diff --git a/project/AppDependencies.scala b/project/AppDependencies.scala index c96902643..7969baf2d 100644 --- a/project/AppDependencies.scala +++ b/project/AppDependencies.scala @@ -4,9 +4,9 @@ import org.apache.ivy.core.module.descriptor.ExcludeRule object AppDependencies { def apply(): Seq[ModuleID] = compileDeps ++ testDeps - lazy val bootstrapVersion = "8.4.0" + lazy val bootstrapVersion = "9.2.0" lazy val hmrcMongoVersion = "1.7.0" - lazy val commonDomainVersion = "0.13.0" + lazy val commonDomainVersion = "0.15.0" lazy val applicationEventVersion = "0.63.0" private lazy val compileDeps = Seq( @@ -15,8 +15,9 @@ object AppDependencies { "commons-net" % "commons-net" % "3.6", "com.github.t3hnar" %% "scala-bcrypt" % "4.1", "commons-validator" % "commons-validator" % "1.7", - "uk.gov.hmrc" %% "internal-auth-client-play-30" % "1.10.0", - "uk.gov.hmrc" %% "api-platform-application-events" % applicationEventVersion + "uk.gov.hmrc" %% "internal-auth-client-play-30" % "3.0.0", + "uk.gov.hmrc" %% "api-platform-application-events" % applicationEventVersion, + "com.iheart" %% "ficus" % "1.5.2" ) private lazy val testDeps = Seq( diff --git a/project/plugins.sbt b/project/plugins.sbt index 5281f72b2..72fa2cedb 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -5,11 +5,11 @@ resolvers += "Typesafe Releases" at "https://repo.typesafe.com/typesafe/releases addSbtPlugin("uk.gov.hmrc" % "sbt-auto-build" % "3.22.0") addSbtPlugin("uk.gov.hmrc" % "sbt-distributables" % "2.5.0") -addSbtPlugin("org.playframework" % "sbt-plugin" % "3.0.1") +addSbtPlugin("org.playframework" % "sbt-plugin" % "3.0.4") addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.9") -addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0") -addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.5.13") +addSbtPlugin("org.scalastyle" % "scalastyle-sbt-plugin" % "1.0.0") +addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.5.15") addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2") -addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.1") +addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.12.1") ThisBuild / libraryDependencySchemes += "org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always diff --git a/test/uk/gov/hmrc/thirdpartyapplication/connector/ApiPlatformEventsConnectorSpec.scala b/test/uk/gov/hmrc/thirdpartyapplication/connector/ApiPlatformEventsConnectorSpec.scala index 8458e9f05..59ab684ac 100644 --- a/test/uk/gov/hmrc/thirdpartyapplication/connector/ApiPlatformEventsConnectorSpec.scala +++ b/test/uk/gov/hmrc/thirdpartyapplication/connector/ApiPlatformEventsConnectorSpec.scala @@ -21,7 +21,8 @@ import scala.concurrent.ExecutionContext.Implicits.global import com.github.tomakehurst.wiremock.client.WireMock._ import play.api.http.Status._ -import uk.gov.hmrc.http.{HeaderCarrier, HttpClient} +import uk.gov.hmrc.http.HeaderCarrier +import uk.gov.hmrc.http.test.HttpClientV2Support import uk.gov.hmrc.apiplatform.modules.common.domain.models.LaxEmailAddress.StringSyntax import uk.gov.hmrc.apiplatform.modules.common.domain.models.{Actors, ApplicationId} @@ -45,14 +46,12 @@ class ApiPlatformEventsConnectorSpec extends ConnectorSpec { requestingAdminEmail = "admin@example.com".toLaxEmail ) - abstract class Setup(enabled: Boolean = true) { + abstract class Setup(enabled: Boolean = true) extends HttpClientV2Support { import uk.gov.hmrc.apiplatform.modules.events.applications.domain.services.EventsInterServiceCallJsonFormatters._ - val http: HttpClient = app.injector.instanceOf[HttpClient] - val config: ApiPlatformEventsConnector.Config = ApiPlatformEventsConnector.Config(wireMockUrl, enabled) - val underTest = new ApiPlatformEventsConnector(http, config) + lazy val underTest = new ApiPlatformEventsConnector(httpClientV2, config) def apiApplicationEventWillReturnCreated(request: ApplicationEvent) = stubFor( diff --git a/test/uk/gov/hmrc/thirdpartyapplication/connector/ApiSubscriptionFieldsConnectorSpec.scala b/test/uk/gov/hmrc/thirdpartyapplication/connector/ApiSubscriptionFieldsConnectorSpec.scala index 1653d76dc..85a0cad13 100644 --- a/test/uk/gov/hmrc/thirdpartyapplication/connector/ApiSubscriptionFieldsConnectorSpec.scala +++ b/test/uk/gov/hmrc/thirdpartyapplication/connector/ApiSubscriptionFieldsConnectorSpec.scala @@ -21,7 +21,8 @@ import scala.concurrent.ExecutionContext.Implicits.global import com.github.tomakehurst.wiremock.client.WireMock._ import play.api.http.Status._ -import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, UpstreamErrorResponse} +import uk.gov.hmrc.http.test.HttpClientV2Support +import uk.gov.hmrc.http.{HeaderCarrier, UpstreamErrorResponse} import uk.gov.hmrc.apiplatform.modules.common.domain.models.ClientId @@ -31,12 +32,10 @@ class ApiSubscriptionFieldsConnectorSpec extends ConnectorSpec { val clientId = ClientId.random - trait Setup { - val http: HttpClient = app.injector.instanceOf[HttpClient] - + trait Setup extends HttpClientV2Support { val config: ApiSubscriptionFieldsConnector.Config = ApiSubscriptionFieldsConnector.Config(wireMockUrl) - val underTest = new ApiSubscriptionFieldsConnector(http, config) + val underTest = new ApiSubscriptionFieldsConnector(httpClientV2, config) def apiSubscriptionFieldsWillReturn(status: Int) = stubFor( diff --git a/test/uk/gov/hmrc/thirdpartyapplication/connector/AwsApiGatewayConnectorSpec.scala b/test/uk/gov/hmrc/thirdpartyapplication/connector/AwsApiGatewayConnectorSpec.scala index 6b5531150..18b357fe0 100644 --- a/test/uk/gov/hmrc/thirdpartyapplication/connector/AwsApiGatewayConnectorSpec.scala +++ b/test/uk/gov/hmrc/thirdpartyapplication/connector/AwsApiGatewayConnectorSpec.scala @@ -26,7 +26,8 @@ import play.api.http.ContentTypes.JSON import play.api.http.HeaderNames.{AUTHORIZATION, CONTENT_TYPE} import play.api.http.Status.{ACCEPTED, INTERNAL_SERVER_ERROR, OK} import play.api.libs.json._ -import uk.gov.hmrc.http.{Authorization, HeaderCarrier, HttpClient, UpstreamErrorResponse} +import uk.gov.hmrc.http.test.HttpClientV2Support +import uk.gov.hmrc.http.{Authorization, HeaderCarrier, UpstreamErrorResponse} import uk.gov.hmrc.apiplatform.modules.applications.core.domain.models.RateLimitTier import uk.gov.hmrc.thirdpartyapplication.models.HasSucceeded @@ -41,7 +42,7 @@ class AwsApiGatewayConnectorSpec extends ConnectorSpec { implicit val requestIdWrites: Writes[RequestId] = (JsPath \ "RequestId").write[String].contramap((r: RequestId) => r.value) - trait Setup { + trait Setup extends HttpClientV2Support { SharedMetricRegistries.clear() implicit val hc: HeaderCarrier = HeaderCarrier(authorization = Some(Authorization("foo"))) @@ -50,11 +51,10 @@ class AwsApiGatewayConnectorSpec extends ConnectorSpec { val expectedDeleteURL: String = s"/v1/api-keys/$applicationName" - val http: HttpClient = app.injector.instanceOf[HttpClient] val awsApiKey: String = UUID.randomUUID().toString val config: AwsApiGatewayConnector.Config = AwsApiGatewayConnector.Config(wireMockUrl, awsApiKey) - val underTest: AwsApiGatewayConnector = new AwsApiGatewayConnector(http, config) + val underTest: AwsApiGatewayConnector = new AwsApiGatewayConnector(httpClientV2, config) } "createOrUpdateApplication" should { diff --git a/test/uk/gov/hmrc/thirdpartyapplication/connector/EmailConnectorSpec.scala b/test/uk/gov/hmrc/thirdpartyapplication/connector/EmailConnectorSpec.scala index 4858b8551..fba0a0d40 100644 --- a/test/uk/gov/hmrc/thirdpartyapplication/connector/EmailConnectorSpec.scala +++ b/test/uk/gov/hmrc/thirdpartyapplication/connector/EmailConnectorSpec.scala @@ -21,7 +21,8 @@ import scala.concurrent.ExecutionContext.Implicits.global import com.github.tomakehurst.wiremock.client.WireMock._ import play.api.http.Status._ -import uk.gov.hmrc.http.{HeaderCarrier, HttpClient} +import uk.gov.hmrc.http.HeaderCarrier +import uk.gov.hmrc.http.test.HttpClientV2Support import uk.gov.hmrc.apiplatform.modules.common.domain.models.ApplicationId import uk.gov.hmrc.apiplatform.modules.common.domain.models.LaxEmailAddress.StringSyntax @@ -37,10 +38,9 @@ class EmailConnectorSpec extends ConnectorSpec with CollaboratorTestData { private val hubUrl = "http://localhost:9685" private val environmentName = "sandbox" - trait Setup { - val http: HttpClient = app.injector.instanceOf[HttpClient] - val config = EmailConnector.Config(wireMockUrl, hubUrl, hubTestTitle, environmentName) - val connector = new EmailConnector(http, config) + trait Setup extends HttpClientV2Support { + val config = EmailConnector.Config(wireMockUrl, hubUrl, hubTestTitle, environmentName) + val connector = new EmailConnector(httpClientV2, config) wireMockServer.resetRequests() diff --git a/test/uk/gov/hmrc/thirdpartyapplication/connector/TotpConnectorSpec.scala b/test/uk/gov/hmrc/thirdpartyapplication/connector/TotpConnectorSpec.scala index e07af033f..efc5af516 100644 --- a/test/uk/gov/hmrc/thirdpartyapplication/connector/TotpConnectorSpec.scala +++ b/test/uk/gov/hmrc/thirdpartyapplication/connector/TotpConnectorSpec.scala @@ -21,7 +21,8 @@ import scala.concurrent.ExecutionContext.Implicits.global import com.github.tomakehurst.wiremock.client.WireMock._ import play.api.http.Status._ -import uk.gov.hmrc.http.{HeaderCarrier, HttpClient} +import uk.gov.hmrc.http.HeaderCarrier +import uk.gov.hmrc.http.test.HttpClientV2Support import uk.gov.hmrc.thirdpartyapplication.models.Totp import uk.gov.hmrc.thirdpartyapplication.util.http.HttpHeaders.X_REQUEST_ID_HEADER @@ -31,13 +32,12 @@ class TotpConnectorSpec extends ConnectorSpec { implicit val hc: HeaderCarrier = HeaderCarrier() private val baseUrl = wireMockUrl - trait Setup { + trait Setup extends HttpClientV2Support { implicit val hc: HeaderCarrier = HeaderCarrier().withExtraHeaders(X_REQUEST_ID_HEADER -> "requestId") val applicationName: String = "third-party-application" - val httpClient = app.injector.instanceOf[HttpClient] val config = TotpConnector.Config(baseUrl) - val underTest = new TotpConnector(httpClient, config) + val underTest = new TotpConnector(httpClientV2, config) } "generateTotp" should { diff --git a/test/uk/gov/hmrc/thirdpartyapplication/connector/WiremockSugar.scala b/test/uk/gov/hmrc/thirdpartyapplication/connector/WiremockSugar.scala index 1f4c04323..f3696f8ab 100644 --- a/test/uk/gov/hmrc/thirdpartyapplication/connector/WiremockSugar.scala +++ b/test/uk/gov/hmrc/thirdpartyapplication/connector/WiremockSugar.scala @@ -28,15 +28,15 @@ trait WiremockSugar extends BeforeAndAfterEach with BeforeAndAfterAll { val stubHost = "localhost" val wireMockUrl = s"http://$stubHost:$stubPort" - private val wireMockConfiguration: WireMockConfiguration = + private lazy val wireMockConfiguration: WireMockConfiguration = wireMockConfig().port(stubPort) - val wireMockServer = new WireMockServer(wireMockConfiguration) + lazy val wireMockServer = new WireMockServer(wireMockConfiguration) override def beforeAll() = { super.beforeAll() - wireMockServer.start() WireMock.configureFor(stubHost, stubPort) + wireMockServer.start() } override protected def afterAll(): Unit = {