Skip to content

Commit

Permalink
add more unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tianle Huang <[email protected]>
  • Loading branch information
tianleh committed Apr 10, 2024
1 parent c00bb41 commit 43ab37e
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 17 deletions.
106 changes: 92 additions & 14 deletions src/plugins/application_config/server/opensearch_config_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

const value = await client.getConfig();

Expand Down Expand Up @@ -77,7 +79,10 @@ describe('OpenSearch Configuration Client', () => {
}),
},
};
const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);

const cache = {};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

await expect(client.getConfig()).rejects.toThrowError(ERROR_MESSAGE);
});
Expand All @@ -99,11 +104,45 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {
has: jest.fn().mockReturnValue(false),
set: jest.fn(),
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

const value = await client.getEntityConfig('config1');

expect(value).toBe('value1');
expect(cache.set).toBeCalledWith('config1', 'value1');
});

it('return configuration value from cache', async () => {
const opensearchClient = {
asInternalUser: {
get: jest.fn().mockImplementation(() => {
return {
body: {
_source: {
value: 'value1',
},
},
};
}),
},
};

const cache = {
has: jest.fn().mockReturnValue(true),
get: jest.fn().mockReturnValue('cachedValue'),
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

const value = await client.getEntityConfig('config1');

expect(value).toBe('cachedValue');
expect(cache.get).toBeCalledWith('config1');
});

it('throws error when input is empty', async () => {
Expand All @@ -121,7 +160,9 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

await expect(client.getEntityConfig(EMPTY_INPUT)).rejects.toThrowError(
ERROR_MESSSAGE_FOR_EMPTY_INPUT
Expand Down Expand Up @@ -151,9 +192,16 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {
has: jest.fn().mockReturnValue(false),
set: jest.fn(),
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

await expect(client.getEntityConfig('config1')).rejects.toThrowError(ERROR_MESSAGE);

expect(cache.set).toBeCalledWith('config1', undefined);
});
});

Expand All @@ -167,11 +215,16 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {
del: jest.fn(),
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

const value = await client.deleteEntityConfig('config1');

expect(value).toBe('config1');
expect(cache.del).toBeCalledWith('config1');
});

it('throws error when input entity is empty', async () => {
Expand All @@ -183,7 +236,9 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

await expect(client.deleteEntityConfig(EMPTY_INPUT)).rejects.toThrowError(
ERROR_MESSSAGE_FOR_EMPTY_INPUT
Expand Down Expand Up @@ -213,11 +268,16 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {
del: jest.fn(),
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

const value = await client.deleteEntityConfig('config1');

expect(value).toBe('config1');
expect(cache.del).toBeCalledWith('config1');
});

it('return deleted document entity when deletion fails due to document not found', async () => {
Expand All @@ -241,11 +301,16 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {
del: jest.fn(),
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

const value = await client.deleteEntityConfig('config1');

expect(value).toBe('config1');
expect(cache.del).toBeCalledWith('config1');
});

it('throws error when opensearch throws error', async () => {
Expand All @@ -271,7 +336,9 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

await expect(client.deleteEntityConfig('config1')).rejects.toThrowError(ERROR_MESSAGE);
});
Expand All @@ -287,11 +354,16 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {
set: jest.fn(),
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

const value = await client.updateEntityConfig('config1', 'newValue1');

expect(value).toBe('newValue1');
expect(cache.set).toBeCalledWith('config1', 'newValue1');
});

it('throws error when entity is empty ', async () => {
Expand All @@ -303,7 +375,9 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

await expect(client.updateEntityConfig(EMPTY_INPUT, 'newValue1')).rejects.toThrowError(
ERROR_MESSSAGE_FOR_EMPTY_INPUT
Expand All @@ -319,7 +393,9 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

await expect(client.updateEntityConfig('config1', EMPTY_INPUT)).rejects.toThrowError(
ERROR_MESSSAGE_FOR_EMPTY_INPUT
Expand Down Expand Up @@ -349,7 +425,9 @@ describe('OpenSearch Configuration Client', () => {
},
};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger);
const cache = {};

const client = new OpenSearchConfigurationClient(opensearchClient, INDEX_NAME, logger, cache);

await expect(client.updateEntityConfig('config1', 'newValue1')).rejects.toThrowError(
ERROR_MESSAGE
Expand Down
108 changes: 105 additions & 3 deletions src/plugins/application_config/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import { of } from 'rxjs';
import { ApplicationConfigPlugin } from './plugin';
import { ConfigurationClient } from './types';
import LRUCache from 'lru-cache';
import { OpenSearchConfigurationClient } from './opensearch_config_client';

jest.mock('lru-cache');
jest.mock('./opensearch_config_client');

describe('application config plugin', () => {
it('throws error when trying to register twice', async () => {
Expand Down Expand Up @@ -54,8 +59,8 @@ describe('application config plugin', () => {

setup.registerConfigurationClient(client1);

const scopedClient = {};
expect(setup.getConfigurationClient(scopedClient)).toBe(client1);
const request = {};
expect(setup.getConfigurationClient(request)).toBe(client1);

const client2: ConfigurationClient = {
getConfig: jest.fn(),
Expand All @@ -71,6 +76,103 @@ describe('application config plugin', () => {
'Configuration client is already registered! Cannot register again!'
);

expect(setup.getConfigurationClient(scopedClient)).toBe(client1);
expect(setup.getConfigurationClient(request)).toBe(client1);
});

it('getConfigurationClient returns opensearch client when no external registration', async () => {
let capturedLRUCacheConstructorArgs = [];

const cache = {
get: jest.fn(),
};

LRUCache.mockImplementation(function (...args) {
capturedLRUCacheConstructorArgs = args;
return cache;
});

let capturedConfigurationClientConstructorArgs = [];

const client: ConfigurationClient = {
getConfig: jest.fn(),
getEntityConfig: jest.fn(),
updateEntityConfig: jest.fn(),
deleteEntityConfig: jest.fn(),
};

OpenSearchConfigurationClient.mockImplementation(function (...args) {
capturedConfigurationClientConstructorArgs = args;
return client;
});

const logger = {
info: jest.fn(),
error: jest.fn(),
};

const initializerContext = {
logger: {
get: jest.fn().mockReturnValue(logger),
},
config: {
legacy: {
globalConfig$: of({
opensearchDashboards: {
configIndex: '.osd_test',
},
}),
},
},
};

const plugin = new ApplicationConfigPlugin(initializerContext);

const coreSetup = {
http: {
createRouter: jest.fn().mockImplementation(() => {
return {
get: jest.fn(),
post: jest.fn(),
delete: jest.fn(),
};
}),
},
};

const setup = await plugin.setup(coreSetup);

const scopedClient = {
asCurrentUser: jest.fn(),
};

const coreStart = {
opensearch: {
client: {
asScoped: jest.fn().mockReturnValue(scopedClient),
},
},
};

await plugin.start(coreStart);

const request = {};

expect(setup.getConfigurationClient(request)).toBe(client);

expect(capturedLRUCacheConstructorArgs).toEqual([
{
max: 100,
maxAge: 600000,
},
]);

expect(capturedConfigurationClientConstructorArgs).toEqual([
scopedClient,
'.osd_test',
logger,
cache,
]);

expect(coreStart.opensearch.client.asScoped).toBeCalledTimes(1);
});
});
Loading

0 comments on commit 43ab37e

Please sign in to comment.