Integration test uses better naming sceme. Json support module cleaned up to use Kotlin features.

This commit is contained in:
Clinton Alexander 2016-12-19 16:26:54 +00:00
parent 00a44c5fbd
commit 3c2610613f
3 changed files with 48 additions and 53 deletions

View File

@ -25,58 +25,53 @@ import java.time.LocalDateTime
* the java.time API, some core types, and Kotlin data classes. * the java.time API, some core types, and Kotlin data classes.
*/ */
object JsonSupport { object JsonSupport {
val javaTimeModule : Module by lazy {
fun createDefaultMapper(identities: IdentityService): ObjectMapper { SimpleModule("java.time").apply {
val mapper = ServiceHubObjectMapper(identities)
mapper.enable(SerializationFeature.INDENT_OUTPUT)
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
mapper.registerModule(createJavaTimeModule())
mapper.registerModule(createCordaModule())
mapper.registerModule(KotlinModule())
return mapper
}
fun createJavaTimeModule(): Module {
val timeModule = SimpleModule("java.time")
timeModule.apply {
addSerializer(LocalDate::class.java, ToStringSerializer) addSerializer(LocalDate::class.java, ToStringSerializer)
addDeserializer(LocalDate::class.java, LocalDateDeserializer) addDeserializer(LocalDate::class.java, LocalDateDeserializer)
addKeyDeserializer(LocalDate::class.java, LocalDateKeyDeserializer) addKeyDeserializer(LocalDate::class.java, LocalDateKeyDeserializer)
addSerializer(LocalDateTime::class.java, ToStringSerializer) addSerializer(LocalDateTime::class.java, ToStringSerializer)
} }
return timeModule
} }
fun createCordaModule(): Module { val cordaModule : Module by lazy {
val cordaModule = SimpleModule("core") SimpleModule("core").apply {
cordaModule.addSerializer(Party::class.java, PartySerializer) addSerializer(Party::class.java, PartySerializer)
cordaModule.addDeserializer(Party::class.java, PartyDeserializer) addDeserializer(Party::class.java, PartyDeserializer)
cordaModule.addSerializer(BigDecimal::class.java, ToStringSerializer) addSerializer(BigDecimal::class.java, ToStringSerializer)
cordaModule.addDeserializer(BigDecimal::class.java, NumberDeserializers.BigDecimalDeserializer()) addDeserializer(BigDecimal::class.java, NumberDeserializers.BigDecimalDeserializer())
cordaModule.addSerializer(SecureHash::class.java, SecureHashSerializer) addSerializer(SecureHash::class.java, SecureHashSerializer)
// It's slightly remarkable, but apparently Jackson works out that this is the only possibility // It's slightly remarkable, but apparently Jackson works out that this is the only possibility
// for a SecureHash at the moment and tries to use SHA256 directly even though we only give it SecureHash // for a SecureHash at the moment and tries to use SHA256 directly even though we only give it SecureHash
cordaModule.addDeserializer(SecureHash.SHA256::class.java, SecureHashDeserializer()) addDeserializer(SecureHash.SHA256::class.java, SecureHashDeserializer())
cordaModule.addDeserializer(BusinessCalendar::class.java, CalendarDeserializer) addDeserializer(BusinessCalendar::class.java, CalendarDeserializer)
// For ed25519 pubkeys // For ed25519 pubkeys
cordaModule.addSerializer(EdDSAPublicKey::class.java, PublicKeySerializer) addSerializer(EdDSAPublicKey::class.java, PublicKeySerializer)
cordaModule.addDeserializer(EdDSAPublicKey::class.java, PublicKeyDeserializer) addDeserializer(EdDSAPublicKey::class.java, PublicKeyDeserializer)
// For composite keys // For composite keys
cordaModule.addSerializer(CompositeKey::class.java, CompositeKeySerializer) addSerializer(CompositeKey::class.java, CompositeKeySerializer)
cordaModule.addDeserializer(CompositeKey::class.java, CompositeKeyDeserializer) addDeserializer(CompositeKey::class.java, CompositeKeyDeserializer)
// For NodeInfo // For NodeInfo
// TODO this tunnels the Kryo representation as a Base58 encoded string. Replace when RPC supports this. // TODO this tunnels the Kryo representation as a Base58 encoded string. Replace when RPC supports this.
cordaModule.addSerializer(NodeInfo::class.java, NodeInfoSerializer) addSerializer(NodeInfo::class.java, NodeInfoSerializer)
cordaModule.addDeserializer(NodeInfo::class.java, NodeInfoDeserializer) addDeserializer(NodeInfo::class.java, NodeInfoDeserializer)
}
return cordaModule
} }
fun createDefaultMapper(identities: IdentityService): ObjectMapper =
ServiceHubObjectMapper(identities).apply {
enable(SerializationFeature.INDENT_OUTPUT)
enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
registerModule(javaTimeModule)
registerModule(cordaModule)
registerModule(KotlinModule())
}
class ServiceHubObjectMapper(val identities: IdentityService) : ObjectMapper() class ServiceHubObjectMapper(val identities: IdentityService) : ObjectMapper()
object ToStringSerializer : JsonSerializer<Any>() { object ToStringSerializer : JsonSerializer<Any>() {

View File

@ -49,30 +49,30 @@ class SimmValuationTest: IntegrationTestCategory {
return HttpApi.fromHostAndPort(nodeAddr, "api/simmvaluationdemo") return HttpApi.fromHostAndPort(nodeAddr, "api/simmvaluationdemo")
} }
private fun getPartyWithName(node: HttpApi, countryparty: String): PortfolioApi.ApiParty = private fun getPartyWithName(partyApi: HttpApi, countryparty: String): PortfolioApi.ApiParty =
getAvailablePartiesFor(node).counterparties.single { it.text == countryparty } getAvailablePartiesFor(partyApi).counterparties.single { it.text == countryparty }
private fun getAvailablePartiesFor(api: HttpApi): PortfolioApi.AvailableParties { private fun getAvailablePartiesFor(partyApi: HttpApi): PortfolioApi.AvailableParties {
return api.getJson<PortfolioApi.AvailableParties>("whoami") return partyApi.getJson<PortfolioApi.AvailableParties>("whoami")
} }
private fun createTradeBetween(api: HttpApi, counterparty: PortfolioApi.ApiParty, tradeId: String): Boolean { private fun createTradeBetween(partyApi: HttpApi, counterparty: PortfolioApi.ApiParty, tradeId: String): Boolean {
val trade = SwapDataModel(tradeId, "desc", valuationDate, "EUR_FIXED_1Y_EURIBOR_3M", val trade = SwapDataModel(tradeId, "desc", valuationDate, "EUR_FIXED_1Y_EURIBOR_3M",
valuationDate, LocalDate.parse("2020-01-02"), BuySell.BUY, BigDecimal.valueOf(1000), BigDecimal.valueOf(0.1)) valuationDate, LocalDate.parse("2020-01-02"), BuySell.BUY, BigDecimal.valueOf(1000), BigDecimal.valueOf(0.1))
return api.putJson("${counterparty.id}/trades", trade) return partyApi.putJson("${counterparty.id}/trades", trade)
} }
private fun tradeExists(api: HttpApi, counterparty: PortfolioApi.ApiParty, tradeId: String): Boolean { private fun tradeExists(partyApi: HttpApi, counterparty: PortfolioApi.ApiParty, tradeId: String): Boolean {
val trades = api.getJson<Array<SwapDataView>>("${counterparty.id}/trades") val trades = partyApi.getJson<Array<SwapDataView>>("${counterparty.id}/trades")
return (trades.find { it.id == tradeId } != null) return (trades.find { it.id == tradeId } != null)
} }
private fun runValuationsBetween(api: HttpApi, counterparty: PortfolioApi.ApiParty): Boolean { private fun runValuationsBetween(partyApi: HttpApi, counterparty: PortfolioApi.ApiParty): Boolean {
return api.postJson("${counterparty.id}/portfolio/valuations/calculate", PortfolioApi.ValuationCreationParams(valuationDate)) return partyApi.postJson("${counterparty.id}/portfolio/valuations/calculate", PortfolioApi.ValuationCreationParams(valuationDate))
} }
private fun valuationExists(api: HttpApi, counterparty: PortfolioApi.ApiParty): Boolean { private fun valuationExists(partyApi: HttpApi, counterparty: PortfolioApi.ApiParty): Boolean {
val valuations = api.getJson<PortfolioApiUtils.ValuationsView>("${counterparty.id}/portfolio/valuations") val valuations = partyApi.getJson<PortfolioApiUtils.ValuationsView>("${counterparty.id}/portfolio/valuations")
return (valuations.initialMargin.call["total"] != 0.0) return (valuations.initialMargin.call["total"] != 0.0)
} }
} }

View File

@ -19,7 +19,7 @@ object HttpUtils {
.readTimeout(60, TimeUnit.SECONDS).build() .readTimeout(60, TimeUnit.SECONDS).build()
} }
val defaultMapper: ObjectMapper by lazy { val defaultMapper: ObjectMapper by lazy {
ObjectMapper().registerModule(JsonSupport.createJavaTimeModule()).registerModule(KotlinModule()) ObjectMapper().registerModule(JsonSupport.javaTimeModule).registerModule(KotlinModule())
} }
fun putJson(url: URL, data: String) : Boolean { fun putJson(url: URL, data: String) : Boolean {