From 902acb640d1ae5cbb6445b421f9e997c1a24fb23 Mon Sep 17 00:00:00 2001 From: arjungarg07 Date: Fri, 11 Feb 2022 00:55:34 +0530 Subject: [PATCH 1/7] fix: work correctly on pre-parsed input --- lib/utils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 94e88146..7f0ab30b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -6,9 +6,10 @@ const parser = require('@asyncapi/parser'); * @param {Array} asyncApiDocs unparsed AsyncAPI documents * @returns {Promise} parsed AsyncAPI documents */ + function validate(asyncApiDocs) { return Promise.all(asyncApiDocs.map(async doc => { - if (doc && doc['x-parser-spec-parsed'] === true) { + if (typeof doc === 'object' && doc.ext && doc.ext('x-parser-spec-parsed')) { return doc; } return parser.parse(doc); From b582aaf0755becc748aef6afb774c006ab2903f9 Mon Sep 17 00:00:00 2001 From: arjungarg07 Date: Sat, 5 Mar 2022 17:24:45 +0530 Subject: [PATCH 2/7] add tests for parsed asyncapi docs --- test/appRelationsDiscovery_test.js | 24 +++++++++++++++++++++++- test/testsUtil.js | 13 ++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/test/appRelationsDiscovery_test.js b/test/appRelationsDiscovery_test.js index 0d0a6af8..176568b4 100644 --- a/test/appRelationsDiscovery_test.js +++ b/test/appRelationsDiscovery_test.js @@ -5,7 +5,7 @@ const expect = chai.expect; chai.use(chaiAsPromised); const {getRelations} = require('../lib/appRelationsDiscovery'); -const {getAsyncApiExamples} = require('./testsUtil'); +const {getAsyncApiExamples,parseAsyncApiExamples} = require('./testsUtil'); const outputMermaid = 'graph TD\n server1[(mqtt://localhost:1883)]\nFlightMonitorService[Flight Monitor Service]\nFlightMonitorService -- flight/update --> server1\nFlightNotifierService[Flight Notifier Service]\nserver1 -- flight/update --> FlightNotifierService\nFlightSubscriberService[Flight Subscriber Service]\nFlightSubscriberService -- flight/queue --> server1\nserver1 -- flight/queue --> FlightMonitorService'; const outputPlantUML = '@startuml\ntitle Classes - Class Diagram\n\nclass server1 { \n url: mqtt://localhost:1883 \n protocol: mqtt\n}\nFlightMonitorService --|> server1:flight/update\nserver1 --|> FlightNotifierService:flight/update\nFlightSubscriberService --|> server1:flight/queue\nserver1 --|> FlightMonitorService:flight/queue\n@enduml'; @@ -19,62 +19,84 @@ describe('appRelationDiscovery', function() { let flightServiceDocs; let correctSlug; let slugOutput; + let parsedSlugOutput; let output; let correctChannelUpdate; let correctChannelQueue; + let parsedDocs; + let parsedOutput; before(async function() { flightServiceDocs = getAsyncApiExamples(); + parsedDocs = await parseAsyncApiExamples(flightServiceDocs); output = await getRelations(flightServiceDocs); + parsedOutput = await getRelations(parsedDocs); correctSlug = 'mqtt://localhost:1883,mqtt'; slugOutput = output.get(correctSlug); + parsedSlugOutput = parsedOutput.get(correctSlug); correctChannelUpdate = 'flight/update'; correctChannelQueue = 'flight/queue'; }); it('should return correct slug', function() { expect(output).to.have.key(correctSlug); + expect(parsedOutput).to.have.key(correctSlug); }); it('should return correct channels', function() { expect(slugOutput).to.have.all.keys(correctChannelUpdate, correctChannelQueue); + expect(parsedSlugOutput).to.have.all.keys(correctChannelUpdate, correctChannelQueue); }); it('should return correct subscriber data for flight/update channel', async function() { const updateChannelOutput = slugOutput.get(correctChannelUpdate); + const updateChannelParsedOutput = parsedSlugOutput.get(correctChannelUpdate); const correctSubOperation = 'Flight Monitor Service'; expect(updateChannelOutput.sub.get(correctSubOperation)).to.deep.equal(flightUpdateSubData.get(correctSubOperation)); + expect(updateChannelParsedOutput.sub.get(correctSubOperation)).to.deep.equal(flightUpdateSubData.get(correctSubOperation)); }); it('should return correct publisher data for flight/update channel', async function() { const updateChannelOutput = slugOutput.get(correctChannelUpdate); + const updateChannelParsedOutput = parsedSlugOutput.get(correctChannelUpdate); const correctPubOperation = 'Flight Notifier Service'; expect(updateChannelOutput.pub.get(correctPubOperation)).to.deep.equal(flightUpdatePubData.get(correctPubOperation)); + expect(updateChannelParsedOutput.pub.get(correctPubOperation)).to.deep.equal(flightUpdatePubData.get(correctPubOperation)); }); it('should return correct subscriber data for flight/queue channel', async function() { const queueChannelOutput = slugOutput.get(correctChannelQueue); + const queueChannelParsedOutput = parsedSlugOutput.get(correctChannelQueue); const correctSubOperation = 'Flight Subscriber Service'; expect(queueChannelOutput.sub.get(correctSubOperation)).to.deep.equal(flightQueueSubData.get(correctSubOperation)); + expect(queueChannelParsedOutput.sub.get(correctSubOperation)).to.deep.equal(flightQueueSubData.get(correctSubOperation)); }); it('should return correct publisher data for flight/queue channel', async function() { const queueChannelOutput = slugOutput.get(correctChannelQueue); + const queueChannelParsedOutput = parsedSlugOutput.get(correctChannelQueue); const correctPubOperation = 'Flight Monitor Service'; expect(queueChannelOutput.pub.get(correctPubOperation)).to.deep.equal(flightQueuePubData.get(correctPubOperation)); + expect(queueChannelParsedOutput.pub.get(correctPubOperation)).to.deep.equal(flightQueuePubData.get(correctPubOperation)); }); it('should return the correct mermaid syntax', async function() { const output = await getRelations(flightServiceDocs,{syntax: 'mermaid'}); + const parsedOutput = await getRelations(parsedDocs,{syntax: 'mermaid'}); expect(output).to.be.equal(outputMermaid); + expect(parsedOutput).to.be.equal(outputMermaid); }); it('should return the correct plantUML syntax', async function() { const output = await getRelations(flightServiceDocs,{syntax: 'plantUML'}); + const parsedOutput = await getRelations(parsedDocs,{syntax: 'plantUML'}); expect(output).to.be.equal(outputPlantUML); + expect(parsedOutput).to.be.equal(outputPlantUML); }); it('should return the correct reactFlow elements array', async function() { const output = await getRelations(flightServiceDocs,{syntax: 'reactFlow'}); + const parsedOutput = await getRelations(parsedDocs,{syntax: 'reactFlow'}); expect(output).to.be.deep.equal(outputReactFlow); + expect(parsedOutput).to.be.deep.equal(outputReactFlow); }); }); diff --git a/test/testsUtil.js b/test/testsUtil.js index 903a64bd..ea072f1a 100644 --- a/test/testsUtil.js +++ b/test/testsUtil.js @@ -1,8 +1,19 @@ const path = require('path'); const fs = require('fs'); +const parser = require('@asyncapi/parser'); const examplesPath = './test/examples/flightService'; +async function parseAsyncApiExamples(asyncApiDocs) { + const docs = []; + for (const doc of asyncApiDocs) { + const parsedDoc = await parser.parse(doc); + docs.push(parsedDoc); + } + console.log(docs); + return docs; +} + function getAsyncApiExamples() { const docs = []; const files = fs.readdirSync(examplesPath); @@ -14,4 +25,4 @@ function getAsyncApiExamples() { return docs; } -module.exports = {getAsyncApiExamples}; \ No newline at end of file +module.exports = {getAsyncApiExamples,parseAsyncApiExamples}; \ No newline at end of file From b94c8e5eb88b4701840b74dc52de4ff1fbf8084a Mon Sep 17 00:00:00 2001 From: Arjun Garg Date: Mon, 7 Mar 2022 23:38:33 +0530 Subject: [PATCH 3/7] Update test/testsUtil.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maciej Urbańczyk --- test/testsUtil.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/testsUtil.js b/test/testsUtil.js index ea072f1a..936863a4 100644 --- a/test/testsUtil.js +++ b/test/testsUtil.js @@ -10,7 +10,6 @@ async function parseAsyncApiExamples(asyncApiDocs) { const parsedDoc = await parser.parse(doc); docs.push(parsedDoc); } - console.log(docs); return docs; } From 1fbfbdad1efd8bd757a016915f63ace39ccdb8a8 Mon Sep 17 00:00:00 2001 From: Arjun Garg Date: Mon, 7 Mar 2022 23:38:52 +0530 Subject: [PATCH 4/7] Update lib/utils.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maciej Urbańczyk --- lib/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 7f0ab30b..47a75235 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -9,7 +9,7 @@ const parser = require('@asyncapi/parser'); function validate(asyncApiDocs) { return Promise.all(asyncApiDocs.map(async doc => { - if (typeof doc === 'object' && doc.ext && doc.ext('x-parser-spec-parsed')) { + if (typeof doc === 'object' && typeof doc.ext === 'function' && doc.ext('x-parser-spec-parsed')) { return doc; } return parser.parse(doc); From 563e9f616f0ed53795deae9a9bc9ca50262749f6 Mon Sep 17 00:00:00 2001 From: arjungarg07 Date: Fri, 8 Apr 2022 08:40:28 +0530 Subject: [PATCH 5/7] chore: remove console --- test/testsUtil.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/testsUtil.js b/test/testsUtil.js index ea072f1a..936863a4 100644 --- a/test/testsUtil.js +++ b/test/testsUtil.js @@ -10,7 +10,6 @@ async function parseAsyncApiExamples(asyncApiDocs) { const parsedDoc = await parser.parse(doc); docs.push(parsedDoc); } - console.log(docs); return docs; } From de5f6820bc3e671b9c507012ab1fbf519b5858b7 Mon Sep 17 00:00:00 2001 From: arjungarg07 Date: Fri, 8 Apr 2022 08:40:47 +0530 Subject: [PATCH 6/7] chore: fix code smells --- test/appRelationsDiscovery_test.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/appRelationsDiscovery_test.js b/test/appRelationsDiscovery_test.js index 176568b4..db805e6b 100644 --- a/test/appRelationsDiscovery_test.js +++ b/test/appRelationsDiscovery_test.js @@ -80,23 +80,23 @@ describe('appRelationDiscovery', function() { }); it('should return the correct mermaid syntax', async function() { - const output = await getRelations(flightServiceDocs,{syntax: 'mermaid'}); - const parsedOutput = await getRelations(parsedDocs,{syntax: 'mermaid'}); - expect(output).to.be.equal(outputMermaid); - expect(parsedOutput).to.be.equal(outputMermaid); + const testOutput = await getRelations(flightServiceDocs,{syntax: 'mermaid'}); + const testParsedOutput = await getRelations(parsedDocs,{syntax: 'mermaid'}); + expect(testOutput).to.be.equal(outputMermaid); + expect(testParsedOutput).to.be.equal(outputMermaid); }); it('should return the correct plantUML syntax', async function() { - const output = await getRelations(flightServiceDocs,{syntax: 'plantUML'}); + const testOutput = await getRelations(flightServiceDocs,{syntax: 'plantUML'}); const parsedOutput = await getRelations(parsedDocs,{syntax: 'plantUML'}); - expect(output).to.be.equal(outputPlantUML); + expect(testOutput).to.be.equal(outputPlantUML); expect(parsedOutput).to.be.equal(outputPlantUML); }); it('should return the correct reactFlow elements array', async function() { - const output = await getRelations(flightServiceDocs,{syntax: 'reactFlow'}); - const parsedOutput = await getRelations(parsedDocs,{syntax: 'reactFlow'}); - expect(output).to.be.deep.equal(outputReactFlow); - expect(parsedOutput).to.be.deep.equal(outputReactFlow); + const testOutput = await getRelations(flightServiceDocs,{syntax: 'reactFlow'}); + const testParsedOutput = await getRelations(parsedDocs,{syntax: 'reactFlow'}); + expect(testOutput).to.be.deep.equal(outputReactFlow); + expect(testParsedOutput).to.be.deep.equal(outputReactFlow); }); }); From 323737d94b88d252fea904afafa352c140672a5b Mon Sep 17 00:00:00 2001 From: arjungarg07 Date: Fri, 8 Apr 2022 08:45:30 +0530 Subject: [PATCH 7/7] fix: code smell --- test/appRelationsDiscovery_test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/appRelationsDiscovery_test.js b/test/appRelationsDiscovery_test.js index db805e6b..c315036c 100644 --- a/test/appRelationsDiscovery_test.js +++ b/test/appRelationsDiscovery_test.js @@ -88,9 +88,9 @@ describe('appRelationDiscovery', function() { it('should return the correct plantUML syntax', async function() { const testOutput = await getRelations(flightServiceDocs,{syntax: 'plantUML'}); - const parsedOutput = await getRelations(parsedDocs,{syntax: 'plantUML'}); + const testParsedOutput = await getRelations(parsedDocs,{syntax: 'plantUML'}); expect(testOutput).to.be.equal(outputPlantUML); - expect(parsedOutput).to.be.equal(outputPlantUML); + expect(testParsedOutput).to.be.equal(outputPlantUML); }); it('should return the correct reactFlow elements array', async function() {