Skip to content

Commit

Permalink
update snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushik-rishi committed Apr 30, 2024
1 parent e6f5fdc commit c32123c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 47 deletions.
20 changes: 10 additions & 10 deletions template/src/api/routes/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { File } from '@asyncapi/generator-react-sdk';
import { camelCase, convertToFilename, toHermesTopic } from '../../../../helpers/index';

function receiveHandler(operation, channelName) {
function receiveHandler(operation, channelName, channelAddress) {
if (!operation.isReceive()) {
return '';
}
Expand All @@ -16,7 +16,7 @@ function receiveHandler(operation, channelName) {
* ${ operation.summary() }
*/
`: ''}
router.use('${toHermesTopic(channelName)}', async (message, next) => {
router.use('${toHermesTopic(channelAddress)}', async (message, next) => {
try {
${(operation.messages().length > 1)
? `
Expand All @@ -32,7 +32,7 @@ function receiveHandler(operation, channelName) {
${
operation.messages().all().map(message => `try {
nValidated = await validateMessage(message.payload,'${ channelName }','${ message.name() }','publish', nValidated);
nValidated = await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','publish', nValidated);
} catch { };`).join('\n')
}
Expand All @@ -44,7 +44,7 @@ function receiveHandler(operation, channelName) {
}
`
: `
await validateMessage(message.payload,'${ channelName }','${ message.name() }','publish');
await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','publish');
await ${camelCase(channelName)}Handler._${ operationId }({message});
next();
`
Expand All @@ -56,7 +56,7 @@ function receiveHandler(operation, channelName) {
`;
}

function sendHandler(operation, channelName) {
function sendHandler(operation, channelName, channelAddress) {
if (!operation.isSend()) {
return '';
}
Expand All @@ -70,7 +70,7 @@ function sendHandler(operation, channelName) {
* ${ operation.summary() }
*/
`: ''}
router.use('${toHermesTopic(channelName)}', async (message, next) => {
router.use('${toHermesTopic(channelAddress)}', async (message, next) => {
try {
${(operation.messages().length > 1)
? `
Expand All @@ -80,7 +80,7 @@ function sendHandler(operation, channelName) {
${
operation.messages().all().map(message => `try {
nValidated = await validateMessage(message.payload,'${ channelName }','${ message.name() }','subscribe', nValidated);
nValidated = await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','subscribe', nValidated);
} catch { };`).join('\n')
}
Expand All @@ -92,7 +92,7 @@ function sendHandler(operation, channelName) {
}
`
: `
await validateMessage(message.payload,'${ channelName }','${ message.name() }','subscribe');
await validateMessage(message.payload,'${ channelAddress }','${ message.name() }','subscribe');
await ${camelCase(channelName)}Handler._${ operationId }({message});
next();
`
Expand Down Expand Up @@ -120,10 +120,10 @@ function routeCode(channel) {

for (const operation of channel.operations()) {
if (operation.isSend()) {
routeHandler += sendHandler(operation, channel.id());
routeHandler += sendHandler(operation, channel.id(), channel.address());
}
if (operation.isReceive()) {
routeHandler += receiveHandler(operation, channel.id());
routeHandler += receiveHandler(operation, channel.id(), channel.address());
}
}

Expand Down
16 changes: 8 additions & 8 deletions test/__snapshots__/integration.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -923,10 +923,10 @@ const router = new Router();
const lightTurnOffHandler = require('../handlers/lightTurnOff');
module.exports = router;
router.use('lightTurnOff', async (message, next) => {
router.use('smartylighting/streetlights/1/0/action/:streetlightId/turn/off', async (message, next) => {
try {
await validateMessage(message.payload, 'lightTurnOff', 'turnOnOff', 'subscribe');
await validateMessage(message.payload, 'smartylighting/streetlights/1/0/action/{streetlightId}/turn/off', 'turnOnOff', 'subscribe');
await lightTurnOffHandler._turnOff({
message
});
Expand All @@ -951,10 +951,10 @@ module.exports = router;
* Inform about environmental lighting conditions of a particular streetlight.
*/
router.use('lightingMeasured', async (message, next) => {
router.use('smartylighting/streetlights/1/0/event/:streetlightId/lighting/measured', async (message, next) => {
try {
await validateMessage(message.payload, 'lightingMeasured', 'lightMeasured', 'publish');
await validateMessage(message.payload, 'smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured', 'lightMeasured', 'publish');
await lightingMeasuredHandler._receiveLightMeasurement({
message
});
Expand All @@ -975,10 +975,10 @@ const router = new Router();
const lightTurnOnHandler = require('../handlers/lightTurnOn');
module.exports = router;
router.use('lightTurnOn', async (message, next) => {
router.use('smartylighting/streetlights/1/0/action/:streetlightId/turn/on', async (message, next) => {
try {
await validateMessage(message.payload, 'lightTurnOn', 'turnOnOff', 'subscribe');
await validateMessage(message.payload, 'smartylighting/streetlights/1/0/action/{streetlightId}/turn/on', 'turnOnOff', 'subscribe');
await lightTurnOnHandler._turnOn({
message
});
Expand All @@ -999,10 +999,10 @@ const router = new Router();
const lightsDimHandler = require('../handlers/lightsDim');
module.exports = router;
router.use('lightsDim', async (message, next) => {
router.use('smartylighting/streetlights/1/0/action/:streetlightId/dim', async (message, next) => {
try {
await validateMessage(message.payload, 'lightsDim', 'dimLight', 'subscribe');
await validateMessage(message.payload, 'smartylighting/streetlights/1/0/action/{streetlightId}/dim', 'dimLight', 'subscribe');
await lightsDimHandler._dimLight({
message
});
Expand Down
93 changes: 64 additions & 29 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,33 +125,68 @@ describe('template integration tests for generated files using the generator and
// describe('template integration tests for generated files using the generator and kafka example - v3 spec', () => {
// jest.setTimeout(30000);

// const outputDir = generateFolderName();
// const params = {
// server: 'test',
// securityScheme: 'certs',
// certFilesDir: './mocks/kafka/dummyCerts'
// };
// const kafkaExamplePath = './mocks/kafka/asyncapi-v3.yml';

// it('should generate proper config for X509 security', async() => {
// const expectedSecuritySetting = 'rejectUnauthorized: true';
// const expectedConfigFile = '/config/common.yml';

// const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
// await generator.generateFromFile(path.resolve('test', kafkaExamplePath));

// const file = await readFile(path.join(outputDir, expectedConfigFile), 'utf8');
// expect(file.includes(expectedSecuritySetting)).toBeTruthy();
// });

// it('should generate proper variable that points to custom cert files location', async() => {
// const expectedVariable = 'const certFilesDir = \'./mocks/kafka/dummyCerts\';';
// const expectedFile = '/src/api/index.js';

// const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
// await generator.generateFromFile(path.resolve('test', kafkaExamplePath));

// const file = await readFile(path.join(outputDir, expectedFile), 'utf8');
// expect(file.includes(expectedVariable)).toBeTruthy();
// });
// // const outputDir = generateFolderName();
// // const params = {
// // server: 'test',
// // securityScheme: 'certs',
// // certFilesDir: './mocks/kafka/dummyCerts'
// // };
// // const kafkaExamplePath = './mocks/kafka/asyncapi-v3.yml';

// it.each`
// server | description
// ${'scram-connections'} | ${'should generate proper handlers and routes files'}
// `(
// '$description',
// async ({ server}) => {
// const outputDir = generateFolderName();
// const params = {
// server
// };
// const mqttExamplePath = './mocks/kafka/asyncapi-v3.yml';

// const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
// await generator.generateFromFile(path.resolve('test', mqttExamplePath));

// const expectedFiles = [
// '/src/api/handlers/lightTurnOff.js',
// '/src/api/handlers/lightingMeasured.js',
// '/src/api/handlers/lightTurnOn.js',
// '/src/api/handlers/lightsDim.js',
// '/src/api/routes/lightTurnOff.js',
// '/src/api/routes/lightingMeasured.js',
// '/src/api/routes/lightTurnOn.js',
// '/src/api/routes/lightsDim.js',
// '/src/api/index.js',
// '/config/common.yml',
// '/package.json'
// ];
// for (const index in expectedFiles) {
// const file = await readFile(path.join(outputDir, expectedFiles[index]), 'utf8');
// expect(file).toMatchSnapshot();
// }
// }
// );

// // it('should generate proper config for X509 security', async() => {
// // const expectedSecuritySetting = 'rejectUnauthorized: true';
// // const expectedConfigFile = '/config/common.yml';

// // const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
// // await generator.generateFromFile(path.resolve('test', kafkaExamplePath));

// // const file = await readFile(path.join(outputDir, expectedConfigFile), 'utf8');
// // expect(file.includes(expectedSecuritySetting)).toBeTruthy();
// // });

// // it('should generate proper variable that points to custom cert files location', async() => {
// // const expectedVariable = 'const certFilesDir = \'./mocks/kafka/dummyCerts\';';
// // const expectedFile = '/src/api/index.js';

// // const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
// // await generator.generateFromFile(path.resolve('test', kafkaExamplePath));

// // const file = await readFile(path.join(outputDir, expectedFile), 'utf8');
// // expect(file.includes(expectedVariable)).toBeTruthy();
// // });
// });

0 comments on commit c32123c

Please sign in to comment.