Skip to content

Commit 40df7bd

Browse files
authored
test: add test coverage for InitConnector (#1951)
Co-authored-by: Harsh Gupta <harsh16official@gmail.com>
1 parent 2be7c9a commit 40df7bd

File tree

2 files changed

+243
-0
lines changed

2 files changed

+243
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import path from 'path';
2+
import { render } from '@asyncapi/generator-react-sdk';
3+
import { Parser, fromFile } from '@asyncapi/parser';
4+
import { getQueryParams } from '@asyncapi/generator-helpers';
5+
import InitConnector from '../../components/InitConnector.js';
6+
7+
const parser = new Parser();
8+
const asyncapiFilePath = path.resolve(__dirname, '../../../../test/__fixtures__/asyncapi-websocket-components.yml');
9+
10+
describe('InitConnector component (integration with AsyncAPI document)', () => {
11+
let parsedAsyncAPIDocument;
12+
let channels;
13+
let queryParamsArray;
14+
let sendOperations;
15+
16+
beforeAll(async () => {
17+
const parseResult = await fromFile(parser, asyncapiFilePath).parse();
18+
parsedAsyncAPIDocument = parseResult.document;
19+
20+
channels = parsedAsyncAPIDocument.channels();
21+
const queryParams = getQueryParams(channels);
22+
queryParamsArray = queryParams ? Array.from(queryParams.entries()) : null;
23+
24+
const operations = parsedAsyncAPIDocument.operations();
25+
sendOperations = operations.filterBySend();
26+
});
27+
28+
test('renders with TimedConnection when queryParamsArray is null', () => {
29+
const result = render(
30+
<InitConnector
31+
queryParamsArray={null}
32+
pathName="/link"
33+
sendOperations={sendOperations}
34+
/>
35+
);
36+
expect(result.trim()).toMatchSnapshot();
37+
});
38+
39+
test('renders with TimedConnection when queryParamsArray is undefined', () => {
40+
const result = render(
41+
<InitConnector
42+
pathName="/link"
43+
sendOperations={sendOperations}
44+
/>
45+
);
46+
expect(result.trim()).toMatchSnapshot();
47+
});
48+
49+
test('renders with TimedConnection when queryParamsArray is empty array', () => {
50+
const result = render(
51+
<InitConnector
52+
queryParamsArray={[]}
53+
pathName="/link"
54+
sendOperations={sendOperations}
55+
/>
56+
);
57+
expect(result.trim()).toMatchSnapshot();
58+
});
59+
60+
test('renders without TimedConnection when queryParamsArray has data from fixture', () => {
61+
const result = render(
62+
<InitConnector
63+
queryParamsArray={queryParamsArray}
64+
pathName="/link"
65+
sendOperations={sendOperations}
66+
/>
67+
);
68+
expect(result.trim()).toMatchSnapshot();
69+
});
70+
});
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`InitConnector component (integration with AsyncAPI document) renders with TimedConnection when queryParamsArray is empty array 1`] = `
4+
"@PostConstruct
5+
void openAndSendMessagesWithDelay() {
6+
new Thread(() -> {
7+
try {
8+
Log.info(\\"Starting WebSocket connection attempt...\\");
9+
WebSocketClientConnection connection = connector.connectAndAwait();
10+
11+
// Wait 2 seconds before first message
12+
Thread.sleep(2000);
13+
14+
// Send 5 messages
15+
for (int i = 1; i <= 5; i++) {
16+
// Send a message to any available operation by including its operation ID (i.e. \\"sendMessage\\")
17+
String msg = \\"Message #\\" + i + \\" from Quarkus for sendMessage\\";
18+
connection.sendTextAndAwait(msg);
19+
Log.info(\\"Sent: \\" + msg);
20+
Thread.sleep(5000); // Wait 5 seconds between messages
21+
}
22+
23+
// Wait 10 seconds after final message
24+
Log.info(\\"All messages sent. Waiting 10 seconds before closing...\\");
25+
Thread.sleep(10000);
26+
27+
28+
// Calling to close the WebSocket connection
29+
30+
31+
connection.closeAndAwait();
32+
Log.info(\\"Connection closed gracefully.\\");
33+
Thread.sleep(1000); // Wait for a second before exiting
34+
System.exit(0);
35+
} catch (Exception e) {
36+
Log.error(\\"Error during WebSocket communication\\", e);
37+
System.exit(1);
38+
}
39+
}).start();
40+
}
41+
}"
42+
`;
43+
44+
exports[`InitConnector component (integration with AsyncAPI document) renders with TimedConnection when queryParamsArray is null 1`] = `
45+
"@PostConstruct
46+
void openAndSendMessagesWithDelay() {
47+
new Thread(() -> {
48+
try {
49+
Log.info(\\"Starting WebSocket connection attempt...\\");
50+
WebSocketClientConnection connection = connector.connectAndAwait();
51+
52+
// Wait 2 seconds before first message
53+
Thread.sleep(2000);
54+
55+
// Send 5 messages
56+
for (int i = 1; i <= 5; i++) {
57+
// Send a message to any available operation by including its operation ID (i.e. \\"sendMessage\\")
58+
String msg = \\"Message #\\" + i + \\" from Quarkus for sendMessage\\";
59+
connection.sendTextAndAwait(msg);
60+
Log.info(\\"Sent: \\" + msg);
61+
Thread.sleep(5000); // Wait 5 seconds between messages
62+
}
63+
64+
// Wait 10 seconds after final message
65+
Log.info(\\"All messages sent. Waiting 10 seconds before closing...\\");
66+
Thread.sleep(10000);
67+
68+
69+
// Calling to close the WebSocket connection
70+
71+
72+
connection.closeAndAwait();
73+
Log.info(\\"Connection closed gracefully.\\");
74+
Thread.sleep(1000); // Wait for a second before exiting
75+
System.exit(0);
76+
} catch (Exception e) {
77+
Log.error(\\"Error during WebSocket communication\\", e);
78+
System.exit(1);
79+
}
80+
}).start();
81+
}
82+
}"
83+
`;
84+
85+
exports[`InitConnector component (integration with AsyncAPI document) renders with TimedConnection when queryParamsArray is undefined 1`] = `
86+
"@PostConstruct
87+
void openAndSendMessagesWithDelay() {
88+
new Thread(() -> {
89+
try {
90+
Log.info(\\"Starting WebSocket connection attempt...\\");
91+
WebSocketClientConnection connection = connector.connectAndAwait();
92+
93+
// Wait 2 seconds before first message
94+
Thread.sleep(2000);
95+
96+
// Send 5 messages
97+
for (int i = 1; i <= 5; i++) {
98+
// Send a message to any available operation by including its operation ID (i.e. \\"sendMessage\\")
99+
String msg = \\"Message #\\" + i + \\" from Quarkus for sendMessage\\";
100+
connection.sendTextAndAwait(msg);
101+
Log.info(\\"Sent: \\" + msg);
102+
Thread.sleep(5000); // Wait 5 seconds between messages
103+
}
104+
105+
// Wait 10 seconds after final message
106+
Log.info(\\"All messages sent. Waiting 10 seconds before closing...\\");
107+
Thread.sleep(10000);
108+
109+
110+
// Calling to close the WebSocket connection
111+
112+
113+
connection.closeAndAwait();
114+
Log.info(\\"Connection closed gracefully.\\");
115+
Thread.sleep(1000); // Wait for a second before exiting
116+
System.exit(0);
117+
} catch (Exception e) {
118+
Log.error(\\"Error during WebSocket communication\\", e);
119+
System.exit(1);
120+
}
121+
}).start();
122+
}
123+
}"
124+
`;
125+
126+
exports[`InitConnector component (integration with AsyncAPI document) renders without TimedConnection when queryParamsArray has data from fixture 1`] = `
127+
"@PostConstruct
128+
void openAndSendMessagesWithDelay() {
129+
new Thread(() -> {
130+
try {
131+
Log.info(\\"Starting WebSocket connection attempt...\\");
132+
// URI parameters
133+
String query = \\"\\";
134+
135+
String heartbeat = System.getenv(\\"HEARTBEAT\\");
136+
if(heartbeat == null || heartbeat.isEmpty()){
137+
throw new IllegalArgumentException(\\"Required environment variable HEARTBEAT is missing or empty\\");
138+
}
139+
query += \\"heartbeat=\\" + URLEncoder.encode(heartbeat, StandardCharsets.UTF_8);
140+
141+
String bids = System.getenv(\\"BIDS\\");
142+
if(bids == null || bids.isEmpty()){
143+
throw new IllegalArgumentException(\\"Required environment variable BIDS is missing or empty\\");
144+
}
145+
query += \\"&bids=\\" + URLEncoder.encode(bids, StandardCharsets.UTF_8);
146+
147+
String sessionId = System.getenv(\\"SESSIONID\\");
148+
if(sessionId == null || sessionId.isEmpty()){
149+
throw new IllegalArgumentException(\\"Required environment variable SESSIONID is missing or empty\\");
150+
}
151+
query += \\"&sessionId=\\" + URLEncoder.encode(sessionId, StandardCharsets.UTF_8);
152+
153+
154+
String queryUri = baseURI + \\"/link\\" + \\"?\\" + query;
155+
WebSocketClientConnection connection = connector.baseUri(queryUri).connectAndAwait();
156+
Thread.sleep(120000); // Keep the connection open for 2 minutes
157+
158+
159+
// Calling to close the WebSocket connection
160+
161+
162+
connection.closeAndAwait();
163+
Log.info(\\"Connection closed gracefully.\\");
164+
Thread.sleep(1000); // Wait for a second before exiting
165+
System.exit(0);
166+
} catch (Exception e) {
167+
Log.error(\\"Error during WebSocket communication\\", e);
168+
System.exit(1);
169+
}
170+
}).start();
171+
}
172+
}"
173+
`;

0 commit comments

Comments
 (0)