Skip to content

Commit d8a7e14

Browse files
committed
added test for ClientConnector
1 parent ec0b5ae commit d8a7e14

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 ClientConnector from '../../components/ClientConnector.js';
6+
7+
const parser = new Parser();
8+
const asyncapiFilePath = path.resolve(__dirname, '../../../../test/__fixtures__/asyncapi-websocket-components.yml');
9+
10+
describe('ClientConnector component (integration with AsyncAPI document)', () => {
11+
let parsedAsyncAPIDocument;
12+
let channels;
13+
let queryParams;
14+
let operations;
15+
let pathName;
16+
17+
beforeAll(async () => {
18+
const parseResult = await fromFile(parser, asyncapiFilePath).parse();
19+
parsedAsyncAPIDocument = parseResult.document;
20+
channels = parsedAsyncAPIDocument.channels();
21+
queryParams = getQueryParams(channels);
22+
operations = parsedAsyncAPIDocument.operations();
23+
24+
const servers = parsedAsyncAPIDocument.servers();
25+
const server = servers.all()[0];
26+
pathName = server.pathname();
27+
});
28+
29+
test('renders with default path when pathName is null', () => {
30+
const result = render(
31+
<ClientConnector
32+
clientName="WebSocketClient"
33+
query={queryParams}
34+
pathName={null}
35+
operations={operations}
36+
/>
37+
);
38+
expect(result.trim()).toMatchSnapshot();
39+
});
40+
41+
test('renders with default path when pathName is undefined', () => {
42+
const result = render(
43+
<ClientConnector
44+
clientName="WebSocketClient"
45+
query={queryParams}
46+
operations={operations}
47+
/>
48+
);
49+
expect(result.trim()).toMatchSnapshot();
50+
});
51+
52+
test('renders with path from fixture', () => {
53+
const result = render(
54+
<ClientConnector
55+
clientName="WebSocketClient"
56+
query={queryParams}
57+
pathName={pathName}
58+
operations={operations}
59+
/>
60+
);
61+
expect(result.trim()).toMatchSnapshot();
62+
});
63+
});
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`ClientConnector component (integration with AsyncAPI document) renders with default path when pathName is null 1`] = `
4+
"@Startup
5+
@Singleton
6+
public class WebSocketClientConnector{
7+
8+
@Inject
9+
WebSocketConnector<WebSocketClient> connector;
10+
11+
12+
@Inject
13+
@ConfigProperty(name = \\"com.asyncapi.WebSocketClient.base-uri\\")
14+
String baseURI;
15+
16+
17+
@PostConstruct
18+
void openAndSendMessagesWithDelay() {
19+
new Thread(() -> {
20+
try {
21+
Log.info(\\"Starting WebSocket connection attempt...\\");
22+
// URI parameters
23+
String query = \\"\\";
24+
25+
String heartbeat = System.getenv(\\"HEARTBEAT\\");
26+
if(heartbeat == null || heartbeat.isEmpty()){
27+
throw new IllegalArgumentException(\\"Required environment variable HEARTBEAT is missing or empty\\");
28+
}
29+
query += \\"heartbeat=\\" + URLEncoder.encode(heartbeat, StandardCharsets.UTF_8);
30+
31+
String bids = System.getenv(\\"BIDS\\");
32+
if(bids == null || bids.isEmpty()){
33+
throw new IllegalArgumentException(\\"Required environment variable BIDS is missing or empty\\");
34+
}
35+
query += \\"&bids=\\" + URLEncoder.encode(bids, StandardCharsets.UTF_8);
36+
37+
String sessionId = System.getenv(\\"SESSIONID\\");
38+
if(sessionId == null || sessionId.isEmpty()){
39+
throw new IllegalArgumentException(\\"Required environment variable SESSIONID is missing or empty\\");
40+
}
41+
query += \\"&sessionId=\\" + URLEncoder.encode(sessionId, StandardCharsets.UTF_8);
42+
43+
44+
String queryUri = baseURI + \\"/\\" + \\"?\\" + query;
45+
WebSocketClientConnection connection = connector.baseUri(queryUri).connectAndAwait();
46+
Thread.sleep(120000); // Keep the connection open for 2 minutes
47+
48+
49+
// Calling to close the WebSocket connection
50+
51+
52+
connection.closeAndAwait();
53+
Log.info(\\"Connection closed gracefully.\\");
54+
Thread.sleep(1000); // Wait for a second before exiting
55+
System.exit(0);
56+
} catch (Exception e) {
57+
Log.error(\\"Error during WebSocket communication\\", e);
58+
System.exit(1);
59+
}
60+
}).start();
61+
}
62+
}"
63+
`;
64+
65+
exports[`ClientConnector component (integration with AsyncAPI document) renders with default path when pathName is undefined 1`] = `
66+
"@Startup
67+
@Singleton
68+
public class WebSocketClientConnector{
69+
70+
@Inject
71+
WebSocketConnector<WebSocketClient> connector;
72+
73+
74+
@Inject
75+
@ConfigProperty(name = \\"com.asyncapi.WebSocketClient.base-uri\\")
76+
String baseURI;
77+
78+
79+
@PostConstruct
80+
void openAndSendMessagesWithDelay() {
81+
new Thread(() -> {
82+
try {
83+
Log.info(\\"Starting WebSocket connection attempt...\\");
84+
// URI parameters
85+
String query = \\"\\";
86+
87+
String heartbeat = System.getenv(\\"HEARTBEAT\\");
88+
if(heartbeat == null || heartbeat.isEmpty()){
89+
throw new IllegalArgumentException(\\"Required environment variable HEARTBEAT is missing or empty\\");
90+
}
91+
query += \\"heartbeat=\\" + URLEncoder.encode(heartbeat, StandardCharsets.UTF_8);
92+
93+
String bids = System.getenv(\\"BIDS\\");
94+
if(bids == null || bids.isEmpty()){
95+
throw new IllegalArgumentException(\\"Required environment variable BIDS is missing or empty\\");
96+
}
97+
query += \\"&bids=\\" + URLEncoder.encode(bids, StandardCharsets.UTF_8);
98+
99+
String sessionId = System.getenv(\\"SESSIONID\\");
100+
if(sessionId == null || sessionId.isEmpty()){
101+
throw new IllegalArgumentException(\\"Required environment variable SESSIONID is missing or empty\\");
102+
}
103+
query += \\"&sessionId=\\" + URLEncoder.encode(sessionId, StandardCharsets.UTF_8);
104+
105+
106+
String queryUri = baseURI + \\"/\\" + \\"?\\" + query;
107+
WebSocketClientConnection connection = connector.baseUri(queryUri).connectAndAwait();
108+
Thread.sleep(120000); // Keep the connection open for 2 minutes
109+
110+
111+
// Calling to close the WebSocket connection
112+
113+
114+
connection.closeAndAwait();
115+
Log.info(\\"Connection closed gracefully.\\");
116+
Thread.sleep(1000); // Wait for a second before exiting
117+
System.exit(0);
118+
} catch (Exception e) {
119+
Log.error(\\"Error during WebSocket communication\\", e);
120+
System.exit(1);
121+
}
122+
}).start();
123+
}
124+
}"
125+
`;
126+
127+
exports[`ClientConnector component (integration with AsyncAPI document) renders with path from fixture 1`] = `
128+
"@Startup
129+
@Singleton
130+
public class WebSocketClientConnector{
131+
132+
@Inject
133+
WebSocketConnector<WebSocketClient> connector;
134+
135+
136+
@Inject
137+
@ConfigProperty(name = \\"com.asyncapi.WebSocketClient.base-uri\\")
138+
String baseURI;
139+
140+
141+
@PostConstruct
142+
void openAndSendMessagesWithDelay() {
143+
new Thread(() -> {
144+
try {
145+
Log.info(\\"Starting WebSocket connection attempt...\\");
146+
// URI parameters
147+
String query = \\"\\";
148+
149+
String heartbeat = System.getenv(\\"HEARTBEAT\\");
150+
if(heartbeat == null || heartbeat.isEmpty()){
151+
throw new IllegalArgumentException(\\"Required environment variable HEARTBEAT is missing or empty\\");
152+
}
153+
query += \\"heartbeat=\\" + URLEncoder.encode(heartbeat, StandardCharsets.UTF_8);
154+
155+
String bids = System.getenv(\\"BIDS\\");
156+
if(bids == null || bids.isEmpty()){
157+
throw new IllegalArgumentException(\\"Required environment variable BIDS is missing or empty\\");
158+
}
159+
query += \\"&bids=\\" + URLEncoder.encode(bids, StandardCharsets.UTF_8);
160+
161+
String sessionId = System.getenv(\\"SESSIONID\\");
162+
if(sessionId == null || sessionId.isEmpty()){
163+
throw new IllegalArgumentException(\\"Required environment variable SESSIONID is missing or empty\\");
164+
}
165+
query += \\"&sessionId=\\" + URLEncoder.encode(sessionId, StandardCharsets.UTF_8);
166+
167+
168+
String queryUri = baseURI + \\"/ws\\" + \\"?\\" + query;
169+
WebSocketClientConnection connection = connector.baseUri(queryUri).connectAndAwait();
170+
Thread.sleep(120000); // Keep the connection open for 2 minutes
171+
172+
173+
// Calling to close the WebSocket connection
174+
175+
176+
connection.closeAndAwait();
177+
Log.info(\\"Connection closed gracefully.\\");
178+
Thread.sleep(1000); // Wait for a second before exiting
179+
System.exit(0);
180+
} catch (Exception e) {
181+
Log.error(\\"Error during WebSocket communication\\", e);
182+
System.exit(1);
183+
}
184+
}).start();
185+
}
186+
}"
187+
`;

0 commit comments

Comments
 (0)