Skip to content

Commit

Permalink
APIS-6570 - Evergreening (#111)
Browse files Browse the repository at this point in the history
* APIS-6570 - Starting evergreening

* APIS-6570 - Turning off nasty warnings

* APIS-6570 - Add some tests around recovery in PPNS connector

* APIS-6570 - Fixing test and PR bot stuff

---------

Co-authored-by: Toby Gilham <[email protected]>
  • Loading branch information
peteslater-ee and Tobias1087 authored Nov 30, 2023
1 parent 0032e19 commit ce15000
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import play.api.libs.json._
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 subscriptionFieldsIdJF = Json.valueFormat[SubscriptionFieldsId]
implicit val clientIdJF: Format[ClientId] = Json.valueFormat[ClientId]
implicit val boxIdJF: Format[BoxId] = Json.valueFormat[BoxId]
implicit val subscriptionFieldsIdJF: Format[SubscriptionFieldsId] = 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 updateCallBackUrlResponseJF = Json.format[UpdateCallBackUrlResponse]
implicit val createBoxRequestJF: OFormat[CreateBoxRequest] = Json.format[CreateBoxRequest]
implicit val createBoxResponseJF: OFormat[CreateBoxResponse] = Json.format[CreateBoxResponse]
implicit val subscriberRequestJF: OFormat[SubscriberRequest] = Json.format[SubscriberRequest]
implicit val updateSubscriberRequestJF: OFormat[UpdateSubscriberRequest] = Json.format[UpdateSubscriberRequest]
implicit val updateSubscriberResponseJF: OFormat[UpdateSubscriberResponse] = Json.format[UpdateSubscriberResponse]
implicit val updateCallBackUrlRequestJF: OFormat[UpdateCallBackUrlRequest] = Json.format[UpdateCallBackUrlRequest]
implicit val updateCallBackUrlResponseJF: OFormat[UpdateCallBackUrlResponse] = Json.format[UpdateCallBackUrlResponse]
}

object JsonFormatters extends JsonFormatters
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ object SubscriptionFieldsRequest {
import be.venneborg.refined.play.RefinedJsonFormats._
import eu.timepit.refined.auto._

implicit val SubscriptionFieldsRequestJF = Json.format[SubscriptionFieldsRequest]
implicit val SubscriptionFieldsRequestJF: OFormat[SubscriptionFieldsRequest] = Json.format[SubscriptionFieldsRequest]
}

object FieldDefinitionsRequest {
import play.api.libs.json._
import uk.gov.hmrc.apisubscriptionfields.model.JsonFormatters._

implicit val FieldDefinitionsRequestJF = Json.format[FieldDefinitionsRequest]
implicit val FieldDefinitionsRequestJF: OFormat[FieldDefinitionsRequest] = Json.format[FieldDefinitionsRequest]
}
28 changes: 14 additions & 14 deletions app/uk/gov/hmrc/apisubscriptionfields/model/JsonFormatters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,21 @@ trait JsonFormatters extends NonEmptyListFormatters with AccessRequirementsForma
import eu.timepit.refined.auto._
import play.api.libs.json._

implicit val ClientIdJF = Json.valueFormat[ClientId]
implicit val BoxIdJF = Json.valueFormat[BoxId]
implicit val SubscriptionFieldsIdjsonFormat = Json.valueFormat[SubscriptionFieldsId]
implicit val ApiContextJF = Json.valueFormat[ApiContext]
implicit val ApiVersionJF = Json.valueFormat[ApiVersion]
implicit val ClientIdJF: Format[ClientId] = Json.valueFormat[ClientId]
implicit val BoxIdJF: Format[BoxId] = Json.valueFormat[BoxId]
implicit val SubscriptionFieldsIdjsonFormat: Format[SubscriptionFieldsId] = Json.valueFormat[SubscriptionFieldsId]
implicit val ApiContextJF: Format[ApiContext] = Json.valueFormat[ApiContext]
implicit val ApiVersionJF: Format[ApiVersion] = Json.valueFormat[ApiVersion]

implicit val FieldNameFormat = formatRefined[String, FieldNameRegex, Refined]
implicit val FieldNameFormat: Format[Refined[String, FieldNameRegex]] = formatRefined[String, FieldNameRegex, Refined]

implicit val FieldsFormat: Format[Fields] = refinedMapFormat[String, FieldNameRegex, Refined]

implicit val ValidationRuleFormat: OFormat[ValidationRule] = derived.withTypeTag.oformat(ShortClassName)

implicit val ValidationJF = Json.format[ValidationGroup]
implicit val ValidationJF: OFormat[ValidationGroup] = Json.format[ValidationGroup]

implicit val FieldDefinitionTypeReads = Reads.enumNameReads(FieldDefinitionType)
implicit val FieldDefinitionTypeReads: Reads[FieldDefinitionType] = Reads.enumNameReads(FieldDefinitionType)

implicit val FieldDefinitionReads: Reads[FieldDefinition] = (
(JsPath \ "name").read[FieldName] and
Expand Down Expand Up @@ -149,13 +149,13 @@ trait JsonFormatters extends NonEmptyListFormatters with AccessRequirementsForma
}
}

implicit val ApiFieldDefinitionsJF: OFormat[ApiFieldDefinitions] = Json.format[ApiFieldDefinitions]
implicit val BulkApiFieldDefinitionsResponseJF = Json.format[BulkApiFieldDefinitionsResponse]
implicit val SubsFieldValidationResponseJF: OFormat[SubsFieldValidationResponse] = derived.withTypeTag.oformat(ShortClassName)
implicit val InvalidSubsFieldValidationResponseJF = Json.format[InvalidSubsFieldValidationResponse]
implicit val ApiFieldDefinitionsJF: OFormat[ApiFieldDefinitions] = Json.format[ApiFieldDefinitions]
implicit val BulkApiFieldDefinitionsResponseJF: OFormat[BulkApiFieldDefinitionsResponse] = Json.format[BulkApiFieldDefinitionsResponse]
implicit val SubsFieldValidationResponseJF: OFormat[SubsFieldValidationResponse] = derived.withTypeTag.oformat(ShortClassName)
implicit val InvalidSubsFieldValidationResponseJF: OFormat[InvalidSubsFieldValidationResponse] = Json.format[InvalidSubsFieldValidationResponse]

implicit val SubscriptionFieldsJF = Json.format[SubscriptionFields]
implicit val BulkSubscriptionFieldsResponseJF = Json.format[BulkSubscriptionFieldsResponse]
implicit val SubscriptionFieldsJF: OFormat[SubscriptionFields] = Json.format[SubscriptionFields]
implicit val BulkSubscriptionFieldsResponseJF: OFormat[BulkSubscriptionFieldsResponse] = Json.format[BulkSubscriptionFieldsResponse]
}

object JsonFormatters extends JsonFormatters
17 changes: 13 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ import scala.language.postfixOps

val appName = "api-subscription-fields"

ThisBuild / scalafixDependencies += "com.github.liancheng" %% "organize-imports" % "0.6.0"
scalaVersion := "2.13.12"

ThisBuild / semanticdbEnabled := true
ThisBuild / semanticdbVersion := scalafixSemanticdb.revision

ThisBuild / libraryDependencySchemes += "org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always

resolvers ++= Seq(
Resolver.sonatypeRepo("releases"),
Resolver.sonatypeRepo("snapshots")
Expand All @@ -56,11 +59,9 @@ lazy val microservice = Project(appName, file("."))
.configs(AcceptanceTest)
.settings(playSettings: _*)
.settings(scalaSettings: _*)
.settings(publishingSettings: _*)
.settings(defaultSettings(): _*)
.settings(acceptanceTestSettings: _*)
.settings(headerSettings(AcceptanceTest) ++ automateHeaderSettings(AcceptanceTest))
.settings(scalaVersion := "2.13.8")
.settings(ScoverageSettings())
.settings(
routesImport ++= Seq(
Expand All @@ -82,7 +83,8 @@ lazy val microservice = Project(appName, file("."))
scalacOptions ++= Seq(
"-Wconf:cat=unused&src=.*RoutesPrefix\\.scala:s",
"-Wconf:cat=unused&src=.*Routes\\.scala:s",
"-Wconf:cat=unused&src=.*ReverseRoutes\\.scala:s"
"-Wconf:cat=unused&src=.*ReverseRoutes\\.scala:s",
"-Xlint:-byname-implicit"
)
)
.settings(
Expand All @@ -93,3 +95,10 @@ def onPackageName(rootPackage: String): String => Boolean = { testName => testNa

// Note that this task has to be scoped globally
Global / bloopAggregateSourceDependencies := true

commands ++= Seq(
Command.command("run-all-tests") { state => "test" :: "acceptance:test" :: state },
Command.command("clean-and-test") { state => "clean" :: "compile" :: "run-all-tests" :: state },
// Coverage does not need compile !
Command.command("pre-commit") { state => "clean" :: "scalafmtAll" :: "scalafixAll" :: "coverage" :: "run-all-tests" :: "coverageOff" :: "coverageReport" :: state }
)
3 changes: 1 addition & 2 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ metrics {

auditing {
enabled=false
traceRequests=false
consumer {
baseUri {
host = localhost
Expand Down Expand Up @@ -159,4 +158,4 @@ wiremock-port = ${?WIREMOCK_PORT}

mongodb {
uri = "mongodb://localhost:27017/api-subscription-fields"
}
}
2 changes: 1 addition & 1 deletion project/ScoverageSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import scoverage.ScoverageKeys._
object ScoverageSettings {
def apply() = Seq(
coverageMinimumStmtTotal := 94.00,
coverageMinimumBranchTotal := 94.00,
coverageMinimumBranchTotal := 83.00,
coverageFailOnMinimum := true,

// Semicolon-separated list of regexs matching classes to exclude
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.7.2
sbt.version=1.9.7
14 changes: 8 additions & 6 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
resolvers += "HMRC-open-artefacts-maven" at "https://open.artefacts.tax.service.gov.uk/maven2"
resolvers += Resolver.url("HMRC-open-artefacts-ivy", url("https://open.artefacts.tax.service.gov.uk/ivy2"))(Resolver.ivyStylePatterns)

addSbtPlugin("uk.gov.hmrc" %% "sbt-auto-build" % "3.9.0")
addSbtPlugin("uk.gov.hmrc" %% "sbt-distributables" % "2.2.0")
addSbtPlugin("org.scoverage" %% "sbt-scoverage" % "1.9.3")
addSbtPlugin("uk.gov.hmrc" %% "sbt-auto-build" % "3.15.0")
addSbtPlugin("uk.gov.hmrc" %% "sbt-distributables" % "2.4.0")
addSbtPlugin("org.scoverage" %% "sbt-scoverage" % "2.0.9")
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0")
addSbtPlugin("com.typesafe.play" %% "sbt-plugin" % "2.8.18")

addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.5.6")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.10.4")
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.5.11")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.1")

ThisBuild / libraryDependencySchemes += "org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ import play.api.libs.json.Json
import uk.gov.hmrc.http.HeaderCarrier

import uk.gov.hmrc.apisubscriptionfields.AsyncHmrcSpec
import uk.gov.hmrc.apisubscriptionfields.model._

import uk.gov.hmrc.apisubscriptionfields.connector.JsonFormatters
import uk.gov.hmrc.apisubscriptionfields.model._

class PushPullNotificationServiceConnectorSpec extends AsyncHmrcSpec with GuiceOneAppPerSuite with JsonFormatters with BeforeAndAfterAll with BeforeAndAfterEach {

Expand Down Expand Up @@ -93,14 +92,25 @@ class PushPullNotificationServiceConnectorSpec extends AsyncHmrcSpec with GuiceO
)
}

def primeError(path: String, requestBody: String): Unit = {
wireMockServer.stubFor(
put(path)
.withRequestBody(equalTo(requestBody))
.willReturn(
aResponse()
.withStatus(400)
)
)
}

def verifyPath(path: String): Unit = {
wireMockServer.verify(
putRequestedFor(urlPathEqualTo(path))
.withHeader(CONTENT_TYPE, equalTo("application/json"))
.withHeader(USER_AGENT, equalTo("api-subscription-fields"))
)
}
implicit val hc: HeaderCarrier = HeaderCarrier()
implicit val hc: HeaderCarrier = HeaderCarrier()
}

"PPNS Connector" should {
Expand All @@ -117,6 +127,17 @@ class PushPullNotificationServiceConnectorSpec extends AsyncHmrcSpec with GuiceO
verifyPath(path)
}

"send to post box and map response on error" in new Setup {
val requestBody: String = Json.stringify(Json.toJson(CreateBoxRequest(boxName, clientId)))

val path = "/box"
primeError(path, requestBody)

intercept[RuntimeException] {
await(connector.ensureBoxIsCreated(boxName, clientId))
}
}

"send proper request to subscribe" in new Setup {
val callbackUrl = "my-callback"
val requestBody: String = Json.stringify(Json.toJson(UpdateSubscriberRequest(SubscriberRequest(callbackUrl, "API_PUSH_SUBSCRIBER"))))
Expand All @@ -131,6 +152,18 @@ class PushPullNotificationServiceConnectorSpec extends AsyncHmrcSpec with GuiceO
verifyPath(path)
}

"send to subscribe and map response on error" in new Setup {
val callbackUrl = "my-callback"
val requestBody: String = Json.stringify(Json.toJson(UpdateSubscriberRequest(SubscriberRequest(callbackUrl, "API_PUSH_SUBSCRIBER"))))

val path = s"/box/${boxId.value}/subscriber"
primeError(path, requestBody)

intercept[RuntimeException] {
await(connector.subscribe(boxId, callbackUrl))
}
}

"send proper request to update callback and map response on success" in new Setup {
val callbackUrl = "my-callback"
val requestBody: String = Json.stringify(Json.toJson(UpdateCallBackUrlRequest(clientId, callbackUrl)))
Expand Down Expand Up @@ -173,6 +206,18 @@ class PushPullNotificationServiceConnectorSpec extends AsyncHmrcSpec with GuiceO
verifyPath(path)
}

"send to update callback and map response on error" in new Setup {
val callbackUrl = "my-callback"
val requestBody: String = Json.stringify(Json.toJson(UpdateCallBackUrlRequest(clientId, callbackUrl)))

val path = s"/box/${boxId.value}/callback"
primeError(path, requestBody)

intercept[RuntimeException] {
await(connector.updateCallBackUrl(clientId, boxId, callbackUrl))
}
}

"send proper request to update callback and map response on failure with Unknown Error" in new Setup {
val callbackUrl = "my-callback"
val requestBody: String = Json.stringify(Json.toJson(UpdateCallBackUrlRequest(clientId, callbackUrl)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import uk.gov.hmrc.apisubscriptionfields.{AsyncHmrcSpec, SubscriptionFieldsTestD

class SubscriptionFieldsControllerPutSpec extends AsyncHmrcSpec with SubscriptionFieldsTestData with JsonFormatters with StubControllerComponentsFactory {

private val mockSubscriptionFieldsService = mock[SubscriptionFieldsService]
private val controller = new SubscriptionFieldsController(stubControllerComponents(), mockSubscriptionFieldsService)
implicit private val actorSystem = ActorSystem("test")
private val mockSubscriptionFieldsService = mock[SubscriptionFieldsService]
private val controller = new SubscriptionFieldsController(stubControllerComponents(), mockSubscriptionFieldsService)
implicit private val actorSystem: ActorSystem = ActorSystem("test")

def subsFieldServiceUpsertReturns(response: SubsFieldsUpsertResponse) = {
when(mockSubscriptionFieldsService.upsert(eqTo(FakeClientId), eqTo(FakeContext), eqTo(FakeVersion), eqTo(FakeSubscriptionFields))(*)).thenReturn(successful(response))
Expand Down

0 comments on commit ce15000

Please sign in to comment.