From 6be21c7da46f9c8f24e33659f6dc859c2c28aaa9 Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Thu, 15 Dec 2016 16:02:34 +0000 Subject: [PATCH] Added test contents with a couple of todos. --- .../net/corda/vega/SimmValuationTest.kt | 38 ++++++++++++++----- .../kotlin/net/corda/vega/api/PortfolioApi.kt | 25 +++++------- .../kotlin/net/corda/testing/http/HttpApi.kt | 7 +++- .../net/corda/testing/http/HttpUtils.kt | 19 ++++++++++ 4 files changed, 64 insertions(+), 25 deletions(-) diff --git a/samples/simm-valuation-demo/src/integration-test/kotlin/net/corda/vega/SimmValuationTest.kt b/samples/simm-valuation-demo/src/integration-test/kotlin/net/corda/vega/SimmValuationTest.kt index da873fd872..b057b7114a 100644 --- a/samples/simm-valuation-demo/src/integration-test/kotlin/net/corda/vega/SimmValuationTest.kt +++ b/samples/simm-valuation-demo/src/integration-test/kotlin/net/corda/vega/SimmValuationTest.kt @@ -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("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") } } \ No newline at end of file diff --git a/samples/simm-valuation-demo/src/main/kotlin/net/corda/vega/api/PortfolioApi.kt b/samples/simm-valuation-demo/src/main/kotlin/net/corda/vega/api/PortfolioApi.kt index fb1bac56df..a386e45ab8 100644 --- a/samples/simm-valuation-demo/src/main/kotlin/net/corda/vega/api/PortfolioApi.kt +++ b/samples/simm-valuation-demo/src/main/kotlin/net/corda/vega/api/PortfolioApi.kt @@ -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) + /** * 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) diff --git a/test-utils/src/main/kotlin/net/corda/testing/http/HttpApi.kt b/test-utils/src/main/kotlin/net/corda/testing/http/HttpApi.kt index 97dafe57d7..d49dd5fdd1 100644 --- a/test-utils/src/main/kotlin/net/corda/testing/http/HttpApi.kt +++ b/test-utils/src/main/kotlin/net/corda/testing/http/HttpApi.kt @@ -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 getJson(path: String, params: Map = mapOf()) = HttpUtils.getJson(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 diff --git a/test-utils/src/main/kotlin/net/corda/testing/http/HttpUtils.kt b/test-utils/src/main/kotlin/net/corda/testing/http/HttpUtils.kt index b98604b980..b867f7349f 100644 --- a/test-utils/src/main/kotlin/net/corda/testing/http/HttpUtils.kt +++ b/test-utils/src/main/kotlin/net/corda/testing/http/HttpUtils.kt @@ -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 getJson(url: URL, params: Map = 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 { + 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()) + } }