-
-
Notifications
You must be signed in to change notification settings - Fork 382
test: add test coverage for EchoWebSocket #1998
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
Merged
asyncapi-bot
merged 3 commits into
asyncapi:master
from
Harsh16gupta:test/EchoWebSocket-1933
Feb 16, 2026
+318
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
packages/templates/clients/websocket/java/quarkus/test/components/EchoWebSocket.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import path from 'path'; | ||
| import { render } from '@asyncapi/generator-react-sdk'; | ||
| import { Parser, fromFile } from '@asyncapi/parser'; | ||
| import { getQueryParams, getTitle } from '@asyncapi/generator-helpers'; | ||
| import { EchoWebSocket } from '../../components/EchoWebSocket.js'; | ||
|
|
||
| const parser = new Parser(); | ||
| const asyncapiFilePath = path.resolve(__dirname, '../../../../test/__fixtures__/asyncapi-websocket-components.yml'); | ||
|
|
||
| describe('EchoWebSocket component (integration with AsyncAPI document)', () => { | ||
| let parsedAsyncAPIDocument; | ||
| let channels; | ||
| let queryParams; | ||
| let operations; | ||
| let title; | ||
| let pathName; | ||
|
|
||
| beforeAll(async () => { | ||
| const parseResult = await fromFile(parser, asyncapiFilePath).parse(); | ||
| parsedAsyncAPIDocument = parseResult.document; | ||
| channels = parsedAsyncAPIDocument.channels(); | ||
| queryParams = getQueryParams(channels); | ||
| operations = parsedAsyncAPIDocument.operations(); | ||
| title = getTitle(parsedAsyncAPIDocument); | ||
| const servers = parsedAsyncAPIDocument.servers(); | ||
| const server = servers.all()[0]; | ||
| pathName = server.pathname(); | ||
| }); | ||
|
|
||
| test('renders with default path when pathName is null', () => { | ||
| const result = render( | ||
| <EchoWebSocket | ||
| clientName="WebSocketClient" | ||
| pathName={null} | ||
| title={title} | ||
| queryParams={queryParams} | ||
| operations={operations} | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('renders with default path when pathName is undefined', () => { | ||
| const result = render( | ||
| <EchoWebSocket | ||
| clientName="WebSocketClient" | ||
| title={title} | ||
| queryParams={queryParams} | ||
| operations={operations} | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('renders with path from fixture', () => { | ||
| const result = render( | ||
| <EchoWebSocket | ||
| clientName="WebSocketClient" | ||
| pathName={pathName} | ||
| title={title} | ||
| queryParams={queryParams} | ||
| operations={operations} | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
| }); | ||
250 changes: 250 additions & 0 deletions
250
...s/clients/websocket/java/quarkus/test/components/__snapshots__/EchoWebSocket.test.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`EchoWebSocket component (integration with AsyncAPI document) renders with default path when pathName is null 1`] = ` | ||
| "@WebSocketClient(path = \\"/\\") | ||
| public class WebSocketClient{ | ||
|
|
||
| @Inject | ||
| public WebSocketClientConnection connection; | ||
|
|
||
| private static final Logger LOG = Logger.getLogger(WebSocketClient.class); | ||
|
|
||
|
|
||
| private HashMap<String, String> params; | ||
| private String heartbeat; | ||
| private String bids; | ||
| private String sessionId; | ||
|
|
||
| public WebSocketClient(){ | ||
| this(\\"false\\", \\"true\\", null); | ||
| } | ||
|
|
||
| public WebSocketClient(String heartbeat, String bids, String sessionId){ | ||
| params = new HashMap<>(); | ||
| this.heartbeat = (heartbeat != null && !heartbeat.isEmpty()) ? heartbeat : System.getenv(\\"HEARTBEAT\\"); | ||
| if (this.heartbeat != null){ | ||
| params.put(\\"heartbeat\\", this.heartbeat); | ||
| } | ||
| this.bids = (bids != null && !bids.isEmpty()) ? bids : System.getenv(\\"BIDS\\"); | ||
| if (this.bids != null){ | ||
| params.put(\\"bids\\", this.bids); | ||
| } | ||
| this.sessionId = (sessionId != null && !sessionId.isEmpty()) ? sessionId : System.getenv(\\"SESSIONID\\"); | ||
| if (this.sessionId != null){ | ||
| params.put(\\"sessionId\\", this.sessionId); | ||
| } | ||
| } | ||
|
|
||
| @OnOpen | ||
| public void onOpen() { | ||
| String broadcastMessage = \\"Echo called from WebSocket Components Test Fixture server\\"; | ||
| LOG.info(\\"Connected to WebSocket Components Test Fixture server\\"); | ||
| LOG.info(broadcastMessage); | ||
| } | ||
|
|
||
| @OnTextMessage | ||
| public void handleTextMessage(String message, WebSocketClientConnection connection) { | ||
| LOG.info(\\"Handler received text message: \\" + message); | ||
| if (message != null && message.contains(\\"sendMessage\\")) { | ||
| sendMessage(message, connection); | ||
| } | ||
| else if (message != null && message.contains(\\"sendNotification\\")) { | ||
| sendNotification(message, connection); | ||
| } | ||
| else { | ||
| LOG.warn(\\"Handler received unrecognized message type. Falling back to default handler.\\"); | ||
| // Note: By default, we route unrecognized messages to the first operation handler. | ||
| // Depending on your business logic, you may want to change this behavior. | ||
| sendMessage(message, connection); | ||
| } | ||
| } | ||
|
|
||
| public void sendMessage(String message, WebSocketClientConnection connection) { | ||
| LOG.info(\\"Processing sendMessage type message: \\" + message); | ||
| // TODO: implement processing logic for sendMessage | ||
| } | ||
|
|
||
| public void sendNotification(String message, WebSocketClientConnection connection) { | ||
| LOG.info(\\"Processing sendNotification type message: \\" + message); | ||
| // TODO: implement processing logic for sendNotification | ||
| } | ||
|
|
||
| @OnError | ||
| public void onError(Throwable throwable) { | ||
| LOG.error(\\"Websocket connection error: \\" + throwable.getMessage()); | ||
| } | ||
|
|
||
|
|
||
| @OnClose | ||
| public void onClose(CloseReason reason, WebSocketClientConnection connection) { | ||
| int code = reason.getCode(); | ||
| LOG.info(\\"Websocket disconnected from WebSocket Components Test Fixture with Close code: \\" + code); | ||
| } | ||
| }" | ||
| `; | ||
|
|
||
| exports[`EchoWebSocket component (integration with AsyncAPI document) renders with default path when pathName is undefined 1`] = ` | ||
| "@WebSocketClient(path = \\"/\\") | ||
| public class WebSocketClient{ | ||
|
|
||
| @Inject | ||
| public WebSocketClientConnection connection; | ||
|
|
||
| private static final Logger LOG = Logger.getLogger(WebSocketClient.class); | ||
|
|
||
|
|
||
| private HashMap<String, String> params; | ||
| private String heartbeat; | ||
| private String bids; | ||
| private String sessionId; | ||
|
|
||
| public WebSocketClient(){ | ||
| this(\\"false\\", \\"true\\", null); | ||
| } | ||
|
|
||
| public WebSocketClient(String heartbeat, String bids, String sessionId){ | ||
| params = new HashMap<>(); | ||
| this.heartbeat = (heartbeat != null && !heartbeat.isEmpty()) ? heartbeat : System.getenv(\\"HEARTBEAT\\"); | ||
| if (this.heartbeat != null){ | ||
| params.put(\\"heartbeat\\", this.heartbeat); | ||
| } | ||
| this.bids = (bids != null && !bids.isEmpty()) ? bids : System.getenv(\\"BIDS\\"); | ||
| if (this.bids != null){ | ||
| params.put(\\"bids\\", this.bids); | ||
| } | ||
| this.sessionId = (sessionId != null && !sessionId.isEmpty()) ? sessionId : System.getenv(\\"SESSIONID\\"); | ||
| if (this.sessionId != null){ | ||
| params.put(\\"sessionId\\", this.sessionId); | ||
| } | ||
| } | ||
|
|
||
| @OnOpen | ||
| public void onOpen() { | ||
| String broadcastMessage = \\"Echo called from WebSocket Components Test Fixture server\\"; | ||
| LOG.info(\\"Connected to WebSocket Components Test Fixture server\\"); | ||
| LOG.info(broadcastMessage); | ||
| } | ||
|
|
||
| @OnTextMessage | ||
| public void handleTextMessage(String message, WebSocketClientConnection connection) { | ||
| LOG.info(\\"Handler received text message: \\" + message); | ||
| if (message != null && message.contains(\\"sendMessage\\")) { | ||
| sendMessage(message, connection); | ||
| } | ||
| else if (message != null && message.contains(\\"sendNotification\\")) { | ||
| sendNotification(message, connection); | ||
| } | ||
| else { | ||
| LOG.warn(\\"Handler received unrecognized message type. Falling back to default handler.\\"); | ||
| // Note: By default, we route unrecognized messages to the first operation handler. | ||
| // Depending on your business logic, you may want to change this behavior. | ||
| sendMessage(message, connection); | ||
| } | ||
| } | ||
|
|
||
| public void sendMessage(String message, WebSocketClientConnection connection) { | ||
| LOG.info(\\"Processing sendMessage type message: \\" + message); | ||
| // TODO: implement processing logic for sendMessage | ||
| } | ||
|
|
||
| public void sendNotification(String message, WebSocketClientConnection connection) { | ||
| LOG.info(\\"Processing sendNotification type message: \\" + message); | ||
| // TODO: implement processing logic for sendNotification | ||
| } | ||
|
|
||
| @OnError | ||
| public void onError(Throwable throwable) { | ||
| LOG.error(\\"Websocket connection error: \\" + throwable.getMessage()); | ||
| } | ||
|
|
||
|
|
||
| @OnClose | ||
| public void onClose(CloseReason reason, WebSocketClientConnection connection) { | ||
| int code = reason.getCode(); | ||
| LOG.info(\\"Websocket disconnected from WebSocket Components Test Fixture with Close code: \\" + code); | ||
| } | ||
| }" | ||
| `; | ||
|
|
||
| exports[`EchoWebSocket component (integration with AsyncAPI document) renders with path from fixture 1`] = ` | ||
| "@WebSocketClient(path = \\"/ws\\") | ||
| public class WebSocketClient{ | ||
|
|
||
| @Inject | ||
| public WebSocketClientConnection connection; | ||
|
|
||
| private static final Logger LOG = Logger.getLogger(WebSocketClient.class); | ||
|
|
||
|
|
||
| private HashMap<String, String> params; | ||
| private String heartbeat; | ||
| private String bids; | ||
| private String sessionId; | ||
|
|
||
| public WebSocketClient(){ | ||
| this(\\"false\\", \\"true\\", null); | ||
| } | ||
|
|
||
| public WebSocketClient(String heartbeat, String bids, String sessionId){ | ||
| params = new HashMap<>(); | ||
| this.heartbeat = (heartbeat != null && !heartbeat.isEmpty()) ? heartbeat : System.getenv(\\"HEARTBEAT\\"); | ||
| if (this.heartbeat != null){ | ||
| params.put(\\"heartbeat\\", this.heartbeat); | ||
| } | ||
| this.bids = (bids != null && !bids.isEmpty()) ? bids : System.getenv(\\"BIDS\\"); | ||
| if (this.bids != null){ | ||
| params.put(\\"bids\\", this.bids); | ||
| } | ||
| this.sessionId = (sessionId != null && !sessionId.isEmpty()) ? sessionId : System.getenv(\\"SESSIONID\\"); | ||
| if (this.sessionId != null){ | ||
| params.put(\\"sessionId\\", this.sessionId); | ||
| } | ||
| } | ||
|
|
||
| @OnOpen | ||
| public void onOpen() { | ||
| String broadcastMessage = \\"Echo called from WebSocket Components Test Fixture server\\"; | ||
| LOG.info(\\"Connected to WebSocket Components Test Fixture server\\"); | ||
| LOG.info(broadcastMessage); | ||
| } | ||
|
|
||
| @OnTextMessage | ||
| public void handleTextMessage(String message, WebSocketClientConnection connection) { | ||
| LOG.info(\\"Handler received text message: \\" + message); | ||
| if (message != null && message.contains(\\"sendMessage\\")) { | ||
| sendMessage(message, connection); | ||
| } | ||
| else if (message != null && message.contains(\\"sendNotification\\")) { | ||
| sendNotification(message, connection); | ||
| } | ||
| else { | ||
| LOG.warn(\\"Handler received unrecognized message type. Falling back to default handler.\\"); | ||
| // Note: By default, we route unrecognized messages to the first operation handler. | ||
| // Depending on your business logic, you may want to change this behavior. | ||
| sendMessage(message, connection); | ||
| } | ||
| } | ||
|
|
||
| public void sendMessage(String message, WebSocketClientConnection connection) { | ||
| LOG.info(\\"Processing sendMessage type message: \\" + message); | ||
| // TODO: implement processing logic for sendMessage | ||
| } | ||
|
|
||
| public void sendNotification(String message, WebSocketClientConnection connection) { | ||
| LOG.info(\\"Processing sendNotification type message: \\" + message); | ||
| // TODO: implement processing logic for sendNotification | ||
| } | ||
|
|
||
| @OnError | ||
| public void onError(Throwable throwable) { | ||
| LOG.error(\\"Websocket connection error: \\" + throwable.getMessage()); | ||
| } | ||
|
|
||
|
|
||
| @OnClose | ||
| public void onClose(CloseReason reason, WebSocketClientConnection connection) { | ||
| int code = reason.getCode(); | ||
| LOG.info(\\"Websocket disconnected from WebSocket Components Test Fixture with Close code: \\" + code); | ||
| } | ||
| }" | ||
| `; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.