Skip to content

Commit

Permalink
finos#1434 fixing default rpc handler to be able to return success re…
Browse files Browse the repository at this point in the history
…sult with no result as well as with result
  • Loading branch information
naleeha committed Aug 19, 2024
1 parent 655a91f commit 3c6102d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.finos.vuu.core.table.DataTable;
import org.finos.vuu.core.table.TableContainer;
import org.finos.vuu.net.rpc.*;
import scala.Function1;
import scala.Some;

import java.util.Arrays;

Expand All @@ -24,14 +24,14 @@ public RpcMethodCallResult processUpdateNameRpcRequest(RpcParams params) {
params.namedParams().get("Id").get().toString(), //how to report error when expected param missing or fail to cast to right type
params.namedParams().get("Name").get().toString()
);
return new RpcMethodSuccess(""); //how to control what viewport action to trigger?
return new RpcMethodSuccess(); //how to control what viewport action to trigger?
}

public RpcMethodCallResult processGetPeopleNameRpcRequest(RpcParams params) {
getPeopleWithNameThatStartWith(
var people = getPeopleWithNameThatStartWith(
Arrays.stream(params.params()).findFirst().toString()
);
return new RpcMethodSuccess(""); //need to return result
return new RpcMethodSuccess(people); //need to return result
}

public String[] updateName(String id, String newName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@

public class RpcMethodHandlerTest {

@Test
public void should_wrap_and_call_java_function_in_method_handler() {
final TestRpcService rpcService = new TestRpcService();

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

assertThat(result)
.isNotNull()
.isExactlyInstanceOf(RpcMethodSuccess.class)
.extracting("result").isEqualTo("It Works");
}

@Test
public void should_register_java_function_as_rpc_in_default_handler() {
final TestRpcService rpcService = new TestRpcService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ViewPortTypeAheadRpcHandler(tableContainer: TableContainer) extends Defaul
params.namedParams("column").toString,
null //todo what to do about request context
)
RpcMethodSuccess(values)
new RpcMethodSuccess(values)
}

def processGetUniqueFieldValuesStartWithRequest(params: RpcParams): RpcMethodCallResult = {
Expand All @@ -28,7 +28,7 @@ class ViewPortTypeAheadRpcHandler(tableContainer: TableContainer) extends Defaul
params.namedParams("starts").toString,
null //todo what to do about request context
)
RpcMethodSuccess(values) //how to control what viewport action to trigger?
new RpcMethodSuccess(values) //how to control what viewport action to trigger?
}

def getUniqueFieldValues(tableName: String, moduleName: String, column: String, ctx: RequestContext): Array[String] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ class DefaultRpcHandler extends RpcHandler with StrictLogging {
val result = processRpcMethodHandler(methodName, params, namedParams, ctx)
result match {
case RpcMethodSuccess(result) =>
if(result == null) ViewPortRpcSuccess()
else DisplayResultAction(result)
result match {
case Some(value) => DisplayResultAction(value)
case None => ViewPortRpcSuccess()
}
case _: RpcMethodFailure => ViewPortRpcFailure(s"Exception occurred calling rpc $methodName")
}
}
Expand All @@ -42,7 +44,7 @@ class DefaultRpcHandler extends RpcHandler with StrictLogging {
val module = Option(msg).map(_.module).getOrElse("")

processRpcMethodHandler(method, params, namedPars, ctx) match {
case result: RpcMethodSuccess => Some(VsMsg(ctx.requestId, ctx.session.sessionId, ctx.token, ctx.session.user, RpcResponse(method, result.result, error = null), module))
case result: RpcMethodSuccess => Some(VsMsg(ctx.requestId, ctx.session.sessionId, ctx.token, ctx.session.user, RpcResponse(method, result.optionalResult.orNull, error = null), module))
case error: RpcMethodFailure => Some(VsMsg(ctx.requestId, ctx.session.sessionId, ctx.token, ctx.session.user, RpcResponse(rpc.method, null, Error(error.error, error.code)), module))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ class RpcParams(val params: Array[Any], val namedParams: Map[String, Any], ctx:

trait RpcMethodCallResult {}

case class RpcMethodSuccess(result: Any) extends RpcMethodCallResult
case class RpcMethodSuccess(optionalResult: Option[Any]) extends RpcMethodCallResult{
def this(result:Any) = this(Some(result))
def this() = this(None)
}

case class RpcMethodFailure(code: Int, error: String, exception: Exception) extends RpcMethodCallResult {
def this(error: String) = this(1, error, null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.finos.vuu.net.rpc

import org.finos.vuu.net.{ClientSessionId, Error, JsonViewServerMessage, RequestContext, RpcCall, RpcResponse}
import org.finos.vuu.viewport.{ViewPortRpcFailure, ViewPortRpcSuccess}
import org.finos.vuu.viewport.{DisplayResultAction, ViewPortRpcFailure, ViewPortRpcSuccess}
import org.scalatest.BeforeAndAfterEach
import org.scalatest.featurespec.AnyFeatureSpec
import org.scalatest.matchers.should.Matchers
Expand All @@ -15,40 +15,48 @@ class DefaultRpcHandlerTest extends AnyFeatureSpec with Matchers with BeforeAndA
handler = new DefaultRpcHandler
}

Feature("Default Rpc Handler") {
Scenario("Register Rpc Method Handler for a method") {
handler.registerRpc("myMethod", _ => RpcMethodSuccess("result"))
Feature("Default Rpc Handler for Viewport Rpc") {
Scenario("Can register and handle Rpc request that perform action") {
handler.registerRpc("myMethod", _ => new RpcMethodSuccess())

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

Scenario("Can register and handle Rpc request that returns result") {
handler.registerRpc("myMethod", _ => new RpcMethodSuccess("result"))

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

Scenario("Throw exception when registering a function under already registered name") {

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

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

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

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") {
Scenario("Rpc request should return ViewPortRpcFailure when the rpc method fails") {
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.registerRpc("myMethod", _ => RpcMethodSuccess("result"))
Feature("Default Rpc Handler for global Rpc") {
Scenario("Rcp handler should return Some(ViewServerMessage) when the rpc method is successful") {
handler.registerRpc("myMethod", _ => new RpcMethodSuccess("result"))

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

Expand All @@ -57,7 +65,7 @@ class DefaultRpcHandlerTest extends AnyFeatureSpec with Matchers with BeforeAndA
result should be(Some(JsonViewServerMessage("requestId", "sessionId", "token", "user", RpcResponse("myMethod", "result", null), module = "TEST_MODULE")))
}

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

val rpcCall = RpcCall("myService", "myMethod", Array("param1"), Map("namedParam1" -> "value1"))
Expand All @@ -68,7 +76,7 @@ class DefaultRpcHandlerTest extends AnyFeatureSpec with Matchers with BeforeAndA
result should be(Some(JsonViewServerMessage("requestId", "sessionId", "token", "user", RpcResponse("myMethod", null, Error("error", 1)), "")))
}

Scenario("The processRpcCall method should return Some(ViewServerMessage) when the rpc method is not found") {
Scenario("Rpc handler should return Some(ViewServerMessage) when the rpc method is not found") {
val rpcCall = RpcCall("myService", "myMethod", Array("param1"), Map("namedParam1" -> "value1"))
handler.processRpcCall(msg, rpcCall)(ctx) should be(Some(JsonViewServerMessage("requestId", "sessionId", "token", "user", RpcResponse("myMethod", null, Error("Could not find rpcMethodHandler myMethod", 1)), "TEST_MODULE")))
}
Expand Down

0 comments on commit 3c6102d

Please sign in to comment.