diff --git a/lib/specs/spec_0_2.js b/lib/specs/spec_0_2.js index 94ca6bb..210f600 100644 --- a/lib/specs/spec_0_2.js +++ b/lib/specs/spec_0_2.js @@ -1,4 +1,5 @@ -var uuid = require('uuid/v4'); +var uuid = require('uuid/v4'); +var empty = require('is-empty') function Spec_0_2(){ this.payload = { @@ -12,6 +13,22 @@ function Spec_0_2(){ */ Spec_0_2.prototype.check = function(){ + if(empty(this.payload['type'])) { + throw {message: "'type' is invalid"}; + } + + if(empty(this.payload['specversion'])) { + throw {message: "'specversion' is invalid"}; + } + + if(this.payload['specversion'] !== '0.2') { + throw {message: "'specversion' value is invalid: '" + + this.payload['specversion'] + "'"}; + } + + if(empty(this.payload['id'])) { + throw {message: "'id' is invalid"}; + } } Spec_0_2.prototype.type = function(_type){ @@ -34,7 +51,25 @@ Spec_0_2.prototype.time = function(_time){ return this; } -//TODO another attributes . . . +Spec_0_2.prototype.schemaurl = function(_schemaurl){ + this.payload['schemaurl'] = _schemaurl; + return this; +} + +Spec_0_2.prototype.contenttype = function(_contenttype){ + this.payload['contenttype'] = _contenttype; + return this; +} + +Spec_0_2.prototype.data = function(_data){ + this.payload['data'] = _data; + return this; +} + +Spec_0_2.prototype.addExtension = function(key, value){ + this.payload[key] = value; + return this; +} module.exports = Spec_0_2; diff --git a/test/cloudevent_spec_0_2.js b/test/cloudevent_spec_0_2.js index a94faa1..18c71f1 100644 --- a/test/cloudevent_spec_0_2.js +++ b/test/cloudevent_spec_0_2.js @@ -1,9 +1,17 @@ var expect = require("chai").expect; var Cloudevent = require("../index.js"); +const type = "com.github.pull.create"; +const source = "urn:event:from:myapi/resourse/123"; +const time = new Date(); +const schemaurl = "http://example.com/registry/myschema.json"; +const contenttype = "application/json"; +const data = {}; +const extensions = {}; + var cloudevent = new Cloudevent(Cloudevent.specs['0.2']) - .type("com.github.pull.create") - .source("urn:event:from:myapi/resourse/123"); + .type(type) + .source(source); describe("CloudEvents Spec 0.2 - JavaScript SDK", () => { @@ -27,6 +35,91 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => { }); }); + describe("Optional context attributes", () => { + it("contains 'time'", () => { + cloudevent.time(time); + expect(cloudevent.format()).to.have.property('time'); + }); + + it("contains 'schemaurl'", () => { + cloudevent.schemaurl(schemaurl); + expect(cloudevent.format()).to.have.property('schemaurl'); + }); + + it("contains 'contenttype'", () => { + cloudevent.contenttype(contenttype); + expect(cloudevent.format()).to.have.property('contenttype'); + }); + + it("contains 'data'", () => { + cloudevent.data(data); + expect(cloudevent.format()).to.have.property('data'); + }); + + it("contains 'extension1'", () => { + cloudevent.addExtension("extension1", "value1"); + expect(cloudevent.format()).to.have.property('extension1'); + }); + + it("'extension2' should have value equals to 'value1'", () => { + cloudevent.addExtension("extension2", "value2"); + expect(cloudevent.format()['extension2']).to.equal('value2'); + }); + }); + + describe("The Constraints check", () => { + describe("'type'", () => { + it("should throw an error when is an empty string", () => { + cloudevent.type(""); + expect(cloudevent.format.bind(cloudevent)) + .to + .throw("'type' is invalid"); + }); + + it("must be a non-empty string", () => { + cloudevent.type(type); + cloudevent.format(); + }); + + it("should be prefixed with a reverse-DNS name", () => { + //TODO how to assert it? + }); + }); + + describe("'specversion'", () => { + it("compliant event producers must use a value of '0.2'", () => { + expect(cloudevent.format()['specversion']).to.equal("0.2"); + }); + + it("should throw an error when is an empty string", () => { + cloudevent.spec.payload.specversion = ""; + expect(cloudevent.format.bind(cloudevent)) + .to + .throw("'specversion' is invalid"); + cloudevent.spec.payload.specversion = "0.2"; + }); + }); + + describe("'id'", () => { + it("should throw an error when is an empty string", () => { + cloudevent.id(""); + expect(cloudevent.format.bind(cloudevent)) + .to + .throw("'id' is invalid"); + }); + it("must be a non-empty string", () => { + cloudevent.id("my.id-0x0090"); + cloudevent.format(); + }); + }); + + describe("'time'", () => { + it("must adhere to the format specified in RFC 3339", () => { + cloudevent.time(time); + expect(cloudevent.format()['time']).to.equal(time.toISOString()); + }); + }); + }); }); });