Skip to content

Commit 265d086

Browse files
committed
[ci] refactor the test
Signed-off-by: Itai Segall <itai.segall@digitalasset.com>
1 parent 1a760b5 commit 265d086

File tree

5 files changed

+132
-86
lines changed

5 files changed

+132
-86
lines changed

apps/common/src/main/scala/org/lfdecentralizedtrust/splice/store/KeyValueStore.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.digitalasset.canton.logging.{ErrorLoggingContext, NamedLoggerFactory,
1010
import com.digitalasset.canton.resource.DbStorage
1111
import com.digitalasset.canton.tracing.TraceContext
1212
import io.circe.{Decoder, Encoder}
13-
import org.lfdecentralizedtrust.splice.store.db.{DbValidatorInternalStore, StoreDescriptor}
13+
import org.lfdecentralizedtrust.splice.store.db.{DbKeyValueStore, StoreDescriptor}
1414

1515
import scala.concurrent.{ExecutionContext, Future}
1616

@@ -67,7 +67,7 @@ object KeyValueStore {
6767
): Future[KeyValueStore] = {
6868
storage match {
6969
case storage: DbStorage =>
70-
DbValidatorInternalStore(
70+
DbKeyValueStore(
7171
descriptor,
7272
tableConfig,
7373
storage,

apps/common/src/main/scala/org/lfdecentralizedtrust/splice/store/db/DbValidatorInternalStore.scala renamed to apps/common/src/main/scala/org/lfdecentralizedtrust/splice/store/db/DbKeyValueStore.scala

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import slick.jdbc.canton.ActionBasedSQLInterpolation.Implicits.actionBasedSQLInt
1818

1919
import scala.concurrent.{ExecutionContext, Future}
2020

21-
class DbValidatorInternalStore private (
21+
class DbKeyValueStore private (
2222
storage: DbStorage,
2323
val dbTableConfig: KeyValueStoreDbTableConfig,
2424
val storeId: Int,
@@ -32,6 +32,9 @@ class DbValidatorInternalStore private (
3232
with NamedLogging {
3333

3434
val profile: JdbcProfile = storage.profile.jdbc
35+
val table = dbTableConfig.tableName
36+
val keyCol = dbTableConfig.keyColumnName
37+
val valCol = dbTableConfig.valueColumnName
3538

3639
override def setValue[T](key: String, value: T)(implicit
3740
tc: TraceContext,
@@ -40,11 +43,10 @@ class DbValidatorInternalStore private (
4043
val jsonValue: Json = value.asJson
4144

4245
val action =
43-
sql"""INSERT INTO ${dbTableConfig.tableName} (${dbTableConfig.keyColumnName}, ${dbTableConfig.valueColumnName}, store_id)
46+
sql"""INSERT INTO #$table (#$keyCol, #$valCol, store_id)
4447
VALUES ($key, $jsonValue, $storeId)
45-
ON CONFLICT (store_id, ${dbTableConfig.keyColumnName}) DO UPDATE
46-
SET ${dbTableConfig.keyColumnName} = excluded.${dbTableConfig.keyColumnName}""".asUpdate
47-
48+
ON CONFLICT (store_id, #$keyCol) DO UPDATE
49+
SET #$valCol = excluded.#$valCol""".asUpdate
4850
val updateAction = storage.update(action, "set-validator-internal-config")
4951

5052
logger.debug(
@@ -59,9 +61,9 @@ class DbValidatorInternalStore private (
5961

6062
logger.debug(s"Retrieving config key $key")
6163

62-
val queryAction = sql"""SELECT ${dbTableConfig.valueColumnName}
63-
FROM ${dbTableConfig.tableName}
64-
WHERE ${dbTableConfig.keyColumnName} = $key AND store_id = $storeId
64+
val queryAction = sql"""SELECT #$valCol
65+
FROM #$table
66+
WHERE #$keyCol = $key AND store_id = $storeId
6567
""".as[Json].headOption
6668

6769
val jsonOptionT: OptionT[FutureUnlessShutdown, Json] =
@@ -95,14 +97,14 @@ class DbValidatorInternalStore private (
9597
)
9698
storage
9799
.update(
98-
sqlu"delete from ${dbTableConfig.tableName} WHERE ${dbTableConfig.keyColumnName} = $key AND store_id = $storeId",
100+
sqlu"delete from #$table WHERE #$keyCol = $key AND store_id = $storeId",
99101
"delete config key",
100102
)
101103
.map(_.discard)
102104
}
103105
}
104106

105-
object DbValidatorInternalStore {
107+
object DbKeyValueStore {
106108

107109
def apply(
108110
storeDescriptor: StoreDescriptor,
@@ -114,12 +116,12 @@ object DbValidatorInternalStore {
114116
lc: ErrorLoggingContext,
115117
cc: CloseContext,
116118
tc: TraceContext,
117-
): Future[DbValidatorInternalStore] = {
119+
): Future[DbKeyValueStore] = {
118120

119121
StoreDescriptorStore
120122
.getStoreIdForDescriptor(storeDescriptor, storage)
121123
.map(storeId => {
122-
new DbValidatorInternalStore(
124+
new DbKeyValueStore(
123125
storage,
124126
dbTableConfig,
125127
storeId,
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package org.lfdecentralizedtrust.splice.store
2+
3+
import com.digitalasset.canton.lifecycle.FutureUnlessShutdown
4+
import com.digitalasset.canton.resource.DbStorage
5+
import com.digitalasset.canton.tracing.TraceContext
6+
import com.digitalasset.canton.HasExecutionContext
7+
import org.lfdecentralizedtrust.splice.store.db.{SplicePostgresTest, StoreDescriptor}
8+
import org.scalatest.matchers.should.Matchers
9+
10+
class KeyValueStoreTest
11+
extends StoreTest
12+
with Matchers
13+
with HasExecutionContext
14+
with SplicePostgresTest {
15+
private val storeDescriptor =
16+
StoreDescriptor(
17+
version = 1,
18+
name = "DbMultiDomainAcsStoreTest",
19+
party = dsoParty,
20+
participant = mkParticipantId("participant"),
21+
key = Map(),
22+
)
23+
24+
private def mkStore() = KeyValueStore(
25+
storeDescriptor,
26+
KeyValueStoreDbTableConfig("validator_internal_config", "config_key", "config_value"),
27+
storage,
28+
loggerFactory,
29+
)
30+
31+
"KeyValueStore" should {
32+
33+
val configKey = "key1"
34+
val configValue = "jsonPayload1"
35+
val otherKey = "key2"
36+
val otherValue = "jsonPayload2"
37+
38+
"set and get a payload successfully" in {
39+
for {
40+
store <- mkStore()
41+
_ <- store.setValue(configKey, configValue)
42+
retrievedValue <- store.getValue[String](configKey).value
43+
} yield {
44+
retrievedValue.value.value shouldBe configValue
45+
}
46+
}
47+
48+
"return None for a non-existent key" in {
49+
for {
50+
store <- mkStore()
51+
retrievedValue <- store.getValue[String]("non-existent-key").value
52+
} yield {
53+
retrievedValue shouldBe None
54+
}
55+
}
56+
57+
"update an existing payload" in {
58+
for {
59+
store <- mkStore()
60+
_ <- store.setValue(configKey, configValue)
61+
_ <- store.setValue(configKey, otherValue)
62+
retrievedValue <- store.getValue[String](configKey).value
63+
} yield {
64+
retrievedValue.value.value shouldBe otherValue
65+
}
66+
}
67+
68+
"handle multiple different keys independently" in {
69+
for {
70+
store <- mkStore()
71+
_ <- store.setValue(configKey, configValue)
72+
_ <- store.setValue(otherKey, otherValue)
73+
74+
configKeyValue <- store.getValue[String](configKey).value
75+
otherKeyValue <- store.getValue[String](otherKey).value
76+
} yield {
77+
configKeyValue.value.value shouldBe configValue
78+
otherKeyValue.value.value shouldBe otherValue
79+
}
80+
}
81+
82+
"delete single key" in {
83+
for {
84+
store <- mkStore()
85+
_ <- store.setValue(configKey, configValue)
86+
_ <- store.setValue(otherKey, otherValue)
87+
88+
_ <- store.deleteKey(configKey)
89+
configKeyValue <- store.getValue[String](configKey).value
90+
otherKeyValue <- store.getValue[String](otherKey).value
91+
} yield {
92+
configKeyValue shouldBe None
93+
otherKeyValue.value.value shouldBe otherValue
94+
}
95+
}
96+
}
97+
98+
override protected def cleanDb(
99+
storage: DbStorage
100+
)(implicit traceContext: TraceContext): FutureUnlessShutdown[?] =
101+
resetAllAppTables(storage)
102+
}

apps/validator/src/main/scala/org/lfdecentralizedtrust/splice/validator/store/ValidatorInternalStore.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package org.lfdecentralizedtrust.splice.validator.store
25

36
import com.digitalasset.canton.lifecycle.CloseContext

apps/validator/src/test/scala/org/lfdecentralizedtrust/splice/validator/store/ValidatorInternalStoreTest.scala renamed to apps/validator/src/test/scala/org/lfdecentralizedtrust/splice/validator/store/ValidatorConfigProviderTest.scala

Lines changed: 11 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,77 +16,16 @@ import org.scalatest.matchers.should.Matchers
1616

1717
import scala.concurrent.Future
1818

19-
abstract class ValidatorInternalStoreTest extends StoreTest with Matchers with HasExecutionContext {
19+
abstract class ValidatorConfigProviderTest
20+
extends StoreTest
21+
with Matchers
22+
with HasExecutionContext {
2023

2124
protected def mkStore(name: String): Future[KeyValueStore]
2225

2326
protected def mkProvider(name: String): Future[ValidatorConfigProvider]
2427

25-
"ValidatorInternalStore" should {
26-
27-
val configKey = "key1"
28-
val configValue = "jsonPayload1"
29-
val otherKey = "key2"
30-
val otherValue = "jsonPayload2"
31-
32-
"set and get a payload successfully" in {
33-
for {
34-
store <- mkStore("alice")
35-
_ <- store.setValue(configKey, configValue)
36-
retrievedValue <- store.getValue[String](configKey).value
37-
} yield {
38-
retrievedValue.value.value shouldBe configValue
39-
}
40-
}
41-
42-
"return None for a non-existent key" in {
43-
for {
44-
store <- mkStore("alice")
45-
retrievedValue <- store.getValue[String]("non-existent-key").value
46-
} yield {
47-
retrievedValue shouldBe None
48-
}
49-
}
50-
51-
"update an existing payload" in {
52-
for {
53-
store <- mkStore("alice")
54-
_ <- store.setValue(configKey, configValue)
55-
_ <- store.setValue(configKey, otherValue)
56-
retrievedValue <- store.getValue[String](configKey).value
57-
} yield {
58-
retrievedValue.value.value shouldBe otherValue
59-
}
60-
}
61-
62-
"handle multiple different keys independently" in {
63-
for {
64-
store <- mkStore("alice")
65-
_ <- store.setValue(configKey, configValue)
66-
_ <- store.setValue(otherKey, otherValue)
67-
68-
configKeyValue <- store.getValue[String](configKey).value
69-
otherKeyValue <- store.getValue[String](otherKey).value
70-
} yield {
71-
configKeyValue.value.value shouldBe configValue
72-
otherKeyValue.value.value shouldBe otherValue
73-
}
74-
}
75-
76-
"delete single key" in {
77-
for {
78-
store <- mkStore("alice")
79-
_ <- store.setValue(configKey, configValue)
80-
_ <- store.setValue(otherKey, otherValue)
81-
82-
_ <- store.deleteKey(configKey)
83-
configKeyValue <- store.getValue[String](configKey).value
84-
otherKeyValue <- store.getValue[String](otherKey).value
85-
} yield {
86-
configKeyValue shouldBe None
87-
otherKeyValue.value.value shouldBe otherValue
88-
}
89-
}
28+
"ValidatorConfigProvider should" {
9029

9130
val scanConfig1: Seq[ScanUrlInternalConfig] = Seq(
9231
ScanUrlInternalConfig("sv1", "url1"),
@@ -98,7 +37,7 @@ abstract class ValidatorInternalStoreTest extends StoreTest with Matchers with H
9837
ScanUrlInternalConfig("svB", "urlB"),
9938
)
10039

101-
"CONFIG_PROVIDER set and retrieve a ScanUrlInternalConfig list successfully" in {
40+
"set and retrieve a ScanUrlInternalConfig list successfully" in {
10241
for {
10342
provider <- mkProvider("alice")
10443
_ <- provider.setScanUrlInternalConfig(scanConfig1)
@@ -108,7 +47,7 @@ abstract class ValidatorInternalStoreTest extends StoreTest with Matchers with H
10847
}
10948
}
11049

111-
"CONFIG_PROVIDER update an existing ScanUrlInternalConfig list" in {
50+
"update an existing ScanUrlInternalConfig list" in {
11251
for {
11352
provider <- mkProvider("alice")
11453
_ <- provider.setScanUrlInternalConfig(scanConfig1)
@@ -119,7 +58,7 @@ abstract class ValidatorInternalStoreTest extends StoreTest with Matchers with H
11958
}
12059
}
12160

122-
"CONFIG_PROVIDER return None when no ScanUrlInternalConfig is found" in {
61+
"return None when no ScanUrlInternalConfig is found" in {
12362
for {
12463
provider <- mkProvider("alice")
12564
retrievedConfigOption <- provider.getScanUrlInternalConfig().value
@@ -128,7 +67,7 @@ abstract class ValidatorInternalStoreTest extends StoreTest with Matchers with H
12867
}
12968
}
13069

131-
"CONFIG_PROVIDER for two different store descriptors should not collide" in {
70+
"for two different store descriptors should not collide" in {
13271
for {
13372
provider1 <- mkProvider("alice")
13473
provider2 <- mkProvider("bob")
@@ -165,8 +104,8 @@ abstract class ValidatorInternalStoreTest extends StoreTest with Matchers with H
165104
}
166105
}
167106

168-
class DbValidatorInternalStoreTest
169-
extends ValidatorInternalStoreTest
107+
class DbValidatorConfigProviderTest
108+
extends ValidatorConfigProviderTest
170109
with HasActorSystem
171110
with SplicePostgresTest {
172111

0 commit comments

Comments
 (0)