Merge branch 'configurationPackage' into 'main'

replace configuration module by dedicated package

See merge request v3/service/matcher!9
This commit is contained in:
Sylvain Briat 2023-05-30 09:50:25 +00:00
commit 0a6e4c0bf6
21 changed files with 113 additions and 499 deletions

111
package-lock.json generated
View File

@ -16,6 +16,7 @@
"@grpc/grpc-js": "^1.8.13",
"@grpc/proto-loader": "^0.7.6",
"@liaoliaots/nestjs-redis": "^9.0.5",
"@mobicoop/configuration-module": "^1.0.0",
"@nestjs/axios": "^2.0.0",
"@nestjs/cache-manager": "^1.0.0",
"@nestjs/common": "^9.0.0",
@ -65,6 +66,24 @@
"typescript": "^4.7.4"
}
},
"node_modules/@acuminous/bitsyntax": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/@acuminous/bitsyntax/-/bitsyntax-0.1.2.tgz",
"integrity": "sha512-29lUK80d1muEQqiUsSo+3A0yP6CdspgC95EnKBMi22Xlwt79i/En4Vr67+cXhU+cZjbti3TgGGC5wy1stIywVQ==",
"dependencies": {
"buffer-more-ints": "~1.0.0",
"debug": "^4.3.4",
"safe-buffer": "~5.1.2"
},
"engines": {
"node": ">=0.8"
}
},
"node_modules/@acuminous/bitsyntax/node_modules/safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"node_modules/@ampproject/remapping": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.0.tgz",
@ -1578,6 +1597,59 @@
"node": ">=8"
}
},
"node_modules/@mobicoop/configuration-module": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@mobicoop/configuration-module/-/configuration-module-1.0.0.tgz",
"integrity": "sha512-ufpJl3YupHhpcGS26h/kEGSplmJ5C3UcE5UkX5MUHbJzJBATN6FmikU3JgKU9bdN/YxHrxylmnknQVKcr3HT1w==",
"dependencies": {
"@golevelup/nestjs-rabbitmq": "^3.6.0",
"@liaoliaots/nestjs-redis": "^9.0.5",
"@nestjs/config": "^2.3.2",
"@nestjs/cqrs": "^9.0.4",
"@types/amqplib": "^0.10.1",
"amqplib": "^0.10.3",
"class-validator": "^0.14.0",
"ioredis": "^5.3.2"
},
"peerDependencies": {
"@nestjs/common": "^9.4.2"
}
},
"node_modules/@mobicoop/configuration-module/node_modules/amqplib": {
"version": "0.10.3",
"resolved": "https://registry.npmjs.org/amqplib/-/amqplib-0.10.3.tgz",
"integrity": "sha512-UHmuSa7n8vVW/a5HGh2nFPqAEr8+cD4dEZ6u9GjP91nHfr1a54RyAKyra7Sb5NH7NBKOUlyQSMXIp0qAixKexw==",
"dependencies": {
"@acuminous/bitsyntax": "^0.1.2",
"buffer-more-ints": "~1.0.0",
"readable-stream": "1.x >=1.1.9",
"url-parse": "~1.5.10"
},
"engines": {
"node": ">=10"
}
},
"node_modules/@mobicoop/configuration-module/node_modules/isarray": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz",
"integrity": "sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ=="
},
"node_modules/@mobicoop/configuration-module/node_modules/readable-stream": {
"version": "1.1.14",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",
"integrity": "sha512-+MeVjFf4L44XUkhM1eYbD8fyEsxcV81pqMSR5gblfcLCHfZvbrqy4/qYHE+/R5HoBUT11WV5O08Cr1n3YXkWVQ==",
"dependencies": {
"core-util-is": "~1.0.0",
"inherits": "~2.0.1",
"isarray": "0.0.1",
"string_decoder": "~0.10.x"
}
},
"node_modules/@mobicoop/configuration-module/node_modules/string_decoder": {
"version": "0.10.31",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz",
"integrity": "sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ=="
},
"node_modules/@nestjs/axios": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@nestjs/axios/-/axios-2.0.0.tgz",
@ -1707,12 +1779,12 @@
}
},
"node_modules/@nestjs/common": {
"version": "9.4.0",
"resolved": "https://registry.npmjs.org/@nestjs/common/-/common-9.4.0.tgz",
"integrity": "sha512-RUcVAQsEF4WPrmzFXEOUfZnPwrLTe1UVlzXTlSyfqfqbdWDPKDGlIPVelBLfc5/+RRUQ0I5iE4+CQvpCmkqldw==",
"version": "9.4.2",
"resolved": "https://registry.npmjs.org/@nestjs/common/-/common-9.4.2.tgz",
"integrity": "sha512-sea+qZnbD5x3YWZDVQT/wbVJ2NiABaM1tyZTLuW9hpkcM2KFA96xKtK3VaCxyz49zoXIgSOefsyK7HuUMCe27Q==",
"dependencies": {
"iterare": "1.2.1",
"tslib": "2.5.0",
"tslib": "2.5.2",
"uid": "2.0.2"
},
"funding": {
@ -1738,10 +1810,15 @@
}
}
},
"node_modules/@nestjs/common/node_modules/tslib": {
"version": "2.5.2",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.2.tgz",
"integrity": "sha512-5svOrSA2w3iGFDs1HibEVBGbDrAY82bFQ3HZ3ixB+88nsbsWQoKqDRb5UBYAUPEzbBn6dAp5gRNXglySbx1MlA=="
},
"node_modules/@nestjs/config": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/@nestjs/config/-/config-2.3.1.tgz",
"integrity": "sha512-Ckzel0NZ9CWhNsLfE1hxfDuxJuEbhQvGxSlmZ1/X8awjRmAA/g3kT6M1+MO1SHj1wMtPyUfd9WpwkiqFbiwQgA==",
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/@nestjs/config/-/config-2.3.2.tgz",
"integrity": "sha512-VtGV8PBpxzMzz68kdxTWqPm9v7SYCSZXQ0tC72AMNnjdmU+CVjUSLpEpdnm0XcWHxE1nV6wSI3HZxsATIV4ZxA==",
"dependencies": {
"dotenv": "16.0.3",
"dotenv-expand": "10.0.0",
@ -1792,9 +1869,9 @@
}
},
"node_modules/@nestjs/cqrs": {
"version": "9.0.3",
"resolved": "https://registry.npmjs.org/@nestjs/cqrs/-/cqrs-9.0.3.tgz",
"integrity": "sha512-hmbrqf51BVdgmnnxErnLVXfPNTEqr4Hz8DyLa9dKLIW3BuOyI5RDwJ/9sKbJ47UDBhumC5nQlNK9qk27mhqHfw==",
"version": "9.0.4",
"resolved": "https://registry.npmjs.org/@nestjs/cqrs/-/cqrs-9.0.4.tgz",
"integrity": "sha512-nWDF+xs4jqs6OjxFg/wVSd0NiIV9+EFCJrJNTo4VRWe78CcAaitbp56CBspUh4gKyfkci95i+EhHdEqRXKFptg==",
"dependencies": {
"uuid": "9.0.0"
},
@ -2228,6 +2305,14 @@
"url": "https://opencollective.com/turf"
}
},
"node_modules/@types/amqplib": {
"version": "0.10.1",
"resolved": "https://registry.npmjs.org/@types/amqplib/-/amqplib-0.10.1.tgz",
"integrity": "sha512-j6ANKT79ncUDnAs/+9r9eDujxbeJoTjoVu33gHHcaPfmLQaMhvfbH2GqSe8KUM444epAp1Vl3peVOQfZk3UIqA==",
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/babel__core": {
"version": "7.20.0",
"resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.0.tgz",
@ -5547,9 +5632,9 @@
}
},
"node_modules/ioredis": {
"version": "5.3.1",
"resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.3.1.tgz",
"integrity": "sha512-C+IBcMysM6v52pTLItYMeV4Hz7uriGtoJdz7SSBDX6u+zwSYGirLdQh3L7t/OItWITcw3gTFMjJReYUwS4zihg==",
"version": "5.3.2",
"resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.3.2.tgz",
"integrity": "sha512-1DKMMzlIHM02eBBVOFQ1+AolGjs6+xEcM4PDL7NqOS6szq7H9jSaEkIUH6/a5Hl241LzW6JLSiAbNvTQjUupUA==",
"dependencies": {
"@ioredis/commands": "^1.1.1",
"cluster-key-slot": "^1.1.0",

View File

@ -39,6 +39,7 @@
"@grpc/grpc-js": "^1.8.13",
"@grpc/proto-loader": "^0.7.6",
"@liaoliaots/nestjs-redis": "^9.0.5",
"@mobicoop/configuration-module": "^1.0.0",
"@nestjs/axios": "^2.0.0",
"@nestjs/cache-manager": "^1.0.0",
"@nestjs/common": "^9.0.0",

View File

@ -2,16 +2,26 @@ import { classes } from '@automapper/classes';
import { AutomapperModule } from '@automapper/nestjs';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ConfigurationModule } from './modules/configuration/configuration.module';
import { HealthModule } from './modules/health/health.module';
import { MatcherModule } from './modules/matcher/matcher.module';
import { AdModule } from './modules/ad/ad.module';
import { ConfigurationModule } from '@mobicoop/configuration-module';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
ConfigurationModule.forRootAsync({
setConfigurationBrokerRoutingKeys: [
'configuration.create',
'configuration.update',
],
deleteConfigurationRoutingKey: 'configuration.delete',
propagateConfigurationRoutingKey: 'configuration.propagate',
setConfigurationBrokerQueue: 'matcher-configuration-create-update',
deleteConfigurationQueue: 'matcher-configuration-delete',
propagateConfigurationQueue: 'matcher-configuration-propagate',
}),
AutomapperModule.forRoot({ strategyInitializer: classes() }),
ConfigurationModule,
HealthModule,
MatcherModule,
AdModule,

View File

@ -1,6 +1,6 @@
import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq';
import { Controller } from '@nestjs/common';
import { CommandBus } from '@nestjs/cqrs';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
import { CreateAdCommand } from '../../commands/create-ad.command';
import { CreateAdRequest } from '../../domain/dtos/create-ad.request';
import { validateOrReject } from 'class-validator';
@ -14,6 +14,7 @@ export class AdMessagerController {
constructor(
private readonly messager: Messager,
private readonly commandBus: CommandBus,
private readonly queryBus: QueryBus,
) {}
@RabbitSubscribe({

View File

@ -1,77 +0,0 @@
import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq';
import { Controller } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { CommandBus } from '@nestjs/cqrs';
import { DeleteConfigurationCommand } from '../../commands/delete-configuration.command';
import { SetConfigurationCommand } from '../../commands/set-configuration.command';
import { DeleteConfigurationRequest } from '../../domain/dtos/delete-configuration.request';
import { SetConfigurationRequest } from '../../domain/dtos/set-configuration.request';
import { Configuration } from '../../domain/entities/configuration';
@Controller()
export class ConfigurationMessagerController {
constructor(
private readonly commandBus: CommandBus,
private readonly configService: ConfigService,
) {}
@RabbitSubscribe({
name: 'setConfiguration',
})
public async setConfigurationHandler(message: string) {
const configuration: Configuration = JSON.parse(message);
if (
configuration.domain ==
this.configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
) {
const setConfigurationRequest: SetConfigurationRequest =
new SetConfigurationRequest();
setConfigurationRequest.domain = configuration.domain;
setConfigurationRequest.key = configuration.key;
setConfigurationRequest.value = configuration.value;
await this.commandBus.execute(
new SetConfigurationCommand(setConfigurationRequest),
);
}
}
@RabbitSubscribe({
name: 'deleteConfiguration',
})
public async configurationDeletedHandler(message: string) {
const deletedConfiguration: Configuration = JSON.parse(message);
if (
deletedConfiguration.domain ==
this.configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
) {
const deleteConfigurationRequest = new DeleteConfigurationRequest();
deleteConfigurationRequest.domain = deletedConfiguration.domain;
deleteConfigurationRequest.key = deletedConfiguration.key;
await this.commandBus.execute(
new DeleteConfigurationCommand(deleteConfigurationRequest),
);
}
}
@RabbitSubscribe({
name: 'propagateConfiguration',
})
public async propagateConfigurationsHandler(message: string) {
const configurations: Configuration[] = JSON.parse(message);
configurations.forEach(async (configuration) => {
if (
configuration.domain ==
this.configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
) {
const setConfigurationRequest: SetConfigurationRequest =
new SetConfigurationRequest();
setConfigurationRequest.domain = configuration.domain;
setConfigurationRequest.key = configuration.key;
setConfigurationRequest.value = configuration.value;
await this.commandBus.execute(
new SetConfigurationCommand(setConfigurationRequest),
);
}
});
}
}

View File

@ -1,21 +0,0 @@
import { InjectRedis } from '@liaoliaots/nestjs-redis';
import { Injectable } from '@nestjs/common';
import { Redis } from 'ioredis';
import { IConfigurationRepository } from '../../domain/interfaces/configuration.repository';
@Injectable()
export class RedisConfigurationRepository extends IConfigurationRepository {
constructor(@InjectRedis() private readonly redis: Redis) {
super();
}
get = async (key: string): Promise<string> => await this.redis.get(key);
set = async (key: string, value: string): Promise<void> => {
await this.redis.set(key, value);
};
del = async (key: string): Promise<void> => {
await this.redis.del(key);
};
}

View File

@ -1,9 +0,0 @@
import { DeleteConfigurationRequest } from '../domain/dtos/delete-configuration.request';
export class DeleteConfigurationCommand {
readonly deleteConfigurationRequest: DeleteConfigurationRequest;
constructor(deleteConfigurationRequest: DeleteConfigurationRequest) {
this.deleteConfigurationRequest = deleteConfigurationRequest;
}
}

View File

@ -1,9 +0,0 @@
import { SetConfigurationRequest } from '../domain/dtos/set-configuration.request';
export class SetConfigurationCommand {
readonly setConfigurationRequest: SetConfigurationRequest;
constructor(setConfigurationRequest: SetConfigurationRequest) {
this.setConfigurationRequest = setConfigurationRequest;
}
}

View File

@ -1,71 +0,0 @@
import { RabbitMQConfig, RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
import { RedisModule, RedisModuleOptions } from '@liaoliaots/nestjs-redis';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { CqrsModule } from '@nestjs/cqrs';
import { ConfigurationMessagerController } from './adapters/primaries/configuration-messager.controller';
import { RedisConfigurationRepository } from './adapters/secondaries/redis-configuration.repository';
import { DeleteConfigurationUseCase } from './domain/usecases/delete-configuration.usecase';
import { GetConfigurationUseCase } from './domain/usecases/get-configuration.usecase';
import { SetConfigurationUseCase } from './domain/usecases/set-configuration.usecase';
@Module({
imports: [
CqrsModule,
RedisModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (
configService: ConfigService,
): Promise<RedisModuleOptions> => ({
config: {
host: configService.get<string>('REDIS_HOST'),
port: configService.get<number>('REDIS_PORT'),
password: configService.get<string>('REDIS_PASSWORD'),
},
}),
}),
RabbitMQModule.forRootAsync(RabbitMQModule, {
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (
configService: ConfigService,
): Promise<RabbitMQConfig> => ({
exchanges: [
{
name: configService.get<string>('RMQ_EXCHANGE'),
type: 'topic',
},
],
handlers: {
setConfiguration: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: ['configuration.create', 'configuration.update'],
queue: 'matcher-configuration-create-update',
},
deleteConfiguration: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'configuration.delete',
queue: 'matcher-configuration-delete',
},
propagateConfiguration: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'configuration.propagate',
queue: 'matcher-configuration-propagate',
},
},
uri: configService.get<string>('RMQ_URI'),
connectionInitOptions: { wait: false },
enableControllerDiscovery: true,
}),
}),
],
controllers: [ConfigurationMessagerController],
providers: [
GetConfigurationUseCase,
SetConfigurationUseCase,
DeleteConfigurationUseCase,
RedisConfigurationRepository,
],
})
export class ConfigurationModule {}

View File

@ -1,11 +0,0 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class DeleteConfigurationRequest {
@IsString()
@IsNotEmpty()
domain: string;
@IsString()
@IsNotEmpty()
key: string;
}

View File

@ -1,15 +0,0 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class SetConfigurationRequest {
@IsString()
@IsNotEmpty()
domain: string;
@IsString()
@IsNotEmpty()
key: string;
@IsString()
@IsNotEmpty()
value: string;
}

View File

@ -1,12 +0,0 @@
import { AutoMap } from '@automapper/classes';
export class Configuration {
@AutoMap()
domain: string;
@AutoMap()
key: string;
@AutoMap()
value: string;
}

View File

@ -1,8 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export abstract class IConfigurationRepository {
abstract get(key: string): Promise<string>;
abstract set(key: string, value: string): Promise<void>;
abstract del(key: string): Promise<void>;
}

View File

@ -1,18 +0,0 @@
import { CommandHandler } from '@nestjs/cqrs';
import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository';
import { DeleteConfigurationCommand } from '../../commands/delete-configuration.command';
@CommandHandler(DeleteConfigurationCommand)
export class DeleteConfigurationUseCase {
constructor(private configurationRepository: RedisConfigurationRepository) {}
execute = async (
deleteConfigurationCommand: DeleteConfigurationCommand,
): Promise<void> => {
await this.configurationRepository.del(
`${deleteConfigurationCommand.deleteConfigurationRequest.domain}
:
${deleteConfigurationCommand.deleteConfigurationRequest.key}`,
);
};
}

View File

@ -1,15 +0,0 @@
import { QueryHandler } from '@nestjs/cqrs';
import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository';
import { GetConfigurationQuery } from '../../queries/get-configuration.query';
@QueryHandler(GetConfigurationQuery)
export class GetConfigurationUseCase {
constructor(private configurationRepository: RedisConfigurationRepository) {}
execute = async (
getConfigurationQuery: GetConfigurationQuery,
): Promise<string> =>
this.configurationRepository.get(
`${getConfigurationQuery.domain}:${getConfigurationQuery.key}`,
);
}

View File

@ -1,19 +0,0 @@
import { CommandHandler } from '@nestjs/cqrs';
import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository';
import { SetConfigurationCommand } from '../../commands/set-configuration.command';
@CommandHandler(SetConfigurationCommand)
export class SetConfigurationUseCase {
constructor(private configurationRepository: RedisConfigurationRepository) {}
execute = async (
setConfigurationCommand: SetConfigurationCommand,
): Promise<void> => {
await this.configurationRepository.set(
`${setConfigurationCommand.setConfigurationRequest.domain}
:
${setConfigurationCommand.setConfigurationRequest.key}`,
setConfigurationCommand.setConfigurationRequest.value,
);
};
}

View File

@ -1,9 +0,0 @@
export class GetConfigurationQuery {
readonly domain: string;
readonly key: string;
constructor(domain: string, key: string) {
this.domain = domain;
this.key = key;
}
}

View File

@ -1,49 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository';
import { DeleteConfigurationCommand } from '../../commands/delete-configuration.command';
import { DeleteConfigurationRequest } from '../../domain/dtos/delete-configuration.request';
import { DeleteConfigurationUseCase } from '../../domain/usecases/delete-configuration.usecase';
const mockRedisConfigurationRepository = {
del: jest.fn().mockResolvedValue(undefined),
};
describe('DeleteConfigurationUseCase', () => {
let deleteConfigurationUseCase: DeleteConfigurationUseCase;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: RedisConfigurationRepository,
useValue: mockRedisConfigurationRepository,
},
DeleteConfigurationUseCase,
],
}).compile();
deleteConfigurationUseCase = module.get<DeleteConfigurationUseCase>(
DeleteConfigurationUseCase,
);
});
it('should be defined', () => {
expect(deleteConfigurationUseCase).toBeDefined();
});
describe('execute', () => {
it('should delete a key', async () => {
jest.spyOn(mockRedisConfigurationRepository, 'del');
const deleteConfigurationRequest: DeleteConfigurationRequest = {
domain: 'my-domain',
key: 'my-key',
};
await deleteConfigurationUseCase.execute(
new DeleteConfigurationCommand(deleteConfigurationRequest),
);
expect(mockRedisConfigurationRepository.del).toHaveBeenCalledTimes(1);
});
});
});

View File

@ -1,43 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository';
import { GetConfigurationUseCase } from '../../domain/usecases/get-configuration.usecase';
import { GetConfigurationQuery } from '../../queries/get-configuration.query';
const mockRedisConfigurationRepository = {
get: jest.fn().mockResolvedValue('my-value'),
};
describe('GetConfigurationUseCase', () => {
let getConfigurationUseCase: GetConfigurationUseCase;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: RedisConfigurationRepository,
useValue: mockRedisConfigurationRepository,
},
GetConfigurationUseCase,
],
}).compile();
getConfigurationUseCase = module.get<GetConfigurationUseCase>(
GetConfigurationUseCase,
);
});
it('should be defined', () => {
expect(getConfigurationUseCase).toBeDefined();
});
describe('execute', () => {
it('should get a value for a key', async () => {
const value: string = await getConfigurationUseCase.execute(
new GetConfigurationQuery('my-domain', 'my-key'),
);
expect(value).toBe('my-value');
});
});
});

View File

@ -1,47 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository';
import { getRedisToken } from '@liaoliaots/nestjs-redis';
const mockRedis = {
get: jest.fn().mockResolvedValue('myValue'),
set: jest.fn().mockImplementation(),
del: jest.fn().mockImplementation(),
};
describe('RedisConfigurationRepository', () => {
let redisConfigurationRepository: RedisConfigurationRepository;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: getRedisToken('default'),
useValue: mockRedis,
},
RedisConfigurationRepository,
],
}).compile();
redisConfigurationRepository = module.get<RedisConfigurationRepository>(
RedisConfigurationRepository,
);
});
it('should be defined', () => {
expect(redisConfigurationRepository).toBeDefined();
});
describe('interact', () => {
it('should get a value', async () => {
expect(await redisConfigurationRepository.get('myKey')).toBe('myValue');
});
it('should set a value', async () => {
expect(
await redisConfigurationRepository.set('myKey', 'myValue'),
).toBeUndefined();
});
it('should delete a value', async () => {
expect(await redisConfigurationRepository.del('myKey')).toBeUndefined();
});
});
});

View File

@ -1,50 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository';
import { SetConfigurationCommand } from '../../commands/set-configuration.command';
import { SetConfigurationRequest } from '../../domain/dtos/set-configuration.request';
import { SetConfigurationUseCase } from '../../domain/usecases/set-configuration.usecase';
const mockRedisConfigurationRepository = {
set: jest.fn().mockResolvedValue(undefined),
};
describe('SetConfigurationUseCase', () => {
let setConfigurationUseCase: SetConfigurationUseCase;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: RedisConfigurationRepository,
useValue: mockRedisConfigurationRepository,
},
SetConfigurationUseCase,
],
}).compile();
setConfigurationUseCase = module.get<SetConfigurationUseCase>(
SetConfigurationUseCase,
);
});
it('should be defined', () => {
expect(setConfigurationUseCase).toBeDefined();
});
describe('execute', () => {
it('should set a value for a key', async () => {
jest.spyOn(mockRedisConfigurationRepository, 'set');
const setConfigurationRequest: SetConfigurationRequest = {
domain: 'my-domain',
key: 'my-key',
value: 'my-value',
};
await setConfigurationUseCase.execute(
new SetConfigurationCommand(setConfigurationRequest),
);
expect(mockRedisConfigurationRepository.set).toHaveBeenCalledTimes(1);
});
});
});