<commit_before>/**
 * The notifier core component can be directly invoked using the notify() function, but it also listens
 * to messages coming from workers.
 * And in particular, messages from the write worker(s), that need to be forwarded to the right listeners.
 *
 * This file tests the function called each time a response has been received from a worker. This listening
 * function then dispatches the response to the right notifyDocument* function
 */
var
  should = require('should'),
  winston = require('winston'),
  rewire = require('rewire'),
  RequestObject = require('root-require')('lib/api/core/models/requestObject'),
  ResponseObject = require('root-require')('lib/api/core/models/responseObject'),
  Notifier = rewire('../../../../lib/api/core/notifier');

require('should-promised');

describe('Test: notifier.workerNotification', function () {
  var
    requestObject = new RequestObject({
      controller: 'write',
      action: '',
      requestId: 'foo',
      collection: 'bar',
      body: { foo: 'bar' }
    }),
    responseObject = new ResponseObject(requestObject, {});

  it('should call notifyDocumentCreate on a "create" event', function () {
    var
      created = false,
      anyOtherAction = false;

    responseObject.action = 'create';

    Notifier.__with__({
      notifyDocumentCreate: function () { created = true; return Promise.resolve({}); },
      notifyDocumentUpdate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentDelete: function () { anyOtherAction = true; return Promise.resolve({}); }
    })(function () {
      (Notifier.__get__('workerNotification'))(responseObject);
      should(created).be.true();
      should(anyOtherAction).be.false();
    });
  });

  it('should call notifyDocumentUpdate on a "update" event', function () {
    var
      updated = false,
      anyOtherAction = false;

    responseObject.action = 'update';

    Notifier.__with__({
      notifyDocumentCreate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentUpdate: function () { updated = true; return Promise.resolve({}); },
      notifyDocumentDelete: function () { anyOtherAction = true; return Promise.resolve({}); }
    })(function () {
      (Notifier.__get__('workerNotification'))(responseObject);
      should(updated).be.true();
      should(anyOtherAction).be.false();
    });
  });

  it('should call notifyDocumentDelete on a "delete" event', function () {
    var
      deleted = false,
      anyOtherAction = false;

    responseObject.action = 'delete';

    Notifier.__with__({
      notifyDocumentCreate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentUpdate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentDelete: function () { deleted = true; return Promise.resolve({}); }
    })(function () {
      (Notifier.__get__('workerNotification'))(responseObject);
      should(deleted).be.true();
      should(anyOtherAction).be.false();
    });
  });

  it('should call notifyDocumentDelete on a "deleteByQuery" event', function () {
    var
      deleted = false,
      anyOtherAction = false;

    responseObject.action = 'deleteByQuery';

    Notifier.__with__({
      notifyDocumentCreate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentUpdate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentDelete: function () { deleted = true; return Promise.resolve({}); }
    })(function () {
      (Notifier.__get__('workerNotification'))(responseObject);
      should(deleted).be.true();
      should(anyOtherAction).be.false();
    });
  });

  it('should do nothing on an unknown event', function () {
    var
      tookAction = false;

    responseObject.action = 'foo';

    Notifier.__with__({
      notifyDocumentCreate: function () { tookAction = true; return Promise.resolve({}); },
      notifyDocumentUpdate: function () { tookAction = true; return Promise.resolve({}); },
      notifyDocumentDelete: function () { tookAction = true; return Promise.resolve({}); }
    })(function () {
      (Notifier.__get__('workerNotification'))(responseObject);
      should(tookAction).be.false();
    });
  });

  it('should log an error when one occurs in a notifyDocument* function', function (done) {
    responseObject.action = 'create';

    Notifier.kuzzle.once('log:error', function () {
      done();
    });

    Notifier.__with__({
      notifyDocumentCreate: function () { return Promise.reject(new Error('')); }
    })(function () {
      (Notifier.__get__('workerNotification')).call(Notifier, responseObject);
    });
  });
});<commit_msg>fix useless unit test<commit_after>/**
 * The notifier core component can be directly invoked using the notify() function, but it also listens
 * to messages coming from workers.
 * And in particular, messages from the write worker(s), that need to be forwarded to the right listeners.
 *
 * This file tests the function called each time a response has been received from a worker. This listening
 * function then dispatches the response to the right notifyDocument* function
 */
var
  should = require('should'),
  winston = require('winston'),
  rewire = require('rewire'),
  RequestObject = require('root-require')('lib/api/core/models/requestObject'),
  ResponseObject = require('root-require')('lib/api/core/models/responseObject'),
  Notifier = rewire('../../../../lib/api/core/notifier'),
  params = require('rc')('kuzzle'),
  Kuzzle = require('root-require')('lib/api/Kuzzle');

require('should-promised');

describe('Test: notifier.workerNotification', function () {
  var
    requestObject = new RequestObject({
      controller: 'write',
      action: '',
      requestId: 'foo',
      collection: 'bar',
      body: { foo: 'bar' }
    }),
    responseObject = new ResponseObject(requestObject, {});

  it('should call notifyDocumentCreate on a "create" event', function () {
    var
      created = false,
      anyOtherAction = false;

    responseObject.action = 'create';

    Notifier.__with__({
      notifyDocumentCreate: function () { created = true; return Promise.resolve({}); },
      notifyDocumentUpdate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentDelete: function () { anyOtherAction = true; return Promise.resolve({}); }
    })(function () {
      (Notifier.__get__('workerNotification'))(responseObject);
      should(created).be.true();
      should(anyOtherAction).be.false();
    });
  });

  it('should call notifyDocumentUpdate on a "update" event', function () {
    var
      updated = false,
      anyOtherAction = false;

    responseObject.action = 'update';

    Notifier.__with__({
      notifyDocumentCreate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentUpdate: function () { updated = true; return Promise.resolve({}); },
      notifyDocumentDelete: function () { anyOtherAction = true; return Promise.resolve({}); }
    })(function () {
      (Notifier.__get__('workerNotification'))(responseObject);
      should(updated).be.true();
      should(anyOtherAction).be.false();
    });
  });

  it('should call notifyDocumentDelete on a "delete" event', function () {
    var
      deleted = false,
      anyOtherAction = false;

    responseObject.action = 'delete';

    Notifier.__with__({
      notifyDocumentCreate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentUpdate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentDelete: function () { deleted = true; return Promise.resolve({}); }
    })(function () {
      (Notifier.__get__('workerNotification'))(responseObject);
      should(deleted).be.true();
      should(anyOtherAction).be.false();
    });
  });

  it('should call notifyDocumentDelete on a "deleteByQuery" event', function () {
    var
      deleted = false,
      anyOtherAction = false;

    responseObject.action = 'deleteByQuery';

    Notifier.__with__({
      notifyDocumentCreate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentUpdate: function () { anyOtherAction = true; return Promise.resolve({}); },
      notifyDocumentDelete: function () { deleted = true; return Promise.resolve({}); }
    })(function () {
      (Notifier.__get__('workerNotification'))(responseObject);
      should(deleted).be.true();
      should(anyOtherAction).be.false();
    });
  });

  it('should do nothing on an unknown event', function () {
    var
      tookAction = false;

    responseObject.action = 'foo';

    Notifier.__with__({
      notifyDocumentCreate: function () { tookAction = true; return Promise.resolve({}); },
      notifyDocumentUpdate: function () { tookAction = true; return Promise.resolve({}); },
      notifyDocumentDelete: function () { tookAction = true; return Promise.resolve({}); }
    })(function () {
      (Notifier.__get__('workerNotification'))(responseObject);
      should(tookAction).be.false();
    });
  });

  it('should log an error when one occurs in a notifyDocument* function', function (done) {
    var kuzzle = new Kuzzle();

    kuzzle.log = new (winston.Logger)({transports: [new (winston.transports.Console)({level: 'silent'})]});
    kuzzle.start(params, {dummy: true}).then(function () {
      responseObject.action = 'create';
      responseObject.collection = false;

      kuzzle.once('log:error', function () {
        done();
      });

      kuzzle.services.list.broker.add(kuzzle.config.queues.coreNotifierTaskQueue, responseObject);
    });
  });
});
{'fraction_non_alphanumeric': 0.1474768280123584, 'fraction_numerical': 0.0, 'mean_word_length': 3.6665064872657376, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2006736', 'n_tokens_mistral': 2614, 'n_tokens_neox': 2553, 'n_words': 645}