Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Websocket samples #109

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 71 additions & 68 deletions README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
<module>sample-binary</module>
<module>samples-soap</module>
<module>samples-db</module>
<module>samples-websocket</module>
</modules>
</project>
34 changes: 34 additions & 0 deletions samples-websocket/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2024 the original author or authors.
~
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.citrusframework.samples</groupId>
<artifactId>citrus-samples-websocket</artifactId>
<version>4.0.0</version>
<name>Citrus Samples:: Websocket Samples</name>
<packaging>pom</packaging>

<modules>
<module>sample-websocket-client</module>
<module>sample-websocket-server</module>
</modules>
</project>
120 changes: 120 additions & 0 deletions samples-websocket/sample-websocket-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
Websockets sample ![Logo][1]
==============

This sample shows how to use the Citrus Websocket client to connect to a socket on the server and send/receive data.
Citrus Websocket features are also described in detail in [reference guide][4]

Objectives
---------

The sample uses a small Quarkus application that provides a server side websocket for clients to connect to.
All messages sent to the socket get pushed to the connected clients.
Citrus is able to connect to the socket as a client in order to send/receive all messages via this socket broadcast.

In the test Citrus will connect to the socket and send some data to it.
The same message is received in a next step to verify the message broadcast.

We need a Websocket client component in the configuration:

```java
@BindToRegistry
public WebSocketClient chatClient() {
return new WebSocketClientBuilder()
.requestUrl("ws://localhost:8081/chat/citrus")
.build();
}
```

In the test cases we can reference this client component in order to send REST calls to the server.

```java
t.when(send("http://localhost:8081/chat/citrus-user")
.message()
.fork(true)
.body("Hello from Citrus!"));
```

**NOTE:** The send action uses `fork=true` option.
This is because the send operation should not block the test to proceed and verify the server side socket communication.

The Quarkus server socket should accept the connection and process the message sent by the Citrus client.
As a result of this we are able to verify the same message on the client because of the server socket broadcast.
This time the message has been adjusted by the Quarkus server with `>> {username}:` prefix.

```java
t.then(receive()
.endpoint(chatClient)
.message()
.body(">> citrus: Hello Quarkus chat!"));
```

Run
---------

The sample application uses QuarkusTest as a framework to run the tests with JUnit Jupiter.
So you can compile, package and test the sample with Maven to run the test.

```shell
mvn clean verify
```

This executes the complete Maven build lifecycle.
The Citrus test cases are part of the unit testing lifecycle and get executed automatically.
The Quarkus application is also started automatically as part of the test.

During the build you will see Citrus performing some integration tests.

System under test
---------

The sample uses a small Quarkus application that provides the Websocket implementation.
You can read more about Quarkus websocket support in [https://quarkus.io/guides/websockets](https://quarkus.io/guides/websockets).

Up to now we have started the Quarkus sample application as part of the unit test during the Maven build lifecycle.
This approach is fantastic when running automated tests in a continuous build.

There may be times we want to test against a standalone application.

You can start the sample Quarkus application in DevServices mode with this command.

```shell
mvn quarkus:dev
```

Now we are ready to execute some Citrus tests in a separate JVM.

Citrus test
---------

Once the sample application is deployed and running you can execute the Citrus test cases.
Open a separate command line terminal and navigate to the sample folder.

Execute all Citrus tests by calling

```shell
mvn verify
```

You can also pick a single test by calling

```shell
mvn clean verify -Dtest=<testname>
```

You should see Citrus performing several tests with lots of debugging output in both terminals (sample application
and Citrus test client).
And of course green tests at the very end of the build.

Of course, you can also start the Citrus tests from your favorite IDE.
Just start the Citrus test using the JUnit Jupiter IDE integration in IntelliJ, Eclipse or Netbeans.

Further information
---------

For more information on Citrus see [www.citrusframework.org][2], including
a complete [reference manual][3].

[1]: https://citrusframework.org/img/brand-logo.png "Citrus"
[2]: https://citrusframework.org
[3]: https://citrusframework.org/reference/html/
[4]: https://citrusframework.org/reference/html#websocket
160 changes: 160 additions & 0 deletions samples-websocket/sample-websocket-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<!--
~ Copyright 2024 the original author or authors.
~
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.citrusframework.samples</groupId>
<artifactId>citrus-sample-websocket-client</artifactId>
<name>Citrus Samples:: Websocket Client</name>
<version>4.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<compiler-plugin.version>3.11.0</compiler-plugin.version>
<surefire-plugin.version>3.1.2</surefire-plugin.version>

<quarkus.platform.version>3.6.6</quarkus.platform.version>
<citrus.version>4.0.2</citrus.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus.platform</groupId>
<artifactId>quarkus-bom</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.citrusframework</groupId>
<artifactId>citrus-bom</artifactId>
<version>${citrus.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- Quarkus platform -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-websockets</artifactId>
</dependency>

<!-- Test scoped dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>

<!-- Citrus test modules -->
<dependency>
<groupId>org.citrusframework</groupId>
<artifactId>citrus-quarkus</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.citrusframework</groupId>
<artifactId>citrus-websocket</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.citrusframework</groupId>
<artifactId>citrus-validation-text</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus.platform</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<configuration>
<systemProperties>
<quarkus.kafka.devservices.port>9092</quarkus.kafka.devservices.port>
</systemProperties>
</configuration>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Loading