diff --git a/packages/templates/clients/websocket/java/quarkus/test/components/ClientConnector.test.js b/packages/templates/clients/websocket/java/quarkus/test/components/ClientConnector.test.js new file mode 100644 index 000000000..2e187d5f4 --- /dev/null +++ b/packages/templates/clients/websocket/java/quarkus/test/components/ClientConnector.test.js @@ -0,0 +1,63 @@ +import path from 'path'; +import { render } from '@asyncapi/generator-react-sdk'; +import { Parser, fromFile } from '@asyncapi/parser'; +import { getQueryParams } from '@asyncapi/generator-helpers'; +import ClientConnector from '../../components/ClientConnector.js'; + +const parser = new Parser(); +const asyncapiFilePath = path.resolve(__dirname, '../../../../test/__fixtures__/asyncapi-websocket-components.yml'); + +describe('ClientConnector component (integration with AsyncAPI document)', () => { + let parsedAsyncAPIDocument; + let channels; + let queryParams; + let operations; + let pathName; + + beforeAll(async () => { + const parseResult = await fromFile(parser, asyncapiFilePath).parse(); + parsedAsyncAPIDocument = parseResult.document; + channels = parsedAsyncAPIDocument.channels(); + queryParams = getQueryParams(channels); + operations = parsedAsyncAPIDocument.operations(); + + const servers = parsedAsyncAPIDocument.servers(); + const server = servers.all()[0]; + pathName = server.pathname(); + }); + + test('renders with default path when pathName is null', () => { + const result = render( + + ); + expect(result.trim()).toMatchSnapshot(); + }); + + test('renders with default path when pathName is undefined', () => { + const result = render( + + ); + expect(result.trim()).toMatchSnapshot(); + }); + + test('renders with path from fixture', () => { + const result = render( + + ); + expect(result.trim()).toMatchSnapshot(); + }); +}); \ No newline at end of file diff --git a/packages/templates/clients/websocket/java/quarkus/test/components/__snapshots__/ClientConnector.test.js.snap b/packages/templates/clients/websocket/java/quarkus/test/components/__snapshots__/ClientConnector.test.js.snap new file mode 100644 index 000000000..7041b7dcc --- /dev/null +++ b/packages/templates/clients/websocket/java/quarkus/test/components/__snapshots__/ClientConnector.test.js.snap @@ -0,0 +1,187 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ClientConnector component (integration with AsyncAPI document) renders with default path when pathName is null 1`] = ` +"@Startup +@Singleton +public class WebSocketClientConnector{ + + @Inject + WebSocketConnector connector; + + + @Inject + @ConfigProperty(name = \\"com.asyncapi.WebSocketClient.base-uri\\") + String baseURI; + + + @PostConstruct + void openAndSendMessagesWithDelay() { + new Thread(() -> { + try { + Log.info(\\"Starting WebSocket connection attempt...\\"); + // URI parameters + String query = \\"\\"; + + String heartbeat = System.getenv(\\"HEARTBEAT\\"); + if(heartbeat == null || heartbeat.isEmpty()){ + throw new IllegalArgumentException(\\"Required environment variable HEARTBEAT is missing or empty\\"); + } + query += \\"heartbeat=\\" + URLEncoder.encode(heartbeat, StandardCharsets.UTF_8); + + String bids = System.getenv(\\"BIDS\\"); + if(bids == null || bids.isEmpty()){ + throw new IllegalArgumentException(\\"Required environment variable BIDS is missing or empty\\"); + } + query += \\"&bids=\\" + URLEncoder.encode(bids, StandardCharsets.UTF_8); + + String sessionId = System.getenv(\\"SESSIONID\\"); + if(sessionId == null || sessionId.isEmpty()){ + throw new IllegalArgumentException(\\"Required environment variable SESSIONID is missing or empty\\"); + } + query += \\"&sessionId=\\" + URLEncoder.encode(sessionId, StandardCharsets.UTF_8); + + + String queryUri = baseURI + \\"/\\" + \\"?\\" + query; + WebSocketClientConnection connection = connector.baseUri(queryUri).connectAndAwait(); + Thread.sleep(120000); // Keep the connection open for 2 minutes + + + // Calling to close the WebSocket connection + + + connection.closeAndAwait(); + Log.info(\\"Connection closed gracefully.\\"); + Thread.sleep(1000); // Wait for a second before exiting + System.exit(0); + } catch (Exception e) { + Log.error(\\"Error during WebSocket communication\\", e); + System.exit(1); + } + }).start(); + } +}" +`; + +exports[`ClientConnector component (integration with AsyncAPI document) renders with default path when pathName is undefined 1`] = ` +"@Startup +@Singleton +public class WebSocketClientConnector{ + + @Inject + WebSocketConnector connector; + + + @Inject + @ConfigProperty(name = \\"com.asyncapi.WebSocketClient.base-uri\\") + String baseURI; + + + @PostConstruct + void openAndSendMessagesWithDelay() { + new Thread(() -> { + try { + Log.info(\\"Starting WebSocket connection attempt...\\"); + // URI parameters + String query = \\"\\"; + + String heartbeat = System.getenv(\\"HEARTBEAT\\"); + if(heartbeat == null || heartbeat.isEmpty()){ + throw new IllegalArgumentException(\\"Required environment variable HEARTBEAT is missing or empty\\"); + } + query += \\"heartbeat=\\" + URLEncoder.encode(heartbeat, StandardCharsets.UTF_8); + + String bids = System.getenv(\\"BIDS\\"); + if(bids == null || bids.isEmpty()){ + throw new IllegalArgumentException(\\"Required environment variable BIDS is missing or empty\\"); + } + query += \\"&bids=\\" + URLEncoder.encode(bids, StandardCharsets.UTF_8); + + String sessionId = System.getenv(\\"SESSIONID\\"); + if(sessionId == null || sessionId.isEmpty()){ + throw new IllegalArgumentException(\\"Required environment variable SESSIONID is missing or empty\\"); + } + query += \\"&sessionId=\\" + URLEncoder.encode(sessionId, StandardCharsets.UTF_8); + + + String queryUri = baseURI + \\"/\\" + \\"?\\" + query; + WebSocketClientConnection connection = connector.baseUri(queryUri).connectAndAwait(); + Thread.sleep(120000); // Keep the connection open for 2 minutes + + + // Calling to close the WebSocket connection + + + connection.closeAndAwait(); + Log.info(\\"Connection closed gracefully.\\"); + Thread.sleep(1000); // Wait for a second before exiting + System.exit(0); + } catch (Exception e) { + Log.error(\\"Error during WebSocket communication\\", e); + System.exit(1); + } + }).start(); + } +}" +`; + +exports[`ClientConnector component (integration with AsyncAPI document) renders with path from fixture 1`] = ` +"@Startup +@Singleton +public class WebSocketClientConnector{ + + @Inject + WebSocketConnector connector; + + + @Inject + @ConfigProperty(name = \\"com.asyncapi.WebSocketClient.base-uri\\") + String baseURI; + + + @PostConstruct + void openAndSendMessagesWithDelay() { + new Thread(() -> { + try { + Log.info(\\"Starting WebSocket connection attempt...\\"); + // URI parameters + String query = \\"\\"; + + String heartbeat = System.getenv(\\"HEARTBEAT\\"); + if(heartbeat == null || heartbeat.isEmpty()){ + throw new IllegalArgumentException(\\"Required environment variable HEARTBEAT is missing or empty\\"); + } + query += \\"heartbeat=\\" + URLEncoder.encode(heartbeat, StandardCharsets.UTF_8); + + String bids = System.getenv(\\"BIDS\\"); + if(bids == null || bids.isEmpty()){ + throw new IllegalArgumentException(\\"Required environment variable BIDS is missing or empty\\"); + } + query += \\"&bids=\\" + URLEncoder.encode(bids, StandardCharsets.UTF_8); + + String sessionId = System.getenv(\\"SESSIONID\\"); + if(sessionId == null || sessionId.isEmpty()){ + throw new IllegalArgumentException(\\"Required environment variable SESSIONID is missing or empty\\"); + } + query += \\"&sessionId=\\" + URLEncoder.encode(sessionId, StandardCharsets.UTF_8); + + + String queryUri = baseURI + \\"/ws\\" + \\"?\\" + query; + WebSocketClientConnection connection = connector.baseUri(queryUri).connectAndAwait(); + Thread.sleep(120000); // Keep the connection open for 2 minutes + + + // Calling to close the WebSocket connection + + + connection.closeAndAwait(); + Log.info(\\"Connection closed gracefully.\\"); + Thread.sleep(1000); // Wait for a second before exiting + System.exit(0); + } catch (Exception e) { + Log.error(\\"Error during WebSocket communication\\", e); + System.exit(1); + } + }).start(); + } +}" +`;