diff --git a/.eslintignore b/.eslintignore index 116e02c42..70ce82e71 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,4 @@ node_modules -test/temp +test output -__transpiled \ No newline at end of file +__transpiled diff --git a/package.json b/package.json index dd60f499d..618ed9311 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,13 @@ "description": "Template for generating Go module based on async api", "main": "index.js", "scripts": { - "test": "npm run testComponents", + "test": "npm run test:components", "release": "semantic-release", "lint": "eslint --max-warnings 0 --fix --config .eslintrc .", "generate:readme:toc": "markdown-toc -i README.md", "generate:assets": "npm run generate:readme:toc", "bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION", - "testComponents": "rimraf test/temp && jest --coverage ./test/components" + "test:components": "jest --coverage ./test/components" }, "repository": { "type": "git", diff --git a/test/components/common.test.js b/test/components/common.test.js index 461ed6b3c..b5fb1ef8c 100644 --- a/test/components/common.test.js +++ b/test/components/common.test.js @@ -1,143 +1,63 @@ import { GetProtocolFlags, GetSubscriberFlags, pascalCase } from '../../components/common'; import AsyncAPIDocument from '@asyncapi/parser/lib/models/asyncapi'; +import parser from '@asyncapi/parser' +import fs from 'fs' +import path from 'path' -const docWithoutChannels = new AsyncAPIDocument({ - asyncapi: '2.1.0', - info: { - title: "Streetlights API", - version: '1.0.0', - description: "The Smartylighting Streetlights API", - } -}); - -const docWithChannels = new AsyncAPIDocument({ - asyncapi: '2.1.0', - info: { - title: "Streetlights API", - version: '1.0.0', - description: "The Smartylighting Streetlights API", - }, - channels: { - "light/measured": { - bindings: { - amqp: { - is: "routingKey", - queue: { - name: "light/measured", - durable: true, - exclusive: true, - autoDelete: false, - vhost: "/" - }, - bindingVersion: "0.2.0" - } - } - } - } -}); - -const docWithAMQPPublisher = new AsyncAPIDocument({ - asyncapi: '2.1.0', - info: { - title: "Streetlights API", - version: '1.0.0', - description: "The Smartylighting Streetlights API", - }, - channels: { - "light/measured": { - bindings: { - amqp: { - is: "routingKey", - queue: { - name: "light/measured", - durable: true, - exclusive: true, - autoDelete: false, - vhost: "/" - }, - bindingVersion: "0.2.0" - } - }, - publish: { - summary: "Inform about environmental lighting conditions for a particular streetlight", - operationId: "onLightMeaasured", - message: { - name: "LightMeasured", - payload: { - "$id": "LightMeasured", - additionalProperties: false, - type: "object", - properties: { - id: { - type: "integer", - minimum: 0, - description : "Id of the streetlight." - }, - lumens: { - type: "integer", - minimum: 0, - description: "Light intensity measured in lumens." - }, - sentAt: { - type: "string", - format: "date-time", - description: "Date and time when the message was sent." - } - } - } - } - } - } - } -}); +const docWithoutProtocols = fs.readFileSync(path.resolve(__dirname, '../files/docWithoutProtocols.yml'), 'utf8'); +const docWithAMQPublisher = fs.readFileSync(path.resolve(__dirname, '../files/docWithAMQPublisher.yml'), 'utf8'); +const docWithoutAMQPublisher = fs.readFileSync(path.resolve(__dirname, '../files/docWithoutAMQPublisher.yml'), 'utf8'); describe('GetProtocolFlags', () => { - it('should return protocols as false when no channels are present ', () => { + it('should return protocols as false when no channels are present ', async function() { const expected = { hasAMQP: false }; - const result = GetProtocolFlags(docWithoutChannels); + const doc = await parser.parse(docWithoutProtocols); + const result = GetProtocolFlags(doc); expect(result).toEqual(expected); }) - it('should return amqp as true when channels with amqp bindings are present ', () => { + it('should return amqp as true when channels with amqp bindings are present ', async function() { const expected = { hasAMQP: true }; - const result = GetProtocolFlags(docWithChannels); + const doc = await parser.parse(docWithoutAMQPublisher); + const result = GetProtocolFlags(doc); expect(result).toEqual(expected); }) }) describe('GetSubscriberFlags', () => { - it('should return subscriberFlags as false when no channels are present ', () => { + it('should return subscriberFlags as false when no channels are present ', async function() { const expected = { hasAMQPSub: false }; - const result = GetSubscriberFlags(docWithoutChannels); + const doc = await parser.parse(docWithoutProtocols); + const result = GetSubscriberFlags(doc); expect(result).toEqual(expected); }) - it('should return subscriberFlags as false when publish channels are NOT present', () => { + it('should return subscriberFlags as false when publish channels are NOT present', async function() { const expected = { hasAMQPSub: false }; - - const result = GetSubscriberFlags(docWithChannels); + const doc = await parser.parse(docWithoutAMQPublisher); + const result = GetSubscriberFlags(doc); expect(result).toEqual(expected); }) - it('should return subscriberFlags as true when publish channels are present', () => { + it('should return subscriberFlags as true when publish channels are present', async function() { const expected = { hasAMQPSub: true }; - - const result = GetSubscriberFlags(docWithAMQPPublisher); + const doc = await parser.parse(docWithAMQPublisher); + const result = GetSubscriberFlags(doc); expect(result).toEqual(expected); }) }) diff --git a/test/files/docWithAMQPublisher.yml b/test/files/docWithAMQPublisher.yml new file mode 100644 index 000000000..8c9483694 --- /dev/null +++ b/test/files/docWithAMQPublisher.yml @@ -0,0 +1,60 @@ +asyncapi: '2.1.0' + +info: + title: Streetlights API + version: '1.0.0' + description: | + The Smartylighting Streetlights API allows you + to remotely manage the city lights. + license: + name: Apache 2.0 + url: 'https://www.apache.org/licenses/LICENSE-2.0' + +defaultContentType: application/json + +servers: + local: + url: localhost:5672 + protocol: amqp + security: + - user-password: [] + +channels: + light/measured: + bindings: + amqp: + is: routingKey + queue: + name: light/measured + durable: true + exclusive: true + autoDelete: false + vhost: / + bindingVersion: 0.2.0 + publish: + summary: Inform about environmental lighting conditions for a particular streetlight. + operationId: onLightMeasured + message: + name: LightMeasured + payload: + $id: LightMeasured + additionalProperties: false + type: object + properties: + id: + type: integer + minimum: 0 + description: Id of the streetlight. + lumens: + type: integer + minimum: 0 + description: Light intensity measured in lumens. + sentAt: + type: string + format: date-time + description: Date and time when the message was sent. + +components: + securitySchemes: + user-password: + type: userPassword diff --git a/test/files/docWithoutAMQPublisher.yml b/test/files/docWithoutAMQPublisher.yml new file mode 100644 index 000000000..67abb93a2 --- /dev/null +++ b/test/files/docWithoutAMQPublisher.yml @@ -0,0 +1,38 @@ +asyncapi: '2.1.0' + +info: + title: Streetlights API + version: '1.0.0' + description: | + The Smartylighting Streetlights API allows you + to remotely manage the city lights. + license: + name: Apache 2.0 + url: 'https://www.apache.org/licenses/LICENSE-2.0' + +defaultContentType: application/json + +servers: + local: + url: localhost:5672 + protocol: amqp + security: + - user-password: [] + +channels: + light/measured: + bindings: + amqp: + is: routingKey + queue: + name: light/measured + durable: true + exclusive: true + autoDelete: false + vhost: / + bindingVersion: 0.2.0 + +components: + securitySchemes: + user-password: + type: userPassword diff --git a/test/files/docWithoutProtocols.yml b/test/files/docWithoutProtocols.yml new file mode 100644 index 000000000..c15f38492 --- /dev/null +++ b/test/files/docWithoutProtocols.yml @@ -0,0 +1,50 @@ +asyncapi: '2.1.0' + +info: + title: Streetlights API + version: '1.0.0' + description: | + The Smartylighting Streetlights API allows you + to remotely manage the city lights. + license: + name: Apache 2.0 + url: 'https://www.apache.org/licenses/LICENSE-2.0' + +defaultContentType: application/json + +servers: + local: + url: localhost:5672 + protocol: amqp + security: + - user-password: [] + +channels: + light/measured: + publish: + summary: Inform about environmental lighting conditions for a particular streetlight. + operationId: onLightMeasured + message: + name: LightMeasured + payload: + $id: LightMeasured + additionalProperties: false + type: object + properties: + id: + type: integer + minimum: 0 + description: Id of the streetlight. + lumens: + type: integer + minimum: 0 + description: Light intensity measured in lumens. + sentAt: + type: string + format: date-time + description: Date and time when the message was sent. + +components: + securitySchemes: + user-password: + type: userPassword