From c32123cc9756079ebc232289f05fd57e1a45169f Mon Sep 17 00:00:00 2001 From: Kaushik Rishi Manchukonda Date: Wed, 1 May 2024 02:32:24 +0530 Subject: [PATCH] update snapshot tests --- template/src/api/routes/route.js | 20 ++--- test/__snapshots__/integration.test.js.snap | 16 ++-- test/integration.test.js | 93 ++++++++++++++------- 3 files changed, 82 insertions(+), 47 deletions(-) diff --git a/template/src/api/routes/route.js b/template/src/api/routes/route.js index 303db098..61f9d500 100644 --- a/template/src/api/routes/route.js +++ b/template/src/api/routes/route.js @@ -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 ''; } @@ -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) ? ` @@ -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') } @@ -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(); ` @@ -56,7 +56,7 @@ function receiveHandler(operation, channelName) { `; } -function sendHandler(operation, channelName) { +function sendHandler(operation, channelName, channelAddress) { if (!operation.isSend()) { return ''; } @@ -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) ? ` @@ -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') } @@ -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(); ` @@ -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()); } } diff --git a/test/__snapshots__/integration.test.js.snap b/test/__snapshots__/integration.test.js.snap index 5bf558fb..4cc9981d 100644 --- a/test/__snapshots__/integration.test.js.snap +++ b/test/__snapshots__/integration.test.js.snap @@ -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 }); @@ -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 }); @@ -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 }); @@ -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 }); diff --git a/test/integration.test.js b/test/integration.test.js index c704ab23..ee8e2216 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -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(); +// // }); // }); \ No newline at end of file