Skip to content

Commit

Permalink
#1434 adding java web socket api level end to end test
Browse files Browse the repository at this point in the history
  • Loading branch information
naleeha authored and keikeicheung committed Sep 5, 2024
1 parent ce4ab99 commit 4117373
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.finos.vuu;

import org.finos.toolbox.time.DefaultClock;
import org.finos.vuu.core.module.TableDefContainer;
import org.finos.vuu.core.module.ViewServerModule;
import org.finos.vuu.module.JavaExampleModule;
import org.finos.vuu.net.*;
import org.finos.vuu.viewport.ViewPortRange;
import org.finos.vuu.viewport.ViewPortTable;
import org.junit.Assert;
import org.junit.Test;
import scala.jdk.javaapi.OptionConverters;

import java.util.List;

import static org.finos.vuu.util.ScalaCollectionConverter.toScala;
import static org.junit.Assert.assertEquals;

//@RunWith(Enclosed.class)
public class PersonRpcHandlerJavaWSApiTest extends WebSocketApiJavaTestBase {

private final String tableName = "PersonManualMapped";
private final String moduleName = JavaExampleModule.NAME;

@Test
public void type_ahead_requested_for_a_column() {
var createViewPortRequest = new CreateViewPortRequest(
new ViewPortTable(tableName, moduleName),
new ViewPortRange(1, 100),
new String [] {"Id", "Name"},
new SortSpec(toScala(List.of())),
new String[0],
null,
new Aggregations[0]
);
var requestId = vuuClient.send(sessionId, tokenId, createViewPortRequest);
var viewPortCreateResponse = vuuClient.awaitForResponse(requestId);

var viewPortCreateResponse2 = OptionConverters.toJava(viewPortCreateResponse);
Assert.assertTrue("view port creation request returns response successfully", viewPortCreateResponse2.isPresent());
CreateViewPortSuccess responseBody = ((CreateViewPortSuccess) viewPortCreateResponse2.get().body());
//todo assert type before casting
//CreateViewPortSuccess responseBody = assertBodyIsInstanceOf(viewPortCreateResponse);
//RpcResponseNew responseBody = assertBodyIsInstanceOf(viewPortCreateResponse);
var viewPortId = responseBody.viewPortId();

//todo type ahead request
}

@Override
public ViewServerModule defineModuleWithTestTables() {
return new JavaExampleModule().create(new TableDefContainer(), new DefaultClock());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class PersonRpcHandlerWSApiTest extends WebSocketApiTestBase {

Scenario("Custom Rpc request with a reference type as param") {

Given("a view port exist")
Given("a view port exist")
val viewPortId: String = createViewPort

When("request GetAccountId given the row key")
Expand All @@ -60,34 +60,32 @@ class PersonRpcHandlerWSApiTest extends WebSocketApiTestBase {
responseBody.action shouldBe a[NoneAction]
}

Scenario("Custom Rpc request with object as params") {

Scenario("Custom Rpc request with object as params") {

Given("a view port exist")
val viewPortId: String = createViewPort

Given("a view port exist")
val viewPortId: String = createViewPort

When("request update name")
val getTypeAheadRequest = RpcRequest(
ViewPortContext(viewPortId),
"UpdateName",
params = Map("Id" -> "uniqueId1", "Name" -> "Chris"))
val requestId = vuuClient.send(sessionId, tokenId, getTypeAheadRequest)
When("request update name")
val getTypeAheadRequest = RpcRequest(
ViewPortContext(viewPortId),
"UpdateName",
params = Map("Id" -> "uniqueId1", "Name" -> "Chris"))
val requestId = vuuClient.send(sessionId, tokenId, getTypeAheadRequest)

Then("return success response")
val response = vuuClient.awaitForResponse(requestId)
Then("return success response")
val response = vuuClient.awaitForResponse(requestId)

val responseBody = assertBodyIsInstanceOf[RpcResponseNew](response)
responseBody.rpcName shouldEqual "UpdateName"
val responseBody = assertBodyIsInstanceOf[RpcResponseNew](response)
responseBody.rpcName shouldEqual "UpdateName"

val result = assertAndCastAsInstanceOf[RpcSuccessResult](responseBody.result)
val result = assertAndCastAsInstanceOf[RpcSuccessResult](responseBody.result)

And("return No Action")
responseBody.action shouldBe a[NoneAction]
And("return No Action")
responseBody.action shouldBe a[NoneAction]

And("return row update with new name")
And("return row update with new name")
}
}
}

override protected def defineModuleWithTestTables(): ViewServerModule =
new JavaExampleModule().create(new TableDefContainer(), new DefaultClock())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.finos.vuu;

import org.finos.toolbox.lifecycle.LifecycleContainer;
import org.finos.toolbox.time.Clock;
import org.finos.toolbox.time.DefaultClock;
import org.finos.vuu.core.module.TableDefContainer;
import org.finos.vuu.core.module.ViewServerModule;
import org.finos.vuu.wsapi.helpers.TestStartUp;
import org.finos.vuu.wsapi.helpers.TestVuuClient;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import scala.jdk.javaapi.OptionConverters;

public abstract class WebSocketApiJavaTestBase {

protected TestVuuClient vuuClient;
protected String tokenId;
protected String sessionId;

protected Clock clock = new DefaultClock();
protected LifecycleContainer lifecycle = new LifecycleContainer(clock);
protected TableDefContainer tableDefContainer = new TableDefContainer();

@Before
public void setUp() {
vuuClient = testStartUp();
tokenId = vuuClient.createAuthToken();
var sessionOption = OptionConverters.toJava(vuuClient.login(tokenId, "testUser"));
Assert.assertTrue("login request returns response successfully", sessionOption.isPresent());
sessionId = sessionOption.get();
}

public TestVuuClient testStartUp() {
var startUp = new TestStartUp(
this::defineModuleWithTestTables,
clock,
lifecycle,
tableDefContainer);
return startUp.startServerAndClient();
}

public abstract ViewServerModule defineModuleWithTestTables();

@After
public void after(){
lifecycle.stop();
}
}
54 changes: 3 additions & 51 deletions vuu/src/test/scala/org/finos/vuu/wsapi/WebSocketApiTestBase.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package org.finos.vuu.wsapi

import org.finos.toolbox.jmx.{MetricsProvider, MetricsProviderImpl}
import org.finos.toolbox.lifecycle.LifecycleContainer
import org.finos.toolbox.time.{Clock, DefaultClock}
import org.finos.vuu.core._
import org.finos.vuu.core.module.{TableDefContainer, ViewServerModule}
import org.finos.vuu.net._
import org.finos.vuu.net.auth.AlwaysHappyAuthenticator
import org.finos.vuu.net.http.VuuHttp2ServerOptions
import org.finos.vuu.net.json.JsonVsSerializer
import org.finos.vuu.net.ws.WebSocketClient
import org.finos.vuu.wsapi.helpers.TestVuuClient
import org.finos.vuu.wsapi.helpers.{TestStartUp, TestVuuClient}
import org.scalatest.featurespec.AnyFeatureSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.{BeforeAndAfterAll, GivenWhenThen}
Expand Down Expand Up @@ -39,50 +33,8 @@ abstract class WebSocketApiTestBase extends AnyFeatureSpec with BeforeAndAfterAl
}

def testStartUp(): TestVuuClient = {

implicit val metrics: MetricsProvider = new MetricsProviderImpl

lifecycle.autoShutdownHook()

val rand = new scala.util.Random
val http = rand.between(10011, 10500)
val ws = rand.between(10011, 10500)

val module: ViewServerModule = defineModuleWithTestTables()

val config = VuuServerConfig(
VuuHttp2ServerOptions()
.withWebRoot("vuu/src/main/resources/www")
.withSslDisabled()
.withDirectoryListings(true)
.withPort(http),
VuuWebSocketOptions()
.withBindAddress("0.0.0.0")
.withUri("websocket")
.withWsPort(ws)
.withWssDisabled(),
VuuSecurityOptions()
.withAuthenticator(new AlwaysHappyAuthenticator)
.withLoginValidator(new AlwaysHappyLoginValidator),
VuuThreadingOptions(),
VuuClientConnectionOptions()
.withHeartbeatDisabled()
)
.withModule(module)

val viewServer = new VuuServer(config)

val client = new WebSocketClient(s"ws://localhost:$ws/websocket", ws) //todo review params - port specified twice
val viewServerClient: ViewServerClient = new WebSocketViewServerClient(client, JsonVsSerializer)
val vuuClient = new TestVuuClient(viewServerClient)

//set up a dependency on ws server from ws client.
lifecycle(client).dependsOn(viewServer)

//lifecycle registration is done in constructor of service classes, so sequence of create is important
lifecycle.start()

vuuClient
val startUp = new TestStartUp(() => defineModuleWithTestTables())
startUp.startServerAndClient()
}

protected def defineModuleWithTestTables(): ViewServerModule
Expand Down
67 changes: 67 additions & 0 deletions vuu/src/test/scala/org/finos/vuu/wsapi/helpers/TestStartUp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.finos.vuu.wsapi.helpers

import org.finos.toolbox.jmx.{MetricsProvider, MetricsProviderImpl}
import org.finos.toolbox.lifecycle.LifecycleContainer
import org.finos.toolbox.time.Clock
import org.finos.vuu.core.{VuuClientConnectionOptions, VuuSecurityOptions, VuuServer, VuuServerConfig, VuuThreadingOptions, VuuWebSocketOptions}
import org.finos.vuu.core.module.{TableDefContainer, ViewServerModule}
import org.finos.vuu.net.{AlwaysHappyLoginValidator, ViewServerClient, WebSocketViewServerClient}
import org.finos.vuu.net.auth.AlwaysHappyAuthenticator
import org.finos.vuu.net.http.VuuHttp2ServerOptions
import org.finos.vuu.net.json.JsonVsSerializer
import org.finos.vuu.net.ws.WebSocketClient

class TestStartUp(moduleFactoryFunc: () => ViewServerModule)(
implicit val timeProvider: Clock,
implicit val lifecycle: LifecycleContainer,
implicit val tableDefContainer: TableDefContainer) {


def startServerAndClient(): TestVuuClient = {

implicit val metrics: MetricsProvider = new MetricsProviderImpl

lifecycle.autoShutdownHook()

val rand = new scala.util.Random
val http = rand.between(10011, 10500)
val ws = rand.between(10011, 10500)

val module: ViewServerModule = moduleFactoryFunc()

val config = VuuServerConfig(
VuuHttp2ServerOptions()
.withWebRoot("vuu/src/main/resources/www")
.withSslDisabled()
.withDirectoryListings(true)
.withPort(http),
VuuWebSocketOptions()
.withBindAddress("0.0.0.0")
.withUri("websocket")
.withWsPort(ws)
.withWssDisabled(),
VuuSecurityOptions()
.withAuthenticator(new AlwaysHappyAuthenticator)
.withLoginValidator(new AlwaysHappyLoginValidator),
VuuThreadingOptions(),
VuuClientConnectionOptions()
.withHeartbeatDisabled()
)
.withModule(module)

val viewServer = new VuuServer(config)

val client = new WebSocketClient(s"ws://localhost:$ws/websocket", ws) //todo review params - port specified twice
val viewServerClient: ViewServerClient = new WebSocketViewServerClient(client, JsonVsSerializer)
val vuuClient = new TestVuuClient(viewServerClient)

//set up a dependency on ws server from ws client.
lifecycle(client).dependsOn(viewServer)

//lifecycle registration is done in constructor of service classes, so sequence of create is important
lifecycle.start()

vuuClient
}

}

0 comments on commit 4117373

Please sign in to comment.