Skip to content

Commit

Permalink
fix remaining errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Sep 28, 2023
1 parent 55b7335 commit 03401a1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ parser.parse = async (asyncapi, oldOptions, generator) => {
if (!document) {
return {document, diagnostics};
}
const correctDocument = this.getProperApiDocument(document, this.templateConfig);
const correctDocument = this.getProperApiDocument(document, generator.templateConfig);
return {document: correctDocument, diagnostics};
};

Expand Down
2 changes: 1 addition & 1 deletion lib/templateConfigValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports.validateTemplateConfig = (templateConfig, templateParams, asyncap
isTemplateCompatible(generator, apiVersion);
isRequiredParamProvided(parameters, templateParams);
isProvidedTemplateRendererSupported(templateConfig);
if (asyncapiDocument) {
if (asyncapiDocument && templateParams.server) {
let server;
if (usesNewAPI(templateConfig)) {
server = asyncapiDocument.servers().get(templateParams.server);
Expand Down
53 changes: 48 additions & 5 deletions test/templateConfigValidator.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable sonarjs/no-duplicate-string */
const { validateTemplateConfig } = require('../lib/templateConfigValidator');
const fs = require('fs');
const path = require('path');
Expand All @@ -10,7 +11,7 @@ describe('Template Configuration Validator', () => {
let asyncapiDocument;

beforeAll(async () => {
const { document } = await parse(dummyYAML, {}, {templateConfig: {apiVersion: 'v2'}});
const { document } = await parse(dummyYAML, {}, {templateConfig: {}});
asyncapiDocument = document;
});

Expand Down Expand Up @@ -189,20 +190,62 @@ describe('Template Configuration Validator', () => {
expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Server "dummy-mqtt" uses the mqtt protocol but this template only supports the following ones: myprotocol.');
});

describe('should work with v2 apiVersion', () => {
describe('should work with v1 apiVersion', () => {
let asyncapiDocument;
const v2TemplateConfig = {apiVersion: 'v1'};
beforeAll(async () => {
const { document } = await parse(dummyYAML, {}, {templateConfig: v2TemplateConfig});
asyncapiDocument = document;
});

it('Validation throw error if specified server is not in asyncapi document', () => {
const templateParams = {
server: 'myserver'
};
const templateConfig = {
...v2TemplateConfig,
parameters: {
server: {
description: ''
}
}
};

expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Couldn\'t find server with name myserver.');
});

it('Validation throw error if given protocol is not supported by template', () => {
const templateParams = {
server: 'dummy-mqtt'
};
const templateConfig = {
...v2TemplateConfig,
supportedProtocols: ['myprotocol'],
parameters: {
server: {
description: ''
}
}
};

expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Server "dummy-mqtt" uses the mqtt protocol but this template only supports the following ones: myprotocol.');
});
});

describe('should work with v2 apiVersion', () => {
let asyncapiDocument;
const v2TemplateConfig = {apiVersion: 'v2'};
beforeAll(async () => {
const { document } = await parse(dummyYAML, {}, {templateConfig: {apiVersion: 'v2'}});
const { document } = await parse(dummyYAML, {}, {templateConfig: v2TemplateConfig});
asyncapiDocument = document;
});

// eslint-disable-next-line sonarjs/no-identical-functions
it('Validation throw error if specified server is not in asyncapi document', () => {

Check failure on line 243 in test/templateConfigValidator.test.js

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Update this function so that its implementation is not identical to the one on line 201
const templateParams = {
server: 'myserver'
};
const templateConfig = {
...v2TemplateConfig,
parameters: {
server: {
description: ''
Expand All @@ -213,12 +256,12 @@ describe('Template Configuration Validator', () => {
expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Couldn\'t find server with name myserver.');
});

// eslint-disable-next-line sonarjs/no-identical-functions
it('Validation throw error if given protocol is not supported by template', () => {

Check failure on line 259 in test/templateConfigValidator.test.js

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Update this function so that its implementation is not identical to the one on line 217
const templateParams = {
server: 'dummy-mqtt'
};
const templateConfig = {
...v2TemplateConfig,
supportedProtocols: ['myprotocol'],
parameters: {
server: {
Expand Down

0 comments on commit 03401a1

Please sign in to comment.