mirror of
https://github.com/corda/corda.git
synced 2024-12-19 04:57:58 +00:00
CORDA-4034 Rebuild AuthDBTests so H2 shuts down correctly (#6718)
* CORDA-4034 Rebuild AuthDBTests so H2 shuts down correctly
This commit is contained in:
parent
6ab9557061
commit
cc14c8e53a
@ -22,6 +22,7 @@ import org.junit.Before
|
|||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
import org.junit.runners.Parameterized
|
import org.junit.runners.Parameterized
|
||||||
|
import java.sql.Connection
|
||||||
import java.sql.Statement
|
import java.sql.Statement
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import javax.sql.DataSource
|
import javax.sql.DataSource
|
||||||
@ -33,7 +34,7 @@ import kotlin.test.assertFailsWith
|
|||||||
*/
|
*/
|
||||||
@RunWith(Parameterized::class)
|
@RunWith(Parameterized::class)
|
||||||
class AuthDBTests : NodeBasedTest() {
|
class AuthDBTests : NodeBasedTest() {
|
||||||
private lateinit var node: NodeWithInfo
|
private var node: NodeWithInfo? = null
|
||||||
private lateinit var client: CordaRPCClient
|
private lateinit var client: CordaRPCClient
|
||||||
private lateinit var db: UsersDB
|
private lateinit var db: UsersDB
|
||||||
|
|
||||||
@ -93,9 +94,10 @@ class AuthDBTests : NodeBasedTest() {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
node = startNode(ALICE_NAME, rpcUsers = emptyList(), configOverrides = securityConfig)
|
node = startNode(ALICE_NAME, rpcUsers = emptyList(), configOverrides = securityConfig).also { node ->
|
||||||
client = CordaRPCClient(node.node.configuration.rpcOptions.address)
|
client = CordaRPCClient(node.node.configuration.rpcOptions.address)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `login with correct credentials`() {
|
fun `login with correct credentials`() {
|
||||||
@ -215,7 +217,7 @@ class AuthDBTests : NodeBasedTest() {
|
|||||||
|
|
||||||
@After
|
@After
|
||||||
fun tearDown() {
|
fun tearDown() {
|
||||||
node.node.stop()
|
node?.node?.stop()
|
||||||
db.close()
|
db.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,7 +231,7 @@ private data class RoleAndPermissions(val role: String, val permissions: List<St
|
|||||||
* Manage in-memory DB mocking a users database with the schema expected by Node's security manager
|
* Manage in-memory DB mocking a users database with the schema expected by Node's security manager
|
||||||
*/
|
*/
|
||||||
private class UsersDB(name: String, users: List<UserAndRoles> = emptyList(), roleAndPermissions: List<RoleAndPermissions> = emptyList()) : AutoCloseable {
|
private class UsersDB(name: String, users: List<UserAndRoles> = emptyList(), roleAndPermissions: List<RoleAndPermissions> = emptyList()) : AutoCloseable {
|
||||||
val jdbcUrl = "jdbc:h2:mem:$name;DB_CLOSE_DELAY=-1"
|
val jdbcUrl = "jdbc:h2:mem:$name"
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val DB_CREATE_SCHEMA = """
|
const val DB_CREATE_SCHEMA = """
|
||||||
@ -270,36 +272,34 @@ private class UsersDB(name: String, users: List<UserAndRoles> = emptyList(), rol
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val dataSource: DataSource
|
private val connection: Connection
|
||||||
private inline fun session(statement: (Statement) -> Unit) {
|
private inline fun session(statement: (Statement) -> Unit) {
|
||||||
dataSource.connection.use {
|
connection.createStatement().use(statement)
|
||||||
it.autoCommit = false
|
connection.commit()
|
||||||
it.createStatement().use(statement)
|
|
||||||
it.commit()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
dataSource = DataSourceFactory.createDataSource(Properties().apply {
|
require(users.map { it.username }.toSet().size == users.size) {
|
||||||
|
"Duplicate username in input"
|
||||||
|
}
|
||||||
|
connection = DataSourceFactory.createDataSource(Properties().apply {
|
||||||
put("dataSourceClassName", "org.h2.jdbcx.JdbcDataSource")
|
put("dataSourceClassName", "org.h2.jdbcx.JdbcDataSource")
|
||||||
put("dataSource.url", jdbcUrl)
|
put("dataSource.url", jdbcUrl)
|
||||||
}, false)
|
}, false)
|
||||||
|
.connection
|
||||||
|
.apply {
|
||||||
|
autoCommit = false
|
||||||
|
}
|
||||||
session {
|
session {
|
||||||
it.execute(DB_CREATE_SCHEMA)
|
it.execute(DB_CREATE_SCHEMA)
|
||||||
}
|
}
|
||||||
require(users.map { it.username }.toSet().size == users.size) {
|
|
||||||
"Duplicate username in input"
|
|
||||||
}
|
|
||||||
users.forEach { insert(it) }
|
users.forEach { insert(it) }
|
||||||
roleAndPermissions.forEach { insert(it) }
|
roleAndPermissions.forEach { insert(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
dataSource.connection.use {
|
// Close the connection, at which point the database will shut down
|
||||||
it.createStatement().use {
|
connection.close()
|
||||||
it.execute("DROP ALL OBJECTS")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user