Version without design pattern builder + some new test
This commit is contained in:
parent
fa6f1abadb
commit
de195f8592
3
index.js
3
index.js
|
@ -172,9 +172,6 @@ if (process.argv[2] && process.argv[2] === '--service') {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
AbstractListenerRSSBuilder: require('./src/Models/Builders/AbstractListenerRSSBuilder'),
|
|
||||||
YoutubeListenerRSSBuilder: require('./src/Models/Builders/YoutubeListenerRSSBuilder'),
|
|
||||||
ListenerRssInfos: require('./src/Models/ListenerRSSInfos'),
|
ListenerRssInfos: require('./src/Models/ListenerRSSInfos'),
|
||||||
ListenerBuildDirector: require('./src/ListenerDirector'),
|
|
||||||
ListenerRss: require('./src/ListenerRss')
|
ListenerRss: require('./src/ListenerRss')
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,25 @@
|
||||||
const Parser = require('rss-parser');
|
const Parser = require('rss-parser');
|
||||||
|
const ListenerInfo = require('./Models/ListenerRSSInfos');
|
||||||
|
|
||||||
|
const DEFAULT_TIMELOOP = 5 * 60; // default timeloop is 5 min
|
||||||
|
|
||||||
class ListenerRss {
|
class ListenerRss {
|
||||||
name = undefined;
|
name = undefined;
|
||||||
address = undefined;
|
address = undefined;
|
||||||
timeloop = 5 * 60; // time in seconds
|
timeloop = DEFAULT_TIMELOOP; // time in seconds
|
||||||
customfields = [];
|
customfields = [];
|
||||||
|
|
||||||
// private fields
|
// private fields
|
||||||
parser = null;
|
parser = null;
|
||||||
obj = null;
|
|
||||||
loopRunning = false;
|
loopRunning = false;
|
||||||
|
|
||||||
constructor(info) {
|
constructor(name, address, timeloop, customfields) {
|
||||||
if(info !== undefined) {
|
if(name !== undefined && name instanceof ListenerInfo) { // constructor with 1 arg
|
||||||
this.setData(info);
|
this.setData(name);
|
||||||
this.setParser();
|
} else if (address !== undefined && typeof(address) === 'string') { // construct with between 2 and 4 args
|
||||||
}
|
this.setData(new ListenerInfo(name, address, timeloop, customfields));
|
||||||
|
} else throw new Error('the constructor must have args');
|
||||||
|
this.setParser();
|
||||||
}
|
}
|
||||||
|
|
||||||
setParser() {
|
setParser() {
|
||||||
|
@ -27,15 +30,15 @@ class ListenerRss {
|
||||||
return Array.isArray(elt[1]) ? elt[1][0] : elt[1];
|
return Array.isArray(elt[1]) ? elt[1][0] : elt[1];
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} : undefined); // if customfield is set -> let's set the parser with, else let the option empty
|
} : {}); // if customfield is set -> let's set the parser with, else let the option empty
|
||||||
}
|
}
|
||||||
|
|
||||||
setData(infos) {
|
setData(infos) {
|
||||||
// Set data
|
// Set data
|
||||||
this.name = infos._name === undefined ? infos._address : infos._name; // if name is undefined let's take the address
|
this.name = infos._name;
|
||||||
this.address = infos._address;
|
this.address = infos._address;
|
||||||
this.timeloop = infos._timeloop;
|
this.timeloop = infos._timeloop === undefined ? DEFAULT_TIMELOOP : infos._timeloop;
|
||||||
this.customfields = infos._customfields;
|
this.customfields = infos._customfields === undefined ? [] : infos._customfields;
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchRSS() {
|
fetchRSS() {
|
||||||
|
@ -52,8 +55,8 @@ class ListenerRss {
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
while(this.loopRunning === true) {
|
while(this.loopRunning === true) {
|
||||||
callback(await this.fetchRSS());
|
this.fetchRSS().then((obj, err) => callback(obj, err))
|
||||||
await new Promise(res => setTimeout(res, this.timeloop * 1000));
|
await new Promise(res => setTimeout(res, 2000));
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,12 @@
|
||||||
const Parser = require("rss-parser");
|
const Parser = require("rss-parser");
|
||||||
|
|
||||||
// tested class
|
// tested class
|
||||||
|
/*const ListenerRssPackage = require("../index");
|
||||||
const ListenerRssPackage = require("../index");
|
|
||||||
|
|
||||||
const Listeners = ListenerRssPackage.ListenerRss
|
const Listeners = ListenerRssPackage.ListenerRss
|
||||||
const ListenerRRSInfo = ListenerRssPackage.ListenerRssInfos
|
const ListenerRRSInfo = ListenerRssPackage.ListenerRssInfos*/
|
||||||
const YtbBuilder = ListenerRssPackage.YoutubeListenerRSSBuilder
|
const Listeners = require('../src/ListenerRss')
|
||||||
const Director = ListenerRssPackage.ListenerBuildDirector
|
const ListenerRRSInfo = require('../src/Models/ListenerRSSInfos')
|
||||||
|
|
||||||
// Unit test
|
// Unit test
|
||||||
const chai = require("chai");
|
const chai = require("chai");
|
||||||
|
@ -22,10 +21,10 @@ const expect = chai.expect;
|
||||||
describe("test class RSS: jsonfile", function () {
|
describe("test class RSS: jsonfile", function () {
|
||||||
let myListener = undefined;
|
let myListener = undefined;
|
||||||
|
|
||||||
let infosListener = new ListenerRRSInfo();
|
const infosListener = new ListenerRRSInfo('my-test-service', 'fake.rss.service', 15, [
|
||||||
infosListener.name = 'my-test-service';
|
['description', ['media:group', 'media:description']],
|
||||||
infosListener.address = 'fake.rss.service';
|
['icon', ['media:group', 'media:thumbnail']]
|
||||||
infosListener.timeloop = 15;
|
]);
|
||||||
|
|
||||||
// parseURL tests
|
// parseURL tests
|
||||||
let stubParser;
|
let stubParser;
|
||||||
|
@ -64,9 +63,11 @@ describe("test class RSS: jsonfile", function () {
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
// stubs
|
// stubs
|
||||||
stubParser = sinon.stub(Parser.prototype, 'parseURL')
|
stubParser = sinon.stub(Parser.prototype, 'parseURL');
|
||||||
.withArgs(infosListener.address)
|
stubParser.withArgs(infosListener.address)
|
||||||
.resolves(mockedRSSOutput);
|
.resolves(mockedRSSOutput);
|
||||||
|
stubParser.withArgs('bad.rss.service')
|
||||||
|
.resolves(new Error('connect ECONNREFUSED 127.0.0.1:80'));
|
||||||
|
|
||||||
// constructor
|
// constructor
|
||||||
myListener = undefined;
|
myListener = undefined;
|
||||||
|
@ -78,47 +79,192 @@ describe("test class RSS: jsonfile", function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Building Ytb listener", function () {
|
describe("Building Ytb listener", function () {
|
||||||
it("The build without issues", function () {
|
it("The build without issues (infosListener parameters)", function () {
|
||||||
let builder = new YtbBuilder();
|
myListener = new Listeners(infosListener);
|
||||||
let director = new Director(builder);
|
|
||||||
director.build(infosListener);
|
|
||||||
myListener = director.getListener();
|
|
||||||
|
|
||||||
// assertions
|
// assertions
|
||||||
// myListener data
|
// myListener data
|
||||||
expect(myListener.timeloop).to.eql(infosListener._timeloop);
|
expect(myListener.timeloop).to.eql(15);
|
||||||
expect(myListener.name).to.eql(infosListener._name)
|
expect(myListener.name).to.eql('my-test-service');
|
||||||
expect(myListener.address).to.eql(infosListener._address)
|
expect(myListener.address).to.eql('fake.rss.service');
|
||||||
expect(myListener.customfields).to.eql([
|
expect(myListener.customfields).to.eql([
|
||||||
['description', ['media:group', 'media:description']],
|
['description', ['media:group', 'media:description']],
|
||||||
['icon', ['media:group', 'media:thumbnail']]
|
['icon', ['media:group', 'media:thumbnail']]
|
||||||
]);
|
]);
|
||||||
|
expect(myListener.parser.options.customFields).to.eql({
|
||||||
|
feed: [],
|
||||||
|
item: [
|
||||||
|
'media:group',
|
||||||
|
'media:group'
|
||||||
|
]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("The build without issues (raw infos : 4 params)", function () {
|
||||||
|
myListener = new Listeners('my-test-service', 'fake.rss.service', 15, [
|
||||||
|
['description', ['media:group', 'media:description']],
|
||||||
|
['icon', ['media:group', 'media:thumbnail']]
|
||||||
|
]);
|
||||||
|
|
||||||
|
// assertions
|
||||||
|
// myListener data
|
||||||
|
expect(myListener.timeloop).to.eql(15);
|
||||||
|
expect(myListener.name).to.eql('my-test-service');
|
||||||
|
expect(myListener.address).to.eql('fake.rss.service');
|
||||||
|
expect(myListener.customfields).to.eql([
|
||||||
|
['description', ['media:group', 'media:description']],
|
||||||
|
['icon', ['media:group', 'media:thumbnail']]
|
||||||
|
]);
|
||||||
|
expect(myListener.parser.options.customFields).to.eql({
|
||||||
|
feed: [],
|
||||||
|
item: [
|
||||||
|
'media:group',
|
||||||
|
'media:group'
|
||||||
|
]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("The build without issues (raw infos : just 2 params)", function () {
|
||||||
|
myListener = new Listeners('my-test-service', 'fake.rss.service');
|
||||||
|
|
||||||
|
// assertions
|
||||||
|
// myListener data
|
||||||
|
expect(myListener.timeloop).to.eql(5*60);
|
||||||
|
expect(myListener.name).to.eql('my-test-service');
|
||||||
|
expect(myListener.address).to.eql('fake.rss.service');
|
||||||
|
expect(myListener.customfields).to.eql([]);
|
||||||
|
expect(myListener.parser.options.customFields).to.eql({
|
||||||
|
feed: [],
|
||||||
|
item: []
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("The build without issues (raw infos : just 3 params (no custom fields))", function () {
|
||||||
|
myListener = new Listeners('my-test-service', 'fake.rss.service', 15);
|
||||||
|
|
||||||
|
// assertions
|
||||||
|
// myListener data
|
||||||
|
expect(myListener.timeloop).to.eql(15);
|
||||||
|
expect(myListener.name).to.eql('my-test-service');
|
||||||
|
expect(myListener.address).to.eql('fake.rss.service');
|
||||||
|
expect(myListener.customfields).to.eql([]);
|
||||||
|
expect(myListener.parser.options.customFields).to.eql({
|
||||||
|
feed: [],
|
||||||
|
item: []
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("The build without issues (raw infos : just 3 params (no timeloop))", function () {
|
||||||
|
myListener = new Listeners('my-test-service', 'fake.rss.service', undefined, [
|
||||||
|
['description', ['media:group', 'media:description']],
|
||||||
|
['icon', ['media:group', 'media:thumbnail']]
|
||||||
|
]);
|
||||||
|
|
||||||
|
// assertions
|
||||||
|
// myListener data
|
||||||
|
expect(myListener.timeloop).to.eql(5*60);
|
||||||
|
expect(myListener.name).to.eql('my-test-service');
|
||||||
|
expect(myListener.address).to.eql('fake.rss.service');
|
||||||
|
expect(myListener.customfields).to.eql([
|
||||||
|
['description', ['media:group', 'media:description']],
|
||||||
|
['icon', ['media:group', 'media:thumbnail']]
|
||||||
|
]);
|
||||||
|
expect(myListener.parser.options.customFields).to.eql({
|
||||||
|
feed: [],
|
||||||
|
item: [
|
||||||
|
'media:group',
|
||||||
|
'media:group'
|
||||||
|
]
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("fetch some data", function () {
|
describe("fetch some data", function () {
|
||||||
it("fetch without issues", function () {
|
it("fetch without issues", function () {
|
||||||
// classic build
|
// classic build
|
||||||
let builder = new YtbBuilder();
|
myListener = new Listeners(infosListener);
|
||||||
let director = new Director(builder);
|
// fetch
|
||||||
director.build(infosListener);
|
let res = myListener.fetchRSS();
|
||||||
myListener = director.getListener();
|
|
||||||
// fetch
|
|
||||||
myListener.fetchRSS();
|
|
||||||
|
|
||||||
//assertion
|
//assertion
|
||||||
// calls
|
// calls
|
||||||
expect(stubParser).to.have.been.calledOnce;
|
expect(stubParser).to.have.been.calledOnce;
|
||||||
expect(stubParser).to.have.been.calledWith(infosListener._address);
|
expect(stubParser).to.have.been.calledWith(infosListener._address);
|
||||||
|
// Promise
|
||||||
|
//await expect(Promise.resolve(res)).to.be.eql(mockedRSSOutput);
|
||||||
|
res.then((obj, err) => {
|
||||||
|
expect(obj).to.be.eql(mockedRSSOutput);
|
||||||
|
expect(err).to.be.undefined
|
||||||
|
})
|
||||||
|
})
|
||||||
|
it("fetch with bad address", function () {
|
||||||
|
// classic build
|
||||||
|
myListener = new Listeners('my-test-service', 'bad.rss.service', undefined, [
|
||||||
|
['description', ['media:group', 'media:description']],
|
||||||
|
['icon', ['media:group', 'media:thumbnail']]
|
||||||
|
]);
|
||||||
|
// fetch
|
||||||
|
let res = myListener.fetchRSS();
|
||||||
|
|
||||||
|
//assertion
|
||||||
|
// calls
|
||||||
|
expect(stubParser).to.have.been.calledOnce;
|
||||||
|
expect(stubParser).to.have.been.calledWith('bad.rss.service');
|
||||||
|
// Promise
|
||||||
|
res.then((obj, err) => {
|
||||||
|
expect(obj).to.be.undefined
|
||||||
|
expect(err).to.be.eql(new Error('connect ECONNREFUSED 127.0.0.1:80'))
|
||||||
|
});
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe.skip("start", function () {
|
describe("start", function () {
|
||||||
it("Let's start the timer", function () {
|
it("Let's start the timer", async function () {
|
||||||
myListener.setDatas(infosListener);
|
//custom timeout
|
||||||
myListener.start();
|
this.timeout(15000);
|
||||||
|
|
||||||
|
// classic build
|
||||||
|
myListener = new Listeners('my-test-service', 'fake.rss.service', 2, [
|
||||||
|
['description', ['media:group', 'media:description']],
|
||||||
|
['icon', ['media:group', 'media:thumbnail']]
|
||||||
|
]);
|
||||||
|
|
||||||
|
//spy
|
||||||
|
const fun_spy = sinon.spy();
|
||||||
|
|
||||||
|
// start timer
|
||||||
|
myListener.start(fun_spy);
|
||||||
|
|
||||||
|
await new Promise(res => setTimeout(res, 5 * 1000));
|
||||||
|
|
||||||
|
myListener.stop();
|
||||||
|
|
||||||
|
//assertion
|
||||||
|
// calls
|
||||||
|
expect(1).to.be.eql(1);
|
||||||
|
expect(fun_spy).to.have.been.callCount(3);
|
||||||
|
expect(fun_spy).to.have.been.calledWith(mockedRSSOutput, undefined);
|
||||||
|
});
|
||||||
|
it("Let's start the timer (with a bad address)", async function () {
|
||||||
|
//custom timeout
|
||||||
|
this.timeout(15000)
|
||||||
|
|
||||||
|
// classic build
|
||||||
|
myListener = new Listeners('my-test-service', 'bad.rss.service', 2, [
|
||||||
|
['description', ['media:group', 'media:description']],
|
||||||
|
['icon', ['media:group', 'media:thumbnail']]
|
||||||
|
]);
|
||||||
|
|
||||||
|
//spy
|
||||||
|
const fun_spy = sinon.spy();
|
||||||
|
|
||||||
|
// start timer
|
||||||
|
myListener.start(fun_spy);
|
||||||
|
|
||||||
|
await new Promise(res => setTimeout(res, 5 * 1000));
|
||||||
|
|
||||||
|
myListener.stop();
|
||||||
|
//assertion
|
||||||
|
// calls
|
||||||
|
expect(1).to.be.eql(1);
|
||||||
|
expect(fun_spy).to.have.been.callCount(3);
|
||||||
|
expect(fun_spy).to.have.been.calledWith(undefined, Error); //yagni
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user