Skip to content

Commit

Permalink
fix tests and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jul 24, 2023
1 parent e72dd5c commit a7092d7
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/commands/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ export default class Bundle extends Command {
let baseFile;
const outputFormat = path.extname(argv[0]);
const AsyncAPIFiles = await this.loadFiles(argv);
const containsAsyncAPI3 = AsyncAPIFiles.map((file) => {

const containsAsyncAPI3 = AsyncAPIFiles.filter((file) => {
return file.isAsyncAPI3();
});
if (containsAsyncAPI3) {
return this.error('One of the files you tried to bundle is AsyncAPI v3 format, the bundle command does not support it yet, please checkout https://github.com/asyncapi/bundler/issues/133');
if (containsAsyncAPI3.length > 0) {
this.error('One of the files you tried to bundle is AsyncAPI v3 format, the bundle command does not support it yet, please checkout https://github.com/asyncapi/bundler/issues/133');
}

if (flags.base) {baseFile = (await load(flags.base)).text();}

const fileContents = AsyncAPIFiles.map((file) => file.text());
Expand Down
2 changes: 1 addition & 1 deletion src/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default class Diff extends Command {
firstDocument = await load(firstDocumentPath);

if (firstDocument.isAsyncAPI3()) {
return this.error('Diff command does not support AsyncAPI v3 yet, please checkout https://github.com/asyncapi/diff/issues/154');
this.error('Diff command does not support AsyncAPI v3 yet, please checkout https://github.com/asyncapi/diff/issues/154');
}

enableWatch(watchMode, {
Expand Down
30 changes: 30 additions & 0 deletions src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ interface ParsedFlags {
mapBaseUrlToFolder: IMapBaseUrlToFlag
}

const templatesNotSupportingV3: Record<string, string> = {
'@asyncapi/minimaltemplate': 'some link', // For testing purpose
'@asyncapi/html-template': 'https://github.com/asyncapi/html-template/issues/430',
'@asyncapi/dotnet-nats-template': 'https://github.com/asyncapi/dotnet-nats-template/issues/384',
'@asyncapi/ts-nats-template': 'https://github.com/asyncapi/ts-nats-template/issues/545',
'@asyncapi/python-paho-template': 'https://github.com/asyncapi/python-paho-template/issues/189',
'@asyncapi/nodejs-ws-template': 'https://github.com/asyncapi/nodejs-ws-template/issues/294',
'@asyncapi/java-spring-cloud-stream-template': 'https://github.com/asyncapi/java-spring-cloud-stream-template/issues/336',
'@asyncapi/go-watermill-template': 'https://github.com/asyncapi/go-watermill-template/issues/243',
'@asyncapi/java-spring-template': 'https://github.com/asyncapi/java-spring-template/issues/308',
'@asyncapi/markdown-template': 'https://github.com/asyncapi/markdown-template/issues/341'
};

/**
* Verify that a given template support v3, if not, return the link to the issue that needs to be solved.
*/
function verifyTemplateSupportForV3(template: string) {
if (templatesNotSupportingV3[`${template}`] !== undefined) {
return templatesNotSupportingV3[`${template}`];
}
return undefined;
}

export default class Template extends Command {
static description = 'Generates whatever you want using templates compatible with AsyncAPI Generator.';

Expand Down Expand Up @@ -99,13 +122,20 @@ export default class Template extends Command {
mapBaseUrlToFolder: parsedFlags.mapBaseUrlToFolder,
disabledHooks: parsedFlags.disableHooks,
};
const asyncapiInput = (await load(asyncapi)) || (await load());

const watchTemplate = flags['watch'];
const genOption: any = {};
if (flags['map-base-url']) {
genOption.resolve = {resolve: this.getMapBaseUrlToFolderResolver(parsedFlags.mapBaseUrlToFolder)};
}

if (asyncapiInput.isAsyncAPI3()) {
const v3IssueLink = verifyTemplateSupportForV3(template);
if (v3IssueLink !== undefined) {
this.error(`${template} template does not support AsyncAPI v3 documents, please checkout ${v3IssueLink}`);
}
}
await this.generate(asyncapi, template, output, options, genOption);
if (watchTemplate) {
const watcherHandler = this.watcherHandler(asyncapi, template, output, options, genOption);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/generate/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class Models extends Command {
const { language, file } = args;
const inputFile = (await load(file)) || (await load());
if (inputFile.isAsyncAPI3()) {
return this.error('Generate Models command does not support AsyncAPI v3 yet, please checkout https://github.com/asyncapi/modelina/issues/1376');
this.error('Generate Models command does not support AsyncAPI v3 yet, please checkout https://github.com/asyncapi/modelina/issues/1376');
}
const { document, diagnostics ,status } = await parse(this, inputFile, flags);
if (!document || status === 'invalid') {
Expand Down
19 changes: 15 additions & 4 deletions src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default class Optimize extends Command {
try {
specFile = await load(filePath);
} catch (err) {
return this.error(
this.error(
new ValidationError({
type: 'invalid-file',
filepath: filePath,
Expand All @@ -60,11 +60,22 @@ export default class Optimize extends Command {
}

if (specFile.isAsyncAPI3()) {
return this.error('Optimize command does not support AsyncAPI v3 yet, please checkout https://github.com/asyncapi/optimizer/issues/168');
this.error('Optimize command does not support AsyncAPI v3 yet, please checkout https://github.com/asyncapi/optimizer/issues/168');
}

const optimizer: Optimizer = new Optimizer(specFile.text());
const report: Report = await optimizer.getReport();
let optimizer: Optimizer;
let report: Report;
try {
optimizer = new Optimizer(specFile.text());
report = await optimizer.getReport();
} catch (err) {
this.error(
new ValidationError({
type: 'invalid-file',
filepath: filePath,
})
);
}
this.isInteractive = !flags['no-tty'];
this.optimizations = flags.optimization as Optimizations[];
this.outputMethod = flags.output as Outputs;
Expand Down
15 changes: 15 additions & 0 deletions test/commands/bundle/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ import path from 'path';
import { fileCleanup } from '../../testHelper';

const spec = fs.readFileSync('./test/commands/bundle/final-asyncapi.yaml', {encoding: 'utf-8'});
const asyncapiv3 = './test/specification-v3.yml';

function validateGeneratedSpec(filePath, spec) {
const generatedSPec = fs.readFileSync(path.resolve(filePath), { encoding: 'utf-8' });
return generatedSPec === spec;
}

describe('bundle', () => {
describe('should handle AsyncAPI v3 document correctly', () => {
test
.stderr()
.stdout()
.command([
'bundle',
asyncapiv3,
'--output=./test/commands/bundle/final.yaml'])
.it('give error', (ctx, done) => {
expect(ctx.stderr).toEqual('Error: One of the files you tried to bundle is AsyncAPI v3 format, the bundle command does not support it yet, please checkout https://github.com/asyncapi/bundler/issues/133\n');
expect(ctx.stdout).toEqual('');
done();
});
});
test
.stdout()
.command([
Expand Down
12 changes: 12 additions & 0 deletions test/commands/diff.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable sonarjs/no-duplicate-string */
import { test } from '@oclif/test';

const asyncapiv3 = './test/specification-v3.yml';
const noChangesJson = '"{\\n \\"changes\\": []\\n}\\n"';
const breakingChangesJson = '"[\\n {\\n \\"action\\": \\"edit\\",\\n \\"path\\": \\"/servers/mosquitto/protocol\\",\\n \\"before\\": \\"mqtt\\",\\n \\"after\\": \\"http\\",\\n \\"type\\": \\"breaking\\"\\n },\\n {\\n \\"action\\": \\"edit\\",\\n \\"path\\": \\"/servers/mosquitto/url\\",\\n \\"before\\": \\"mqtt://test.mosquitto.org\\",\\n \\"after\\": \\"http://test.mosquitto.org\\",\\n \\"type\\": \\"breaking\\"\\n }\\n]\\n"';
const nonBreakingChangesJson = '"[\\n {\\n \\"action\\": \\"add\\",\\n \\"path\\": \\"/channels/user~1signedup\\",\\n \\"after\\": {\\n \\"subscribe\\": {\\n \\"message\\": {\\n \\"payload\\": {\\n \\"type\\": \\"object\\",\\n \\"properties\\": {\\n \\"displayName\\": {\\n \\"type\\": \\"string\\",\\n \\"description\\": \\"Name of the user\\",\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-2>\\"\\n },\\n \\"email\\": {\\n \\"type\\": \\"string\\",\\n \\"format\\": \\"email\\",\\n \\"description\\": \\"Email of the user\\",\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-3>\\"\\n }\\n },\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-1>\\"\\n },\\n \\"x-parser-message-name\\": \\"UserSignedUp\\",\\n \\"x-parser-original-schema-format\\": \\"application/vnd.aai.asyncapi;version=2.1.0\\",\\n \\"schemaFormat\\": \\"application/vnd.aai.asyncapi;version=2.1.0\\",\\n \\"x-parser-original-payload\\": {\\n \\"type\\": \\"object\\",\\n \\"properties\\": {\\n \\"displayName\\": {\\n \\"type\\": \\"string\\",\\n \\"description\\": \\"Name of the user\\",\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-2>\\"\\n },\\n \\"email\\": {\\n \\"type\\": \\"string\\",\\n \\"format\\": \\"email\\",\\n \\"description\\": \\"Email of the user\\",\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-3>\\"\\n }\\n },\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-1>\\"\\n },\\n \\"x-parser-message-parsed\\": true\\n }\\n }\\n },\\n \\"type\\": \\"non-breaking\\"\\n },\\n {\\n \\"action\\": \\"edit\\",\\n \\"path\\": \\"/info/title\\",\\n \\"before\\": \\"Streetlights API\\",\\n \\"after\\": \\"Streetlights API V2\\",\\n \\"type\\": \\"non-breaking\\"\\n },\\n {\\n \\"action\\": \\"add\\",\\n \\"path\\": \\"/components\\",\\n \\"after\\": {\\n \\"messages\\": {\\n \\"UserSignedUp\\": {\\n \\"payload\\": {\\n \\"type\\": \\"object\\",\\n \\"properties\\": {\\n \\"displayName\\": {\\n \\"type\\": \\"string\\",\\n \\"description\\": \\"Name of the user\\",\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-2>\\"\\n },\\n \\"email\\": {\\n \\"type\\": \\"string\\",\\n \\"format\\": \\"email\\",\\n \\"description\\": \\"Email of the user\\",\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-3>\\"\\n }\\n },\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-1>\\"\\n },\\n \\"x-parser-message-name\\": \\"UserSignedUp\\",\\n \\"x-parser-original-schema-format\\": \\"application/vnd.aai.asyncapi;version=2.1.0\\",\\n \\"schemaFormat\\": \\"application/vnd.aai.asyncapi;version=2.1.0\\",\\n \\"x-parser-original-payload\\": {\\n \\"type\\": \\"object\\",\\n \\"properties\\": {\\n \\"displayName\\": {\\n \\"type\\": \\"string\\",\\n \\"description\\": \\"Name of the user\\",\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-2>\\"\\n },\\n \\"email\\": {\\n \\"type\\": \\"string\\",\\n \\"format\\": \\"email\\",\\n \\"description\\": \\"Email of the user\\",\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-3>\\"\\n }\\n },\\n \\"x-parser-schema-id\\": \\"<anonymous-schema-1>\\"\\n },\\n \\"x-parser-message-parsed\\": true\\n }\\n }\\n },\\n \\"type\\": \\"non-breaking\\"\\n }\\n]\\n"';
Expand All @@ -16,6 +17,17 @@ const markdownJsonOutput = "\"## Unclassified\\n\\n\\n - **Path**: `/channels/li
const markdownYamlOutput = '"## Unclassified\\n\\n\\n - **Path**: `/channels/light~1measured/publish/message/x-parser-original-payload/x-parser-schema-id`\\n - **Action**: edit\\n - **Before**: <anonymous-schema-1>\\n - **After**: <anonymous-schema-4>\\n \\n - **Path**: `/channels/light~1measured/publish/message/x-parser-original-payload/properties/sentAt/x-parser-schema-id`\\n - **Action**: edit\\n - **Before**: <anonymous-schema-4>\\n - **After**: <anonymous-schema-7>\\n \\n - **Path**: `/channels/light~1measured/publish/message/x-parser-original-payload/properties/lumens/x-parser-schema-id`\\n - **Action**: edit\\n - **Before**: <anonymous-schema-3>\\n - **After**: <anonymous-schema-6>\\n \\n - **Path**: `/channels/light~1measured/publish/message/x-parser-original-payload/properties/id/x-parser-schema-id`\\n - **Action**: edit\\n - **Before**: <anonymous-schema-2>\\n - **After**: <anonymous-schema-5>\\n \\n - **Path**: `/channels/light~1measured/publish/message/x-parser-original-payload/properties/id/minimum`\\n - **Action**: edit\\n - **Before**: 0\\n - **After**: 1\\n \\n - **Path**: `/channels/light~1measured/publish/message/payload/x-parser-schema-id`\\n - **Action**: edit\\n - **Before**: <anonymous-schema-1>\\n - **After**: <anonymous-schema-4>\\n \\n - **Path**: `/channels/light~1measured/publish/message/payload/properties/sentAt/x-parser-schema-id`\\n - **Action**: edit\\n - **Before**: <anonymous-schema-4>\\n - **After**: <anonymous-schema-7>\\n \\n - **Path**: `/channels/light~1measured/publish/message/payload/properties/lumens/x-parser-schema-id`\\n - **Action**: edit\\n - **Before**: <anonymous-schema-3>\\n - **After**: <anonymous-schema-6>\\n \\n - **Path**: `/channels/light~1measured/publish/message/payload/properties/id/x-parser-schema-id`\\n - **Action**: edit\\n - **Before**: <anonymous-schema-2>\\n - **After**: <anonymous-schema-5>\\n \\n - **Path**: `/channels/light~1measured/publish/message/payload/properties/id/minimum`\\n - **Action**: edit\\n - **Before**: 0\\n - **After**: 1\\n \\n\\n## Non-breaking\\n\\n\\n - **Path**: `/channels/user~1signedup`\\n - **Action**: add\\n - <details>\\n <summary> After </summary>\\n \\n ```yaml\\n subscribe:\\n message:\\n payload:\\n type: object\\n properties:\\n displayName:\\n type: string\\n description: Name of the user\\n x-parser-schema-id: <anonymous-schema-2>\\n email:\\n type: string\\n format: email\\n description: Email of the user\\n x-parser-schema-id: <anonymous-schema-3>\\n x-parser-schema-id: <anonymous-schema-1>\\n x-parser-message-name: UserSignedUp\\n x-parser-original-schema-format: application/vnd.aai.asyncapi;version=2.1.0\\n schemaFormat: application/vnd.aai.asyncapi;version=2.1.0\\n x-parser-original-payload:\\n type: object\\n properties:\\n displayName:\\n type: string\\n description: Name of the user\\n x-parser-schema-id: <anonymous-schema-2>\\n email:\\n type: string\\n format: email\\n description: Email of the user\\n x-parser-schema-id: <anonymous-schema-3>\\n x-parser-schema-id: <anonymous-schema-1>\\n x-parser-message-parsed: true\\n \\n ``` \\n </details> \\n \\n \\n - **Path**: `/info/title`\\n - **Action**: edit\\n - **Before**: Streetlights API\\n - **After**: Streetlights API V2\\n \\n - **Path**: `/components`\\n - **Action**: add\\n - <details>\\n <summary> After </summary>\\n \\n ```yaml\\n messages:\\n UserSignedUp:\\n payload:\\n type: object\\n properties:\\n displayName:\\n type: string\\n description: Name of the user\\n x-parser-schema-id: <anonymous-schema-2>\\n email:\\n type: string\\n format: email\\n description: Email of the user\\n x-parser-schema-id: <anonymous-schema-3>\\n x-parser-schema-id: <anonymous-schema-1>\\n x-parser-message-name: UserSignedUp\\n x-parser-original-schema-format: application/vnd.aai.asyncapi;version=2.1.0\\n schemaFormat: application/vnd.aai.asyncapi;version=2.1.0\\n x-parser-original-payload:\\n type: object\\n properties:\\n displayName:\\n type: string\\n description: Name of the user\\n x-parser-schema-id: <anonymous-schema-2>\\n email:\\n type: string\\n format: email\\n description: Email of the user\\n x-parser-schema-id: <anonymous-schema-3>\\n x-parser-schema-id: <anonymous-schema-1>\\n x-parser-message-parsed: true\\n \\n ``` \\n </details> \\n \\n \\n\\n## Breaking\\n\\n\\n - **Path**: `/servers/mosquitto/protocol`\\n - **Action**: edit\\n - **Before**: mqtt\\n - **After**: http\\n \\n - **Path**: `/servers/mosquitto/url`\\n - **Action**: edit\\n - **Before**: mqtt://test.mosquitto.org\\n - **After**: http://test.mosquitto.org\\n \\n\\n"';

describe('diff', () => {
describe('should handle AsyncAPI v3 document correctly', () => {
test
.stderr()
.stdout()
.command(['diff', asyncapiv3, asyncapiv3])
.it('give error', (ctx, done) => {
expect(ctx.stderr).toEqual('Error: Diff command does not support AsyncAPI v3 yet, please checkout https://github.com/asyncapi/diff/issues/154\n');
expect(ctx.stdout).toEqual('');
done();
});
});
describe('with file paths, and there are no difference between the files', () => {
test
.stderr()
Expand Down
15 changes: 15 additions & 0 deletions test/commands/generate/fromTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const generalOptions = [
'./test/specification.yml',
'@asyncapi/minimaltemplate',
];
const asyncapiv3 = './test/specification-v3.yml';

function cleanup(filepath: string) {
rimraf.sync(filepath);
Expand All @@ -28,6 +29,20 @@ describe('template', () => {
done();
});

describe('should handle AsyncAPI v3 document correctly', () => {
test
.stderr()
.stdout()
.command([
'generate:fromTemplate',
asyncapiv3,
'@asyncapi/minimaltemplate'])
.it('give error', (ctx, done) => {
expect(ctx.stderr).toEqual('Error: @asyncapi/minimaltemplate template does not support AsyncAPI v3 documents, please checkout some link\n');
expect(ctx.stdout).toEqual('');
done();
});
});
describe('git clash', () => {
const pathToOutput = './test/docs/2';
beforeAll(() => {
Expand Down
14 changes: 13 additions & 1 deletion test/commands/generate/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ import { test } from '@oclif/test';
import { createMockServer, stopMockServer } from '../../testHelper';
const generalOptions = ['generate:models'];
const outputDir = './test/commands/generate/models';

const asyncapiv3 = './test/specification-v3.yml';
describe('models', () => {
beforeAll(() => {
createMockServer();
});
afterAll(() => {
stopMockServer();
});
describe('should handle AsyncAPI v3 document correctly', () => {
test
.stderr()
.stdout()
.command([
...generalOptions, 'typescript', asyncapiv3])
.it('give error', (ctx, done) => {
expect(ctx.stderr).toEqual('Error: Generate Models command does not support AsyncAPI v3 yet, please checkout https://github.com/asyncapi/modelina/issues/1376\n');
expect(ctx.stdout).toEqual('');
done();
});
});
test
.stderr()
.stdout()
Expand Down
Loading

0 comments on commit a7092d7

Please sign in to comment.