Skip to content

Commit

Permalink
API-6156 Upgrades including Java 11 (#108)
Browse files Browse the repository at this point in the history
* API-6156 Copyright year

* API-6156 Scalafmt config

* API-6156 Scalafmt changes

* API-6156 Scalafix config

* API-6156 Scalafix changes

* API-6156 Scalastyle changes
  • Loading branch information
petekirby-ee authored Jan 17, 2023
1 parent 3750fcc commit 0643b24
Show file tree
Hide file tree
Showing 54 changed files with 759 additions and 659 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.*

!.gitignore
!.scalafmt.conf
!.scalafix.conf

.cache
/.classpath
Expand Down
25 changes: 25 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
rules = [
OrganizeImports
]

OrganizeImports {
blankLines = Manual
coalesceToWildcardImportThreshold = 5
expandRelative = true
groupExplicitlyImportedImplicitsSeparately = false
groupedImports = Merge
groups = [
"re:(javax?|scala)\\."
"---"
"*"
"---"
"play"
"uk.gov.hmrc."
"---"
"uk.gov.hmrc.apisubscriptionfields."
]
importSelectorsOrder = Ascii
importsOrder = Ascii
preset = DEFAULT
removeUnused = true
}
19 changes: 19 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version="3.5.3"
runner.dialect = "scala212"

maxColumn = 180

assumeStandardLibraryStripMargin = true
align.stripMargin = true
align.preset = most
align.multiline = true
indent.extendSite = 4
indent.defnSite = 4
align.arrowEnumeratorGenerator = true
align.closeParenSite = true
align.openParenCallSite = false
align.openParenDefnSite = false
align.openParenCtrlSite = false
danglingParentheses.defnSite = true
danglingParentheses.callSite = true
danglingParentheses.exclude = []
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 HM Revenue & Customs
* Copyright 2023 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 HM Revenue & Customs
* Copyright 2023 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 HM Revenue & Customs
* Copyright 2023 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 HM Revenue & Customs
* Copyright 2023 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 HM Revenue & Customs
* Copyright 2023 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 HM Revenue & Customs
* Copyright 2023 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.apisubscriptionfields.controller

import uk.gov.hmrc.apisubscriptionfields.model.{Types, FieldDefinition}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package uk.gov.hmrc.apisubscriptionfields.config

import com.google.inject.{ImplementedBy, Inject, Singleton}

import play.api.Configuration

@ImplementedBy(classOf[ApplicationConfigImpl])
Expand All @@ -25,10 +26,9 @@ trait ApplicationConfig {
}

@Singleton
class ApplicationConfigImpl @Inject()(config: Configuration) extends ApplicationConfig {
class ApplicationConfigImpl @Inject() (config: Configuration) extends ApplicationConfig {
private val HOCON = config.underlying

// Moving away from complex layers configurations
val pushPullNotificationServiceURL = HOCON.getString("microservice.services.push-pull-notification.uri")
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
package uk.gov.hmrc.apisubscriptionfields.connector

import play.api.libs.json._
import uk.gov.hmrc.apisubscriptionfields.model.{ClientId, BoxId, SubscriptionFieldsId}

import uk.gov.hmrc.apisubscriptionfields.model.{BoxId, ClientId, SubscriptionFieldsId}

trait JsonFormatters {
implicit val clientIdJF = Json.valueFormat[ClientId]
implicit val boxIdJF = Json.valueFormat[BoxId]
implicit val clientIdJF = Json.valueFormat[ClientId]
implicit val boxIdJF = Json.valueFormat[BoxId]
implicit val subscriptionFieldsIdJF = Json.valueFormat[SubscriptionFieldsId]

implicit val createBoxRequestJF = Json.format[CreateBoxRequest]
implicit val createBoxResponseJF = Json.format[CreateBoxResponse]
implicit val subscriberRequestJF = Json.format[SubscriberRequest]
implicit val updateSubscriberRequestJF = Json.format[UpdateSubscriberRequest]
implicit val updateSubscriberResponseJF = Json.format[UpdateSubscriberResponse]
implicit val updateCallBackUrlRequestJF = Json.format[UpdateCallBackUrlRequest]
implicit val createBoxRequestJF = Json.format[CreateBoxRequest]
implicit val createBoxResponseJF = Json.format[CreateBoxResponse]
implicit val subscriberRequestJF = Json.format[SubscriberRequest]
implicit val updateSubscriberRequestJF = Json.format[UpdateSubscriberRequest]
implicit val updateSubscriberResponseJF = Json.format[UpdateSubscriberResponse]
implicit val updateCallBackUrlRequestJF = Json.format[UpdateCallBackUrlRequest]
implicit val updateCallBackUrlResponseJF = Json.format[UpdateCallBackUrlResponse]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
package uk.gov.hmrc.apisubscriptionfields.connector

import javax.inject.{Inject, Singleton}
import uk.gov.hmrc.apisubscriptionfields.config.ApplicationConfig
import uk.gov.hmrc.apisubscriptionfields.model.Types.FieldValue
import uk.gov.hmrc.apisubscriptionfields.model._
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.http.HttpClient
import uk.gov.hmrc.http.HttpReads.Implicits._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal

import uk.gov.hmrc.http.HttpReads.Implicits._
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient}
import uk.gov.hmrc.play.http.metrics.common._

import uk.gov.hmrc.apisubscriptionfields.config.ApplicationConfig
import uk.gov.hmrc.apisubscriptionfields.model.Types.FieldValue
import uk.gov.hmrc.apisubscriptionfields.model._

@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("api-subscription-fields")
Expand All @@ -39,32 +40,35 @@ class PushPullNotificationServiceConnector @Inject()(http: HttpClient, appConfig
def ensureBoxIsCreated(boxName: String, clientId: ClientId)(implicit hc: HeaderCarrier): Future[BoxId] = {
val payload = CreateBoxRequest(boxName, clientId)

http.PUT[CreateBoxRequest, CreateBoxResponse](s"$externalServiceUri/box", payload)
.map(_.boxId)
.recover {
case NonFatal(e) => throw new RuntimeException(s"Unexpected response from $externalServiceUri: ${e.getMessage}")
}
http
.PUT[CreateBoxRequest, CreateBoxResponse](s"$externalServiceUri/box", payload)
.map(_.boxId)
.recover { case NonFatal(e) =>
throw new RuntimeException(s"Unexpected response from $externalServiceUri: ${e.getMessage}")
}
}

def subscribe(boxId: BoxId, callbackUrl: String)(implicit hc: HeaderCarrier): Future[Unit] = {
val payload = UpdateSubscriberRequest(SubscriberRequest(callbackUrl, "API_PUSH_SUBSCRIBER"))

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}")
}
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}")
}
}

def updateCallBackUrl(clientId: ClientId, boxId: BoxId, callbackUrl: FieldValue)
(implicit hc: HeaderCarrier): Future[PPNSCallBackUrlValidationResponse] = {
def updateCallBackUrl(clientId: ClientId, boxId: BoxId, callbackUrl: FieldValue)(implicit hc: HeaderCarrier): Future[PPNSCallBackUrlValidationResponse] = {
val payload = UpdateCallBackUrlRequest(clientId, callbackUrl)
http.PUT[UpdateCallBackUrlRequest, UpdateCallBackUrlResponse](s"$externalServiceUri/box/${boxId.value.toString}/callback", payload)
http
.PUT[UpdateCallBackUrlRequest, UpdateCallBackUrlResponse](s"$externalServiceUri/box/${boxId.value.toString}/callback", payload)
.map(response =>
if (response.successful) PPNSCallBackUrlSuccessResponse
else response.errorMessage.fold(PPNSCallBackUrlFailedResponse("Unknown Error"))(PPNSCallBackUrlFailedResponse)
).recover {
case NonFatal(e) => throw new RuntimeException(s"Unexpected response from $externalServiceUri: ${e.getMessage}")
}
)
.recover { case NonFatal(e) =>
throw new RuntimeException(s"Unexpected response from $externalServiceUri: ${e.getMessage}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@

package uk.gov.hmrc.apisubscriptionfields.controller

import java.util.UUID
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}

import play.api.libs.json.{JsValue, Json, JsSuccess, JsError}
import play.api.libs.json.{JsError, JsSuccess, JsValue, Json}
import play.api.mvc._
import uk.gov.hmrc.apisubscriptionfields.model._

import uk.gov.hmrc.apisubscriptionfields.model._
import uk.gov.hmrc.apisubscriptionfields.service.ApiFieldDefinitionsService
import scala.concurrent.Future
import scala.util.{Try,Success,Failure}
import java.util.UUID
import scala.concurrent.ExecutionContext
import uk.gov.hmrc.apisubscriptionfields.utils.ApplicationLogger

@Singleton
class ApiFieldDefinitionsController @Inject() (cc: ControllerComponents, service: ApiFieldDefinitionsService)(implicit ec: ExecutionContext) extends CommonController with ApplicationLogger {
class ApiFieldDefinitionsController @Inject() (cc: ControllerComponents, service: ApiFieldDefinitionsService)(implicit ec: ExecutionContext)
extends CommonController
with ApplicationLogger {

import JsonFormatters._

Expand All @@ -46,25 +47,25 @@ class ApiFieldDefinitionsController @Inject() (cc: ControllerComponents, service
def validateFieldsDefinition(): Action[JsValue] = Action(parse.json) { request =>
Try(request.body.validate[FieldDefinitionsRequest]) match {
case Success(JsSuccess(payload, _)) => Ok("")
case Success(JsError(errs)) => {
badRequestWithTag( (tag:UUID) => s"A JSON error occurred: [${tag.toString}] ${Json.prettyPrint(JsError.toJson(errs))}")
case Success(JsError(errs)) => {
badRequestWithTag((tag: UUID) => s"A JSON error occurred: [${tag.toString}] ${Json.prettyPrint(JsError.toJson(errs))}")
}
case Failure(e) => {
badRequestWithTag{ (tag:UUID) => s"An error occurred during JSON validation: [${tag.toString}] ${e.getMessage}" }
case Failure(e) => {
badRequestWithTag { (tag: UUID) => s"An error occurred during JSON validation: [${tag.toString}] ${e.getMessage}" }
}
}
}

def upsertFieldsDefinition(apiContext: ApiContext, apiVersion: ApiVersion): Action[JsValue] = Action.async(parse.json) { implicit request =>
withJsonBody[FieldDefinitionsRequest] { payload =>
service.upsert(apiContext, apiVersion, payload.fieldDefinitions) map {
case (response, true) => Created(Json.toJson(response))
case (response, true) => Created(Json.toJson(response))
case (response, false) => Ok(Json.toJson(response))
}
} recover recovery
}

def getAllFieldsDefinitions: Action[AnyContent] = Action.async { _ =>
def getAllFieldsDefinitions: Action[AnyContent] = Action.async { _ =>
service.getAll map (defs => Ok(Json.toJson(defs))) recover recovery
}

Expand All @@ -75,15 +76,15 @@ class ApiFieldDefinitionsController @Inject() (cc: ControllerComponents, service

def deleteFieldsDefinition(apiContext: ApiContext, apiVersion: ApiVersion): Action[AnyContent] = Action.async { _ =>
service.delete(apiContext, apiVersion) map {
case true => NoContent
case true => NoContent
case false => notFoundResponse(apiContext, apiVersion)
} recover recovery
}

private def asActionResult(eventualMaybeResponse: Future[Option[ApiFieldDefinitions]], apiContext: ApiContext, apiVersion: ApiVersion) = {
eventualMaybeResponse map {
case Some(subscriptionFields) => Ok(Json.toJson(subscriptionFields))
case None => notFoundResponse(apiContext, apiVersion)
case None => notFoundResponse(apiContext, apiVersion)
} recover recovery
}

Expand Down
59 changes: 31 additions & 28 deletions app/uk/gov/hmrc/apisubscriptionfields/controller/Binders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,39 @@

package uk.gov.hmrc.apisubscriptionfields.controller

import play.api.mvc.PathBindable
import uk.gov.hmrc.apisubscriptionfields.model.{ApiContext,ApiVersion, ClientId, SubscriptionFieldsId}
import java.{util => ju}

object Binders {

implicit object apiContextPathBindable extends PathBindable.Parsing[ApiContext](
ApiContext.apply,
_.value,
(key: String, e: Exception) => "Cannot parse parameter %s as ApiContext: %s".format(key, e.getMessage)
)

implicit object apiVersionPathBindable extends PathBindable.Parsing[ApiVersion](
ApiVersion.apply,
_.value,
(key: String, e: Exception) => "Cannot parse parameter %s as ApiVersion: %s".format(key, e.getMessage)
)

implicit object clientIdPathBindable extends PathBindable.Parsing[ClientId](
ClientId.apply,
_.value,
(key: String, e: Exception) => "Cannot parse parameter %s as ClientId: %s".format(key, e.getMessage)
)

implicit object subscriptionFieldsIdPathBindable extends PathBindable.Parsing[SubscriptionFieldsId](
s => SubscriptionFieldsId(ju.UUID.fromString(s)),
_.value.toString,
(key: String, e: Exception) => "Cannot parse parameter %s as SubscriptionFieldsId: %s".format(key, e.getMessage)
)
}
import play.api.mvc.PathBindable

import uk.gov.hmrc.apisubscriptionfields.model.{ApiContext, ApiVersion, ClientId, SubscriptionFieldsId}

object Binders {

implicit object apiContextPathBindable
extends PathBindable.Parsing[ApiContext](
ApiContext.apply,
_.value,
(key: String, e: Exception) => "Cannot parse parameter %s as ApiContext: %s".format(key, e.getMessage)
)

implicit object apiVersionPathBindable
extends PathBindable.Parsing[ApiVersion](
ApiVersion.apply,
_.value,
(key: String, e: Exception) => "Cannot parse parameter %s as ApiVersion: %s".format(key, e.getMessage)
)

implicit object clientIdPathBindable
extends PathBindable.Parsing[ClientId](
ClientId.apply,
_.value,
(key: String, e: Exception) => "Cannot parse parameter %s as ClientId: %s".format(key, e.getMessage)
)

implicit object subscriptionFieldsIdPathBindable
extends PathBindable.Parsing[SubscriptionFieldsId](
s => SubscriptionFieldsId(ju.UUID.fromString(s)),
_.value.toString,
(key: String, e: Exception) => "Cannot parse parameter %s as SubscriptionFieldsId: %s".format(key, e.getMessage)
)
}
Loading

0 comments on commit 0643b24

Please sign in to comment.