Refactor DemoBench's configuration classes (#429)

* Reuse User class from :node-api module.

* Test that DemoBench can understand the node's configuration file.

* Add return type to User.toMap().
This commit is contained in:
Chris Rankin 2017-03-25 00:45:01 +00:00 committed by GitHub
parent bb74f0ca34
commit cec4e20bc8
8 changed files with 64 additions and 26 deletions

View File

@ -48,12 +48,8 @@ dependencies {
// Controls FX: more java FX components http://fxexperience.com/controlsfx/
compile "org.controlsfx:controlsfx:$controlsfx_version"
compile (project(':client:rpc')) {
exclude module: 'junit'
}
compile (project(':finance')) {
exclude module: 'junit'
}
compile project(':client:rpc')
compile project(':finance')
compile "com.h2database:h2:$h2_version"
compile "net.java.dev.jna:jna-platform:$jna_version"
@ -73,6 +69,8 @@ dependencies {
compile ':jediterm-terminal-2.5'
compile ':pty4j-0.7.2'
testCompile project(':node')
testCompile "junit:junit:$junit_version"
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
}

View File

@ -25,11 +25,12 @@ class Explorer internal constructor(private val explorerController: ExplorerCont
}
try {
val user = config.users.elementAt(0)
val p = explorerController.process(
"--host=localhost",
"--port=${config.rpcPort}",
"--username=${config.users[0].user}",
"--password=${config.users[0].password}")
"--username=${user.username}",
"--password=${user.password}")
.directory(explorerDir)
.start()
process = p

View File

@ -1,6 +1,7 @@
package net.corda.demobench.model
import com.typesafe.config.*
import net.corda.nodeapi.User
import java.io.File
import java.nio.file.Files
import java.nio.file.Path

View File

@ -1,18 +1,20 @@
@file:JvmName("User")
package net.corda.demobench.model
data class User(val user: String, val password: String, val permissions: List<String>) {
fun toMap() = mapOf(
"user" to user,
"password" to password,
"permissions" to permissions
)
}
import net.corda.nodeapi.User
import java.util.*
fun User.toMap(): Map<String, Any> = mapOf(
"user" to username,
"password" to password,
"permissions" to permissions
)
@Suppress("UNCHECKED_CAST")
fun toUser(map: Map<String, Any>) = User(
map.getOrElse("user", { "none" }) as String,
map.getOrElse("password", { "none" }) as String,
map.getOrElse("permissions", { emptyList<String>() }) as List<String>
LinkedHashSet<String>(map.getOrElse("permissions", { emptyList<String>() }) as Collection<String>)
)
fun user(name: String) = User(name, "letmein", listOf("ALL"))
fun user(name: String) = User(name, "letmein", setOf("ALL"))

View File

@ -22,7 +22,8 @@ class NodeRPC(config: NodeConfig, start: () -> Unit, invoke: (CordaRPCOps) -> Un
val setupTask = object : TimerTask() {
override fun run() {
try {
rpcClient.start(config.users[0].user, config.users[0].password)
val user = config.users.elementAt(0)
rpcClient.start(user.username, user.password)
val ops = rpcClient.proxy()
// Cancel the "setup" task now that we've created the RPC client.

View File

@ -1,7 +1,12 @@
package net.corda.demobench.model
import com.google.common.net.HostAndPort
import net.corda.node.internal.NetworkMapInfo
import net.corda.node.services.config.FullNodeConfiguration
import net.corda.nodeapi.User
import java.nio.file.Path
import java.nio.file.Paths
import java.util.*
import kotlin.test.*
import org.junit.Test
@ -158,6 +163,33 @@ class NodeConfigTest {
+ "}", config.toText().stripWhitespace())
}
@Test
fun `reading node configuration`() {
val config = createConfig(
legalName = "My Name",
nearestCity = "Stockholm",
p2pPort = 10001,
rpcPort = 40002,
webPort = 20001,
h2Port = 30001,
services = listOf("my.service"),
users = listOf(user("jenny"))
)
config.networkMap = NetworkMapConfig("Notary", 12345)
val fullConfig = FullNodeConfiguration(Paths.get("."), config.toFileConfig())
assertEquals("My Name", fullConfig.myLegalName)
assertEquals("Stockholm", fullConfig.nearestCity)
assertEquals(localPort(20001), fullConfig.webAddress)
assertEquals(localPort(40002), fullConfig.rpcAddress)
assertEquals(localPort(10001), fullConfig.p2pAddress)
assertEquals(listOf("my.service"), fullConfig.extraAdvertisedServiceIds)
assertEquals(listOf(user("jenny")), fullConfig.rpcUsers)
assertEquals(NetworkMapInfo(localPort(12345), "Notary"), fullConfig.networkMapService)
assertTrue(fullConfig.useTestClock)
}
@Test
fun `test moving`() {
val config = createConfig(legalName = "My Name")
@ -190,4 +222,5 @@ class NodeConfigTest {
users = users
)
private fun localPort(port: Int) = HostAndPort.fromParts("localhost", port)
}

View File

@ -1,5 +1,6 @@
package net.corda.demobench.model
import net.corda.nodeapi.User
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.test.*

View File

@ -1,5 +1,6 @@
package net.corda.demobench.model
import net.corda.nodeapi.User
import org.junit.Test
import kotlin.test.*
@ -8,9 +9,9 @@ class UserTest {
@Test
fun createFromEmptyMap() {
val user = toUser(emptyMap())
assertEquals("none", user.user)
assertEquals("none", user.username)
assertEquals("none", user.password)
assertEquals(emptyList<String>(), user.permissions)
assertEquals(emptySet<String>(), user.permissions)
}
@Test
@ -21,26 +22,26 @@ class UserTest {
"permissions" to listOf("Flow.MyFlow")
)
val user = toUser(map)
assertEquals("MyName", user.user)
assertEquals("MyName", user.username)
assertEquals("MyPassword", user.password)
assertEquals(listOf("Flow.MyFlow"), user.permissions)
assertEquals(setOf("Flow.MyFlow"), user.permissions)
}
@Test
fun userToMap() {
val user = User("MyName", "MyPassword", listOf("Flow.MyFlow"))
val user = User("MyName", "MyPassword", setOf("Flow.MyFlow"))
val map = user.toMap()
assertEquals("MyName", map["user"])
assertEquals("MyPassword", map["password"])
assertEquals(listOf("Flow.MyFlow"), map["permissions"])
assertEquals(setOf("Flow.MyFlow"), map["permissions"])
}
@Test
fun `default user`() {
val user = user("guest")
assertEquals("guest", user.user)
assertEquals("guest", user.username)
assertEquals("letmein", user.password)
assertEquals(listOf("ALL"), user.permissions)
assertEquals(setOf("ALL"), user.permissions)
}
}