Skip to content

Commit ac0a8a3

Browse files
committed
Spec 0.2 impl
Signed-off-by: Fabio José <[email protected]>
1 parent 3f35c4b commit ac0a8a3

File tree

2 files changed

+132
-4
lines changed

2 files changed

+132
-4
lines changed

lib/specs/spec_0_2.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var uuid = require('uuid/v4');
1+
var uuid = require('uuid/v4');
2+
var empty = require('is-empty')
23

34
function Spec_0_2(){
45
this.payload = {
@@ -12,6 +13,22 @@ function Spec_0_2(){
1213
*/
1314
Spec_0_2.prototype.check = function(){
1415

16+
if(empty(this.payload['type'])) {
17+
throw {message: "'type' is invalid"};
18+
}
19+
20+
if(empty(this.payload['specversion'])) {
21+
throw {message: "'specversion' is invalid"};
22+
}
23+
24+
if(this.payload['specversion'] !== '0.2') {
25+
throw {message: "'specversion' value is invalid: '"
26+
+ this.payload['specversion'] + "'"};
27+
}
28+
29+
if(empty(this.payload['id'])) {
30+
throw {message: "'id' is invalid"};
31+
}
1532
}
1633

1734
Spec_0_2.prototype.type = function(_type){
@@ -34,7 +51,25 @@ Spec_0_2.prototype.time = function(_time){
3451
return this;
3552
}
3653

37-
//TODO another attributes . . .
54+
Spec_0_2.prototype.schemaurl = function(_schemaurl){
55+
this.payload['schemaurl'] = _schemaurl;
56+
return this;
57+
}
58+
59+
Spec_0_2.prototype.contenttype = function(_contenttype){
60+
this.payload['contenttype'] = _contenttype;
61+
return this;
62+
}
63+
64+
Spec_0_2.prototype.data = function(_data){
65+
this.payload['data'] = _data;
66+
return this;
67+
}
68+
69+
Spec_0_2.prototype.addExtension = function(key, value){
70+
this.payload[key] = value;
71+
return this;
72+
}
3873

3974
module.exports = Spec_0_2;
4075

test/cloudevent_spec_0_2.js

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
var expect = require("chai").expect;
22
var Cloudevent = require("../index.js");
33

4+
const type = "com.github.pull.create";
5+
const source = "urn:event:from:myapi/resourse/123";
6+
const time = new Date();
7+
const schemaurl = "http://example.com/registry/myschema.json";
8+
const contenttype = "application/json";
9+
const data = {};
10+
const extensions = {};
11+
412
var cloudevent = new Cloudevent(Cloudevent.specs['0.2'])
5-
.type("com.github.pull.create")
6-
.source("urn:event:from:myapi/resourse/123");
13+
.type(type)
14+
.source(source);
715

816
describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
917

@@ -27,6 +35,91 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
2735
});
2836
});
2937

38+
describe("Optional context attributes", () => {
39+
it("contains 'time'", () => {
40+
cloudevent.time(time);
41+
expect(cloudevent.format()).to.have.property('time');
42+
});
43+
44+
it("contains 'schemaurl'", () => {
45+
cloudevent.schemaurl(schemaurl);
46+
expect(cloudevent.format()).to.have.property('schemaurl');
47+
});
48+
49+
it("contains 'contenttype'", () => {
50+
cloudevent.contenttype(contenttype);
51+
expect(cloudevent.format()).to.have.property('contenttype');
52+
});
53+
54+
it("contains 'data'", () => {
55+
cloudevent.data(data);
56+
expect(cloudevent.format()).to.have.property('data');
57+
});
58+
59+
it("contains 'extension1'", () => {
60+
cloudevent.addExtension("extension1", "value1");
61+
expect(cloudevent.format()).to.have.property('extension1');
62+
});
63+
64+
it("'extension2' should have value equals to 'value1'", () => {
65+
cloudevent.addExtension("extension2", "value2");
66+
expect(cloudevent.format()['extension2']).to.equal('value2');
67+
});
68+
});
69+
70+
describe("The Constraints check", () => {
71+
describe("'type'", () => {
72+
it("should throw an error when is an empty string", () => {
73+
cloudevent.type("");
74+
expect(cloudevent.format.bind(cloudevent))
75+
.to
76+
.throw("'type' is invalid");
77+
});
78+
79+
it("must be a non-empty string", () => {
80+
cloudevent.type(type);
81+
cloudevent.format();
82+
});
83+
84+
it("should be prefixed with a reverse-DNS name", () => {
85+
//TODO how to assert it?
86+
});
87+
});
88+
89+
describe("'specversion'", () => {
90+
it("compliant event producers must use a value of '0.2'", () => {
91+
expect(cloudevent.format()['specversion']).to.equal("0.2");
92+
});
93+
94+
it("should throw an error when is an empty string", () => {
95+
cloudevent.spec.payload.specversion = "";
96+
expect(cloudevent.format.bind(cloudevent))
97+
.to
98+
.throw("'specversion' is invalid");
99+
cloudevent.spec.payload.specversion = "0.2";
100+
});
101+
});
102+
103+
describe("'id'", () => {
104+
it("should throw an error when is an empty string", () => {
105+
cloudevent.id("");
106+
expect(cloudevent.format.bind(cloudevent))
107+
.to
108+
.throw("'id' is invalid");
109+
});
110+
it("must be a non-empty string", () => {
111+
cloudevent.id("my.id-0x0090");
112+
cloudevent.format();
113+
});
114+
});
115+
116+
describe("'time'", () => {
117+
it("must adhere to the format specified in RFC 3339", () => {
118+
cloudevent.time(time);
119+
expect(cloudevent.format()['time']).to.equal(time.toISOString());
120+
});
121+
});
122+
});
30123
});
31124

32125
});

0 commit comments

Comments
 (0)