From 8f1d58a508872409343072f01b3fd952aa03bc74 Mon Sep 17 00:00:00 2001 From: Matthew Nesbit Date: Tue, 28 Mar 2017 11:36:47 +0100 Subject: [PATCH] Fix unit tests causing problems on windows --- .../corda/core/node/AttachmentClassLoaderTests.kt | 2 +- .../registration/HTTPNetworkRegistrationService.kt | 7 +++++-- .../node/services/AbstractNetworkMapServiceTest.kt | 14 ++++++++++++-- .../kotlin/net/corda/irs/IRSDemoTest.kt | 4 ++-- .../kotlin/net/corda/irs/api/IrsDemoClientApi.kt | 4 ++-- .../kotlin/net/corda/irs/utilities/HttpUtils.kt | 1 + 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/core/src/test/kotlin/net/corda/core/node/AttachmentClassLoaderTests.kt b/core/src/test/kotlin/net/corda/core/node/AttachmentClassLoaderTests.kt index c2d3cd4545..5b7c8beb5a 100644 --- a/core/src/test/kotlin/net/corda/core/node/AttachmentClassLoaderTests.kt +++ b/core/src/test/kotlin/net/corda/core/node/AttachmentClassLoaderTests.kt @@ -149,7 +149,7 @@ class AttachmentClassLoaderTests { val att2 = storage.importAttachment(ByteArrayInputStream(fakeAttachment("file2.txt", "some other data"))) val cl = AttachmentsClassLoader(arrayOf(att0, att1, att2).map { storage.openAttachment(it)!! }) - val txt = IOUtils.toString(cl.getResourceAsStream("file1.txt")) + val txt = IOUtils.toString(cl.getResourceAsStream("file1.txt"), Charsets.UTF_8.name()) assertEquals("some data", txt) } diff --git a/node/src/main/kotlin/net/corda/node/utilities/registration/HTTPNetworkRegistrationService.kt b/node/src/main/kotlin/net/corda/node/utilities/registration/HTTPNetworkRegistrationService.kt index 66c1b0562b..8c7aa991b4 100644 --- a/node/src/main/kotlin/net/corda/node/utilities/registration/HTTPNetworkRegistrationService.kt +++ b/node/src/main/kotlin/net/corda/node/utilities/registration/HTTPNetworkRegistrationService.kt @@ -1,5 +1,6 @@ package net.corda.node.utilities.registration +import com.google.common.net.MediaType import net.corda.core.crypto.CertificateStream import org.apache.commons.io.IOUtils import org.bouncycastle.pkcs.PKCS10CertificationRequest @@ -50,7 +51,7 @@ class HTTPNetworkRegistrationService(val server: URL) : NetworkRegistrationServi conn.outputStream.write(request.encoded) return when (conn.responseCode) { - HTTP_OK -> IOUtils.toString(conn.inputStream) + HTTP_OK -> IOUtils.toString(conn.inputStream, conn.charset) HTTP_FORBIDDEN -> throw IOException("Client version $clientVersion is forbidden from accessing permissioning server, please upgrade to newer version.") else -> throwUnexpectedResponseCode(conn) } @@ -60,5 +61,7 @@ class HTTPNetworkRegistrationService(val server: URL) : NetworkRegistrationServi throw IOException("Unexpected response code ${connection.responseCode} - ${connection.errorMessage}") } - private val HttpURLConnection.errorMessage: String get() = IOUtils.toString(errorStream) + private val HttpURLConnection.charset: String get() = MediaType.parse(getContentType()).charset().or(Charsets.UTF_8).name() + + private val HttpURLConnection.errorMessage: String get() = IOUtils.toString(errorStream, charset) } diff --git a/node/src/test/kotlin/net/corda/node/services/AbstractNetworkMapServiceTest.kt b/node/src/test/kotlin/net/corda/node/services/AbstractNetworkMapServiceTest.kt index f25193f0af..1e827068bb 100644 --- a/node/src/test/kotlin/net/corda/node/services/AbstractNetworkMapServiceTest.kt +++ b/node/src/test/kotlin/net/corda/node/services/AbstractNetworkMapServiceTest.kt @@ -49,6 +49,7 @@ abstract class AbstractNetworkMapServiceTest alice = second } network.runNetwork() + lastSerial = System.currentTimeMillis() } @After @@ -197,10 +198,18 @@ abstract class AbstractNetworkMapServiceTest return response.getOrThrow().node } + private var lastSerial = Long.MIN_VALUE + private fun MockNode.registration(addOrRemove: AddOrRemove, - serial: Long = System.currentTimeMillis()): ListenableFuture { + serial: Long? = null): ListenableFuture { + val distinctSerial = if (serial == null) { + ++lastSerial + } else { + lastSerial = serial + serial + } val expires = Instant.now() + NetworkMapService.DEFAULT_EXPIRATION_PERIOD - val nodeRegistration = NodeRegistration(info, serial, addOrRemove, expires) + val nodeRegistration = NodeRegistration(info, distinctSerial, addOrRemove, expires) val request = RegistrationRequest(nodeRegistration.toWire(services.legalIdentityKey.private), info.address) val response = services.networkService.sendRequest(REGISTER_TOPIC, request, mapServiceNode.info.address) network.runNetwork() @@ -235,6 +244,7 @@ abstract class AbstractNetworkMapServiceTest private fun addNewNodeToNetworkMap(legalName: String): MockNode { val node = network.createNode(networkMapAddress = mapServiceNode.info.address, legalName = legalName) network.runNetwork() + lastSerial = System.currentTimeMillis() return node } diff --git a/samples/irs-demo/src/integration-test/kotlin/net/corda/irs/IRSDemoTest.kt b/samples/irs-demo/src/integration-test/kotlin/net/corda/irs/IRSDemoTest.kt index 3dc32edae7..97caae2a8b 100644 --- a/samples/irs-demo/src/integration-test/kotlin/net/corda/irs/IRSDemoTest.kt +++ b/samples/irs-demo/src/integration-test/kotlin/net/corda/irs/IRSDemoTest.kt @@ -74,14 +74,14 @@ class IRSDemoTest : IntegrationTestCategory { } private fun runTrade(nodeAddr: HostAndPort, fixedRatePayer: Party, floatingRatePayer: Party) { - val fileContents = IOUtils.toString(Thread.currentThread().contextClassLoader.getResourceAsStream("example-irs-trade.json")) + val fileContents = IOUtils.toString(Thread.currentThread().contextClassLoader.getResourceAsStream("example-irs-trade.json"), Charsets.UTF_8.name()) val tradeFile = fileContents.replace("tradeXXX", "trade1") val url = URL("http://$nodeAddr/api/irs/deals") assert(postJson(url, tradeFile)) } private fun runUploadRates(host: HostAndPort) { - val fileContents = IOUtils.toString(Thread.currentThread().contextClassLoader.getResourceAsStream("example.rates.txt")) + val fileContents = IOUtils.toString(Thread.currentThread().contextClassLoader.getResourceAsStream("example.rates.txt"), Charsets.UTF_8.name()) val url = URL("http://$host/upload/interest-rates") assert(uploadFile(url, fileContents)) } diff --git a/samples/irs-demo/src/main/kotlin/net/corda/irs/api/IrsDemoClientApi.kt b/samples/irs-demo/src/main/kotlin/net/corda/irs/api/IrsDemoClientApi.kt index 887f984cb7..864f52794d 100644 --- a/samples/irs-demo/src/main/kotlin/net/corda/irs/api/IrsDemoClientApi.kt +++ b/samples/irs-demo/src/main/kotlin/net/corda/irs/api/IrsDemoClientApi.kt @@ -13,7 +13,7 @@ class IRSDemoClientApi(private val hostAndPort: HostAndPort) { private val api = HttpApi.fromHostAndPort(hostAndPort, apiRoot) fun runTrade(tradeId: String): Boolean { - val fileContents = IOUtils.toString(javaClass.classLoader.getResourceAsStream("example-irs-trade.json")) + val fileContents = IOUtils.toString(javaClass.classLoader.getResourceAsStream("example-irs-trade.json"), Charsets.UTF_8.name()) val tradeFile = fileContents.replace("tradeXXX", tradeId) return api.postJson("deals", tradeFile) } @@ -24,7 +24,7 @@ class IRSDemoClientApi(private val hostAndPort: HostAndPort) { // TODO: Add uploading of files to the HTTP API fun runUploadRates() { - val fileContents = IOUtils.toString(Thread.currentThread().contextClassLoader.getResourceAsStream("example.rates.txt")) + val fileContents = IOUtils.toString(Thread.currentThread().contextClassLoader.getResourceAsStream("example.rates.txt"), Charsets.UTF_8.name()) val url = URL("http://$hostAndPort/upload/interest-rates") assert(uploadFile(url, fileContents)) } diff --git a/samples/irs-demo/src/main/kotlin/net/corda/irs/utilities/HttpUtils.kt b/samples/irs-demo/src/main/kotlin/net/corda/irs/utilities/HttpUtils.kt index b0b99ad7a0..3bc03a01ed 100644 --- a/samples/irs-demo/src/main/kotlin/net/corda/irs/utilities/HttpUtils.kt +++ b/samples/irs-demo/src/main/kotlin/net/corda/irs/utilities/HttpUtils.kt @@ -37,5 +37,6 @@ private fun makeRequest(request: Request): Boolean { if (!response.isSuccessful) { println("Could not fulfill HTTP request. Status Code: ${response.code()}. Message: ${response.body().string()}") } + response.close() return response.isSuccessful }