From e7cd0f4f7d8a5cf055f389656e6aaabbf046adb2 Mon Sep 17 00:00:00 2001 From: Dale Lane Date: Thu, 7 Mar 2024 14:44:12 +0000 Subject: [PATCH] refactor: reduce duplicate code in new test file Signed-off-by: Dale Lane --- test/Kafka.test.js | 324 +++++++++++++++++---------------------- test/Types.utils.test.js | 60 ++------ 2 files changed, 153 insertions(+), 231 deletions(-) diff --git a/test/Kafka.test.js b/test/Kafka.test.js index e42e98f..ddfd514 100644 --- a/test/Kafka.test.js +++ b/test/Kafka.test.js @@ -13,216 +13,180 @@ describe('kafka integration tests using the generator', () => { jest.setTimeout(30000); - it('should generate Java for a secured, encrypted Kafka', async () => { + const generateJavaProject = async (PACKAGE, params, asyncApiDoc, expectedFiles, expectedConnectionHelperLines) => { const OUTPUT_DIR = generateFolderName(); - const PACKAGE = 'com.asyncapi'; const PACKAGE_PATH = path.join(...PACKAGE.split('.')); - const params = { - server: 'production' - }; - const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true, templateParams: params }); - await generator.generateFromFile(path.resolve('test', 'mocks/kafka-example.yml')); - const expectedFiles = [ - 'pom.xml', - `${PACKAGE_PATH}/Connection.java`, - `${PACKAGE_PATH}/ConnectionHelper.java`, - `${PACKAGE_PATH}/LoggingHelper.java`, - `${PACKAGE_PATH}/DemoProducer.java`, - `${PACKAGE_PATH}/DemoSubscriber.java`, - `${PACKAGE_PATH}/PubSubBase.java`, - `${PACKAGE_PATH}/SongReleasedProducer.java`, - `${PACKAGE_PATH}/SongReleasedSubscriber.java`, - `${PACKAGE_PATH}/models/ModelContract.java`, - `${PACKAGE_PATH}/models/Song.java`, - 'Dockerfile', - 'env.json' - ]; + // try running the generator + const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true, templateParams: params }); + await generator.generateFromFile(path.resolve('test', asyncApiDoc)); + // check that the files specific to this AsyncAPI doc are generated for (const file of expectedFiles) { - expect(existsSync(path.join(OUTPUT_DIR, file))).toBe(true); + expect(existsSync(path.join(OUTPUT_DIR, `${PACKAGE_PATH}/${file}`))).toBe(true); } - const connectionHelper = readFileSync(path.join(OUTPUT_DIR, `${PACKAGE_PATH}/ConnectionHelper.java`), 'utf-8'); - expect(connectionHelper.includes('props.put("security.protocol", "SASL_SSL")')).toBe(true); - expect(connectionHelper.includes('props.put("sasl.mechanism", "SCRAM-SHA-512")')).toBe(true); - }); - - it('should generate Java for an encrypted Kafka', async () => { - const OUTPUT_DIR = generateFolderName(); - const PACKAGE = 'com.custom.package'; - const PACKAGE_PATH = path.join(...PACKAGE.split('.')); - const params = { - server: 'production', - package: PACKAGE - }; - console.log(OUTPUT_DIR); - const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true, templateParams: params }); - await generator.generateFromFile(path.resolve('test', 'mocks/kafka-example-encrypt.yml')); - - const expectedFiles = [ + // check that standard files common to every project are generated + const commonFiles = [ 'pom.xml', + 'Dockerfile', + 'env.json', `${PACKAGE_PATH}/Connection.java`, `${PACKAGE_PATH}/ConnectionHelper.java`, `${PACKAGE_PATH}/LoggingHelper.java`, - `${PACKAGE_PATH}/DemoProducer.java`, `${PACKAGE_PATH}/PubSubBase.java`, - `${PACKAGE_PATH}/SongReleasedProducer.java`, - `${PACKAGE_PATH}/models/ModelContract.java`, - `${PACKAGE_PATH}/models/Song.java`, - 'Dockerfile', - 'env.json' ]; - - for (const file of expectedFiles) { + for (const file of commonFiles) { expect(existsSync(path.join(OUTPUT_DIR, file))).toBe(true); } + // check that the expected connection parameters are found const connectionHelper = readFileSync(path.join(OUTPUT_DIR, `${PACKAGE_PATH}/ConnectionHelper.java`), 'utf-8'); - expect(connectionHelper.includes('props.put("security.protocol", "SSL")')).toBe(true); - }); + for (const expectedLine of expectedConnectionHelperLines) { + expect(connectionHelper.includes(expectedLine)).toBe(true); + } - it('should generate Java for a plain Kafka', async () => { - const OUTPUT_DIR = generateFolderName(); - const PACKAGE = 'asyncapi.kafka'; - const PACKAGE_PATH = path.join(...PACKAGE.split('.')); - const params = { - server: 'production', - package: PACKAGE - }; - const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true, templateParams: params }); - await generator.generateFromFile(path.resolve('test', 'mocks/kafka-example-plain.yml')); + return true; + }; - const expectedFiles = [ - 'pom.xml', - `${PACKAGE_PATH}/Connection.java`, - `${PACKAGE_PATH}/ConnectionHelper.java`, - `${PACKAGE_PATH}/LoggingHelper.java`, - `${PACKAGE_PATH}/DemoProducer.java`, - `${PACKAGE_PATH}/DemoSubscriber.java`, - `${PACKAGE_PATH}/PubSubBase.java`, - `${PACKAGE_PATH}/SongReleasedProducer.java`, - `${PACKAGE_PATH}/SongReleasedSubscriber.java`, - `${PACKAGE_PATH}/models/ModelContract.java`, - `${PACKAGE_PATH}/models/Song.java`, - 'Dockerfile', - 'env.json' - ]; + it('should generate Java for a secured, encrypted Kafka', async () => { + const verified = await generateJavaProject( + 'com.asyncapi', + { + server: 'production' + }, + 'mocks/kafka-example.yml', + [ + 'DemoProducer.java', + 'DemoSubscriber.java', + 'SongReleasedProducer.java', + 'SongReleasedSubscriber.java', + 'models/ModelContract.java', + 'models/Song.java', + ], + [ + 'props.put("security.protocol", "SASL_SSL")', + 'props.put("sasl.mechanism", "SCRAM-SHA-512")' + ]); + expect(verified).toBe(true); + }); - for (const file of expectedFiles) { - expect(existsSync(path.join(OUTPUT_DIR, file))).toBe(true); - } + it('should generate Java for an encrypted Kafka', async () => { + const PACKAGE = 'com.custom.package'; + const verified = await generateJavaProject( + PACKAGE, + { + server: 'production', + package: PACKAGE + }, + 'mocks/kafka-example-encrypt.yml', + [ + 'DemoProducer.java', + 'SongReleasedProducer.java', + 'models/ModelContract.java', + 'models/Song.java', + ], + [ + 'props.put("security.protocol", "SSL")', + ]); + expect(verified).toBe(true); + }); - const connectionHelper = readFileSync(path.join(OUTPUT_DIR, `${PACKAGE_PATH}/ConnectionHelper.java`), 'utf-8'); - expect(connectionHelper.includes('props.put("security.protocol", "PLAINTEXT")')).toBe(true); + it('should generate Java for a plain Kafka', async () => { + const PACKAGE = 'plain'; + const verified = await generateJavaProject( + PACKAGE, + { + server: 'production', + package: PACKAGE + }, + 'mocks/kafka-example-plain.yml', + [ + 'DemoProducer.java', + 'DemoSubscriber.java', + 'SongReleasedProducer.java', + 'SongReleasedSubscriber.java', + 'models/ModelContract.java', + 'models/Song.java', + ], + [ + 'props.put("security.protocol", "PLAINTEXT")', + ]); + expect(verified).toBe(true); }); it('should generate Java for a secured Kafka', async () => { - const OUTPUT_DIR = generateFolderName(); - const PACKAGE = 'asyncapi.kafka'; - const PACKAGE_PATH = path.join(...PACKAGE.split('.')); - const params = { - server: 'production', - package: PACKAGE - }; - const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true, templateParams: params }); - await generator.generateFromFile(path.resolve('test', 'mocks/kafka-example-auth.yml')); - - const expectedFiles = [ - 'pom.xml', - `${PACKAGE_PATH}/Connection.java`, - `${PACKAGE_PATH}/ConnectionHelper.java`, - `${PACKAGE_PATH}/LoggingHelper.java`, - `${PACKAGE_PATH}/DemoSubscriber.java`, - `${PACKAGE_PATH}/PubSubBase.java`, - `${PACKAGE_PATH}/SongReleasedSubscriber.java`, - `${PACKAGE_PATH}/models/ModelContract.java`, - `${PACKAGE_PATH}/models/Song.java`, - 'Dockerfile', - 'env.json' - ]; - - for (const file of expectedFiles) { - expect(existsSync(path.join(OUTPUT_DIR, file))).toBe(true); - } - const connectionHelper = readFileSync(path.join(OUTPUT_DIR, `${PACKAGE_PATH}/ConnectionHelper.java`), 'utf-8'); - expect(connectionHelper.includes('props.put("security.protocol", "SASL_PLAINTEXT")')).toBe(true); - expect(connectionHelper.includes('props.put("sasl.mechanism", "PLAIN")')).toBe(true); + const PACKAGE = 'secured.kafka.doc'; + const verified = await generateJavaProject( + PACKAGE, + { + server: 'production', + package: PACKAGE + }, + 'mocks/kafka-example-auth.yml', + [ + 'DemoSubscriber.java', + 'SongReleasedSubscriber.java', + 'models/ModelContract.java', + 'models/Song.java', + ], + [ + 'props.put("security.protocol", "SASL_PLAINTEXT")', + 'props.put("sasl.mechanism", "PLAIN")', + ]); + expect(verified).toBe(true); }); it('should generate Java for the streetlights example', async () => { - const OUTPUT_DIR = generateFolderName(); const PACKAGE = 'com.asyncapi.examples.streetlights.v2'; - const PACKAGE_PATH = path.join(...PACKAGE.split('.')); - const params = { - server: 'mtls-connections', - package: PACKAGE - }; - const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true, templateParams: params }); - await generator.generateFromFile(path.resolve('test', 'mocks/kafka-streetlights-v2.yml')); - - const expectedFiles = [ - 'pom.xml', - `${PACKAGE_PATH}/Connection.java`, - `${PACKAGE_PATH}/ConnectionHelper.java`, - `${PACKAGE_PATH}/LoggingHelper.java`, - `${PACKAGE_PATH}/DemoSubscriber.java`, - `${PACKAGE_PATH}/PubSubBase.java`, - `${PACKAGE_PATH}/SmartylightingStreetlights10ActionStreetlightIdDimProducer.java`, - `${PACKAGE_PATH}/SmartylightingStreetlights10ActionStreetlightIdTurnOffProducer.java`, - `${PACKAGE_PATH}/SmartylightingStreetlights10ActionStreetlightIdTurnOnProducer.java`, - `${PACKAGE_PATH}/SmartylightingStreetlights10EventStreetlightIdLightingMeasuredSubscriber.java`, - `${PACKAGE_PATH}/models/DimLight.java`, - `${PACKAGE_PATH}/models/LightMeasured.java`, - `${PACKAGE_PATH}/models/ModelContract.java`, - `${PACKAGE_PATH}/models/TurnOnOff.java`, - 'Dockerfile', - 'env.json' - ]; - - for (const file of expectedFiles) { - expect(existsSync(path.join(OUTPUT_DIR, file))).toBe(true); - } - const connectionHelper = readFileSync(path.join(OUTPUT_DIR, `${PACKAGE_PATH}/ConnectionHelper.java`), 'utf-8'); - expect(connectionHelper.includes('props.put("security.protocol", "SSL")')).toBe(true); + const verified = await generateJavaProject( + PACKAGE, + { + server: 'mtls-connections', + package: PACKAGE + }, + 'mocks/kafka-streetlights-v2.yml', + [ + 'DemoSubscriber.java', + 'SmartylightingStreetlights10ActionStreetlightIdDimProducer.java', + 'SmartylightingStreetlights10ActionStreetlightIdTurnOffProducer.java', + 'SmartylightingStreetlights10ActionStreetlightIdTurnOnProducer.java', + 'SmartylightingStreetlights10EventStreetlightIdLightingMeasuredSubscriber.java', + 'models/DimLight.java', + 'models/LightMeasured.java', + 'models/ModelContract.java', + 'models/TurnOnOff.java', + ], + [ + 'props.put("security.protocol", "SSL")', + ]); + expect(verified).toBe(true); }); it('should generate Java for a v3 AsyncAPI', async () => { - const OUTPUT_DIR = generateFolderName(); - console.log(OUTPUT_DIR); const PACKAGE = 'com.asyncapi.examples.streetlights.v3'; - const PACKAGE_PATH = path.join(...PACKAGE.split('.')); - const params = { - server: 'scram-connections', - package: PACKAGE - }; - const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true, templateParams: params }); - await generator.generateFromFile(path.resolve('test', 'mocks/kafka-streetlights-v3.yml')); - - const expectedFiles = [ - 'pom.xml', - `${PACKAGE_PATH}/Connection.java`, - `${PACKAGE_PATH}/ConnectionHelper.java`, - `${PACKAGE_PATH}/LoggingHelper.java`, - `${PACKAGE_PATH}/DemoSubscriber.java`, - `${PACKAGE_PATH}/PubSubBase.java`, - `${PACKAGE_PATH}/LightingMeasuredSubscriber.java`, - `${PACKAGE_PATH}/LightsDimProducer.java`, - `${PACKAGE_PATH}/LightTurnOffProducer.java`, - `${PACKAGE_PATH}/LightTurnOnProducer.java`, - `${PACKAGE_PATH}/models/DimLight.java`, - `${PACKAGE_PATH}/models/LightMeasured.java`, - `${PACKAGE_PATH}/models/ModelContract.java`, - `${PACKAGE_PATH}/models/TurnOn.java`, - 'Dockerfile', - 'env.json' - ]; - - for (const file of expectedFiles) { - expect(existsSync(path.join(OUTPUT_DIR, file))).toBe(true); - } - const connectionHelper = readFileSync(path.join(OUTPUT_DIR, `${PACKAGE_PATH}/ConnectionHelper.java`), 'utf-8'); - expect(connectionHelper.includes('props.put("security.protocol", "SASL_SSL")')).toBe(true); - expect(connectionHelper.includes('props.put("sasl.mechanism", "SCRAM-SHA-256")')).toBe(true); + const verified = await generateJavaProject( + PACKAGE, + { + server: 'scram-connections', + package: PACKAGE + }, + 'mocks/kafka-streetlights-v3.yml', + [ + 'DemoSubscriber.java', + 'LightingMeasuredSubscriber.java', + 'LightsDimProducer.java', + 'LightTurnOffProducer.java', + 'LightTurnOnProducer.java', + 'models/DimLight.java', + 'models/LightMeasured.java', + 'models/ModelContract.java', + 'models/TurnOn.java', + ], + [ + 'props.put("security.protocol", "SASL_SSL")', + 'props.put("sasl.mechanism", "SCRAM-SHA-256")', + ]); + expect(verified).toBe(true); }); -}); \ No newline at end of file +}); diff --git a/test/Types.utils.test.js b/test/Types.utils.test.js index c739edd..ac556bd 100644 --- a/test/Types.utils.test.js +++ b/test/Types.utils.test.js @@ -46,69 +46,27 @@ test('Check local variables is changed', () => { ]); }); -// Test class defineVariablesForProperties test('Define Variables For Properties', async () => { - const generateFolderName = () => { - return path.resolve(MAIN_TEST_RESULT_PATH, crypto.randomBytes(4).toString('hex')); - }; - jest.setTimeout(30000); - - const OUTPUT_DIR = generateFolderName(); - - const params = { - server: 'production' - }; - - const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true, templateParams: params }); - await generator.generateFromFile(path.resolve('test', 'mocks/single-channel.yml')); - - const payload = generator.asyncapi.allMessages().get('song').payload(); - expect(typesUtils.defineVariablesForProperties(payload)).toStrictEqual(['public String title;', 'public String artist;', 'public String album;', 'public String genre;', 'public int length;'],); -}); + const OUTPUT_DIR = path.resolve(MAIN_TEST_RESULT_PATH, crypto.randomBytes(4).toString('hex')); -// Test class passJavaArgs -test('Pass Java Args', async () => { - const generateFolderName = () => { - return path.resolve(MAIN_TEST_RESULT_PATH, crypto.randomBytes(4).toString('hex')); - }; - - jest.setTimeout(30000); - - const OUTPUT_DIR = generateFolderName(); - const params = { server: 'production' }; - - const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true, templateParams: params }); - await generator.generateFromFile(path.resolve('test', 'mocks/single-channel.yml')); - - const properties = generator.asyncapi.allMessages().get('song').payload().properties(); - expect(typesUtils.passJavaArgs(properties)).toStrictEqual('title,artist,album,genre,length'); -}); - -// Test class createJavaArgsFromProperties -test('Create Java Args from Properties', async () => { - const generateFolderName = () => { - return path.resolve(MAIN_TEST_RESULT_PATH, crypto.randomBytes(4).toString('hex')); - }; - - jest.setTimeout(30000); - - const OUTPUT_DIR = generateFolderName(); - - const params = { - server: 'production' - }; - const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true, templateParams: params }); await generator.generateFromFile(path.resolve('test', 'mocks/single-channel.yml')); - + + const payload = generator.asyncapi.allMessages().get('song').payload(); const properties = generator.asyncapi.allMessages().get('song').payload().properties(); + // Test class defineVariablesForProperties + expect(typesUtils.defineVariablesForProperties(payload)).toStrictEqual(['public String title;', 'public String artist;', 'public String album;', 'public String genre;', 'public int length;'],); + + // Test class passJavaArgs + expect(typesUtils.passJavaArgs(properties)).toStrictEqual('title,artist,album,genre,length'); + // Test class createJavaArgsFromProperties expect(typesUtils.createJavaArgsFromProperties(properties)).toStrictEqual(['String title', 'String artist', 'String album', 'String genre', 'int length']); });