Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/models/message.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const MessageTrait = require('./message-trait');
const MessageTraitable = require('./message-traitable');
const Schema = require('./schema');

Expand All @@ -24,6 +25,22 @@ class Message extends MessageTraitable {
return new Schema(this._json.payload);
}

/**
* @returns {MessageTrait[]}
*/
traits() {
const traits = this._json['x-parser-original-traits'] || this._json.traits;
if (!traits) return [];
return traits.map(t => new MessageTrait(t));
}

/**
* @returns {boolean}
*/
hasTraits() {
return !!this._json['x-parser-original-traits'] || !!this._json.traits;
}

/**
* @returns {any}
*/
Expand Down
17 changes: 17 additions & 0 deletions lib/models/operation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const OperationTraitable = require('./operation-traitable');
const Message = require('./message');
const OperationTrait = require('./operation-trait');

/**
* Implements functions to deal with an Operation object.
Expand All @@ -18,6 +19,22 @@ class Operation extends OperationTraitable {
return false;
}

/**
* @returns {OperationTrait[]}
*/
traits() {
const traits = this._json['x-parser-original-traits'] || this._json.traits;
if (!traits) return [];
return traits.map(t => new OperationTrait(t));
}

/**
* @returns {boolean}
*/
hasTraits() {
return !!this._json['x-parser-original-traits'] || !!this._json.traits;
}

/**
* @returns {Message[]}
*/
Expand Down
39 changes: 37 additions & 2 deletions test/models/message_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai');
const js = { schemaFormat: 'mySchema', headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, payload: { test: true }, 'x-parser-original-payload': { testing: true }, correlationId: { test: true }, 'x-parser-original-schema-format': 'application/vnd.apache.avro;version=1.9.0', contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{name: 'test', summary: 'test summary', payload: {test: true}}], 'x-test': 'testing' };
const js = { schemaFormat: 'mySchema', traits: [{headers: {type: 'object',properties: {'my-app-header': {type: 'integer',minimum: 0,maximum: 100}}}}], headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, payload: { test: true }, 'x-parser-original-payload': { testing: true }, correlationId: { test: true }, 'x-parser-original-schema-format': 'application/vnd.apache.avro;version=1.9.0', contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{name: 'test', summary: 'test summary', payload: {test: true}}], 'x-test': 'testing' };

const Message = require('../../lib/models/message');

Expand All @@ -25,7 +25,7 @@ describe('Message', function() {
it('should return a string with the base64 representation of the object when x-parser-message-name extension and name are not available', function() {
const msg = { ...js, ...{ name: undefined } };
const d = new Message(msg);
expect(d.uid()).to.be.equal('eyJzY2hlbWFGb3JtYXQiOiJteVNjaGVtYSIsImhlYWRlcnMiOnsicHJvcGVydGllcyI6eyJ0ZXN0MSI6eyJ0eXBlIjoic3RyaW5nIn0sInRlc3QyIjp7InR5cGUiOiJudW1iZXIifX19LCJwYXlsb2FkIjp7InRlc3QiOnRydWV9LCJ4LXBhcnNlci1vcmlnaW5hbC1wYXlsb2FkIjp7InRlc3RpbmciOnRydWV9LCJjb3JyZWxhdGlvbklkIjp7InRlc3QiOnRydWV9LCJ4LXBhcnNlci1vcmlnaW5hbC1zY2hlbWEtZm9ybWF0IjoiYXBwbGljYXRpb24vdm5kLmFwYWNoZS5hdnJvO3ZlcnNpb249MS45LjAiLCJjb250ZW50VHlwZSI6ImFwcGxpY2F0aW9uL2pzb24iLCJ0aXRsZSI6IlRlc3QiLCJzdW1tYXJ5IjoidGVzdCIsImRlc2NyaXB0aW9uIjoidGVzdGluZyIsImV4dGVybmFsRG9jcyI6eyJ0ZXN0Ijp0cnVlfSwidGFncyI6W3sibmFtZSI6InRhZzEifV0sImJ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CJ4LXRlc3QiOiJ0ZXN0aW5nIn0=');
});
});

Expand Down Expand Up @@ -109,6 +109,41 @@ describe('Message', function() {
});
});

describe('#traits()', function() {
it('should return a list of traits from traits', function() {
const d = new Message(js);
expect(d.traits()[0].json()).to.equal(js.traits[0]);
});

it('should return a list of traits from x-parser-original-traits', function() {
const { traits, ...newJs } = js;
newJs['x-parser-original-traits'] = traits;
const d = new Message(newJs);
expect(d.traits()[0].json()).to.be.equal(newJs['x-parser-original-traits'][0]);
});
});

describe('#hasTraits()', function() {
it('should return true if in traits', function() {
const d = new Message(js);
expect(d.hasTraits()).to.equal(true);
});

it('should return true if in x-parser-original-traits', function() {
const { traits, ...newJs } = js;
newJs['x-parser-original-traits'] = traits;
const d = new Message(newJs);
expect(d.hasTraits()).to.equal(true);
});

it('should return false', function() {
// eslint-disable-next-line no-unused-vars
const { traits, ...newJs } = js;
const d = new Message(newJs);
expect(d.hasTraits()).to.equal(false);
});
});

describe('#examples()', function() {
it('should return an array of examples', function() {
const d = new Message(js);
Expand Down
39 changes: 37 additions & 2 deletions test/models/operation_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai');
const js = { summary: 't', description: 'test', operationId: 'test', tags: [{name: 'tag1'}], externalDocs: { url: 'somewhere' }, bindings: { amqp: { test: true } }, message: { test: true }, 'x-test': 'testing' };
const js = { summary: 't', description: 'test', traits: [{bindings: {kafka: {clientId: 'my-app-id'}}}], operationId: 'test', tags: [{name: 'tag1'}], externalDocs: { url: 'somewhere' }, bindings: { amqp: { test: true } }, message: { test: true }, 'x-test': 'testing' };

const Operation = require('../../lib/models/operation');

Expand All @@ -16,7 +16,42 @@ describe('Operation', function() {
expect(d.summary()).to.be.equal(js.summary);
});
});


describe('#traits()', function() {
it('should return a list of traits', function() {
const d = new Operation(js);
expect(d.traits()[0].json()).to.equal(js.traits[0]);
});

it('should return a list of traits from x-parser-original-traits', function() {
const { traits, ...newJs } = js;
newJs['x-parser-original-traits'] = traits;
const d = new Operation(newJs);
expect(d.traits()[0].json()).to.be.equal(newJs['x-parser-original-traits'][0]);
});
});

describe('#hasTraits()', function() {
it('should return true', function() {
const d = new Operation(js);
expect(d.hasTraits()).to.equal(true);
});

it('should return true if in x-parser-original-traits', function() {
const { traits, ...newJs } = js;
newJs['x-parser-original-traits'] = traits;
const d = new Operation(newJs);
expect(d.hasTraits()).to.equal(true);
});

it('should return false', function() {
// eslint-disable-next-line no-unused-vars
const { traits, ...newJs } = js;
const d = new Operation(newJs);
expect(d.hasTraits()).to.equal(false);
});
});

describe('#id()', function() {
it('should return a string', function() {
const d = new Operation(js);
Expand Down