Skip to content

Commit 5f1e08e

Browse files
Harsh16guptaasyncapi-botAdi-204
authored
test: add test coverage for OnTextMessageHandler (#1866)
Co-authored-by: Harsh Gupta <harsh16official@gmail.com> Co-authored-by: Chan <bot+chan@asyncapi.io> Co-authored-by: Adi Boghawala <adiboghawala@gmail.com>
1 parent 012f391 commit 5f1e08e

File tree

4 files changed

+161
-1
lines changed

4 files changed

+161
-1
lines changed

packages/templates/clients/websocket/java/quarkus/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"author": "Shuaib Salad <sh.salat@gmail.com>",
1313
"license": "Apache-2.0",
1414
"scripts": {
15-
"test": "jest --coverage --passWithNoTests",
15+
"test": "jest --coverage",
1616
"test:update": "npm run test -- -u",
1717
"lint": "eslint --max-warnings 0 --config ../../../../../../.eslintrc --ignore-path ../../../../../../.eslintignore .",
1818
"lint:fix": "eslint --fix --max-warnings 0 --config ../../../../../../.eslintrc --ignore-path ../../../../../../.eslintignore ."
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import path from 'path';
2+
import { render } from '@asyncapi/generator-react-sdk';
3+
import { Parser, fromFile } from '@asyncapi/parser';
4+
import OnTextMessageHandler from '../../components/OnTextMessageHandler.js';
5+
6+
const parser = new Parser();
7+
const asyncapiFilePath = path.resolve(__dirname, '../../../../test/__fixtures__/asyncapi-websocket-components.yml');
8+
9+
describe('OnTextMessageHandler component (integration with AsyncAPI document)', () => {
10+
let parsedAsyncAPIDocument;
11+
12+
beforeAll(async () => {
13+
const parseResult = await fromFile(parser, asyncapiFilePath).parse();
14+
parsedAsyncAPIDocument = parseResult.document;
15+
});
16+
17+
test('renders default handler when sendOperations is null', () => {
18+
const result = render(<OnTextMessageHandler sendOperations={null} />);
19+
expect(result.trim()).toMatchSnapshot();
20+
});
21+
22+
test('renders default handler when sendOperations is empty array', () => {
23+
const result = render(<OnTextMessageHandler sendOperations={[]} />);
24+
expect(result.trim()).toMatchSnapshot();
25+
});
26+
27+
test('renders default handler without sendOperations', () => {
28+
const result = render(<OnTextMessageHandler />);
29+
expect(result.trim()).toMatchSnapshot();
30+
});
31+
32+
test('renders handler with if statement for single send operation', () => {
33+
const operations = parsedAsyncAPIDocument.operations();
34+
const allSendOps = operations.filterBySend();
35+
const sendOpsArray = Array.from(allSendOps);
36+
expect(sendOpsArray.length).toBeGreaterThan(0);
37+
const singleOperation = [sendOpsArray[0]];
38+
const result = render(<OnTextMessageHandler sendOperations={singleOperation} />);
39+
expect(result.trim()).toMatchSnapshot();
40+
});
41+
42+
test('renders handler with if-else-if chain for multiple send operations', () => {
43+
const operations = parsedAsyncAPIDocument.operations();
44+
const sendOperations = operations.filterBySend();
45+
const sendOpsArray = Array.from(sendOperations);
46+
expect(sendOpsArray.length).toBeGreaterThan(1);
47+
const result = render(<OnTextMessageHandler sendOperations={sendOperations} />);
48+
expect(result.trim()).toMatchSnapshot();
49+
});
50+
});
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`OnTextMessageHandler component (integration with AsyncAPI document) renders default handler when sendOperations is empty array 1`] = `
4+
"@OnTextMessage
5+
public void processTextMessage(String message, WebSocketClientConnection connection) {
6+
LOG.info(\\"Received text message: \\" + message);
7+
}"
8+
`;
9+
10+
exports[`OnTextMessageHandler component (integration with AsyncAPI document) renders default handler when sendOperations is null 1`] = `
11+
"@OnTextMessage
12+
public void processTextMessage(String message, WebSocketClientConnection connection) {
13+
LOG.info(\\"Received text message: \\" + message);
14+
}"
15+
`;
16+
17+
exports[`OnTextMessageHandler component (integration with AsyncAPI document) renders default handler without sendOperations 1`] = `
18+
"@OnTextMessage
19+
public void processTextMessage(String message, WebSocketClientConnection connection) {
20+
LOG.info(\\"Received text message: \\" + message);
21+
}"
22+
`;
23+
24+
exports[`OnTextMessageHandler component (integration with AsyncAPI document) renders handler with if statement for single send operation 1`] = `
25+
"@OnTextMessage
26+
public void handleTextMessage(String message, WebSocketClientConnection connection) {
27+
LOG.info(\\"Handler received text message: \\" + message);
28+
if (message != null && message.contains(\\"sendMessage\\")) {
29+
sendMessage(message, connection);
30+
}
31+
else {
32+
LOG.warn(\\"Handler received unrecognized message type. Falling back to default handler.\\");
33+
// Note: By default, we route unrecognized messages to the first operation handler.
34+
// Depending on your business logic, you may want to change this behavior.
35+
sendMessage(message, connection);
36+
}
37+
}
38+
39+
public void sendMessage(String message, WebSocketClientConnection connection) {
40+
LOG.info(\\"Processing sendMessage type message: \\" + message);
41+
// TODO: implement processing logic for sendMessage
42+
}"
43+
`;
44+
45+
exports[`OnTextMessageHandler component (integration with AsyncAPI document) renders handler with if-else-if chain for multiple send operations 1`] = `
46+
"@OnTextMessage
47+
public void handleTextMessage(String message, WebSocketClientConnection connection) {
48+
LOG.info(\\"Handler received text message: \\" + message);
49+
if (message != null && message.contains(\\"sendMessage\\")) {
50+
sendMessage(message, connection);
51+
}
52+
else if (message != null && message.contains(\\"sendNotification\\")) {
53+
sendNotification(message, connection);
54+
}
55+
else {
56+
LOG.warn(\\"Handler received unrecognized message type. Falling back to default handler.\\");
57+
// Note: By default, we route unrecognized messages to the first operation handler.
58+
// Depending on your business logic, you may want to change this behavior.
59+
sendMessage(message, connection);
60+
}
61+
}
62+
63+
public void sendMessage(String message, WebSocketClientConnection connection) {
64+
LOG.info(\\"Processing sendMessage type message: \\" + message);
65+
// TODO: implement processing logic for sendMessage
66+
}
67+
68+
public void sendNotification(String message, WebSocketClientConnection connection) {
69+
LOG.info(\\"Processing sendNotification type message: \\" + message);
70+
// TODO: implement processing logic for sendNotification
71+
}"
72+
`;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
asyncapi: 3.0.0
2+
info:
3+
title: WebSocket Components Test Fixture
4+
version: 1.0.0
5+
description: AsyncAPI document used as a test fixture for WebSocket client components.
6+
7+
servers:
8+
testServer:
9+
host: test.example.com
10+
protocol: wss
11+
description: Test WebSocket server
12+
13+
channels:
14+
testChannel:
15+
address: /test
16+
description: Test channel for operation routing
17+
messages:
18+
testMessage:
19+
summary: Test message
20+
payload:
21+
type: string
22+
23+
operations:
24+
sendMessage:
25+
action: send
26+
channel:
27+
$ref: '#/channels/testChannel'
28+
summary: Send a message
29+
messages:
30+
- $ref: '#/channels/testChannel/messages/testMessage'
31+
32+
sendNotification:
33+
action: send
34+
channel:
35+
$ref: '#/channels/testChannel'
36+
summary: Send notification
37+
messages:
38+
- $ref: '#/channels/testChannel/messages/testMessage'

0 commit comments

Comments
 (0)