diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..f46b547 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,53 @@ +{ + "bitwise": true, + "camelcase": true, + "curly": false, + "eqeqeq": true, + "es3": false, + "forin": true, + "immed": true, + "indent": 2, + "latedef": "nofunc", + "newcap": true, + "noarg": true, + "noempty": true, + "nonew": false, + "plusplus": false, + "quotmark": true, + "regexp": false, + "undef": true, + "unused": true, + "strict": false, + "trailing": true, + "maxparams": 4, + "maxdepth": 2, + "maxstatements": 30, + "maxcomplexity": 10, + "maxlen": 110, + + "asi": false, + "boss": false, + "debug": false, + "eqnull": true, + "esnext": true, + "evil": false, + "expr": false, + "funcscope": false, + "globalstrict": false, + "iterator": false, + "lastsemic": false, + "laxbreak": false, + "laxcomma": false, + "loopfunc": false, + "moz": false, + "multistr": true, + "proto": false, + "scripturl": false, + "smarttabs": false, + "shadow": false, + "sub": false, + "supernew": false, + "validthis": true, + + "node": true +} diff --git a/.travis.yml b/.travis.yml index 87f8cd9..0de7c90 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: node_js node_js: - - "0.10" \ No newline at end of file + - 6.9 diff --git a/lib/client.js b/lib/client.js index b174763..ba17c60 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1,10 +1,10 @@ -var request = require('request'), - _ = require('lodash'), - _s = require('underscore.string'), - urlParser = require('url'); +const request = require('request'); +const _ = require('lodash'); +const _s = require('underscore.string'); +const urlParser = require('url'); // Avalable APIS -var apis = ['search', 'moreLikeThis', 'indexes', 'fields', 'documents', 'templates']; +const apis = ['search', 'moreLikeThis', 'indexes', 'fields', 'documents', 'templates', 'replication']; // Build API function buildApi(name) { @@ -30,21 +30,23 @@ function Client(options) { // Make an http request to defined endpoint Client.prototype.request = function (options, callback) { - var url = urlParser.format(_(this.options) + const url = urlParser.format(_(this.options) .pick(['hostname', 'port', 'protocol', 'query']) .defaults({ protocol: 'http', - pathname: options.pathname + pathname: options.pathname, + search: options.search, + body: options.body }).value()); request(_(options) .defaults({ - url: url, + url, json: true }) - .value(), function (err, res, body) { + .value(), (err, res, body) => { if (err) { - var message = err.message; + let message = err.message; if (err.code === 'ECONNREFUSED') { message = 'Cannot connect to OpenSearchServer at ' + url; @@ -62,5 +64,4 @@ Client.prototype.request = function (options, callback) { }); }; - -module.exports = Client; \ No newline at end of file +module.exports = Client; diff --git a/lib/documents.js b/lib/documents.js index 1169127..b186626 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -1,6 +1,6 @@ -var _ = require('lodash'); +const _ = require('lodash'); -var documents = module.exports = { +const documents = module.exports = { documents: {} }; @@ -31,4 +31,4 @@ var documents = module.exports = { }, callback); }; -}).call(documents.documents); \ No newline at end of file +}).call(documents.documents); diff --git a/lib/fields.js b/lib/fields.js index 0864f75..4f4f9aa 100644 --- a/lib/fields.js +++ b/lib/fields.js @@ -1,8 +1,6 @@ -var util = require('./util'); +const util = require('./util'); -var fields = module.exports = { - fields: {} -}; +const fields = module.exports = { fields: {} }; (function () { @@ -10,13 +8,13 @@ var fields = module.exports = { this.createOrUpdate = function (index, options, callback) { options = options || {}; - ['indexed', 'stored', 'termVector'].forEach(function (param) { + ['indexed', 'stored', 'termVector'].forEach((param) => { if (typeof options[param] !== 'undefined') options[param] = util.convertParam(options[param]); }); this.request({ - pathname: '/services/rest/index/' + index + '/field/' + options.name, + pathname: `/services/rest/index/${index}/field/${options.name}`, method: 'PUT', json: options }, callback); @@ -28,7 +26,7 @@ var fields = module.exports = { // Remove an existing field this.destroy = function (index, field, callback) { this.request({ - pathname: '/services/rest/index/' + index + '/field/' + field, + pathname: `/services/rest/index/${index}/field/${field}`, method: 'DELETE' }, callback); }; @@ -36,7 +34,7 @@ var fields = module.exports = { // Set the unique and the default fields this.setUniqueDefault = function (index, options, callback) { this.request({ - pathname: '/services/rest/index/' + index + '/field', + pathname: `/services/rest/index/${index}/field`, method: 'POST', qs: options }, callback); @@ -45,9 +43,9 @@ var fields = module.exports = { // list fields this.list = function (index, callback) { this.request({ - pathname: '/services/rest/index/' + index + '/field/', + pathname: `/services/rest/index/${index}/field/`, method: 'GET' }, callback); }; -}).call(fields.fields); \ No newline at end of file +}).call(fields.fields); diff --git a/lib/indexes.js b/lib/indexes.js index 9c891e2..c6af927 100644 --- a/lib/indexes.js +++ b/lib/indexes.js @@ -1,8 +1,6 @@ -var _ = require('lodash'); +const _ = require('lodash'); -var indexes = module.exports = { - indexes: {} -}; +const indexes = module.exports = { indexes: {} }; (function () { @@ -17,8 +15,11 @@ var indexes = module.exports = { template: '' }); + const { template } = options; + const templateQueryString = template ? '/template/' + template : ''; + this.request({ - pathname: '/services/rest/index/' + index + (options.template ? '/template/' + options.template : ''), + pathname: `/services/rest/index/${index}${templateQueryString}`, method: 'POST' }, callback); }; @@ -26,7 +27,7 @@ var indexes = module.exports = { // Test if index exists this.exists = function (index, callback) { this.request({ - pathname: '/services/rest/index/' + index, + pathname: `/services/rest/index/${index}`, method: 'GET' }, callback); }; @@ -34,9 +35,9 @@ var indexes = module.exports = { // Remove an existing index this.destroy = function (index, callback) { this.request({ - pathname: '/services/rest/index/' + index, + pathname: `/services/rest/index/${index}`, method: 'DELETE' }, callback); }; -}).call(indexes.indexes); \ No newline at end of file +}).call(indexes.indexes); diff --git a/lib/more-like-this.js b/lib/more-like-this.js index 5280bd1..5acc0fa 100644 --- a/lib/more-like-this.js +++ b/lib/more-like-this.js @@ -1,13 +1,13 @@ -var _ = require('lodash'); +const _ = require('lodash'); -var moreLikeThis = module.exports = {}; +const moreLikeThis = module.exports = {}; (function () { // More like this this.moreLikeThis = function (index, options, callback) { this.request({ - pathname: '/services/rest/index/' + index + '/morelikethis', + pathname: `/services/rest/index/${index}/morelikethis`, method: 'POST', json: _.defaults({}, options || {}, { analyzerName: 'StandardAnalyzer', @@ -25,4 +25,4 @@ var moreLikeThis = module.exports = {}; }, callback); }; -}).call(moreLikeThis); \ No newline at end of file +}).call(moreLikeThis); diff --git a/lib/oss.js b/lib/oss.js index 858d00c..1173618 100644 --- a/lib/oss.js +++ b/lib/oss.js @@ -1,5 +1,5 @@ -var Client = require('./client'); -var util = require('./util'); +const Client = require('./client'); +const util = require('./util'); // Create a new client function createClient(options) { @@ -8,4 +8,4 @@ function createClient(options) { exports.Client = Client; exports.createClient = createClient; -exports.util = util; \ No newline at end of file +exports.util = util; diff --git a/lib/replication.js b/lib/replication.js new file mode 100644 index 0000000..4817ca6 --- /dev/null +++ b/lib/replication.js @@ -0,0 +1,33 @@ +const _ = require('lodash'); + +const replication = module.exports = {}; + +(function () { + + this.createReplicationIndex = function(index, searcher, callback) { + this.request({ + pathname: `/services/rest/index/${index}/replication`, + method: 'PUT', + body: { + replicationType: 'MAIN_INDEX', + remoteUrl: buildUrl(searcher), + remoteIndexName: index, + secTimeOut: 120 + } + }, callback); + }; + + this.replicate = function(index, searcher, callback) { + const searcherUrl = buildUrl(searcher); + this.request({ + pathname: `/services/rest/index/${index}/replication/run`, + method: 'PUT', + search: `name=${searcherUrl}/${index}` + }, callback); + }; + + function buildUrl({ protocol = 'http', hostname, port }) { + return `${protocol}://${hostname}:${port}`; + } + +}).call(replication); diff --git a/lib/search.js b/lib/search.js index ed02297..3a611ea 100644 --- a/lib/search.js +++ b/lib/search.js @@ -1,6 +1,6 @@ -var _ = require('lodash'); +const _ = require('lodash'); -var search = module.exports = {}; +const search = module.exports = {}; (function () { @@ -11,10 +11,10 @@ var search = module.exports = {}; }); this.request({ - pathname: '/services/rest/index/' + index + '/search/' + options.type, + pathname: `/services/rest/index/${index}/search/${options.type}`, method: 'POST', json: _.omit(options, 'type') }, callback); }; -}).call(search); \ No newline at end of file +}).call(search); diff --git a/lib/templates.js b/lib/templates.js index 9061bee..8227949 100644 --- a/lib/templates.js +++ b/lib/templates.js @@ -1,15 +1,13 @@ -var _ = require('lodash'); +const _ = require('lodash'); -var templates = module.exports = { - templates: {} -}; +const templates = module.exports = { templates: {} }; (function () { // Get a search template. this.get = function (index, name, callback) { this.request({ - pathname: '/services/rest/index/' + index + '/search/template/' + name, + pathname: `/services/rest/index/${index}/search/template/${name}`, method: 'GET' }, callback); }; @@ -17,7 +15,7 @@ var templates = module.exports = { // List all search templates. this.list = function (index, callback) { this.request({ - pathname: '/services/rest/index/' + index + '/search/template/', + pathname: `/services/rest/index/${index}/search/template/`, method: 'GET' }, callback); }; @@ -29,7 +27,7 @@ var templates = module.exports = { }); this.request({ - pathname: '/services/rest/index/' + index + '/search/' + options.type + '/' + name, + pathname: `/services/rest/index/${index}/search/${options.type}/${name}`, method: 'PUT', json: _.omit(options, 'type') }, callback); @@ -41,9 +39,9 @@ var templates = module.exports = { // Remove an existing template. this.destroy = function (index, name, callback) { this.request({ - pathname: '/services/rest/index/' + index + '/search/template/' + name, + pathname: `/services/rest/index/${index}/search/template/${name}`, method: 'DELETE' }, callback); }; -}).call(templates.templates); \ No newline at end of file +}).call(templates.templates); diff --git a/lib/util.js b/lib/util.js index b48fb64..dc62299 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,4 +1,4 @@ -var _ = require('lodash'); +const _ = require('lodash'); function convertParam(val) { return val === true ? 'YES' : @@ -7,4 +7,4 @@ function convertParam(val) { val; } -exports.convertParam = convertParam; \ No newline at end of file +exports.convertParam = convertParam; diff --git a/package.json b/package.json index acec9a7..a9e55d0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-oss-client", - "version": "1.0.0", + "version": "2.0.0", "description": "Node.js client for Open Search Server.", "main": "index.js", "scripts": { diff --git a/test/.jshintrc b/test/.jshintrc new file mode 100644 index 0000000..6519203 --- /dev/null +++ b/test/.jshintrc @@ -0,0 +1,78 @@ +{ + "bitwise": true, + "camelcase": false, + "curly": false, + "eqeqeq": true, + "es3": false, + "forin": true, + "immed": true, + "indent": 2, + "latedef": "nofunc", + "newcap": true, + "noarg": true, + "noempty": true, + "nonew": true, + "plusplus": false, + "quotmark": true, + "regexp": false, + "undef": true, + "unused": true, + "strict": false, + "trailing": true, + "maxparams": false, + "maxdepth": 2, + "maxstatements": false, + "maxcomplexity": 10, + "maxlen": 110, + "globals": { + "describe": false, + "it": false, + "after": false, + "afterEach": false, + "before": false, + "beforeEach": false, + "context": false, + "define": false, + "context": false, + "xit": false, + "xdescribe": false, + "window": false, + "document": false, + "inject": false, + "mocha": false, + "sinon": true, + "expect": true, + "chai": true, + "e2e": true, + "cms": true, + "localStorage": false, + "by": true, + "element": true, + "browser": true, + }, + "asi": false, + "boss": false, + "debug": false, + "eqnull": true, + "esnext": true, + "evil": false, + "expr": true, + "funcscope": false, + "globalstrict": false, + "iterator": false, + "lastsemic": false, + "laxbreak": false, + "laxcomma": false, + "loopfunc": false, + "moz": false, + "multistr": true, + "proto": false, + "scripturl": false, + "smarttabs": false, + "shadow": false, + "sub": false, + "supernew": false, + "validthis": false, + "node": true, + "-W024": false +} diff --git a/test/bootstrap.js b/test/bootstrap.js new file mode 100644 index 0000000..f149d09 --- /dev/null +++ b/test/bootstrap.js @@ -0,0 +1,8 @@ +// chai +chai = require('chai'); +expect = chai.expect; +chai.config.includeStack = true; + +// sinon +sinon = require('sinon'); +chai.use(require('sinon-chai')); diff --git a/test/client.js b/test/client.js index 76db8c6..ab6fd00 100644 --- a/test/client.js +++ b/test/client.js @@ -1,31 +1,28 @@ /* globals describe, it, beforeEach */ -var expect = require('chai').expect, - nock = require('nock'), - Client = require('../lib/client'); +const nock = require('nock'); +const Client = require('../lib/client'); - nock.enableNetConnect(); +nock.enableNetConnect(); -describe('Client', function () { +describe('Client', () => { - it('should have default options', function () { - var client = new Client(); + it('should have default options', () => { + const client = new Client(); expect(client.options).to.have.property('hostname'); expect(client.options).to.have.property('port'); }); - describe('#request', function () { - describe('without credentials', function () { - var client; + describe('#request', () => { + describe('without credentials', () => { + let client; - beforeEach(function () { + beforeEach(() => { client = new Client(); nock('http://localhost:9090') .post('/my-path') - .reply(200, { - foo: 'bar' - }); + .reply(200, { foo: 'bar' }); nock('http://localhost:9090') .post('/my-error-path') @@ -37,28 +34,19 @@ describe('Client', function () { }); - it('should be possible to make a request', function (done) { + it('should be possible to make a request', done => { - client.request({ - pathname: '/my-path', - method: 'POST' - }, function (err, res) { + client.request({ pathname: '/my-path', method: 'POST' }, (err, res) => { if (err) return done(err); - expect(res).to.deep.equal({ - foo: 'bar' - }); - + expect(res).to.deep.equal({ foo: 'bar' }); done(); }); }); - it('should handle error correctly', function (done) { + it('should handle error correctly', done => { - client.request({ - pathname: '/my-error-path', - method: 'POST' - }, function (err, res) { + client.request({ pathname: '/my-error-path', method: 'POST' }, (err, res) => { expect(err instanceof Error).to.be.true; expect(err.message).to.be.equal('My error'); expect(res).to.be.undefined; @@ -67,12 +55,9 @@ describe('Client', function () { }); }); - it('should handle OSS internal error correctly', function (done) { + it('should handle OSS internal error correctly', done => { - client.request({ - pathname: '/my-internal-error-path', - method: 'POST' - }, function (err, res) { + client.request({ pathname: '/my-internal-error-path', method: 'POST' }, (err, res) => { expect(err instanceof Error).to.be.true; expect(err.message).to.be.equal(''); expect(res).to.be.undefined; @@ -81,21 +66,15 @@ describe('Client', function () { }); }); - describe('when the server is not reachable', function() { + describe('when the server is not reachable', () => { - beforeEach(function() { - client = new Client({ - hostname: 'localhost', - port: 99999999 - }); + beforeEach(() => { + client = new Client({ hostname: 'localhost', port: 8080 }); }); - it('gives a usefull error message', function(done) { - client.request({ - pathname: '/my-path', - method: 'POST' - }, function(err) { - var msg = 'Cannot connect to OpenSearchServer at http://localhost:99999999/my-path'; + it('gives a usefull error message', done => { + client.request({ pathname: '/my-path', method: 'POST' }, err => { + const msg = 'Cannot connect to OpenSearchServer at http://localhost:8080/my-path'; expect(err.message).to.equals(msg); done(); }); @@ -103,79 +82,50 @@ describe('Client', function () { }); }); - describe('with credentials', function () { - var client; + describe('with credentials', () => { + let client; - beforeEach(function () { - client = new Client({ - login: 'mylogin', - key: 'mykey' - }); + beforeEach(() => { + client = new Client({ login: 'mylogin', key: 'mykey' }); nock('http://localhost:9090') .post('/my-path?login=mylogin&key=mykey') - .reply(200, { - foo: 'bar' - }); + .reply(200, { foo: 'bar' }); }); - it('should be possible to make a request', function (done) { - client.request({ - pathname: '/my-path', - method: 'POST' - }, function (err, res) { + it('should be possible to make a request', done => { + client.request({ pathname: '/my-path', method: 'POST' }, (err, res) => { if (err) return done(err); - - expect(res).to.deep.equal({ - foo: 'bar' - }); - + expect(res).to.deep.equal({ foo: 'bar' }); done(); }); }); }); }); - describe('instances', function () { + describe('instances', () => { - beforeEach(function () { + beforeEach(() => { nock('http://host1:9090') .get('/x') - .reply(200, { - host: 1 - }); + .reply(200, { host: 1 }); nock('http://host2:9090') .get('/x') - .reply(200, { - host: 2 - }); + .reply(200, { host: 2 }); }); - it('must be independent', function () { - var client1 = new Client({ - hostname: 'host1' - }); - - var client2 = new Client({ - hostname: 'host2' - }); + it('must be independent', () => { + const client1 = new Client({ hostname: 'host1' }); + const client2 = new Client({ hostname: 'host2' }); - client1.indexes.request({ - pathname: 'x' - }, function (err, res) { - expect(res).to.deep.equal({ - host: 1 - }); + client1.indexes.request({ pathname: 'x' }, (err, res) => { + expect(res).to.deep.equal({ host: 1 }); }); - client2.indexes.request({ - pathname: 'x' - }, function (err, res) { - expect(res).to.deep.equal({ - host: 2 - }); + client2.indexes.request({ pathname: 'x' }, function (err, res) { + expect(res).to.deep.equal({ host: 2 }); }); }); }); -}); \ No newline at end of file +}); diff --git a/test/documents.js b/test/documents.js index 87448b5..d80ca24 100644 --- a/test/documents.js +++ b/test/documents.js @@ -1,27 +1,24 @@ /* globals describe, it, beforeEach */ -var chai = require('chai').use(require('sinon-chai')), - expect = chai.expect, - sinon = require('sinon'), - oss = require('../'); +const oss = require('../'); -describe('Documents', function () { +describe('Documents', () => { - var client, rq; + let client, rq; - beforeEach(function () { + beforeEach(() => { client = oss.createClient(); rq = sinon.stub(client.documents, 'request'); }); - describe('#createOrUpdate', function () { + describe('#createOrUpdate', () => { - it('should be possible to create a single document', function () { + it('should be possible to create a single document', () => { client.documents.createOrUpdate('my_index', { fields: [ - {name: 'id', value: 1}, - {name: 'text', value: 'my value'} + { name: 'id', value: 1 }, + { name: 'text', value: 'my value' } ] }); @@ -31,27 +28,27 @@ describe('Documents', function () { json: [ { fields: [ - {name: 'id', value: 1}, - {name: 'text', value: 'my value'} + { name: 'id', value: 1 }, + { name: 'text', value: 'my value' } ] } ] }); }); - it('should be possible to create multiple documents', function () { + it('should be possible to create multiple documents', () => { client.documents.createOrUpdate('my_index', [ { fields: [ - {name: 'id', value: 1}, - {name: 'text', value: 'my value'} + { name: 'id', value: 1 }, + { name: 'text', value: 'my value' } ] }, { fields: [ - {name: 'id', value: 2}, - {name: 'text', value: 'my second value'} + { name: 'id', value: 2 }, + { name: 'text', value: 'my second value' } ] } ]); @@ -62,26 +59,26 @@ describe('Documents', function () { json: [ { fields: [ - {name: 'id', value: 1}, - {name: 'text', value: 'my value'} + { name: 'id', value: 1 }, + { name: 'text', value: 'my value' } ] }, { fields: [ - {name: 'id', value: 2}, - {name: 'text', value: 'my second value'} + { name: 'id', value: 2 }, + { name: 'text', value: 'my second value' } ] } ] }); }); - it('should be possible to update a single document', function () { + it('should be possible to update a single document', () => { client.documents.createOrUpdate('my_index', { fields: [ - {name: 'id', value: 1}, - {name: 'text', value: 'my new value'} + { name: 'id', value: 1 }, + { name: 'text', value: 'my new value' } ] }); @@ -91,8 +88,8 @@ describe('Documents', function () { json: [ { fields: [ - {name: 'id', value: 1}, - {name: 'text', value: 'my new value'} + { name: 'id', value: 1 }, + { name: 'text', value: 'my new value' } ] } ] @@ -100,14 +97,11 @@ describe('Documents', function () { }); }); - describe('#destroy', function () { + describe('#destroy', () => { - it('should be possible to destroy an index', function () { + it('should be possible to destroy an index', () => { - client.documents.destroy('my_index', { - field: 'id', - values: 1 - }); + client.documents.destroy('my_index', { field: 'id', values: 1 }); expect(rq).to.be.calledWith({ method: 'DELETE', @@ -116,4 +110,4 @@ describe('Documents', function () { }); }); }); -}); \ No newline at end of file +}); diff --git a/test/fields.js b/test/fields.js index 7ba1c08..fc37a39 100644 --- a/test/fields.js +++ b/test/fields.js @@ -1,59 +1,44 @@ /* globals describe, it, beforeEach */ -var chai = require('chai').use(require('sinon-chai')), - expect = chai.expect, - sinon = require('sinon'), - oss = require('../'); +const oss = require('../'); -describe('Fields', function () { +describe('Fields', () => { - var client, rq; + let client, rq; - beforeEach(function () { + beforeEach(() => { client = oss.createClient(); rq = sinon.stub(client.fields, 'request'); }); - describe('#createOrUpdate', function () { + describe('#createOrUpdate', () => { - it('should be possible to create a field', function () { + it('should be possible to create a field', () => { - client.fields.createOrUpdate('my_index', { - name: 'my_field', - stored: true - }); + client.fields.createOrUpdate('my_index', { name: 'my_field', stored: true }); expect(rq).to.be.calledWith({ method: 'PUT', pathname: '/services/rest/index/my_index/field/my_field', - json: { - name: 'my_field', - stored: 'YES' - } + json: { name: 'my_field', stored: 'YES' } }); }); - it('should be possible to update a field', function () { + it('should be possible to update a field', () => { - client.fields.createOrUpdate('my_index', { - name: 'my_field', - stored: true - }); + client.fields.createOrUpdate('my_index', { name: 'my_field', stored: true }); expect(rq).to.be.calledWith({ method: 'PUT', pathname: '/services/rest/index/my_index/field/my_field', - json: { - name: 'my_field', - stored: 'YES' - } + json: { name: 'my_field', stored: 'YES' } }); }); }); - describe('#destroy', function () { + describe('#destroy', () => { - it('should be possible to destroy an index', function () { + it('should be possible to destroy an index', () => { client.fields.destroy('my_index', 'my_field'); @@ -64,29 +49,23 @@ describe('Fields', function () { }); }); - describe('#setUniqueDefault', function () { + describe('#setUniqueDefault', () => { - it('should be possible to define unique and default field', function () { + it('should be possible to define unique and default field', () => { - client.fields.setUniqueDefault('my_index', { - default: 'my_default_field', - unique: 'my_unique_field' - }); + client.fields.setUniqueDefault('my_index', { default: 'my_default_field', unique: 'my_unique_field' }); expect(rq).to.be.calledWith({ method: 'POST', pathname: '/services/rest/index/my_index/field', - qs: { - default: 'my_default_field', - unique: 'my_unique_field' - } + qs: { default: 'my_default_field', unique: 'my_unique_field' } }); }); }); - describe('#list', function () { + describe('#list', () => { - it('should be possible to list the fileds of an index', function () { + it('should be possible to list the fileds of an index', () => { client.fields.list('my_index'); @@ -96,4 +75,4 @@ describe('Fields', function () { }); }); }); -}); \ No newline at end of file +}); diff --git a/test/indexes.js b/test/indexes.js index 423606b..749ec04 100644 --- a/test/indexes.js +++ b/test/indexes.js @@ -1,22 +1,19 @@ /* globals describe, it, beforeEach */ -var chai = require('chai').use(require('sinon-chai')), - expect = chai.expect, - sinon = require('sinon'), - oss = require('../'); +const oss = require('../'); -describe('Indexes', function () { +describe('Indexes', () => { - var client, rq; + let client, rq; - beforeEach(function () { + beforeEach(() => { client = oss.createClient(); rq = sinon.stub(client.indexes, 'request'); }); - describe('#create', function () { + describe('#create', () => { - it('should be possible to create an index', function () { + it('should be possible to create an index', () => { client.indexes.create('my_index'); @@ -26,11 +23,9 @@ describe('Indexes', function () { }); }); - it('should be possible to create an index with a template', function () { + it('should be possible to create an index with a template', () => { - client.indexes.create('my_index', { - template: 'my_template' - }); + client.indexes.create('my_index', { template: 'my_template' }); expect(rq).to.be.calledWith({ method: 'POST', @@ -39,9 +34,9 @@ describe('Indexes', function () { }); }); - describe('#exists', function () { + describe('#exists', () => { - it('should return if an index exists', function () { + it('should return if an index exists', () => { client.indexes.exists('my_index'); @@ -52,9 +47,9 @@ describe('Indexes', function () { }); }); - describe('#destroy', function () { + describe('#destroy', () => { - it('should be possible to destroy an index', function () { + it('should be possible to destroy an index', () => { client.indexes.destroy('my_index'); @@ -64,4 +59,4 @@ describe('Indexes', function () { }); }); }); -}); \ No newline at end of file +}); diff --git a/test/more-like-this.js b/test/more-like-this.js index 7c4bb54..6665283 100644 --- a/test/more-like-this.js +++ b/test/more-like-this.js @@ -1,26 +1,19 @@ -'use strict'; - /* globals describe, it, beforeEach */ -var oss = require('../'); -var chai = require('chai').use(require('sinon-chai')); -var sinon = require('sinon'); -var expect = chai.expect; +const oss = require('../'); -describe('More like this', function () { +describe('More like this', () => { - var client, rq; + let client, rq; - beforeEach(function () { + beforeEach(() => { client = oss.createClient(); rq = sinon.stub(client, 'request'); }); - it('should be possible to request more like this', function () { + it('should be possible to request more like this', () => { - client.moreLikeThis('my_index', { - minWordLen: 2 - }); + client.moreLikeThis('my_index', { minWordLen: 2 }); expect(rq).to.be.calledWith({ json: { @@ -40,4 +33,4 @@ describe('More like this', function () { pathname: '/services/rest/index/my_index/morelikethis' }); }); -}); \ No newline at end of file +}); diff --git a/test/replication.js b/test/replication.js new file mode 100644 index 0000000..5339efb --- /dev/null +++ b/test/replication.js @@ -0,0 +1,43 @@ +/* globals describe, it, beforeEach */ + +const oss = require('../'); + +describe('Replication', () => { + + let client, request, searcher; + + beforeEach(() => { + client = oss.createClient(); + request = sinon.stub(client, 'request'); + searcher = { hostname: 'searcher-oss.com', port: 8000 }; + }); + + describe('#createReplicationIndex', () => { + + it('should request the API to create a replication index', () => { + + client.createReplicationIndex('my_index', searcher); + + expect(request).to.be.calledWithMatch({ + method: 'PUT', + pathname: '/services/rest/index/my_index/replication', + body: { + replicationType: 'MAIN_INDEX', + remoteUrl: 'http://searcher-oss.com:8000', + remoteIndexName: 'my_index', + secTimeOut: 120 + } + }); + }); + + it('should request the API to start a replication on an index', () => { + client.replicate('my_index', searcher); + + expect(request).to.be.calledWithMatch({ + method: 'PUT', + pathname: '/services/rest/index/my_index/replication/run', + search: 'name=http://searcher-oss.com:8000/my_index' + }); + }); + }); +}); diff --git a/test/search.js b/test/search.js index ecf5096..5004676 100644 --- a/test/search.js +++ b/test/search.js @@ -1,31 +1,24 @@ /* globals describe, it, beforeEach */ -var chai = require('chai').use(require('sinon-chai')), - expect = chai.expect, - sinon = require('sinon'), - oss = require('../'); +const oss = require('../'); -describe('Search', function () { +describe('Search', () => { - var client, rq; + let client, rq; beforeEach(function () { client = oss.createClient(); rq = sinon.stub(client, 'request'); }); - it('should be possible to make a search', function () { + it('should be possible to make a search', () => { - client.search('my_index', { - query: 'My query' - }); + client.search('my_index', { query: 'My query' }); expect(rq).to.be.calledWith({ - json: { - query: 'My query' - }, + json: { query: 'My query' }, method: 'POST', pathname: '/services/rest/index/my_index/search/field' }); }); -}); \ No newline at end of file +}); diff --git a/test/templates.js b/test/templates.js index 29c0e1f..d7317c9 100644 --- a/test/templates.js +++ b/test/templates.js @@ -1,21 +1,18 @@ /* globals describe, it, beforeEach */ -var chai = require('chai').use(require('sinon-chai')), - expect = chai.expect, - sinon = require('sinon'), - oss = require('../'); +const oss = require('../'); -describe('Templates', function () { +describe('Templates', () => { - var client, rq; + let client, rq; - beforeEach(function () { + beforeEach(() => { client = oss.createClient(); rq = sinon.stub(client.templates, 'request'); }); - describe('#createOrUpdate', function () { - it('should be possible to create a field', function () { + describe('#createOrUpdate', () => { + it('should be possible to create a field', () => { client.templates.createOrUpdate('my_index', 'my_template', { returnedFields: ['my_field'] @@ -24,15 +21,13 @@ describe('Templates', function () { expect(rq).to.be.calledWith({ method: 'PUT', pathname: '/services/rest/index/my_index/search/field/my_template', - json: { - returnedFields: ['my_field'] - } + json: { returnedFields: ['my_field'] } }); }); }); - describe('#destroy', function () { - it('should be possible to destroy an index', function () { + describe('#destroy', () => { + it('should be possible to destroy an index', () => { client.templates.destroy('my_index', 'my_template'); @@ -43,8 +38,8 @@ describe('Templates', function () { }); }); - describe('#list', function () { - it('should be possible to list templates', function () { + describe('#list', () => { + it('should be possible to list templates', () => { client.templates.list('my_index'); @@ -55,8 +50,8 @@ describe('Templates', function () { }); }); - describe('#get', function () { - it('should be possible to get a template', function () { + describe('#get', () => { + it('should be possible to get a template', () => { client.templates.get('my_index', 'my_template'); @@ -66,4 +61,4 @@ describe('Templates', function () { }); }); }); -}); \ No newline at end of file +}); diff --git a/test/util.js b/test/util.js index 18e10f2..a36a1e5 100644 --- a/test/util.js +++ b/test/util.js @@ -1,24 +1,23 @@ /* globals describe, it */ -var expect = require('chai').expect, - util = require('../lib/util'); +const util = require('../lib/util'); -describe('Util', function () { +describe('Util', () => { - describe('#convertParam', function () { + describe('#convertParam', () => { - it('should transform true and false to "YES" and "NO"', function () { + it('should transform true and false to "YES" and "NO"', () => { expect(util.convertParam(true)).to.equal('YES'); expect(util.convertParam(false)).to.equal('NO'); }); - it('should uppercase string', function () { + it('should uppercase string', () => { expect(util.convertParam('my_string')).to.equal('MY_STRING'); }); - it('should do nothing on other parameters', function () { - var object = {kung: 'foo'}; + it('should do nothing on other parameters', () => { + const object = { kung: 'foo' }; expect(util.convertParam(object)).to.equal(object); }); }); -}); \ No newline at end of file +});