Skip to content

Commit

Permalink
#1434 replacing the RpcMethodHandler object with at type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
naleeha authored and keikeicheung committed Aug 20, 2024
1 parent a74b510 commit 58ac06a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ public PersonRpcHandler(DataTable table, TableContainer tableContainer) {
registerRpc("GetPeopleWithName", (params) -> processGetPeopleNameRpcRequest(params));
}

private void registerRpc(String functionName, Function1<RpcParams, RpcMethodCallResult> handlerFunc) {
this.registerRpcMethodHandler(functionName, new RpcFunctionMethodHandler(handlerFunc));
}

public RpcMethodCallResult processUpdateNameRpcRequest(RpcParams params) {
updateName(
params.namedParams().get("Id").get().toString(), //how to report error when expected param missing or fail to cast to right type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public class RpcMethodHandlerTest {
public void should_wrap_and_call_java_function_in_method_handler() {
final TestRpcService rpcService = new TestRpcService();

final RpcMethodHandler handler = new RpcFunctionMethodHandler(rpcService::rpcFunction);

final RpcMethodCallResult result = handler.call(new RpcParams(new Object[]{}, ScalaCollectionConverter.toScala(Collections.emptyMap()), null));
final RpcMethodCallResult result = rpcService.rpcFunction(new RpcParams(new Object[]{}, ScalaCollectionConverter.toScala(Collections.emptyMap()), null));

assertThat(result)
.isNotNull()
Expand All @@ -34,10 +32,9 @@ public void should_wrap_and_call_java_function_in_method_handler() {
@Test
public void should_register_java_function_as_rpc_in_default_handler() {
final TestRpcService rpcService = new TestRpcService();
final RpcMethodHandler handler = new RpcFunctionMethodHandler(rpcService::rpcFunction);

final DefaultRpcHandler defaultRpcHandler = new DefaultRpcHandler();
defaultRpcHandler.registerRpcMethodHandler("helloWorld", handler);
defaultRpcHandler.registerRpc("helloWorld", rpcService::rpcFunction);

RpcCall call = new RpcCall("service", "helloWorld", new Object[]{}, ScalaCollectionConverter.toScala(Collections.emptyMap()));
Option<ViewServerMessage> response = defaultRpcHandler.processRpcCall(createRandomViewServerMessage(new LoginRequest("token", "user")), call, ViewPortTestUtils.requestContext());
Expand Down
27 changes: 15 additions & 12 deletions vuu/src/main/scala/org/finos/vuu/net/rpc/DefaultRpcHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ import java.util.concurrent.ConcurrentHashMap

class DefaultRpcHandler extends RpcHandler with StrictLogging {

private val methodHandlers = new ConcurrentHashMap[String, RpcMethodHandler]()
type RpcFunction = RpcParams => RpcMethodCallResult

private val rpcHandlerMap = new ConcurrentHashMap[String, RpcFunction]()

/**
* Register a handler for a given rpc method
* Register a handler for a given rpc function
*
* @param methodName name of the rpc method
* @param handler RpcMethodHandler
* @return
* @param functionName name of the rpc function
* @param handlerFunc handler function that takes RpcParams and return RpcMethodCallResult
*/
def registerRpcMethodHandler(methodName: String, handler: RpcMethodHandler): RpcMethodHandler = {
if (methodHandlers.containsKey(methodName)) {
throw new IllegalArgumentException(s"Method $methodName already registered")

def registerRpc(functionName: String, handlerFunc: RpcFunction): Unit = {

if (rpcHandlerMap.containsKey(functionName)) {
throw new IllegalArgumentException(s"Function $functionName already registered")
}
methodHandlers.put(methodName, handler)
rpcHandlerMap.put(functionName, handlerFunc)
}

override def processViewPortRpcCall(methodName: String, params: Array[Any], namedParams: Map[String, Any])(ctx: RequestContext): ViewPortAction = {
Expand All @@ -45,10 +48,10 @@ class DefaultRpcHandler extends RpcHandler with StrictLogging {
}

private def processRpcMethodHandler(methodName: String, params: Array[Any], namedParams: Map[String, Any], ctx: RequestContext) = {
if (methodHandlers.containsKey(methodName)) {
if (rpcHandlerMap.containsKey(methodName)) {
try {
val handler = methodHandlers.get(methodName)
handler.call(new RpcParams(params, namedParams, ctx))
val handler = rpcHandlerMap.get(methodName)
handler(new RpcParams(params, namedParams, ctx))
} catch {
case e: Exception =>
logger.error(s"Error processing rpc method $methodName", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ package org.finos.vuu.net.rpc

import org.finos.vuu.net.RequestContext

trait RpcMethodHandler {
def call(rpcParams: RpcParams): RpcMethodCallResult
}

class RpcFunctionMethodHandler(handler: RpcParams => RpcMethodCallResult) extends RpcMethodHandler {
override def call(rpcParams: RpcParams): RpcMethodCallResult = handler(rpcParams)
}

class RpcParams(val params: Array[Any], val namedParams: Map[String, Any], ctx: RequestContext)

trait RpcMethodCallResult {}
Expand Down
2 changes: 0 additions & 2 deletions vuu/src/test/scala/org/finos/vuu/net/TestVuuClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class TestVuuClient(vsClient: ViewServerClient) {
def awaitForMsgWithBody[T <: AnyRef](implicit t: ClassTag[T]): Option[T] =
awaitForMsg.map(msg => msg.body.asInstanceOf[T])



def awaitForMsg[T <: AnyRef](implicit t: ClassTag[T]): Option[ViewServerMessage] = {
failAfter(timeout){
val msg = vsClient.awaitMsg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,38 @@ class DefaultRpcHandlerTest extends AnyFeatureSpec with Matchers with BeforeAndA

Feature("Default Rpc Handler") {
Scenario("Register Rpc Method Handler for a method") {
val rpcFunctionMethodHandler = new RpcFunctionMethodHandler(_ => RpcMethodSuccess("result"))

handler.registerRpcMethodHandler("myMethod", rpcFunctionMethodHandler)
handler.registerRpc("myMethod", _ => RpcMethodSuccess("result"))

handler.processViewPortRpcCall("myMethod", Array("param1"), Map("namedParam1" -> "value1"))(null) should be(ViewPortRpcSuccess())
}

Scenario("Throw exception when registering a function under already registered name") {
val rpcFunctionMethodHandler = new RpcFunctionMethodHandler(_ => RpcMethodSuccess("result"))

handler.registerRpcMethodHandler("myMethod", rpcFunctionMethodHandler)
handler.registerRpc("myMethod", _ => RpcMethodSuccess("result1"))

assertThrows[IllegalArgumentException] {
handler.registerRpcMethodHandler("myMethod", rpcFunctionMethodHandler)
handler.registerRpc("myMethod", _ => RpcMethodSuccess("result2"))
}
}

Scenario("ProcessViewPortRpcCall method with null params should return ViewPortRpcSuccess when the rpc method is successful") {
handler.registerRpcMethodHandler("myMethod", _ => RpcMethodSuccess("result"))
handler.registerRpc("myMethod", _ => RpcMethodSuccess("result"))

val result = handler.processViewPortRpcCall("myMethod", null, null)(null)

result should be(ViewPortRpcSuccess())
}

Scenario("The processViewPortRpcCall method with null params should return ViewPortRpcFailure when the rpc method fails") {
handler.registerRpcMethodHandler("myMethod", _ => RpcMethodFailure(1, "error", new Exception("exception")))
handler.registerRpc("myMethod", _ => RpcMethodFailure(1, "error", new Exception("exception")))

val result = handler.processViewPortRpcCall("myMethod", null, null)(null)

result should be(ViewPortRpcFailure("Exception occurred calling rpc myMethod"))
}

Scenario("The processRpcCall method should return Some(ViewServerMessage) when the rpc method is successful") {
handler.registerRpcMethodHandler("myMethod", _ => RpcMethodSuccess("result"))
handler.registerRpc("myMethod", _ => RpcMethodSuccess("result"))

val rpcCall = RpcCall("myService", "myMethod", Array("param1"), Map("namedParam1" -> "value1"))

Expand All @@ -61,7 +58,7 @@ class DefaultRpcHandlerTest extends AnyFeatureSpec with Matchers with BeforeAndA
}

Scenario("The processRpcCall method should return Some(ViewServerMessage) when the rpc method fails") {
handler.registerRpcMethodHandler("myMethod", _ => RpcMethodFailure(1, "error", new Exception("exception")))
handler.registerRpc("myMethod", _ => RpcMethodFailure(1, "error", new Exception("exception")))

val rpcCall = RpcCall("myService", "myMethod", Array("param1"), Map("namedParam1" -> "value1"))

Expand Down

0 comments on commit 58ac06a

Please sign in to comment.