diff --git a/index.js b/index.js index 37501b1..4d132a4 100644 --- a/index.js +++ b/index.js @@ -172,9 +172,6 @@ if (process.argv[2] && process.argv[2] === '--service') { } module.exports = { - AbstractListenerRSSBuilder: require('./src/Models/Builders/AbstractListenerRSSBuilder'), - YoutubeListenerRSSBuilder: require('./src/Models/Builders/YoutubeListenerRSSBuilder'), ListenerRssInfos: require('./src/Models/ListenerRSSInfos'), - ListenerBuildDirector: require('./src/ListenerDirector'), ListenerRss: require('./src/ListenerRss') } diff --git a/src/ListenerRss.js b/src/ListenerRss.js index 9cf5b98..fb0d9d7 100644 --- a/src/ListenerRss.js +++ b/src/ListenerRss.js @@ -1,22 +1,25 @@ const Parser = require('rss-parser'); +const ListenerInfo = require('./Models/ListenerRSSInfos'); +const DEFAULT_TIMELOOP = 5 * 60; // default timeloop is 5 min class ListenerRss { name = undefined; address = undefined; - timeloop = 5 * 60; // time in seconds + timeloop = DEFAULT_TIMELOOP; // time in seconds customfields = []; // private fields parser = null; - obj = null; loopRunning = false; - constructor(info) { - if(info !== undefined) { - this.setData(info); - this.setParser(); - } + constructor(name, address, timeloop, customfields) { + if(name !== undefined && name instanceof ListenerInfo) { // constructor with 1 arg + this.setData(name); + } 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() { @@ -27,15 +30,15 @@ class ListenerRss { 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) { // 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.timeloop = infos._timeloop; - this.customfields = infos._customfields; + this.timeloop = infos._timeloop === undefined ? DEFAULT_TIMELOOP : infos._timeloop; + this.customfields = infos._customfields === undefined ? [] : infos._customfields; } fetchRSS() { @@ -52,8 +55,8 @@ class ListenerRss { (async () => { while(this.loopRunning === true) { - callback(await this.fetchRSS()); - await new Promise(res => setTimeout(res, this.timeloop * 1000)); + this.fetchRSS().then((obj, err) => callback(obj, err)) + await new Promise(res => setTimeout(res, 2000)); } })(); } diff --git a/src/ListenerDirector.js b/src/old/ListenerDirector.js similarity index 100% rename from src/ListenerDirector.js rename to src/old/ListenerDirector.js diff --git a/src/Models/Builders/AbstractListenerRSSBuilder.js b/src/old/Models/Builders/AbstractListenerRSSBuilder.js similarity index 100% rename from src/Models/Builders/AbstractListenerRSSBuilder.js rename to src/old/Models/Builders/AbstractListenerRSSBuilder.js diff --git a/src/Models/Builders/YoutubeListenerRSSBuilder.js b/src/old/Models/Builders/YoutubeListenerRSSBuilder.js similarity index 100% rename from src/Models/Builders/YoutubeListenerRSSBuilder.js rename to src/old/Models/Builders/YoutubeListenerRSSBuilder.js diff --git a/src/Models/ListenerRSSInfos.js b/src/old/Models/ListenerRSSInfos.js similarity index 100% rename from src/Models/ListenerRSSInfos.js rename to src/old/Models/ListenerRSSInfos.js diff --git a/tests/rss-youtube-spec.js b/tests/rss-youtube-spec.js index 5922f95..9f8f173 100644 --- a/tests/rss-youtube-spec.js +++ b/tests/rss-youtube-spec.js @@ -2,13 +2,12 @@ const Parser = require("rss-parser"); // tested class - -const ListenerRssPackage = require("../index"); +/*const ListenerRssPackage = require("../index"); const Listeners = ListenerRssPackage.ListenerRss -const ListenerRRSInfo = ListenerRssPackage.ListenerRssInfos -const YtbBuilder = ListenerRssPackage.YoutubeListenerRSSBuilder -const Director = ListenerRssPackage.ListenerBuildDirector +const ListenerRRSInfo = ListenerRssPackage.ListenerRssInfos*/ + const Listeners = require('../src/ListenerRss') + const ListenerRRSInfo = require('../src/Models/ListenerRSSInfos') // Unit test const chai = require("chai"); @@ -22,10 +21,10 @@ const expect = chai.expect; describe("test class RSS: jsonfile", function () { let myListener = undefined; - let infosListener = new ListenerRRSInfo(); - infosListener.name = 'my-test-service'; - infosListener.address = 'fake.rss.service'; - infosListener.timeloop = 15; + const infosListener = new ListenerRRSInfo('my-test-service', 'fake.rss.service', 15, [ + ['description', ['media:group', 'media:description']], + ['icon', ['media:group', 'media:thumbnail']] + ]); // parseURL tests let stubParser; @@ -64,9 +63,11 @@ describe("test class RSS: jsonfile", function () { beforeEach(function () { // stubs - stubParser = sinon.stub(Parser.prototype, 'parseURL') - .withArgs(infosListener.address) + stubParser = sinon.stub(Parser.prototype, 'parseURL'); + stubParser.withArgs(infosListener.address) .resolves(mockedRSSOutput); + stubParser.withArgs('bad.rss.service') + .resolves(new Error('connect ECONNREFUSED 127.0.0.1:80')); // constructor myListener = undefined; @@ -78,47 +79,192 @@ describe("test class RSS: jsonfile", function () { }); describe("Building Ytb listener", function () { - it("The build without issues", function () { - let builder = new YtbBuilder(); - let director = new Director(builder); - director.build(infosListener); - myListener = director.getListener(); + it("The build without issues (infosListener parameters)", function () { + myListener = new Listeners(infosListener); // assertions // myListener data - expect(myListener.timeloop).to.eql(infosListener._timeloop); - expect(myListener.name).to.eql(infosListener._name) - expect(myListener.address).to.eql(infosListener._address) + 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 : 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 () { - it("fetch without issues", function () { + it("fetch without issues", function () { // classic build - let builder = new YtbBuilder(); - let director = new Director(builder); - director.build(infosListener); - myListener = director.getListener(); - // fetch - myListener.fetchRSS(); + myListener = new Listeners(infosListener); + // fetch + let res = myListener.fetchRSS(); //assertion - // calls + // calls expect(stubParser).to.have.been.calledOnce; 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 () { - it("Let's start the timer", function () { - myListener.setDatas(infosListener); - myListener.start(); + describe("start", function () { + it("Let's start the timer", async function () { + //custom timeout + 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 }); }); });