mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
feat(Splunk Node): Overhaul (#9813)
This commit is contained in:
37
packages/nodes-base/nodes/Splunk/test/v2/node/alert.test.ts
Normal file
37
packages/nodes-base/nodes/Splunk/test/v2/node/alert.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type { IExecuteFunctions } from 'n8n-workflow';
|
||||
import * as alert from '../../../v2/actions/alert';
|
||||
import * as transport from '../../../v2/transport';
|
||||
|
||||
jest.mock('../../../v2/transport', () => ({
|
||||
splunkApiJsonRequest: jest.fn(),
|
||||
}));
|
||||
|
||||
describe('Splunk, alert resource', () => {
|
||||
const response = [{ id: '123' }, { id: '345' }];
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
test('getMetrics operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
(transport.splunkApiJsonRequest as jest.Mock).mockReturnValue(response);
|
||||
const responseData = await alert.getMetrics.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiJsonRequest).toHaveBeenCalledWith(
|
||||
'GET',
|
||||
'/services/alerts/metric_alerts',
|
||||
);
|
||||
expect(responseData).toEqual(response);
|
||||
});
|
||||
|
||||
test('getReport operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
(transport.splunkApiJsonRequest as jest.Mock).mockReturnValue(response);
|
||||
const responseData = await alert.getReport.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiJsonRequest).toHaveBeenCalledWith(
|
||||
'GET',
|
||||
'/services/alerts/fired_alerts',
|
||||
);
|
||||
expect(responseData).toEqual(response);
|
||||
});
|
||||
});
|
||||
103
packages/nodes-base/nodes/Splunk/test/v2/node/report.test.ts
Normal file
103
packages/nodes-base/nodes/Splunk/test/v2/node/report.test.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type { IExecuteFunctions } from 'n8n-workflow';
|
||||
import * as report from '../../../v2/actions/report';
|
||||
import * as transport from '../../../v2/transport';
|
||||
import { SPLUNK } from '../../../v1/types';
|
||||
|
||||
jest.mock('../../../v2/transport', () => ({
|
||||
splunkApiJsonRequest: jest.fn(),
|
||||
splunkApiRequest: jest.fn(),
|
||||
}));
|
||||
|
||||
describe('Splunk, report resource', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
test('create operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.calledWith('name', 0).mockReturnValue('someName');
|
||||
executeFunctions.getNodeParameter.calledWith('searchJobId', 0).mockReturnValue('12345');
|
||||
|
||||
(transport.splunkApiJsonRequest as jest.Mock).mockReturnValue([
|
||||
{
|
||||
id: '12345',
|
||||
cronSchedule: '*/5 * * * *',
|
||||
earliestTime: '2020-01-01T00:00:00.000Z',
|
||||
latestTime: '2020-01-01T00:05:00.000Z',
|
||||
isScheduled: true,
|
||||
search: 'search index=_internal | stats count by source',
|
||||
name: 'someName',
|
||||
},
|
||||
]);
|
||||
|
||||
const response = {
|
||||
feed: {
|
||||
entry: [
|
||||
{
|
||||
id: '1',
|
||||
content: { [SPLUNK.DICT]: { [SPLUNK.KEY]: [{ $: { name: 'key1' }, _: 'value1' }] } },
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
(transport.splunkApiRequest as jest.Mock).mockReturnValue(Promise.resolve(response));
|
||||
|
||||
const responseData = await report.create.execute.call(executeFunctions, 0);
|
||||
|
||||
expect(transport.splunkApiJsonRequest).toHaveBeenCalledWith(
|
||||
'GET',
|
||||
'/services/search/jobs/12345',
|
||||
);
|
||||
expect(transport.splunkApiRequest).toHaveBeenCalledWith('POST', '/services/saved/searches', {
|
||||
alert_type: 'always',
|
||||
cron_schedule: '*/5 * * * *',
|
||||
'dispatch.earliest_time': '2020-01-01T00:00:00.000Z',
|
||||
'dispatch.latest_time': '2020-01-01T00:05:00.000Z',
|
||||
is_scheduled: true,
|
||||
name: 'someName',
|
||||
search: 'search index=_internal | stats count by source',
|
||||
});
|
||||
expect(responseData).toEqual([{ entryUrl: '1', id: '1', key1: 'value1' }]);
|
||||
});
|
||||
|
||||
test('deleteReport operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.mockReturnValue('12345');
|
||||
(transport.splunkApiRequest as jest.Mock).mockReturnValue({});
|
||||
const responseData = await report.deleteReport.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiRequest).toHaveBeenCalledWith(
|
||||
'DELETE',
|
||||
'/services/saved/searches/12345',
|
||||
);
|
||||
expect(responseData).toEqual({ success: true });
|
||||
});
|
||||
|
||||
test('get operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.calledWith('reportId', 0).mockReturnValue('12345');
|
||||
|
||||
(transport.splunkApiJsonRequest as jest.Mock).mockReturnValue([{ test: 'test' }]);
|
||||
const responseData = await report.get.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiJsonRequest).toHaveBeenCalledWith(
|
||||
'GET',
|
||||
'/services/saved/searches/12345',
|
||||
);
|
||||
expect(responseData).toEqual([{ test: 'test' }]);
|
||||
});
|
||||
|
||||
test('getAll operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.calledWith('options', 0).mockReturnValue({});
|
||||
executeFunctions.getNodeParameter.calledWith('returnAll', 0).mockReturnValue(true);
|
||||
|
||||
(transport.splunkApiJsonRequest as jest.Mock).mockReturnValue([{ test: 'test' }]);
|
||||
const responseData = await report.getAll.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiJsonRequest).toHaveBeenCalledWith(
|
||||
'GET',
|
||||
'/services/saved/searches',
|
||||
{},
|
||||
{ count: 0 },
|
||||
);
|
||||
expect(responseData).toEqual([{ test: 'test' }]);
|
||||
});
|
||||
});
|
||||
104
packages/nodes-base/nodes/Splunk/test/v2/node/search.test.ts
Normal file
104
packages/nodes-base/nodes/Splunk/test/v2/node/search.test.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type { IExecuteFunctions } from 'n8n-workflow';
|
||||
import * as search from '../../../v2/actions/search';
|
||||
import * as transport from '../../../v2/transport';
|
||||
|
||||
jest.mock('../../../v2/transport', () => ({
|
||||
splunkApiJsonRequest: jest.fn(),
|
||||
splunkApiRequest: jest.fn(),
|
||||
}));
|
||||
describe('Splunk, search resource', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
test('create operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter
|
||||
.calledWith('search', 0)
|
||||
.mockReturnValue('search index=_internal | stats count by source');
|
||||
executeFunctions.getNodeParameter.calledWith('additionalFields', 0).mockReturnValue({
|
||||
earliest_time: '2020-01-01T00:00:00.000Z',
|
||||
latest_time: '2020-01-01T00:05:00.000Z',
|
||||
index_earliest: '2020-01-01T00:00:00.000Z',
|
||||
index_latest: '2020-01-01T00:05:00.000Z',
|
||||
});
|
||||
(transport.splunkApiRequest as jest.Mock).mockReturnValue({ response: { sid: '12345' } });
|
||||
(transport.splunkApiJsonRequest as jest.Mock).mockReturnValue([{ test: 'test' }]);
|
||||
const responseData = await search.create.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiRequest).toHaveBeenCalledWith('POST', '/services/search/jobs', {
|
||||
earliest_time: 1577836800,
|
||||
index_earliest: 1577836800,
|
||||
index_latest: 1577837100,
|
||||
latest_time: 1577837100,
|
||||
search: 'search index=_internal | stats count by source',
|
||||
});
|
||||
expect(transport.splunkApiJsonRequest).toHaveBeenCalledWith(
|
||||
'GET',
|
||||
'/services/search/jobs/12345',
|
||||
);
|
||||
expect(responseData).toEqual([{ test: 'test' }]);
|
||||
});
|
||||
|
||||
test('deleteJob operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.mockReturnValue('12345');
|
||||
(transport.splunkApiRequest as jest.Mock).mockReturnValue({});
|
||||
const responseData = await search.deleteJob.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiRequest).toHaveBeenCalledWith(
|
||||
'DELETE',
|
||||
'/services/search/jobs/12345',
|
||||
);
|
||||
expect(responseData).toEqual({ success: true });
|
||||
});
|
||||
|
||||
test('get operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.calledWith('searchJobId', 0).mockReturnValue('12345');
|
||||
|
||||
(transport.splunkApiJsonRequest as jest.Mock).mockReturnValue([{ test: 'test' }]);
|
||||
const responseData = await search.get.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiJsonRequest).toHaveBeenCalledWith(
|
||||
'GET',
|
||||
'/services/search/jobs/12345',
|
||||
);
|
||||
expect(responseData).toEqual([{ test: 'test' }]);
|
||||
});
|
||||
|
||||
test('getAll operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.calledWith('sort.values', 0).mockReturnValue({});
|
||||
executeFunctions.getNodeParameter.calledWith('returnAll', 0).mockReturnValue(true);
|
||||
|
||||
(transport.splunkApiJsonRequest as jest.Mock).mockReturnValue([{ test: 'test' }]);
|
||||
const responseData = await search.getAll.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiJsonRequest).toHaveBeenCalledWith(
|
||||
'GET',
|
||||
'/services/search/jobs',
|
||||
{},
|
||||
{ count: 0 },
|
||||
);
|
||||
expect(responseData).toEqual([{ test: 'test' }]);
|
||||
});
|
||||
|
||||
test('getResult operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.calledWith('searchJobId', 0).mockReturnValue('12345');
|
||||
executeFunctions.getNodeParameter.calledWith('filters', 0).mockReturnValue({
|
||||
keyValueMatch: { keyValuePair: { key: 'key1', value: 'test1' } },
|
||||
});
|
||||
executeFunctions.getNodeParameter.calledWith('returnAll', 0).mockReturnValue(false);
|
||||
executeFunctions.getNodeParameter.calledWith('limit', 0).mockReturnValue(10);
|
||||
executeFunctions.getNodeParameter.calledWith('options', 0).mockReturnValue({});
|
||||
|
||||
(transport.splunkApiJsonRequest as jest.Mock).mockReturnValue([{ test: 'test' }]);
|
||||
const responseData = await search.getResult.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiJsonRequest).toHaveBeenCalledWith(
|
||||
'GET',
|
||||
'/services/search/jobs/12345/results',
|
||||
{},
|
||||
{ count: 10, search: 'search key1=test1' },
|
||||
);
|
||||
expect(responseData).toEqual([{ test: 'test' }]);
|
||||
});
|
||||
});
|
||||
125
packages/nodes-base/nodes/Splunk/test/v2/node/user.test.ts
Normal file
125
packages/nodes-base/nodes/Splunk/test/v2/node/user.test.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type { IExecuteFunctions } from 'n8n-workflow';
|
||||
import * as user from '../../../v2/actions/user';
|
||||
import * as transport from '../../../v2/transport';
|
||||
import { SPLUNK } from '../../../v1/types';
|
||||
|
||||
jest.mock('../../../v2/transport', () => ({
|
||||
splunkApiJsonRequest: jest.fn(),
|
||||
splunkApiRequest: jest.fn(),
|
||||
}));
|
||||
describe('Splunk, user resource', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
test('create operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.calledWith('roles', 0).mockReturnValue(['role1', 'role2']);
|
||||
executeFunctions.getNodeParameter.calledWith('name', 0).mockReturnValue('John Doe');
|
||||
executeFunctions.getNodeParameter.calledWith('password', 0).mockReturnValue('password');
|
||||
executeFunctions.getNodeParameter.calledWith('additionalFields', 0).mockReturnValue({});
|
||||
|
||||
(transport.splunkApiRequest as jest.Mock).mockReturnValue({
|
||||
feed: {
|
||||
entry: [
|
||||
{
|
||||
id: '1',
|
||||
content: { [SPLUNK.DICT]: { [SPLUNK.KEY]: [{ $: { name: 'test' }, _: 'test1' }] } },
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const responseData = await user.create.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/services/authentication/users',
|
||||
{ name: 'John Doe', password: 'password', roles: ['role1', 'role2'] },
|
||||
);
|
||||
|
||||
expect(responseData).toEqual([
|
||||
{
|
||||
id: '1',
|
||||
test: 'test1',
|
||||
entryUrl: '1',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('deleteUser operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.mockReturnValue('12345');
|
||||
(transport.splunkApiRequest as jest.Mock).mockReturnValue({});
|
||||
const responseData = await user.deleteUser.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiRequest).toHaveBeenCalledWith(
|
||||
'DELETE',
|
||||
'/services/authentication/users/12345',
|
||||
);
|
||||
expect(responseData).toEqual({ success: true });
|
||||
});
|
||||
|
||||
test('get operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.calledWith('userId', 0).mockReturnValue('12345');
|
||||
|
||||
(transport.splunkApiJsonRequest as jest.Mock).mockReturnValue([{ test: 'test' }]);
|
||||
const responseData = await user.get.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiJsonRequest).toHaveBeenCalledWith(
|
||||
'GET',
|
||||
'/services/authentication/users/12345',
|
||||
);
|
||||
expect(responseData).toEqual([{ test: 'test' }]);
|
||||
});
|
||||
|
||||
test('getAll operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter.calledWith('returnAll', 0).mockReturnValue(true);
|
||||
|
||||
(transport.splunkApiJsonRequest as jest.Mock).mockReturnValue([{ test: 'test' }]);
|
||||
const responseData = await user.getAll.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiJsonRequest).toHaveBeenCalledWith(
|
||||
'GET',
|
||||
'/services/authentication/users',
|
||||
{},
|
||||
{ count: 0 },
|
||||
);
|
||||
expect(responseData).toEqual([{ test: 'test' }]);
|
||||
});
|
||||
|
||||
test('update operation', async () => {
|
||||
const executeFunctions = mock<IExecuteFunctions>();
|
||||
executeFunctions.getNodeParameter
|
||||
.calledWith('updateFields', 0)
|
||||
.mockReturnValue({ roles: ['role1', 'role2'], email: 'testW@example.com' });
|
||||
executeFunctions.getNodeParameter.calledWith('userId', 0).mockReturnValue('12345');
|
||||
|
||||
(transport.splunkApiRequest as jest.Mock).mockReturnValue(
|
||||
Promise.resolve({
|
||||
feed: {
|
||||
entry: [
|
||||
{
|
||||
id: '1',
|
||||
content: { [SPLUNK.DICT]: { [SPLUNK.KEY]: [{ $: { name: 'test' }, _: 'test1' }] } },
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
const responseData = await user.update.execute.call(executeFunctions, 0);
|
||||
expect(transport.splunkApiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/services/authentication/users/12345',
|
||||
{ email: 'testW@example.com', roles: ['role1', 'role2'] },
|
||||
);
|
||||
|
||||
expect(responseData).toEqual([
|
||||
{
|
||||
id: '1',
|
||||
test: 'test1',
|
||||
entryUrl: '1',
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
267
packages/nodes-base/nodes/Splunk/test/v2/utils.test.ts
Normal file
267
packages/nodes-base/nodes/Splunk/test/v2/utils.test.ts
Normal file
@@ -0,0 +1,267 @@
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type { IDataObject, IExecuteFunctions } from 'n8n-workflow';
|
||||
import {
|
||||
formatEntry,
|
||||
extractErrorDescription,
|
||||
getId,
|
||||
populate,
|
||||
formatFeed,
|
||||
setReturnAllOrLimit,
|
||||
parseXml,
|
||||
} from '../../v2/helpers/utils';
|
||||
import { SPLUNK } from '../../v1/types';
|
||||
|
||||
describe('Splunk, formatEntry', () => {
|
||||
test('should format the entry correctly when doNotFormatContent is false', () => {
|
||||
const entry = {
|
||||
id: 'http://example.com/id/123',
|
||||
content: {
|
||||
[SPLUNK.DICT]: {
|
||||
[SPLUNK.KEY]: [
|
||||
{ $: { name: 'key1' }, _: 'value1' },
|
||||
{ $: { name: 'key2' }, _: 'value2' },
|
||||
],
|
||||
},
|
||||
},
|
||||
link: 'http://example.com/link',
|
||||
otherField: 'otherValue',
|
||||
};
|
||||
|
||||
const expectedFormattedEntry = {
|
||||
otherField: 'otherValue',
|
||||
key1: 'value1',
|
||||
key2: 'value2',
|
||||
entryUrl: 'http://example.com/id/123',
|
||||
id: '123',
|
||||
};
|
||||
|
||||
const result = formatEntry(entry);
|
||||
expect(result).toEqual(expectedFormattedEntry);
|
||||
});
|
||||
|
||||
test('should format the entry correctly when doNotFormatContent is true', () => {
|
||||
const entry = {
|
||||
id: 'http://example.com/id/123',
|
||||
key1: 'value1',
|
||||
key2: 'value2',
|
||||
};
|
||||
|
||||
const expectedFormattedEntry = {
|
||||
key1: 'value1',
|
||||
key2: 'value2',
|
||||
entryUrl: 'http://example.com/id/123',
|
||||
id: '123',
|
||||
};
|
||||
|
||||
const result = formatEntry(entry, true);
|
||||
expect(result).toEqual(expectedFormattedEntry);
|
||||
});
|
||||
|
||||
test('should handle entries without id correctly', () => {
|
||||
const entry = {
|
||||
content: {
|
||||
[SPLUNK.DICT]: {
|
||||
[SPLUNK.KEY]: [
|
||||
{ $: { name: 'key1' }, _: 'value1' },
|
||||
{ $: { name: 'key2' }, _: 'value2' },
|
||||
],
|
||||
},
|
||||
},
|
||||
otherField: 'otherValue',
|
||||
};
|
||||
|
||||
const expectedFormattedEntry = {
|
||||
otherField: 'otherValue',
|
||||
key1: 'value1',
|
||||
key2: 'value2',
|
||||
};
|
||||
|
||||
const result = formatEntry(entry);
|
||||
expect(result).toEqual(expectedFormattedEntry);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Splunk, extractErrorDescription', () => {
|
||||
test('should extract the error description correctly when messages are present', () => {
|
||||
const rawError = {
|
||||
response: {
|
||||
messages: {
|
||||
msg: {
|
||||
$: { type: 'ERROR' },
|
||||
_: 'This is an error message',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const expectedErrorDescription = {
|
||||
error: 'This is an error message',
|
||||
};
|
||||
|
||||
const result = extractErrorDescription(rawError);
|
||||
expect(result).toEqual(expectedErrorDescription);
|
||||
});
|
||||
|
||||
test('should return the raw error when messages are not present', () => {
|
||||
const rawError = { response: {} };
|
||||
|
||||
const result = extractErrorDescription(rawError);
|
||||
expect(result).toEqual(rawError);
|
||||
});
|
||||
|
||||
test('should return the raw error when response is not present', () => {
|
||||
const rawError = {};
|
||||
|
||||
const result = extractErrorDescription(rawError);
|
||||
expect(result).toEqual(rawError);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Splunk, getId', () => {
|
||||
test('should return id extracted from the id parameter if it is url', () => {
|
||||
const executeFunctionsMock = mock<IExecuteFunctions>();
|
||||
const endpoint = 'http://example.com/endpoint/admin';
|
||||
|
||||
executeFunctionsMock.getNodeParameter.mockReturnValueOnce(endpoint);
|
||||
const id = getId.call(executeFunctionsMock, 0, 'userId', 'http://example.com/endpoint/');
|
||||
|
||||
expect(id).toBe('admin');
|
||||
});
|
||||
|
||||
test('should return the unchanged id', () => {
|
||||
const executeFunctionsMock = mock<IExecuteFunctions>();
|
||||
|
||||
executeFunctionsMock.getNodeParameter.mockReturnValueOnce('123');
|
||||
const id = getId.call(executeFunctionsMock, 0, 'searchConfigurationId', 'endpoint');
|
||||
|
||||
expect(id).toBe('123');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Splunk, populate', () => {
|
||||
test('should populate destination object with source object properties', () => {
|
||||
const source = {
|
||||
key1: 'value1',
|
||||
key2: 'value2',
|
||||
};
|
||||
|
||||
const destination = {
|
||||
existingKey: 'existingValue',
|
||||
};
|
||||
|
||||
populate(source, destination);
|
||||
|
||||
expect(destination).toEqual({
|
||||
existingKey: 'existingValue',
|
||||
key1: 'value1',
|
||||
key2: 'value2',
|
||||
});
|
||||
});
|
||||
|
||||
test('should not modify destination object if source object is empty', () => {
|
||||
const source = {};
|
||||
|
||||
const destination = {
|
||||
existingKey: 'existingValue',
|
||||
};
|
||||
|
||||
populate(source, destination);
|
||||
|
||||
expect(destination).toEqual({
|
||||
existingKey: 'existingValue',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Splunk, formatFeed', () => {
|
||||
test('should return an empty array when feed entries are not present', () => {
|
||||
const responseData = {
|
||||
feed: {
|
||||
entry: [],
|
||||
},
|
||||
};
|
||||
|
||||
const result = formatFeed(responseData);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
test('should format feed entries correctly when entries are an array', () => {
|
||||
const responseData = {
|
||||
feed: {
|
||||
entry: [
|
||||
{
|
||||
id: '1',
|
||||
content: { [SPLUNK.DICT]: { [SPLUNK.KEY]: [{ $: { name: 'key1' }, _: 'value1' }] } },
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
content: { [SPLUNK.DICT]: { [SPLUNK.KEY]: [{ $: { name: 'key2' }, _: 'value2' }] } },
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const expectedFormattedEntries = [
|
||||
{ id: '1', key1: 'value1', entryUrl: '1' },
|
||||
{ id: '2', key2: 'value2', entryUrl: '2' },
|
||||
];
|
||||
|
||||
const result = formatFeed(responseData);
|
||||
expect(result).toEqual(expectedFormattedEntries);
|
||||
});
|
||||
|
||||
test('should format feed entry correctly when entry is a single object', () => {
|
||||
const responseData = {
|
||||
feed: {
|
||||
entry: {
|
||||
id: '1',
|
||||
content: { [SPLUNK.DICT]: { [SPLUNK.KEY]: [{ $: { name: 'key1' }, _: 'value1' }] } },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const expectedFormattedEntries = [{ id: '1', key1: 'value1', entryUrl: '1' }];
|
||||
|
||||
const result = formatFeed(responseData);
|
||||
expect(result).toEqual(expectedFormattedEntries);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Splunk, setCount', () => {
|
||||
test('should set count to 0 if returnAll', () => {
|
||||
const executeFunctionsMock = mock<IExecuteFunctions>();
|
||||
const qs: IDataObject = {};
|
||||
executeFunctionsMock.getNodeParameter.calledWith('returnAll', 0).mockReturnValue(true);
|
||||
|
||||
setReturnAllOrLimit.call(executeFunctionsMock, qs);
|
||||
|
||||
expect(qs.count).toBe(0);
|
||||
});
|
||||
|
||||
test('should set count to limit if returnAll is false', () => {
|
||||
const executeFunctionsMock = mock<IExecuteFunctions>();
|
||||
const qs: IDataObject = {};
|
||||
executeFunctionsMock.getNodeParameter.calledWith('returnAll', 0).mockReturnValue(false);
|
||||
executeFunctionsMock.getNodeParameter.calledWith('limit', 0).mockReturnValue(10);
|
||||
|
||||
setReturnAllOrLimit.call(executeFunctionsMock, qs);
|
||||
|
||||
expect(qs.count).toBe(10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Splunk, parseXml', () => {
|
||||
test('should parse valid XML string correctly', async () => {
|
||||
const xmlString = '<root><name>John</name><age>30</age></root>';
|
||||
|
||||
const result = await parseXml(xmlString);
|
||||
|
||||
expect(result).toEqual({ root: { name: 'John', age: '30' } });
|
||||
});
|
||||
|
||||
test('should throw an error if XML string is invalid', async () => {
|
||||
const xmlString = '<invalid-xml>';
|
||||
|
||||
await expect(parseXml(xmlString)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user