Added test contents with a couple of todos.

This commit is contained in:
Clinton Alexander 2016-12-15 16:02:34 +00:00 committed by Clinton Alexander
parent 2334824a73
commit 6be21c7da4
4 changed files with 64 additions and 25 deletions

View File

@ -1,24 +1,44 @@
package net.corda.vega
import net.corda.core.getOrThrow
import net.corda.core.node.services.ServiceInfo
import net.corda.node.driver.driver
import net.corda.node.services.transactions.SimpleNotaryService
import net.corda.testing.IntegrationTestCategory
import net.corda.testing.getHostAndPort
import net.corda.testing.http.HttpApi
import net.corda.vega.api.PortfolioApi
import net.corda.vega.portfolio.Portfolio
import org.junit.Test
class SimmValuationTest: IntegrationTestCategory {
@Test fun `runs SIMM valuation demo`() {
driver(isDebug = true) {
val nodeBLegalName = "Bank B"
val controller = startNode("Controller", setOf(ServiceInfo(SimpleNotaryService.type))).getOrThrow()
val nodeA = startNode("Bank A").getOrThrow()
val nodeB = startNode("Bank B").getOrThrow()
assert(createTrade())
assert(runValuations())
val nodeAAddr = startNode("Bank A").getOrThrow().config.getHostAndPort("webAddress")
val nodeBAddr = startNode(nodeBLegalName).getOrThrow().config.getHostAndPort("webAddress")
val nodeA = HttpApi.fromHostAndPort(nodeAAddr, "simmvaluationdemo")
val parties = getAvailablePartiesFor(nodeA)
val nodeB = parties.counterparties.single { it.text == nodeBLegalName}
assert(createTradeBetween(nodeA, nodeB))
assert(runValuationsBetween(nodeA, nodeB))
}
}
// TODO: create, verify, run, verify or determine a better test structure.
private fun createTrade(): Boolean {
private fun getAvailablePartiesFor(api: HttpApi): PortfolioApi.AvailableParties {
return api.getJson<PortfolioApi.AvailableParties>("whoami")
}
private fun runValuations(): Boolean {
// TODO: create, verify, run, verify or determine a better test structure.
private fun createTradeBetween(api: HttpApi, counterparty: PortfolioApi.ApiParty): Boolean {
return api.postJson("{$counterparty.id}/trades", mapOf("a" to "a"))
}
private fun runValuationsBetween(api: HttpApi, counterparty: PortfolioApi.ApiParty): Boolean {
return api.postJson("{$counterparty.id/trade/TODO/valuations/calculate")
}
}

View File

@ -239,28 +239,23 @@ class PortfolioApi(val rpc: CordaRPCOps) {
}
}
data class ApiParty(val id: String, val text: String)
data class AvailableParties(val self: ApiParty, val counterparties: List<ApiParty>)
/**
* Returns the identity of the current node as well as a list of other counterparties that it is aware of.
*/
@GET
@Path("whoami")
@Produces(MediaType.APPLICATION_JSON)
fun getWhoAmI(): Any {
val counterParties = rpc.networkMapUpdates().first.filter { it.legalIdentity.name != "NetworkMapService" && it.legalIdentity.name != "Notary" && it.legalIdentity.name != ownParty.name }
return json {
obj(
"self" to obj(
"id" to ownParty.owningKey.toBase58String(),
"text" to ownParty.name
),
"counterparties" to counterParties.map {
obj(
"id" to it.legalIdentity.owningKey.toBase58String(),
"text" to it.legalIdentity.name
)
}
)
fun getWhoAmI(): AvailableParties {
val counterParties = rpc.networkMapUpdates().first.filter {
it.legalIdentity.name != "NetworkMapService" && it.legalIdentity.name != "Notary" && it.legalIdentity.name != ownParty.name
}
return AvailableParties(
self = ApiParty(ownParty.owningKey.toBase58String(), ownParty.name),
counterparties = counterParties.map { ApiParty(it.legalIdentity.owningKey.toBase58String(), it.legalIdentity.name) })
}
data class ValuationCreationParams(val valuationDate: LocalDate)

View File

@ -18,7 +18,12 @@ class HttpApi(val root: URL) {
*/
fun postJson(path: String, data: Any = Unit) = HttpUtils.postJson(URL(root, path), toJson(data))
private fun toJson(any: Any) = if (any is String) any else ObjectMapper().writeValueAsString(any)
/**
* Send a GET request to the path on the API specified.
*/
inline fun<reified T: Any> getJson(path: String, params: Map<String, String> = mapOf()) = HttpUtils.getJson<T>(URL(root, path), params)
private fun toJson(any: Any) = any as? String ?: ObjectMapper().writeValueAsString(any)
companion object {
fun fromHostAndPort(hostAndPort: HostAndPort, base: String, protocol: String = "http"): HttpApi

View File

@ -1,5 +1,6 @@
package net.corda.testing.http
import com.fasterxml.jackson.databind.ObjectMapper
import net.corda.core.utilities.loggerFor
import okhttp3.*
import java.net.URL
@ -26,12 +27,30 @@ object HttpUtils {
return makeRequest(Request.Builder().url(url).header("Content-Type", "application/json").post(body).build())
}
inline fun<reified T: Any> getJson(url: URL, params: Map<String, String> = mapOf()) : T {
val paramString = if(params.isEmpty()) "" else "?" + params.map { "${it.key}=${it.value}" }.joinToString("&")
val parameterisedUrl = URL(url.toExternalForm() + paramString)
return ObjectMapper().readValue(parameterisedUrl, T::class.java)
}
private fun makeRequest(request: Request): Boolean {
val response = client.newCall(request).execute()
if (!response.isSuccessful) {
logger.error("Could not fulfill HTTP request of type ${request.method()} to ${request.url()}. Status Code: ${response.code()}. Message: ${response.body().string()}")
}
return response.isSuccessful
}
private fun getRequest(request: Request): Pair<Boolean, String> {
val response = client.newCall(request).execute()
if (!response.isSuccessful) {
logger.error("Could not fulfill HTTP request of type ${request.method()} to ${request.url()}. Status Code: ${response.code()}. Message: ${response.body().string()}")
return Pair(false, "")
}
return Pair(true, response.body().string())
}
}