Skip to content

Commit

Permalink
feat: help deserialize marshaled flow result to intended target type
Browse files Browse the repository at this point in the history
  • Loading branch information
manosbatsis committed Dec 29, 2023
1 parent 83d2813 commit 302d0fe
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 19 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ open class DemoApplicationTests {
val myFlowArgs = MyFlowArgs(aliceNode.memberX500Name, bobNode.memberX500Name)
val createdStatus = aliceNode.waitForFlow(
FlowRequest(
flowClassName = MyFlow::class.java.canonicalName,
requestBody = myFlowArgs
flowClass = MyFlow::class.java,
requestBody = myFlowArgs,
// Either String or the type
// (here MyFlowResult) marshaled to string by the flow,
flowResultClass = MyFlowResult::class.java
)
)
// Check flow status
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=com.github.manosbatsis.corda5.testutils
version=1.0.3
version=1.0.5
vendorName=Manos Batsis

kotlin.code.style=official
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.manosbatsis.corda5.testutils.integration.junit5.nodehandles

import com.fasterxml.jackson.databind.ObjectMapper
import com.github.manosbatsis.corda5.testutils.rest.client.FlowsClient
import com.github.manosbatsis.corda5.testutils.rest.client.loggerFor
import com.github.manosbatsis.corda5.testutils.rest.client.model.FlowRequest
Expand All @@ -10,16 +11,24 @@ import java.util.concurrent.TimeUnit
data class NodeHandle(
val memberX500Name: MemberX500Name,
val holdingIdentityShortHash: String,
var flowsClient: FlowsClient
var flowsClient: FlowsClient,
val objectMapper: ObjectMapper
) {

companion object {
private val logger = loggerFor(NodeHandle::class.java)
}

fun waitForFlow(flowRequest: FlowRequest, maxWaitSec: Int = 60): FlowStatusResponse {
fun <T> waitForFlow(
flowRequest: FlowRequest<T>,
maxWaitSec: Int = 60
): FlowStatusResponse<T> {
val clientRequestId: String = flowRequest.clientRequestId
var flowStatusResponse: FlowStatusResponse = flowsClient.flow(flowRequest, holdingIdentityShortHash)
var flowStatusResponse = flowsClient.flow(
// OpenFeign won't do generics, switch to string temporarily
flowRequest.withFlowResultClass(String::class.java),
holdingIdentityShortHash
)
for (i in 0..maxWaitSec) {
TimeUnit.SECONDS.sleep(1)
flowStatusResponse = flowsClient.flowStatus(holdingIdentityShortHash, clientRequestId)
Expand All @@ -28,6 +37,11 @@ data class NodeHandle(
else -> logger.info("Non-final flow status will retry $flowStatusResponse")
}
}
return flowStatusResponse

return flowStatusResponse.withFlowResult(
flowStatusResponse.flowResult?.let {
objectMapper.readValue(it, flowRequest.flowResultClass)
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ class NodeHandlesHelper(
NodeHandle(
MemberX500Name.parse(it.holdingIdentity.x500Name),
it.holdingIdentity.shortHash,
flowsClient
flowsClient,
objectMapper
)
}
return NodeHandles(nodes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import feign.RequestLine

interface FlowsClient {
@RequestLine("POST /flow/{holdingidentityshorthash}")
fun flow(
flowRequest: FlowRequest,
fun <T> flow(
flowRequest: FlowRequest<T>,
@Param("holdingidentityshorthash") shortHash: String
): FlowStatusResponse
): FlowStatusResponse<T>

@RequestLine("GET /flow/{holdingidentityshorthash}/{clientrequestid}")
fun flowStatus(
fun <T> flowStatus(
@Param("holdingidentityshorthash") shortHash: String,
@Param("clientrequestid") requestId: String
): FlowStatusResponse
): FlowStatusResponse<T>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.manosbatsis.corda5.testutils.rest.client

import kotlin.reflect.full.companionObject
import java.util.logging.Logger
import kotlin.reflect.full.companionObject

fun loggerFor(forClass: Class<*>): Logger =
Logger.getLogger(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
package com.github.manosbatsis.corda5.testutils.rest.client.model

import com.fasterxml.jackson.annotation.JsonIgnore
import java.util.*

data class FlowRequest(
data class FlowRequest<T>(
val clientRequestId: String = UUID.randomUUID().toString(),
val flowClassName: String,
val requestBody: Any?
)
val requestBody: Any?,
@JsonIgnore
val flowResultClass: Class<T>,
) {
constructor(
clientRequestId: String = UUID.randomUUID().toString(),
flowClass: Class<*>,
requestBody: Any?,
flowResultClass: Class<T>,
) : this(clientRequestId, flowClass.canonicalName, requestBody, flowResultClass)

fun <N> withFlowResultClass(other: Class<N>) = FlowRequest(
clientRequestId = clientRequestId,
flowClassName = flowClassName,
requestBody = requestBody,
flowResultClass = other
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import java.time.Instant
* @param timestamp The timestamp of when the status was last updated (in UTC)
*/
@JsonIgnoreProperties(ignoreUnknown = true)
data class FlowStatusResponse(
data class FlowStatusResponse<T>(
val holdingIdentityShortHash: String,
val clientRequestId: String?,
val flowId: String?,
val flowStatus: String,
val flowResult: String?,
val flowResult: T?,
val flowError: FlowStateErrorResponse?,
val timestamp: Instant
) {
Expand All @@ -33,6 +33,16 @@ data class FlowStatusResponse(
val finalStatuses = setOf(COMPLETED, FAILED)
}

fun <N> withFlowResult(result: N?) = FlowStatusResponse<N>(
holdingIdentityShortHash = holdingIdentityShortHash,
clientRequestId = clientRequestId,
flowId = flowId,
flowStatus = flowStatus,
flowResult = result,
flowError = flowError,
timestamp = timestamp,
)

@JsonIgnore
fun isFinal() = finalStatuses.contains(this.flowStatus)

Expand Down

0 comments on commit 302d0fe

Please sign in to comment.