Spec v1.0 required and optional attributes

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-10-29 10:58:16 -03:00
parent 136045ae62
commit acd29e142d
2 changed files with 136 additions and 0 deletions

View File

@ -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;

69
test/spec_1_tests.js Normal file
View File

@ -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);
});
});
});