Skip to content

Commit

Permalink
Apply prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Machi3mfl committed Dec 20, 2024
1 parent f8e7b16 commit 2382b23
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 279 deletions.
2 changes: 1 addition & 1 deletion plugins/main/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class WazuhPlugin implements Plugin<WazuhPluginSetup, WazuhPluginStart> {
server: contextServer,
});
*/

// Migration tasks
jobMigrationTasksRun({
core,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface IConfigurationProvider {
get(key: string): Promise<any>;
set?(key: string, value: any): Promise<void>;
getAll(): Promise<any>;
setName(name: string): void;
getName(): string;
}
get(key: string): Promise<any>;
set?(key: string, value: any): Promise<void>;
getAll(): Promise<any>;
setName(name: string): void;
getName(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ function createMockLogger() {
const noop = () => {};

const logger = {
info: noop,
error: noop,
debug: noop,
warn: noop,
trace: noop,
fatal: noop,
log: noop,
get: () => logger,
};
info: noop,
error: noop,
debug: noop,
warn: noop,
trace: noop,
fatal: noop,
log: noop,
get: () => logger,
};
return logger;
}

Expand All @@ -23,7 +23,7 @@ const mockedProvider = jest.mocked<IConfigurationProvider>({
set: jest.fn(),
setName: jest.fn(),
getName: jest.fn(),
})
});

beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -46,42 +46,49 @@ describe(`[service] ConfigurationStore`, () => {
const configurationStore = new ConfigurationStore(logger);
configurationStore.registerProvider('test', mockedProvider);
expect(mockedProvider.setName).toBeCalledWith('test');
})
});

it('should return error if provider is not defined when registering', () => {
const logger = createMockLogger();
const configurationStore = new ConfigurationStore(logger);
// @ts-ignore
expect(() => configurationStore.registerProvider('test', null)).toThrowError('Provider is required');
})

expect(() =>
configurationStore.registerProvider('test', null),
).toThrowError('Provider is required');
});

it('should return a configuration from a provider', () => {
const logger = createMockLogger();
const configurationStore = new ConfigurationStore(logger);
configurationStore.registerProvider('test', mockedProvider);
(mockedProvider.getAll as jest.Mock).mockResolvedValue({ test: 'test' });
expect(configurationStore.getProviderConfiguration('test')).resolves.toEqual({ test: 'test' });
})
expect(
configurationStore.getProviderConfiguration('test'),
).resolves.toEqual({ test: 'test' });
});

it('should return error if provider is not defined when getting configuration', () => {
const logger = createMockLogger();
const configurationStore = new ConfigurationStore(logger);
expect(configurationStore.getProviderConfiguration('test')).rejects.toThrowError('Provider test not found');
})
expect(
configurationStore.getProviderConfiguration('test'),
).rejects.toThrowError('Provider test not found');
});

it('should return the provider', () => {
const logger = createMockLogger();
const configurationStore = new ConfigurationStore(logger);
configurationStore.registerProvider('test', mockedProvider);
expect(configurationStore.getProvider('test')).toBe(mockedProvider);
})
});

it('should return error if provider is not defined when getting provider', () => {
const logger = createMockLogger();
const configurationStore = new ConfigurationStore(logger);
expect(() => configurationStore.getProvider('test')).toThrowError('Provider test not found');
})
expect(() => configurationStore.getProvider('test')).toThrowError(
'Provider test not found',
);
});

it('should return a configuration value by key', async () => {
const logger = createMockLogger();
Expand All @@ -90,27 +97,28 @@ describe(`[service] ConfigurationStore`, () => {
(mockedProvider.get as jest.Mock).mockResolvedValue('test');
configurationStore.registerProvider('test', mockedProvider);
expect(await configurationStore.get('test')).toEqual('test');
})
});

it('should return error if the configuration key is not found', async () => {
const logger = createMockLogger();
const configurationStore = new ConfigurationStore(logger);
(mockedProvider.getAll as jest.Mock).mockResolvedValue({ test: 'test' });
configurationStore.registerProvider('test', mockedProvider);
(mockedProvider.get as jest.Mock).mockRejectedValue(new Error('test error'));
try {
await configurationStore.get('test')
(mockedProvider.get as jest.Mock).mockRejectedValue(
new Error('test error'),
);
try {
await configurationStore.get('test');
} catch (error) {
expect(error).toEqual(new Error('test error'));
}
})
});

it('should return all the configuration from the registered providers', () => {
const logger = createMockLogger();
const configurationStore = new ConfigurationStore(logger);
configurationStore.registerProvider('test', mockedProvider);
(mockedProvider.getAll as jest.Mock).mockResolvedValue({ test: 'test' });
expect(configurationStore.getAll()).resolves.toEqual({ test: 'test' });
})

});
});
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { Logger } from 'opensearch-dashboards/server';
import {
IConfigurationStore,
} from './configuration';
import { IConfigurationStore } from './configuration';
import { IConfigurationProvider } from './configuration-provider';


export class ConfigurationStore implements IConfigurationStore {
private providers: Map<string, IConfigurationProvider>;
constructor(private logger: Logger) {
this.providers = new Map();
}

registerProvider(name: string, provider: IConfigurationProvider) {
if(!provider){
if (!provider) {
throw new Error('Provider is required');
}

provider.setName(name);
this.providers.set(name, provider);
}
Expand Down Expand Up @@ -61,7 +58,7 @@ export class ConfigurationStore implements IConfigurationStore {
this.logger.debug('Stop');
}
async get(configName: string): Promise<any | { [key: string]: any }> {
// get all the providers and check if the setting is in any of them
// get all the providers and check if the setting is in any of them
const configuration = await this.getAll();
// check if the configName exist in the object
if (Object.keys(configuration).includes(configName)) {
Expand All @@ -70,7 +67,7 @@ export class ConfigurationStore implements IConfigurationStore {
throw new Error(`Configuration ${configName} not found`);
}

async getAll(): Promise<{[key: string]: any}> {
async getAll(): Promise<{ [key: string]: any }> {
const providers = Array.from(this.providers.values());
let configuration = {};
for (const provider of providers) {
Expand All @@ -79,7 +76,7 @@ export class ConfigurationStore implements IConfigurationStore {
configuration = {
...configuration,
...settings,
}
};
} catch (error) {
let err = error as Error;
this.logger.error(
Expand All @@ -98,5 +95,4 @@ export class ConfigurationStore implements IConfigurationStore {
async clear(...settings: string[]): Promise<any> {
throw new Error('Method not implemented yet.');
}

}
}
142 changes: 72 additions & 70 deletions plugins/wazuh-core/common/services/configuration/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { Configuration, IConfigurationStore } from './configuration';
import { ConfigurationStore } from './configuration-store';


function createMockLogger() {
const noop = () => {};

const logger = {
info: noop,
error: noop,
debug: noop,
warn: noop,
trace: noop,
fatal: noop,
log: noop,
get: () => logger,
};
info: noop,
error: noop,
debug: noop,
warn: noop,
trace: noop,
fatal: noop,
log: noop,
get: () => logger,
};
return logger;
}

Expand All @@ -28,73 +27,76 @@ const mockConfigurationStore: IConfigurationStore = {
getProviderConfiguration: jest.fn(),
registerProvider: jest.fn(),
getProvider: jest.fn(),
}
};

describe('Configuration service', () => {
beforeEach(() => {
jest.clearAllMocks();
});

beforeEach(() => {
jest.clearAllMocks();
});
it('should create an instance of Configuration', () => {
const logger = createMockLogger();
const configuration = new Configuration(logger, mockConfigurationStore);
expect(configuration).toBeInstanceOf(Configuration);
});

it('should create an instance of Configuration', () => {
const logger = createMockLogger()
const configuration = new Configuration(logger, mockConfigurationStore);
expect(configuration).toBeInstanceOf(Configuration);
})
it('should set store', () => {
const logger = createMockLogger();
const configuration = new Configuration(logger, mockConfigurationStore);
configuration.setStore(mockConfigurationStore);
expect(configuration.store).toBe(mockConfigurationStore);
});

it('should set store', () => {
const logger = createMockLogger()
const configuration = new Configuration(logger, mockConfigurationStore);
configuration.setStore(mockConfigurationStore);
expect(configuration.store).toBe(mockConfigurationStore);
});
it('should return error if store is not provided', () => {
const logger = createMockLogger();
try {
// @ts-ignore
new Configuration(logger, null);
} catch (error) {
expect(error).toEqual(new Error('Configuration store is required'));
}
});

it('should return error if store is not provided', () => {
const logger = createMockLogger()
try {
// @ts-ignore
new Configuration(logger, null)
}catch(error){
expect(error).toEqual(new Error('Configuration store is required'));
}
})

it('should return a configuration setting value', () => {
const logger = createMockLogger()
const configuration = new Configuration(logger, mockConfigurationStore);
configuration.get('key');
expect(mockConfigurationStore.get).toBeCalledWith('key');
expect(mockConfigurationStore.get).toBeCalledTimes(1);
});
it('should return a configuration setting value', () => {
const logger = createMockLogger();
const configuration = new Configuration(logger, mockConfigurationStore);
configuration.get('key');
expect(mockConfigurationStore.get).toBeCalledWith('key');
expect(mockConfigurationStore.get).toBeCalledTimes(1);
});

it('should return error if the configuration setting key not exists', async () => {
const logger = createMockLogger()
const configuration = new Configuration(logger, mockConfigurationStore);
(mockConfigurationStore.get as jest.Mock).mockRejectedValue(new Error('Configuration setting not found'));
try {
await configuration.get('key');
} catch (error) {
expect(mockConfigurationStore.get).toBeCalledWith('key');
expect(error).toEqual(new Error('Configuration setting not found'));
}
})
it('should return error if the configuration setting key not exists', async () => {
const logger = createMockLogger();
const configuration = new Configuration(logger, mockConfigurationStore);
(mockConfigurationStore.get as jest.Mock).mockRejectedValue(
new Error('Configuration setting not found'),
);
try {
await configuration.get('key');
} catch (error) {
expect(mockConfigurationStore.get).toBeCalledWith('key');
expect(error).toEqual(new Error('Configuration setting not found'));
}
});

it('should return all configuration settings', () => {
const logger = createMockLogger()
const configuration = new Configuration(logger, mockConfigurationStore);
configuration.getAll();
expect(mockConfigurationStore.getAll).toBeCalledTimes(1);
});
it('should return all configuration settings', () => {
const logger = createMockLogger();
const configuration = new Configuration(logger, mockConfigurationStore);
configuration.getAll();
expect(mockConfigurationStore.getAll).toBeCalledTimes(1);
});

it('should return error if the configuration settings not exists', async () => {
const logger = createMockLogger()
const configuration = new Configuration(logger, mockConfigurationStore);
(mockConfigurationStore.getAll as jest.Mock).mockRejectedValue(new Error('Configuration settings not found'));
try {
await configuration.getAll();
} catch (error) {
expect(mockConfigurationStore.getAll).toBeCalledTimes(1);
expect(error).toEqual(new Error('Configuration settings not found'));
}
})
it('should return error if the configuration settings not exists', async () => {
const logger = createMockLogger();
const configuration = new Configuration(logger, mockConfigurationStore);
(mockConfigurationStore.getAll as jest.Mock).mockRejectedValue(
new Error('Configuration settings not found'),
);
try {
await configuration.getAll();
} catch (error) {
expect(mockConfigurationStore.getAll).toBeCalledTimes(1);
expect(error).toEqual(new Error('Configuration settings not found'));
}
});
});
Loading

0 comments on commit 2382b23

Please sign in to comment.