@@ -18,33 +18,49 @@ package uk.gov.hmrc.apiscope.repository
1818
1919import scala .concurrent .ExecutionContext .Implicits .global
2020
21+ import org .bson .codecs .configuration .CodecRegistries
2122import org .mongodb .scala .Document
23+ import org .mongodb .scala .MongoClient .DEFAULT_CODEC_REGISTRY
2224import org .mongodb .scala .bson .{BsonDocument , BsonString }
23- import org .scalatest .concurrent . Eventually
25+ import org .scalatest .BeforeAndAfterEach
2426import org .scalatest .matchers .must .Matchers .convertToAnyMustWrapper
25- import org .scalatest .{BeforeAndAfterAll , BeforeAndAfterEach }
2627import org .scalatestplus .play .guice .GuiceOneAppPerSuite
2728
28- import uk .gov .hmrc .mongo .play .json .PlayMongoRepository
29+ import play .api .Application
30+ import play .api .inject .guice .GuiceApplicationBuilder
31+ import play .api .libs .json .{Format , JsObject , Json }
32+ import uk .gov .hmrc .mongo .play .json .Codecs
33+ import uk .gov .hmrc .mongo .test .DefaultPlayMongoRepositorySupport
2934
3035import uk .gov .hmrc .apiscope .models .ConfidenceLevel ._
3136import uk .gov .hmrc .apiscope .models .Scope
3237import uk .gov .hmrc .util .AsyncHmrcSpec
3338
3439class ScopeRepositorySpec extends AsyncHmrcSpec
35- with BeforeAndAfterEach with BeforeAndAfterAll
40+ with BeforeAndAfterEach
3641 with GuiceOneAppPerSuite
37- with MongoApp [Scope ]
38- with Eventually {
42+ with DefaultPlayMongoRepositorySupport [Scope ] {
3943
40- val scope1 : Scope = Scope (" key1" , " name1" , " description1" )
41- val scope2 : Scope = Scope (" key2" , " name2" , " description2" , confidenceLevel = Some (L200 ))
44+ val basicScope : Scope = Scope (" key1" , " name1" , " description1" )
45+ val scopeConfidence200 : Scope = Scope (" key2" , " name2" , " description2" , confidenceLevel = Some (L200 ))
46+ val scopeConfidence500 : Scope = Scope (" key3" , " name3" , " description3" , confidenceLevel = Some (L500 ))
4247
43- override protected val repository : PlayMongoRepository [ Scope ] = app.injector.instanceOf[ScopeRepository ]
44- val repo : ScopeRepository = repository. asInstanceOf [ ScopeRepository ]
48+ override val repository : ScopeRepository = app.injector.instanceOf[ScopeRepository ]
49+ override implicit lazy val app : Application = appBuilder.build()
4550
4651 private def getIndexes (): List [BsonDocument ] = {
47- await(repo.collection.listIndexes().map(toBsonDocument).toFuture().map(_.toList))
52+ await(repository.collection.listIndexes().map(toBsonDocument).toFuture().map(_.toList))
53+ }
54+
55+ def insertRaw (raw : JsObject ) = {
56+ val db = mongoComponent.database.withCodecRegistry(
57+ CodecRegistries .fromRegistries(
58+ CodecRegistries .fromCodecs(Codecs .playFormatCodec[Scope ](repository.domainFormat)),
59+ CodecRegistries .fromCodecs(Codecs .playFormatCodec[JsObject ](implicitly[Format [JsObject ]])),
60+ DEFAULT_CODEC_REGISTRY
61+ )
62+ )
63+ await(db.getCollection[JsObject ](repository.collectionName).insertOne(raw).toFuture())
4864 }
4965
5066 private def toBsonDocument (index : Document ): BsonDocument = {
@@ -55,38 +71,97 @@ class ScopeRepositorySpec extends AsyncHmrcSpec
5571 d
5672 }
5773
74+ protected def appBuilder : GuiceApplicationBuilder =
75+ new GuiceApplicationBuilder ()
76+ .configure(
77+ " mongodb.uri" -> s " mongodb://127.0.0.1:27017/test- ${this .getClass.getSimpleName}"
78+ )
79+
5880 " saveScope" should {
5981 " create scopes and retrieve them from database" in {
60- await(repo.save(scope1))
61- await(repo.save(scope2))
82+ await(repository.save(basicScope))
83+ await(repository.save(scopeConfidence200))
84+
85+ await(repository.fetch(basicScope.key)).get shouldBe basicScope
86+ await(repository.fetch(scopeConfidence200.key)).get shouldBe scopeConfidence200
87+ }
6288
63- await(repo.fetch(scope1.key)).get shouldBe scope1
64- await(repo.fetch(scope2.key)).get shouldBe scope2
89+ " create scope with ConfidenceLevel of 500 and retrieve from database" in {
90+ await(repository.save(scopeConfidence500))
91+
92+ await(repository.fetch(scopeConfidence500.key)).get shouldBe scopeConfidence500
93+ }
94+
95+ " create scope with ConfidenceLevel of 250 and retrieve from database" in {
96+ await(repository.save(basicScope.copy(confidenceLevel = Some (L250 ))))
97+
98+ await(repository.fetch(basicScope.key)).head.confidenceLevel shouldBe Some (L250 )
6599 }
66100
67101 " update a scope" in {
68- await(repo.save(scope1))
69- await(repo.save(scope2))
102+ await(repository.save(basicScope))
103+ await(repository.save(scopeConfidence200))
104+
105+ val updatedScope1 = Scope (basicScope.key, " updatedName1" , " updatedDescription1" )
106+ val updatedScope2 = Scope (scopeConfidence200.key, " updatedName2" , " updatedDescription2" , confidenceLevel = Some (L50 ))
70107
71- val updatedScope1 = Scope (scope1.key, " updatedName1 " , " updatedDescription1 " )
72- val updatedScope2 = Scope (scope2.key, " updatedName2 " , " updatedDescription2 " , confidenceLevel = Some ( L50 ))
108+ await(repository.save(updatedScope1) )
109+ await(repository.save(updatedScope2 ))
73110
74- await(repo.save(updatedScope1))
75- await(repo.save(updatedScope2))
111+ await(repository.fetch(basicScope.key)).get shouldEqual updatedScope1
112+ await(repository.fetch(scopeConfidence200.key)).get shouldEqual updatedScope2
113+ }
114+ }
115+ " read a scope" should {
116+ val scopeName = " some scope name"
117+ val scopeKey = " read:some-scope-key"
118+ val scopeDescription = " some scope description"
119+ " map deprecated confidence level 100 to supported 200" in {
120+
121+ val outdatedScope : JsObject =
122+ Json .obj(" key" -> scopeKey, " name" -> scopeName, " description" -> scopeDescription, " confidenceLevel" -> 100 )
123+ insertRaw(outdatedScope)
124+
125+ val scopesRead = await(repository.fetchAll())
126+ scopesRead.size shouldEqual 1
127+ scopesRead.head.confidenceLevel shouldEqual Some (L200 )
128+ }
129+
130+ " map deprecated confidence level 300 to supported 200" in {
131+ val outdatedScope : JsObject =
132+ Json .obj(" key" -> scopeKey, " name" -> scopeName, " description" -> scopeDescription, " confidenceLevel" -> 300 )
133+ insertRaw(outdatedScope)
134+
135+ val scopesRead = await(repository.fetchAll())
136+ scopesRead.size shouldEqual 1
137+ scopesRead.head.confidenceLevel shouldEqual Some (L200 )
138+ }
139+
140+ " handle an unsupported confidence level" in {
141+ val invalidScope : JsObject =
142+ Json .obj(" key" -> scopeKey, " name" -> scopeName, " description" -> scopeDescription, " confidenceLevel" -> 666 )
143+ insertRaw(invalidScope)
144+
145+ val e : RuntimeException = intercept[RuntimeException ] {
146+ await(repository.fetch(scopeKey))
147+ }
148+ e.getMessage should include(" Bad data in confidence level of 666" )
149+ }
76150
77- await(repo.fetch(scope1.key)).get shouldEqual updatedScope1
78- await(repo.fetch(scope2.key)).get shouldEqual updatedScope2
151+ " handle a key which cannot be found" in {
152+ val scopesRead : Option [Scope ] = await(repository.fetch(" some-non-existent-key" ))
153+ scopesRead shouldBe Option .empty
79154 }
80155 }
81156
82157 " fetchAll" should {
83158 " retrieve all the scopes from database" in {
84- await(repo .save(scope1 ))
85- await(repo .save(scope2 ))
159+ await(repository .save(basicScope ))
160+ await(repository .save(scopeConfidence200 ))
86161
87- val allScopes = await(repo .fetchAll())
162+ val allScopes = await(repository .fetchAll())
88163
89- allScopes.should(contain.allOf(scope1, scope2 ))
164+ allScopes.should(contain.allOf(basicScope, scopeConfidence200 ))
90165 }
91166 }
92167
0 commit comments