From acd29e142d41028e99655891cd3ad877e5ecd8a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Tue, 29 Oct 2019 10:58:16 -0300 Subject: [PATCH] Spec v1.0 required and optional attributes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/specs/spec_1.js | 67 ++++++++++++++++++++++++++++++++++++++++++ test/spec_1_tests.js | 69 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 test/spec_1_tests.js diff --git a/lib/specs/spec_1.js b/lib/specs/spec_1.js index 7b6a96e..b3e7ee6 100644 --- a/lib/specs/spec_1.js +++ b/lib/specs/spec_1.js @@ -75,4 +75,71 @@ Spec1.prototype.getId = function() { return this.payload["id"]; }; +Spec1.prototype.source = function(_source){ + this.payload["source"] = _source; + return this; +}; + +Spec1.prototype.getSource = function() { + return this.payload["source"]; +}; + +Spec1.prototype.specversion = function(_specversion){ + // does not set! This is right + return this; +}; + +Spec1.prototype.getSpecversion = function() { + return this.payload["specversion"]; +}; + +Spec1.prototype.type = function(_type){ + this.payload["type"] = _type; + return this; +}; + +Spec1.prototype.getType = function(){ + return this.payload["type"]; +}; + +Spec1.prototype.dataContentType = function(_contenttype){ + this.payload["datacontenttype"] = _contenttype; + return this; +}; +Spec1.prototype.getDataContentType = function() { + return this.payload["datacontenttype"]; +}; + +Spec1.prototype.dataschema = function(_schema){ + this.payload["dataschema"] = _schema; + return this; +}; +Spec1.prototype.getDataschema = function() { + return this.payload["dataschema"]; +}; + +Spec1.prototype.subject = function(_subject){ + this.payload["subject"] = _subject; + return this; +}; +Spec1.prototype.getSubject = function() { + return this.payload["subject"]; +}; + +Spec1.prototype.time = function(_time){ + this.payload["time"] = _time.toISOString(); + return this; +}; +Spec1.prototype.getTime = function() { + return this.payload["time"]; +}; + +Spec1.prototype.data = function(_data){ + this.payload["data"] = _data; + return this; +}; +Spec1.prototype.getData = function() { + return this.payload["data"]; +}; + module.exports = Spec1; diff --git a/test/spec_1_tests.js b/test/spec_1_tests.js new file mode 100644 index 0000000..0e40fa2 --- /dev/null +++ b/test/spec_1_tests.js @@ -0,0 +1,69 @@ +const expect = require("chai").expect; +const Spec1 = require("../lib/specs/spec_1.js"); +const Cloudevent = require("../index.js"); +const uuid = require("uuid/v4"); + +const id = uuid(); +const type = "com.github.pull.create"; +const source = "urn:event:from:myapi/resourse/123"; +const time = new Date(); +const dataschema = "http://example.com/registry/myschema.json"; +const dataContentType = "application/json"; +const data = { + much : "wow" +}; +const extensions = {}; +const subject = "subject-x0"; + +const cloudevent = + new Cloudevent(Spec1) + .id(id) + .source(source) + .type(type) + .dataContentType(dataContentType) + .dataschema(dataschema) + .subject(subject) + .time(time) + .data(data); + +describe("CloudEvents Spec v1.0", () => { + describe("REQUIRED Attributes", () => { + it("Should have 'id'", () => { + expect(cloudevent.getId()).to.equal(id); + }); + + it("Should have 'source'", () => { + expect(cloudevent.getSource()).to.equal(source); + }); + + it("Should have 'specversion'", () => { + expect(cloudevent.getSpecversion()).to.equal("1.0"); + }); + + it("Should have 'type'", () => { + expect(cloudevent.getType()).to.equal(type); + }); + }); + + describe("OPTIONAL Attributes", () => { + it("Should have 'datacontenttype'", () => { + expect(cloudevent.getDataContentType()).to.equal(dataContentType); + }); + + it("Should have 'dataschema'", () => { + expect(cloudevent.getDataschema()).to.equal(dataschema); + }); + + it("Should have 'subject'", () => { + expect(cloudevent.getSubject()).to.equal(subject); + }); + + it("Should have 'time'", () => { + expect(cloudevent.getTime()).to.equal(time.toISOString()); + }); + + it("Should have 'data'", () => { + expect(cloudevent.getData()).to.deep.equal(data); + }); + }); +});