Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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(
<ClientConnector
clientName="WebSocketClient"
query={queryParams}
pathName={null}
operations={operations}
/>
);
expect(result.trim()).toMatchSnapshot();
});

test('renders with default path when pathName is undefined', () => {
const result = render(
<ClientConnector
clientName="WebSocketClient"
query={queryParams}
operations={operations}
/>
);
expect(result.trim()).toMatchSnapshot();
});

test('renders with path from fixture', () => {
const result = render(
<ClientConnector
clientName="WebSocketClient"
query={queryParams}
pathName={pathName}
operations={operations}
/>
);
expect(result.trim()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -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<WebSocketClient> 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<WebSocketClient> 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<WebSocketClient> 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();
}
}"
`;