Added ServiceInfo unit tests

This commit is contained in:
Andrius Dagys 2016-10-04 10:56:42 +01:00
parent 5efa0fd5b3
commit 62a9dfe900
2 changed files with 41 additions and 2 deletions

View File

@ -11,8 +11,10 @@ data class ServiceInfo(val type: ServiceType, val name: String? = null) {
companion object {
fun parse(encoded: String): ServiceInfo {
val parts = encoded.split("|")
require(parts.size > 0 && parts.size <= 2)
return ServiceInfo(object : ServiceType(parts[0]) {}, parts[1])
require(parts.size > 0 && parts.size <= 2) { "Invalid number of elements found" }
val type = object : ServiceType(parts[0]) {}
val name = parts.getOrNull(1)
return ServiceInfo(type, name)
}
}

View File

@ -0,0 +1,37 @@
package com.r3corda.core.node
import com.r3corda.core.node.services.ServiceInfo
import com.r3corda.core.node.services.ServiceType
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class ServiceInfoTests {
val serviceType = object : ServiceType("corda.service.subservice") {}
val name = "service.name"
@Test
fun `type and name encodes correctly`() {
assertEquals(ServiceInfo(serviceType, name).toString(), "$serviceType|$name")
}
@Test
fun `type and name parses correctly`() {
assertEquals(ServiceInfo.parse("$serviceType|$name"), ServiceInfo(serviceType, name))
}
@Test
fun `type only encodes correctly`() {
assertEquals(ServiceInfo(serviceType).toString(), "$serviceType")
}
@Test
fun `type only parses correctly`() {
assertEquals(ServiceInfo.parse("$serviceType"), ServiceInfo(serviceType))
}
@Test
fun `invalid encoding throws`() {
assertFailsWith<IllegalArgumentException> { ServiceInfo.parse("$serviceType|$name|something") }
}
}