<commit_before>'use strict';

const proxyquire = require('proxyquire');
const config = require('../../common/config');
const _ = require('lodash');
const Promise = require('bluebird');
const CONST = require('../../common/constants');

const pubSubStub = {
  publish: () => undefined,
  subscribe: () => undefined
};

const riemannJSStub = {
  send: () => true,
  /* jshint unused:false */
  Event: (event) => true,
  disconnect: () => true
};

let riemannClientEventHandlers = {};
const RiemannClient = proxyquire('../../common/utils/EventLogRiemannClient', {
  'riemannjs': {
    createClient: function () {
      return {
        on: function (event, callback) {
          _.set(riemannClientEventHandlers, event, callback);
          return true;
        },
        disconnect: function () {
          return riemannJSStub.disconnect();
        },
        Event: function (event) {
          return riemannJSStub.Event(event);
        },
        send: function () {
          return riemannJSStub.send();
        }
      };
    }
  },
  'pubsub-js': {
    subscribe: function (eventType, callBack) {
      return pubSubStub.subscribe(eventType, callBack);
    }
  }
});

describe('utils', function () {
  /* jshint expr:true */
  describe('EventLogRiemannClient', function () {
    let pubSubSpy, riemannSendSpy, riemannEventSpy, riemannDisconnectSpy;

    beforeEach(function () {
      pubSubSpy = sinon.stub(pubSubStub, 'subscribe');
      riemannSendSpy = sinon.stub(riemannJSStub, 'send');
      riemannEventSpy = sinon.stub(riemannJSStub, 'Event');
      riemannDisconnectSpy = sinon.stub(riemannJSStub, 'disconnect');
      pubSubSpy.returns(true);
      riemannSendSpy.returns(true);
      riemannEventSpy.returns(true);
      riemannDisconnectSpy.returns(true);
    });

    afterEach(function () {
      pubSubSpy.restore();
      riemannSendSpy.restore();
      riemannEventSpy.restore();
      riemannDisconnectSpy.restore();
      riemannClientEventHandlers = {};
    });

    describe('create', function () {
      it('should create Riemann Client Successfully and subscribe to the input event type', function () {
        const riemannOptions = _
          .chain({})
          .assign(config.riemann)
          .set('event_type', config.internal.event_type)
          .value();
        const riemannClient = new RiemannClient(riemannOptions);
        expect(pubSubSpy).to.be.called;
        const eventType = pubSubSpy.firstCall.args[0];
        expect(riemannClient).to.be.an('object');
        expect(riemannClient.status).to.eql(CONST.EVENT_LOG_RIEMANN_CLIENT_STATUS.INITIALIZING);
        expect(riemannClient.QUEUED_REQUESTS).to.eql([]);
        expect(eventType).to.eql(config.internal.event_type);
        expect(riemannClientEventHandlers.connect).to.be.not.null;
        expect(riemannClientEventHandlers.error).to.be.not.null;
        expect(riemannClientEventHandlers.disconnect).to.be.not.null;
      });
    });

    describe('disconnect', function () {
      it('should disconnect from Riemann Client Successfully', function () {
        const riemannOptions = _
          .chain({})
          .assign(config.riemann)
          .set('event_type', config.internal.event_type)
          .value();
        const riemannClient = new RiemannClient(riemannOptions);
        riemannClient.disconnect();
        expect(riemannDisconnectSpy).to.be.calledOnce;
        expect(riemannClient.status).to.eql(CONST.EVENT_LOG_RIEMANN_CLIENT_STATUS.DISCONNECTED);
      });
    });

    describe('send', function () {
      const riemannOptions = _
        .chain({})
        .assign(config.riemann)
        .set('event_type', config.internal.event_type)
        .value();
      const riemannClient = new RiemannClient(riemannOptions);
      if (riemannClientEventHandlers.connect && _.isFunction(riemannClientEventHandlers.connect)) {
        riemannClientEventHandlers.connect.call(riemannClientEventHandlers.connect);
      } else {
        expect.fail('Event Handlers not registered for riemann client');
      }

      it('should log event successfully to Riemann with response details', function () {
        const event = {
          host: 'INLN50932351A',
          eventName: 'CF.servicefabrik.broker_catalog',
          metric: 0,
          state: 'ok',
          description: 'Get broker service catalog succeeded',
          tags: ['catalog'],
          time: 1483353454485,
          request: {
            user: {
              name: 'broker'
            }
          },
          response: {}
        };
        const expectedFirstResultObject = _
          .chain(event)
          .pick('metric', 'state', 'description', 'tags')
          .set('service', event.eventName)
          .set('host', _.get(config, 'riemann.prefix', 'CF'))
          .set('attributes', [{
            key: 'request',
            value: (typeof event.request === 'object' ? JSON.stringify(event.request) : event.request)
          }, {
            key: 'response',
            value: (typeof event.response === 'object' ? JSON.stringify(event.response) : event.response)
          }])
          .value();
        riemannClient.handleEvent(config.internal.event_type, {
          event: event,
          options: {
            include_response_body: true
          }
        });
        expect(riemannSendSpy).to.be.calledOnce;
        expect(riemannEventSpy).to.be.calledOnce;
        const firstResponse = riemannEventSpy.firstCall.args[0];
        expect(firstResponse).to.be.an('object');
        expect(firstResponse).to.eql(expectedFirstResultObject);
      });

      it('should log 2 events successfully to Riemann with response details', function () {
        const event = {
          host: 'INLN50932351A',
          eventName: 'CF.broker.0.service-fabrik.director.create_instance ',
          metric: 0,
          state: 'ok',
          description: 'Create a new service instance',
          tags: ['create'],
          time: 1483353454485,
          request: {
            user: {
              name: 'broker'
            },
            instance_id: 'fe27a9ea-0e93-485a-86f2-600aa725fc88',
            service_id: '24731fb8-7b84-4f57-914f-c3d55d793dd4'
          },
          response: {}
        };
        const expectedFirstResultObject = _
          .chain(event)
          .pick('metric', 'state', 'description', 'tags')
          .set('service', event.eventName)
          .set('host', _.get(config, 'riemann.prefix', 'CF'))
          .set('attributes', [{
            key: 'request',
            value: (typeof event.request === 'object' ? JSON.stringify(event.request) : event.request)
          }, {
            key: 'response',
            value: (typeof event.response === 'object' ? JSON.stringify(event.response) : event.response)
          }])
          .value();
        const expectedSecondResultObject = _
          .chain(event)
          .pick('metric', 'state', 'description', 'tags')
          .set('service', `${event.eventName}.instance_id.fe27a9ea-0e93-485a-86f2-600aa725fc88.service_name.blueprint`)
          .set('host', _.get(config, 'riemann.prefix', 'CF'))
          .set('attributes', [{
            key: 'request',
            value: (typeof event.request === 'object' ? JSON.stringify(event.request) : event.request)
          }, {
            key: 'response',
            value: (typeof event.response === 'object' ? JSON.stringify(event.response) : event.response)
          }])
          .value();

        riemannClient.handleEvent(config.internal.event_type, {
          event: event,
          options: {
            include_response_body: true
          }
        });
        expect(riemannSendSpy).to.be.calledTwice;
        expect(riemannEventSpy).to.be.calledTwice;
        const firstResponse = riemannEventSpy.firstCall.args[0];
        expect(firstResponse).to.be.an('object');
        expect(firstResponse).to.eql(expectedFirstResultObject);
        const secondResponse = riemannEventSpy.secondCall.args[0];
        expect(secondResponse).to.be.an('object');
        expect(secondResponse).to.eql(expectedSecondResultObject);<commit_msg>Added missing tests<commit_after>'use strict';

const proxyquire = require('proxyquire');
const config = require('../../common/config');
const _ = require('lodash');
const Promise = require('bluebird');
const CONST = require('../../common/constants');

const pubSubStub = {
  publish: () => undefined,
  subscribe: () => undefined
};

const riemannJSStub = {
  send: () => true,
  /* jshint unused:false */
  Event: (event) => true,
  disconnect: () => true
};

let riemannClientEventHandlers = {};
const RiemannClient = proxyquire('../../common/utils/EventLogRiemannClient', {
  'riemannjs': {
    createClient: function () {
      return {
        on: function (event, callback) {
          _.set(riemannClientEventHandlers, event, callback);
          return true;
        },
        disconnect: function () {
          return riemannJSStub.disconnect();
        },
        Event: function (event) {
          return riemannJSStub.Event(event);
        },
        send: function () {
          return riemannJSStub.send();
        }
      };
    }
  },
  'pubsub-js': {
    subscribe: function (eventType, callBack) {
      return pubSubStub.subscribe(eventType, callBack);
    }
  }
});

describe('utils', function () {
  /* jshint expr:true */
  describe('EventLogRiemannClient', function () {
    let pubSubSpy, riemannSendSpy, riemannEventSpy, riemannDisconnectSpy;

    beforeEach(function () {
      pubSubSpy = sinon.stub(pubSubStub, 'subscribe');
      riemannSendSpy = sinon.stub(riemannJSStub, 'send');
      riemannEventSpy = sinon.stub(riemannJSStub, 'Event');
      riemannDisconnectSpy = sinon.stub(riemannJSStub, 'disconnect');
      pubSubSpy.returns(true);
      riemannSendSpy.returns(true);
      riemannEventSpy.returns(true);
      riemannDisconnectSpy.returns(true);
    });

    afterEach(function () {
      pubSubSpy.restore();
      riemannSendSpy.restore();
      riemannEventSpy.restore();
      riemannDisconnectSpy.restore();
      riemannClientEventHandlers = {};
    });

    describe('create', function () {
      it('should create Riemann Client Successfully and subscribe to the input event type', function () {
        const riemannOptions = _
          .chain({})
          .assign(config.riemann)
          .set('event_type', config.internal.event_type)
          .value();
        const riemannClient = new RiemannClient(riemannOptions);
        expect(pubSubSpy).to.be.called;
        const eventType = pubSubSpy.firstCall.args[0];
        expect(riemannClient).to.be.an('object');
        expect(riemannClient.status).to.eql(CONST.EVENT_LOG_RIEMANN_CLIENT_STATUS.INITIALIZING);
        expect(riemannClient.QUEUED_REQUESTS).to.eql([]);
        expect(eventType).to.eql(config.internal.event_type);
        expect(riemannClientEventHandlers.connect).to.be.not.null;
        expect(riemannClientEventHandlers.error).to.be.not.null;
        expect(riemannClientEventHandlers.disconnect).to.be.not.null;
      });
    });

    describe('disconnect', function () {
      it('should disconnect from Riemann Client Successfully', function () {
        const riemannOptions = _
          .chain({})
          .assign(config.riemann)
          .set('event_type', config.internal.event_type)
          .value();
        const riemannClient = new RiemannClient(riemannOptions);
        riemannClient.disconnect();
        expect(riemannDisconnectSpy).to.be.calledOnce;
        expect(riemannClient.status).to.eql(CONST.EVENT_LOG_RIEMANN_CLIENT_STATUS.DISCONNECTED);
      });
    });

    describe('send', function () {
      const riemannOptions = _
        .chain({})
        .assign(config.riemann)
        .set('event_type', config.internal.event_type)
        .value();
      const riemannClient = new RiemannClient(riemannOptions);
      if (riemannClientEventHandlers.connect && _.isFunction(riemannClientEventHandlers.connect)) {
        riemannClientEventHandlers.connect.call(riemannClientEventHandlers.connect);
      } else {
        expect.fail('Event Handlers not registered for riemann client');
      }

      it('should log event successfully to Riemann with response details', function () {
        const event = {
          host: 'INLN50932351A',
          eventName: 'CF.servicefabrik.broker_catalog',
          metric: 0,
          state: 'ok',
          description: 'Get broker service catalog succeeded',
          tags: ['catalog'],
          time: 1483353454485,
          request: {
            user: {
              name: 'broker'
            }
          },
          response: {}
        };
        const expectedFirstResultObject = _
          .chain(event)
          .pick('metric', 'state', 'description', 'tags')
          .set('service', event.eventName)
          .set('host', _.get(config, 'riemann.prefix', 'CF'))
          .set('attributes', [{
            key: 'request',
            value: (typeof event.request === 'object' ? JSON.stringify(event.request) : event.request)
          }, {
            key: 'response',
            value: (typeof event.response === 'object' ? JSON.stringify(event.response) : event.response)
          }])
          .value();
        riemannClient.handleEvent(config.internal.event_type, {
          event: event,
          options: {
            include_response_body: true
          }
        });
        expect(riemannSendSpy).to.be.calledOnce;
        expect(riemannEventSpy).to.be.calledOnce;
        const firstResponse = riemannEventSpy.firstCall.args[0];
        expect(firstResponse).to.be.an('object');
        expect(firstResponse).to.eql(expectedFirstResultObject);
      });

      it('should log 2 events successfully to Riemann with response details for create instance', function () {
        const event = {
          host: 'INLN50932351A',
          eventName: 'CF.broker.0.service-fabrik.director.create_instance ',
          metric: 0,
          state: 'ok',
          description: 'Create a new service instance',
          tags: ['create'],
          time: 1483353454485,
          request: {
            user: {
              name: 'broker'
            },
            instance_id: 'fe27a9ea-0e93-485a-86f2-600aa725fc88',
            service_id: '24731fb8-7b84-4f57-914f-c3d55d793dd4'
          },
          response: {}
        };
        const expectedFirstResultObject = _
          .chain(event)
          .pick('metric', 'state', 'description', 'tags')
          .set('service', event.eventName)
          .set('host', _.get(config, 'riemann.prefix', 'CF'))
          .set('attributes', [{
            key: 'request',
            value: (typeof event.request === 'object' ? JSON.stringify(event.request) : event.request)
          }, {
            key: 'response',
            value: (typeof event.response === 'object' ? JSON.stringify(event.response) : event.response)
          }])
          .value();
        const expectedSecondResultObject = _
          .chain(event)
          .pick('metric', 'state', 'description', 'tags')
          .set('service', `${event.eventName}.instance_id.fe27a9ea-0e93-485a-86f2-600aa725fc88.service_name.blueprint`)
          .set('host', _.get(config, 'riemann.prefix', 'CF'))
          .set('attributes', [{
            key: 'request',
            value: (typeof event.request === 'object' ? JSON.stringify(event.request) : event.request)
          }, {
            key: 'response',
            value: (typeof event.response === 'object' ? JSON.stringify(event.response) : event.response)
          }])
          .value();

        riemannClient.handleEvent(config.internal.event_type, {
          event: event,
          options: {
            include_response_body: true
          }
        });
        expect(riemannSendSpy).to.be.calledTwice;
        expect(riemannEventSpy).to.be.calledTwice;
        const firstResponse = riemannEventSpy.firstCall.args[0];
        expect(firstResponse).to.be.an('object');
        expect(firstResponse).to.eql(expectedFirstResultObject);
        const secondResponse = riemannEventSpy.secondCall.args[0];
        expect(secondResponse).to.be.an('object');
        expect(secondResponse).to.eql(expectedSecondResultObject);
      });

      it('should log 2 events successfully to Riemann with response details for start backup', function () {
        const event = {
          host: 'INLN50932351A',
          eventName: 'CF.broker.0.service-fabrik.director.create_backup ',
          metric: 0,
          state: 'ok',
          description: 'Backup service instance',
          tags: ['backup'],
          time: 1483353454485,
          request: {
            user: {
              name: 'broker'
            },
            instance_id: 'fe27a9ea-0e93-485a-86f2-600aa725fc88',
            backup_guid: '24731fb8-7b84-4f57-914f-c3d55d793dd4'
          },
          response: {}
        };
        const expectedFirstResultObject = _
          .chain(event)
          .pick('metric', 'state', 'description', 'tags')
          .set('service', event.eventName)
          .set('host', _.get(config, 'riemann.prefix', 'CF'))
          .set('attributes', [{
            key: 'request',
            value: (typeof event.request === 'object' ? JSON.stringify(event.request) : event.request)
          }, {
            key: 'response',
            value: (typeof event.response === 'object' ? JSON.stringify(event.response) : event.response)
          }])
          .value();
        const expectedSecondResultObject = _
          .chain(event)
          .pick('metric', 'state', 'description', 'tags')
          .set('service', `${event.eventName}.instance_id.fe27a9ea-0e93-485a-86f2-600aa725fc88.backup_guid.24731fb8-7b84-4f57-914f-c3d55d793dd4`)
          .set('host', _.get(config, 'riemann.prefix', 'CF'))
          .set('attributes', [{
            key: 'request',
            value: (typeof event.request === 'object' ? JSON.stringify(event.request) : event.request)
          }, {
            key: 'response',
            value: (typeof event.response === 'object' ? JSON.stringify(event.response) : event.response)
          }])
          .value();

        riemannClient.handleEvent(config.internal.event_type, {
          event: event,
          options: {
            include_response_body: true
          }
        });
        expect(riemannSendSpy).to.be.calledTwice;
        expect(riemannEventSpy).to.be.calledTwice;
        const firstResponse = riemannEventSpy.firstCall.args[0];
        expect(firstResponse).to.be.an('object');
        expect(firstResponse).to.eql(expectedFirstResultObject);
        const secondResponse = riemannEventSpy.secondCall.args[0];
        expect(secondResponse).to.be.an('object');
        expect(secondResponse).to.eql(expectedSecondResultObject);
      });

      it('should log 2 events successfully to Riemann with response details for create binding', function () {
        const event = {
          host: 'INLN50932351A',
          eventName: 'CF.broker.0.service-fabrik.director.create_binding',
          metric: 0,
          state: 'ok',
          description: 'Bind service instance',
          tags: ['binding'],
          time: 1483353454485,
          request: {
            user: {
              name: 'broker'
            },
            instance_id: 'fe27a9ea-0e93-485a-86f2-600aa725fc88',
            app_guid: '24731fb8-7b84-4f57-914f-c3d55d793dd4'
          },
          response: {}
        };
        const expectedFirstResultObject = _
          .chain(event)
          .pick('metric', 'state', 'description', 'tags')
          .set('service', event.eventName)
          .set('host', _.get(config, 'riemann.prefix', 'CF'))
          .set('attributes', [{
            key: 'request',
            value: (typeof event.request === 'object' ? JSON.stringify(event.request) : event.request)
          }, {
            key: 'response',
            value: (typeof event.response === 'object' ? JSON.stringify(event.response) : event.response)
          }])
          .value();
        const expectedSecondResultObject = _
          .chain(event)
          .pick('metric', 'state', 'description', 'tags')
          .set('service', `${event.eventName}.instance_id.fe27a9ea-0e93-485a-86f2-600aa725fc88.app_guid.24731fb8-7b84-4f57-914f-c3d55d793dd4`)
          .set('host', _.get(config, 'riemann.prefix', 'CF'))
          .set('attributes', [{
            key: 'request',
            value: (typeof event.request === 'object' ? JSON.stringify(event.request) : event.request)
          }, {
            key: 'response',
            value: (typeof event.response === 'object' ? JSON.stringify(event.response) : event.response)
          }])
          .value();

        riemannClient.handleEvent(config.internal.event_type, {
          event: event,
          options: {
            include_response_body: true
          }
        });
        expect(riemannSendSpy).to.be.calledTwice;
        expect(riemannEventSpy).to.be.calledTwice;
        const firstResponse = riemannEventSpy.firstCall.args[0];
        expect(firstResponse).to.be.an('object');
        expect(firstResponse).to.eql(expectedFirstResultObject);
        const secondResponse = riemannEventSpy.secondCall.args[0];
        expect(secondResponse).to.be.an('object');
        expect(secondResponse).to.eql(expectedSecondResultObject);
{'fraction_non_alphanumeric': 0.13611046103440205, 'fraction_numerical': 0.020781652234963727, 'mean_word_length': 2.3285558498208445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '4436347', 'n_tokens_mistral': 6584, 'n_tokens_neox': 6174, 'n_words': 1026}