Skip to content

Commit c47b795

Browse files
authored
APIS-6687 - Update mongo driver and remove enums (#84)
1 parent 28c85f3 commit c47b795

File tree

9 files changed

+52
-80
lines changed

9 files changed

+52
-80
lines changed

app/uk/gov/hmrc/apiscope/controllers/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ package object controllers extends ApplicationLogger {
7676
InternalServerError(error(ErrorCode.UNKNOWN_ERROR, "An unexpected error occurred"))
7777
}
7878

79-
def error(code: ErrorCode.Value, message: String): JsValue = {
79+
def error(code: ErrorCode, message: String): JsValue = {
8080
toJson(ErrorResponse(code, message))
8181
}
8282

app/uk/gov/hmrc/apiscope/models/ConfidenceLevel.scala

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,28 @@
1616

1717
package uk.gov.hmrc.apiscope.models
1818

19-
import scala.collection.immutable
19+
import play.api.libs.json._
2020

21-
import enumeratum.values.{IntEnum, IntEnumEntry, IntPlayJsonValueEnum}
21+
sealed abstract class ConfidenceLevel(val value: Int)
2222

23-
sealed abstract class ConfidenceLevel(val value: Int) extends IntEnumEntry
24-
25-
object ConfidenceLevel extends IntEnum[ConfidenceLevel] with IntPlayJsonValueEnum[ConfidenceLevel] {
26-
val values: immutable.IndexedSeq[ConfidenceLevel] = findValues
23+
object ConfidenceLevel {
2724

2825
case object L50 extends ConfidenceLevel(50)
2926
case object L200 extends ConfidenceLevel(200)
3027
case object L250 extends ConfidenceLevel(250)
3128
case object L500 extends ConfidenceLevel(500)
29+
val values: Set[ConfidenceLevel] = Set(L50, L200, L250, L500)
30+
def apply(int: Int): Option[ConfidenceLevel] = ConfidenceLevel.values.find(_.value == int)
31+
32+
def unsafeApply(int: Int): ConfidenceLevel = apply(int).getOrElse(throw new RuntimeException(s"$int is not a valid Confidence Level"))
33+
34+
private val reads: Reads[ConfidenceLevel] = {
35+
case JsNumber(number) => apply(number.intValue).fold[JsResult[ConfidenceLevel]] { JsError(s"$number is not a valid Confidence Level") }(JsSuccess(_))
36+
case e => JsError(s"Cannot parse Confidence Level from '$e'")
37+
}
38+
39+
private val writes: Writes[ConfidenceLevel] = level => JsNumber(level.value)
40+
41+
implicit val format: Format[ConfidenceLevel] = Format(reads, writes)
42+
3243
}

app/uk/gov/hmrc/apiscope/models/EnumJson.scala

Lines changed: 0 additions & 45 deletions
This file was deleted.

app/uk/gov/hmrc/apiscope/models/ErrorCode.scala

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,28 @@
1717
package uk.gov.hmrc.apiscope.models
1818

1919
import play.api.libs.json._
20+
import uk.gov.hmrc.apiplatform.modules.common.domain.services.SealedTraitJsonFormatting
2021

21-
object ErrorCode extends Enumeration {
22+
sealed trait ErrorCode
2223

23-
implicit val format: Format[ErrorCode.Value] = EnumJson.enumFormat(ErrorCode)
24+
object ErrorCode {
25+
case object SCOPE_NOT_FOUND extends ErrorCode
26+
case object INVALID_REQUEST_PAYLOAD extends ErrorCode
27+
case object UNKNOWN_ERROR extends ErrorCode
28+
case object API_INVALID_JSON extends ErrorCode
29+
case object API_SCOPE_ALREADY_IN_USE extends ErrorCode
30+
val values: Set[ErrorCode] = Set(SCOPE_NOT_FOUND, INVALID_REQUEST_PAYLOAD, UNKNOWN_ERROR, API_INVALID_JSON, API_SCOPE_ALREADY_IN_USE)
31+
def apply(text: String): Option[ErrorCode] = ErrorCode.values.find(_.toString() == text.toUpperCase)
2432

25-
type ErrorCode = Value
33+
def unsafeApply(text: String): ErrorCode = apply(text).getOrElse(throw new RuntimeException(s"$text is not a valid Error Code"))
2634

27-
val SCOPE_NOT_FOUND = Value("SCOPE_NOT_FOUND")
28-
val INVALID_REQUEST_PAYLOAD = Value("INVALID_REQUEST_PAYLOAD")
29-
val UNKNOWN_ERROR = Value("UNKNOWN_ERROR")
30-
val API_INVALID_JSON = Value("API_INVALID_JSON")
31-
val API_SCOPE_ALREADY_IN_USE = Value("API_SCOPE_ALREADY_IN_USE")
35+
implicit val format: Format[ErrorCode] = SealedTraitJsonFormatting.createFormatFor[ErrorCode]("Error Code", apply)
3236
}
3337

34-
case class ErrorResponse(code: ErrorCode.Value, message: String, details: Option[Seq[ErrorDescription]] = None)
38+
case class ErrorResponse(code: ErrorCode, message: String, details: Option[Seq[ErrorDescription]] = None)
3539

3640
object ErrorResponse {
3741
implicit val format1: OFormat[ErrorDescription] = Json.format[ErrorDescription]
38-
implicit val format2: Format[ErrorCode.Value] = EnumJson.enumFormat(ErrorCode)
3942
implicit val format3: OFormat[ErrorResponse] = Json.format[ErrorResponse]
4043
}
4144

app/uk/gov/hmrc/apiscope/repository/ScopeRepository.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class ScopeRepository @Inject() (mongoComponent: MongoComponent)(implicit val ec
7272
replaceIndexes = true,
7373
extraCodecs = Seq(Codecs.playFormatCodec(ScopeFormats.scopeFormat))
7474
) {
75-
private val logger = Logger(this.getClass)
75+
private val logger = Logger(this.getClass)
76+
override lazy val requiresTtlIndex = false
7677

7778
def save(scope: Scope): Future[Scope] = {
7879
var updateSeq = Seq(

integration/uk/gov/hmrc/apiscope/ScopeFeatureSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class ScopeFeatureSpec extends BaseFeatureSpec {
9595

9696
def dropDatabase = {
9797
repository.collection.drop()
98-
result(repository.ensureIndexes, timeout)
98+
result(repository.ensureIndexes(), timeout)
9999
}
100100

101101
}

project/AppDependencies.scala

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@ object AppDependencies {
66
lazy val libraryDependencies = compile ++ test
77

88
private lazy val bootstrapVersion = "7.12.0"
9-
private lazy val hmrcMongoVersion = "0.74.0"
10-
private lazy val scalaJVersion = "2.4.2"
9+
private lazy val hmrcMongoVersion = "1.7.0"
10+
private lazy val scalaJVersion = "2.4.2"
11+
val commonDomainVersion = "0.10.0"
1112

1213
private lazy val compile = Seq(
13-
"uk.gov.hmrc" %% "bootstrap-backend-play-28" % bootstrapVersion,
14-
"uk.gov.hmrc.mongo" %% "hmrc-mongo-play-28" % hmrcMongoVersion,
15-
"com.beachape" %% "enumeratum-play" % "1.7.0"
14+
"uk.gov.hmrc" %% "bootstrap-backend-play-28" % bootstrapVersion,
15+
"uk.gov.hmrc.mongo" %% "hmrc-mongo-play-28" % hmrcMongoVersion,
16+
"uk.gov.hmrc" %% "api-platform-common-domain" % commonDomainVersion
1617
)
1718

1819
private lazy val test = Seq(
19-
"uk.gov.hmrc.mongo" %% "hmrc-mongo-test-play-28" % hmrcMongoVersion,
20-
"org.scalaj" %% "scalaj-http" % scalaJVersion,
21-
"uk.gov.hmrc" %% "bootstrap-test-play-28" % bootstrapVersion,
22-
"org.mockito" %% "mockito-scala-scalatest" % "1.17.29"
20+
"uk.gov.hmrc.mongo" %% "hmrc-mongo-test-play-28" % hmrcMongoVersion,
21+
"org.scalaj" %% "scalaj-http" % scalaJVersion,
22+
"uk.gov.hmrc" %% "bootstrap-test-play-28" % bootstrapVersion,
23+
"org.mockito" %% "mockito-scala-scalatest" % "1.17.29",
24+
"uk.gov.hmrc" %% "api-platform-test-common-domain" % commonDomainVersion
2325
).map(_ % "test, it")
2426

2527
}

test/uk/gov/hmrc/apiscope/models/ErrorCodeSpec.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class ErrorCodeSpec extends HmrcSpec {
2424

2525
"read" should {
2626
"read valid error code" in {
27-
Json.fromJson[ErrorCode.Value](JsString("SCOPE_NOT_FOUND")) shouldBe JsSuccess(ErrorCode.SCOPE_NOT_FOUND)
27+
Json.fromJson[ErrorCode](JsString("SCOPE_NOT_FOUND")) shouldBe JsSuccess(ErrorCode.SCOPE_NOT_FOUND)
2828
}
2929

3030
"report invalid error code" in {
31-
Json.fromJson[ErrorCode.Value](JsNumber(0)) should matchPattern {
31+
Json.fromJson[ErrorCode](JsNumber(0)) should matchPattern {
3232
case e: JsError =>
3333
}
34-
Json.fromJson[ErrorCode.Value](JsString("NOT VALID")) should matchPattern {
34+
Json.fromJson[ErrorCode](JsString("NOT VALID")) should matchPattern {
3535
case e: JsError =>
3636
}
3737
}

test/uk/gov/hmrc/apiscope/repository/ScopeRepositorySpec.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class ScopeRepositorySpec extends AsyncHmrcSpec
3737
with MongoApp[Scope]
3838
with Eventually {
3939

40-
val scope1 = Scope("key1", "name1", "description1")
41-
val scope2 = Scope("key2", "name2", "description2", confidenceLevel = Some(L200))
42-
val repo = repository.asInstanceOf[ScopeRepository]
40+
val scope1: Scope = Scope("key1", "name1", "description1")
41+
val scope2: Scope = Scope("key2", "name2", "description2", confidenceLevel = Some(L200))
4342

44-
override protected def repository: PlayMongoRepository[Scope] = app.injector.instanceOf[ScopeRepository]
43+
override protected val repository: PlayMongoRepository[Scope] = app.injector.instanceOf[ScopeRepository]
44+
val repo: ScopeRepository = repository.asInstanceOf[ScopeRepository]
4545

4646
private def getIndexes(): List[BsonDocument] = {
4747
await(repo.collection.listIndexes().map(toBsonDocument).toFuture().map(_.toList))

0 commit comments

Comments
 (0)