Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
[ingest] Creates the simulate api route
Browse files Browse the repository at this point in the history
  • Loading branch information
BigFunger committed Feb 5, 2016
1 parent 57008e6 commit d209809
Show file tree
Hide file tree
Showing 8 changed files with 557 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/plugins/kibana/common/ingest_processor_types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const _ = require('lodash');

export default [
{ // set
typeId: 'set',
title: 'Set',
targetField: '',
getDefinition: function (processor) {
return {
'set' : {
'tag': processor.processorId,
'field' : processor.targetField ? processor.targetField : '',
'value': processor.value ? processor.value : ''
}
};
},
getDescription: function (processor) {
const target = (processor.targetField) ? processor.targetField : '?';
return `[${target}]`;
}
}
];
87 changes: 87 additions & 0 deletions src/plugins/kibana/common/lib/_tests_/keys_deep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
var expect = require('expect.js');
var sinon = require('sinon');

var keysDeep = require('../keys_deep');

describe('keys deep', function () {

it('should list first level properties', function () {
let object = {
property1: 'value1',
property2: 'value2'
};
let expected = [
'property1',
'property2'
];

const keys = keysDeep(object);

expect(keys).to.eql(expected);
});

it('should list nested properties', function () {
let object = {
property1: 'value1',
property2: 'value2',
property3: {
subProperty1: 'value1.1'
}
};
let expected = [
'property1',
'property2',
'property3.subProperty1',
'property3'
];

const keys = keysDeep(object);

expect(keys).to.eql(expected);
});

it('should recursivly list nested properties', function () {
let object = {
property1: 'value1',
property2: 'value2',
property3: {
subProperty1: 'value1.1',
subProperty2: {
prop1: 'value1.2.1',
prop2: 'value2.2.2'
},
subProperty3: 'value1.3'
}
};
let expected = [
'property1',
'property2',
'property3.subProperty1',
'property3.subProperty2.prop1',
'property3.subProperty2.prop2',
'property3.subProperty2',
'property3.subProperty3',
'property3'
];

const keys = keysDeep(object);

expect(keys).to.eql(expected);
});

it('should list array properties, but not contents', function () {
let object = {
property1: 'value1',
property2: [ 'item1', 'item2' ]
};
let expected = [
'property1',
'property2'
];

const keys = keysDeep(object);

expect(keys).to.eql(expected);
});

});
21 changes: 21 additions & 0 deletions src/plugins/kibana/common/lib/keys_deep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const _ = require('lodash');

export default function keysDeep(object, base) {
let result = [];
let delimitedBase = base ? base + '.' : '';

_.forEach(object, (value, key) => {
var fullKey = delimitedBase + key;
if (_.isPlainObject(value)) {
result = result.concat(keysDeep(value, fullKey));
} else {
result.push(fullKey);
}
});

if (base) {
result.push(base);
}

return result;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
const expect = require('expect.js');
const _ = require('lodash');
import { buildRequest } from '../ingest_simulate';

describe('buildRequest', function () {

const processorTypes = [
{
typeId: 'simple1',
getDefinition: function (processor) {
return {
'modified_value': `modified_${processor.value}`
};
}
},
{
typeId: 'simple2',
getDefinition: function (processor) {
return {
'value1': processor.value,
'value2': `${processor.typeId}-${processor.value}`
};
}
}
];

it('should throw an error if no processorTypes argument is passed or the argument is not a plain object', function () {
expect(buildRequest).to.throwException(/requires a processorTypes object array argument/);
expect(buildRequest).withArgs('').to.throwException(/requires a processorTypes object array argument/);
expect(buildRequest).withArgs({}).to.throwException(/requires a processorTypes object array argument/);
expect(buildRequest).withArgs([]).to.throwException(/requires a processorTypes object array argument/);
});

it('should throw an error if no pipeline argument is passed or the argument is not a plain object', function () {
expect(buildRequest).withArgs([{}], []).to.throwException(/requires a pipeline object argument/);
});

it('should throw an error if pipeline contains no processors', function () {
expect(buildRequest).withArgs([{}], {}).to.throwException(/pipeline contains no processors/);
expect(buildRequest).withArgs([{}], { processors: 'foo' }).to.throwException(/pipeline contains no processors/);
expect(buildRequest).withArgs([{}], { processors: {} }).to.throwException(/pipeline contains no processors/);
expect(buildRequest).withArgs([{}], { processors: [] }).to.throwException(/pipeline contains no processors/);
});

it('populates the docs._source section', function () {

function buildSamplePipeline(input) {
return {
processors: [ { typeId: 'simple1', value: 'foo' } ],
input: input
};
}

function buildExpected(input) {
return {
'pipeline' : {
'processors': [ { modified_value: 'modified_foo' } ]
},
'docs' : [
{ '_source': input }
]
};
}

let expected;
let actual;

expected = buildExpected(undefined);
actual = buildRequest(processorTypes, buildSamplePipeline(undefined));
expect(actual).to.eql(expected);

expected = buildExpected('foo');
actual = buildRequest(processorTypes, buildSamplePipeline('foo'));
expect(actual).to.eql(expected);

expected = buildExpected({ foo: 'bar' });
actual = buildRequest(processorTypes, buildSamplePipeline({ foo: 'bar' }));
expect(actual).to.eql(expected);
});

describe('populates the pipeline.processors section with type.getDefinition()', function () {

it(' - single processor type', function () {
const pipeline = {
processors: [ { typeId: 'simple1', value: 'foo' } ],
input: {}
};
const expected = {
'pipeline' : {
'processors': [ { modified_value: 'modified_foo' } ]
},
'docs' : [
{ '_source': {} }
]
};

const actual = buildRequest(processorTypes, pipeline);

expect(actual).to.eql(expected);
});

it(' - multiple of same type of processor type', function () {
const pipeline = {
processors: [
{ typeId: 'simple1', value: 'foo' },
{ typeId: 'simple1', value: 'bar' },
{ typeId: 'simple1', value: 'baz' }
],
input: {}
};
const expected = {
'pipeline' : {
'processors': [
{ modified_value: 'modified_foo' },
{ modified_value: 'modified_bar' },
{ modified_value: 'modified_baz' }
]
},
'docs' : [
{ '_source': {} }
]
};

const actual = buildRequest(processorTypes, pipeline);

expect(actual).to.eql(expected);
});

it(' - multiple processor types', function () {
const pipeline = {
processors: [
{ typeId: 'simple1', value: 'foo' },
{ typeId: 'simple2', value: 'bar' },
{ typeId: 'simple1', value: 'baz' }
],
input: {}
};
const expected = {
'pipeline' : {
'processors': [
{ modified_value: 'modified_foo' },
{ value1: 'bar', value2: 'simple2-bar' },
{ modified_value: 'modified_baz' }
]
},
'docs' : [
{ '_source': {} }
]
};

const actual = buildRequest(processorTypes, pipeline);

expect(actual).to.eql(expected);
});

});

});
Loading

0 comments on commit d209809

Please sign in to comment.