/**
 * 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);
    });
  });
});

@@ -752,16 +752,106 @@
 tifier')
+,%0A  params = require('rc')('kuzzle'),%0A  Kuzzle = require('root-require')('lib/api/Kuzzle')
 ;%0A%0Arequi
@@ -4455,24 +4455,225 @@
 on (done) %7B%0A
+    var kuzzle = new Kuzzle();%0A%0A    kuzzle.log = new (winston.Logger)(%7Btransports: %5Bnew (winston.transports.Console)(%7Blevel: 'silent'%7D)%5D%7D);%0A    kuzzle.start(params, %7Bdummy: true%7D).then(function () %7B%0A  
     response
@@ -4698,30 +4698,64 @@
 reate';%0A
-%0A
+  
     
-Notifier.
+responseObject.collection = false;%0A%0A      
 kuzzle.o
@@ -4795,16 +4795,18 @@
     
+  
 done();%0A
     
@@ -4797,24 +4797,26 @@
     done();%0A
+  
     %7D);%0A%0A   
@@ -4820,191 +4820,84 @@
     
-Notifier.__with__(%7B%0A      notifyDocumentCreate: function () %7B return Promise.reject(new Error('')); %7D%0A    %7D)(function () %7B%0A      (Notifier.__get__('workerNotification')).call(Notifier
+  kuzzle.services.list.broker.add(kuzzle.config.queues.coreNotifierTaskQueue
 , re

fix useless unit test