Merge branch 'refactorMessager' into 'main'

refactor message broker

See merge request v3/service/territory!10
This commit is contained in:
Sylvain Briat 2023-03-16 10:05:04 +00:00
commit cdc5c62c24
18 changed files with 100 additions and 139 deletions

View File

@ -8,6 +8,7 @@ DATABASE_URL="postgresql://territory:territory@v3-territory-db:5432/territory?sc
# RABBIT MQ # RABBIT MQ
RMQ_URI=amqp://v3-broker:5672 RMQ_URI=amqp://v3-broker:5672
RMQ_EXCHANGE=mobicoop
# POSTGRES # POSTGRES
POSTGRES_IMAGE=postgis/postgis:15-3.3 POSTGRES_IMAGE=postgis/postgis:15-3.3

View File

@ -16,9 +16,7 @@ export class ConfigurationMessagerController {
) {} ) {}
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'configuration', name: 'setConfiguration',
routingKey: ['create', 'update'],
queue: 'territory-configuration-update',
}) })
public async setConfigurationHandler(message: string) { public async setConfigurationHandler(message: string) {
const configuration: Configuration = JSON.parse(message); const configuration: Configuration = JSON.parse(message);
@ -38,9 +36,7 @@ export class ConfigurationMessagerController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'configuration', name: 'deleteConfiguration',
routingKey: 'delete',
queue: 'territory-configuration-delete',
}) })
public async configurationDeletedHandler(message: string) { public async configurationDeletedHandler(message: string) {
const deletedConfiguration: Configuration = JSON.parse(message); const deletedConfiguration: Configuration = JSON.parse(message);
@ -58,9 +54,7 @@ export class ConfigurationMessagerController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'configuration', name: 'propagateConfiguration',
routingKey: 'propagate',
queue: 'territory-configuration-propagate',
}) })
public async propagateConfigurationsHandler(message: string) { public async propagateConfigurationsHandler(message: string) {
const configurations: Array<Configuration> = JSON.parse(message); const configurations: Array<Configuration> = JSON.parse(message);

View File

@ -32,10 +32,24 @@ import { SetConfigurationUseCase } from './domain/usecases/set-configuration.use
): Promise<RabbitMQConfig> => ({ ): Promise<RabbitMQConfig> => ({
exchanges: [ exchanges: [
{ {
name: 'configuration', name: configService.get<string>('RMQ_EXCHANGE'),
type: 'topic', type: 'topic',
}, },
], ],
handlers: {
setConfiguration: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: ['configuration.create', 'configuration.update'],
},
deleteConfiguration: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'configuration.delete',
},
propagateConfiguration: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'configuration.propagate',
},
},
uri: configService.get<string>('RMQ_URI'), uri: configService.get<string>('RMQ_URI'),
connectionInitOptions: { wait: false }, connectionInitOptions: { wait: false },
enableControllerDiscovery: true, enableControllerDiscovery: true,

View File

@ -1,11 +1,15 @@
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq'; import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { IMessageBroker } from '../../domain/interfaces/message-broker'; import { IMessageBroker } from '../../domain/interfaces/message-broker';
@Injectable() @Injectable()
export class LoggingMessager extends IMessageBroker { export class Messager extends IMessageBroker {
constructor(private readonly _amqpConnection: AmqpConnection) { constructor(
super('logging'); private readonly _amqpConnection: AmqpConnection,
configService: ConfigService,
) {
super(configService.get<string>('RMQ_EXCHANGE'));
} }
publish(routingKey: string, message: string): void { publish(routingKey: string, message: string): void {

View File

@ -1,14 +0,0 @@
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { Injectable } from '@nestjs/common';
import { IMessageBroker } from '../../domain/interfaces/message-broker';
@Injectable()
export class TerritoryMessager extends IMessageBroker {
constructor(private readonly _amqpConnection: AmqpConnection) {
super('territory');
}
publish(routingKey: string, message: string): void {
this._amqpConnection.publish(this.exchange, routingKey, message);
}
}

View File

@ -0,0 +1,9 @@
import { AutoMap } from '@automapper/classes';
export class TerritoryPresenter {
@AutoMap()
uuid: string;
@AutoMap()
name: string;
}

View File

@ -1,20 +1,19 @@
import { Mapper } from '@automapper/core'; import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs'; import { InjectMapper } from '@automapper/nestjs';
import { CommandHandler } from '@nestjs/cqrs'; import { CommandHandler } from '@nestjs/cqrs';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { CreateTerritoryCommand } from '../../commands/create-territory.command'; import { CreateTerritoryCommand } from '../../commands/create-territory.command';
import { TerritoryMessager } from '../../adapters/secondaries/territory.messager'; import { Messager } from '../../adapters/secondaries/messager';
import { Territory } from '../entities/territory'; import { Territory } from '../entities/territory';
import { CreateTerritoryRequest } from '../dtos/create-territory.request'; import { CreateTerritoryRequest } from '../dtos/create-territory.request';
import { TerritoryLoggingPresenter } from '../../adapters/secondaries/territory-logging.presenter'; import { TerritoryLoggingPresenter } from '../../adapters/secondaries/territory-logging.presenter';
import { TerritoryPresenter } from '../../adapters/secondaries/territory.presenter';
@CommandHandler(CreateTerritoryCommand) @CommandHandler(CreateTerritoryCommand)
export class CreateTerritoryUseCase { export class CreateTerritoryUseCase {
constructor( constructor(
private readonly _repository: TerritoriesRepository, private readonly _repository: TerritoriesRepository,
private readonly _territoryMessager: TerritoryMessager, private readonly _messager: Messager,
private readonly _loggingMessager: LoggingMessager,
@InjectMapper() private readonly _mapper: Mapper, @InjectMapper() private readonly _mapper: Mapper,
) {} ) {}
@ -27,25 +26,25 @@ export class CreateTerritoryUseCase {
try { try {
const territory = await this._repository.createTerritory(entity); const territory = await this._repository.createTerritory(entity);
this._territoryMessager.publish( this._messager.publish(
'create', 'territory.create',
JSON.stringify( JSON.stringify(
this._mapper.map(territory, Territory, TerritoryLoggingPresenter), this._mapper.map(territory, Territory, TerritoryPresenter),
), ),
); );
this._loggingMessager.publish( this._messager.publish(
'territory.create.info', 'logging.territory.create.info',
JSON.stringify( JSON.stringify(
this._mapper.map(territory, Territory, TerritoryLoggingPresenter), this._mapper.map(territory, Territory, TerritoryLoggingPresenter),
), ),
); );
return territory; return territory;
} catch (error) { } catch (error) {
let key = 'territory.create.crit'; let key = 'logging.territory.create.crit';
if (error.message.includes('already exists')) { if (error.message.includes('already exists')) {
key = 'territory.create.warning'; key = 'logging.territory.create.warning';
} }
this._loggingMessager.publish( this._messager.publish(
key, key,
JSON.stringify({ JSON.stringify({
command, command,

View File

@ -1,7 +1,6 @@
import { CommandHandler } from '@nestjs/cqrs'; import { CommandHandler } from '@nestjs/cqrs';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { TerritoryMessager } from '../../adapters/secondaries/territory.messager'; import { Messager } from '../../adapters/secondaries/messager';
import { DeleteTerritoryCommand } from '../../commands/delete-territory.command'; import { DeleteTerritoryCommand } from '../../commands/delete-territory.command';
import { Territory } from '../entities/territory'; import { Territory } from '../entities/territory';
@ -9,25 +8,24 @@ import { Territory } from '../entities/territory';
export class DeleteTerritoryUseCase { export class DeleteTerritoryUseCase {
constructor( constructor(
private readonly _repository: TerritoriesRepository, private readonly _repository: TerritoriesRepository,
private readonly _territoryMessager: TerritoryMessager, private readonly _messager: Messager,
private readonly _loggingMessager: LoggingMessager,
) {} ) {}
async execute(command: DeleteTerritoryCommand): Promise<Territory> { async execute(command: DeleteTerritoryCommand): Promise<Territory> {
try { try {
const territory = await this._repository.delete(command.uuid); const territory = await this._repository.delete(command.uuid);
this._territoryMessager.publish( this._messager.publish(
'delete', 'territory.delete',
JSON.stringify({ uuid: territory.uuid }), JSON.stringify({ uuid: territory.uuid }),
); );
this._loggingMessager.publish( this._messager.publish(
'territory.delete.info', 'logging.territory.delete.info',
JSON.stringify({ uuid: territory.uuid }), JSON.stringify({ uuid: territory.uuid }),
); );
return territory; return territory;
} catch (error) { } catch (error) {
this._loggingMessager.publish( this._messager.publish(
'territory.delete.crit', 'logging.territory.delete.crit',
JSON.stringify({ JSON.stringify({
command, command,
error, error,

View File

@ -1,7 +1,7 @@
import { NotFoundException } from '@nestjs/common'; import { NotFoundException } from '@nestjs/common';
import { QueryHandler } from '@nestjs/cqrs'; import { QueryHandler } from '@nestjs/cqrs';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { Messager } from '../../adapters/secondaries/messager';
import { FindTerritoryByUuidQuery } from '../../queries/find-territory-by-uuid.query'; import { FindTerritoryByUuidQuery } from '../../queries/find-territory-by-uuid.query';
import { Territory } from '../entities/territory'; import { Territory } from '../entities/territory';
@ -9,7 +9,7 @@ import { Territory } from '../entities/territory';
export class FindTerritoryByUuidUseCase { export class FindTerritoryByUuidUseCase {
constructor( constructor(
private readonly _repository: TerritoriesRepository, private readonly _repository: TerritoriesRepository,
private readonly _loggingMessager: LoggingMessager, private readonly _messager: Messager,
) {} ) {}
async execute( async execute(
@ -22,8 +22,8 @@ export class FindTerritoryByUuidUseCase {
if (!territory) throw new NotFoundException(); if (!territory) throw new NotFoundException();
return territory; return territory;
} catch (error) { } catch (error) {
this._loggingMessager.publish( this._messager.publish(
'territory.read.warning', 'logging.territory.read.warning',
JSON.stringify({ JSON.stringify({
query: findTerritoryByUuidQuery, query: findTerritoryByUuidQuery,
error, error,

View File

@ -1,20 +1,19 @@
import { Mapper } from '@automapper/core'; import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs'; import { InjectMapper } from '@automapper/nestjs';
import { CommandHandler } from '@nestjs/cqrs'; import { CommandHandler } from '@nestjs/cqrs';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { TerritoryMessager } from '../../adapters/secondaries/territory.messager'; import { Messager } from '../../adapters/secondaries/messager';
import { TerritoryLoggingPresenter } from '../../adapters/secondaries/territory-logging.presenter'; import { TerritoryLoggingPresenter } from '../../adapters/secondaries/territory-logging.presenter';
import { UpdateTerritoryCommand } from '../../commands/update-territory.command'; import { UpdateTerritoryCommand } from '../../commands/update-territory.command';
import { UpdateTerritoryRequest } from '../dtos/update-territory.request'; import { UpdateTerritoryRequest } from '../dtos/update-territory.request';
import { Territory } from '../entities/territory'; import { Territory } from '../entities/territory';
import { TerritoryPresenter } from '../../adapters/secondaries/territory.presenter';
@CommandHandler(UpdateTerritoryCommand) @CommandHandler(UpdateTerritoryCommand)
export class UpdateTerritoryUseCase { export class UpdateTerritoryUseCase {
constructor( constructor(
private readonly _repository: TerritoriesRepository, private readonly _repository: TerritoriesRepository,
private readonly _territoryMessager: TerritoryMessager, private readonly _messager: Messager,
private readonly _loggingMessager: LoggingMessager,
@InjectMapper() private readonly _mapper: Mapper, @InjectMapper() private readonly _mapper: Mapper,
) {} ) {}
@ -28,18 +27,18 @@ export class UpdateTerritoryUseCase {
Territory, Territory,
), ),
); );
this._territoryMessager.publish( this._messager.publish(
'update', 'territory.update',
JSON.stringify( JSON.stringify(
this._mapper.map( this._mapper.map(
command.updateTerritoryRequest, command.updateTerritoryRequest,
UpdateTerritoryRequest, UpdateTerritoryRequest,
TerritoryLoggingPresenter, TerritoryPresenter,
), ),
), ),
); );
this._loggingMessager.publish( this._messager.publish(
'territory.update.info', 'logging.territory.update.info',
JSON.stringify( JSON.stringify(
this._mapper.map( this._mapper.map(
command.updateTerritoryRequest, command.updateTerritoryRequest,
@ -50,8 +49,8 @@ export class UpdateTerritoryUseCase {
); );
return territory; return territory;
} catch (error) { } catch (error) {
this._loggingMessager.publish( this._messager.publish(
'territory.update.crit', 'logging.territory.update.crit',
JSON.stringify({ JSON.stringify({
command, command,
error, error,

View File

@ -2,6 +2,7 @@ import { createMap, forMember, ignore, Mapper } from '@automapper/core';
import { AutomapperProfile, InjectMapper } from '@automapper/nestjs'; import { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { TerritoryPresenter } from '../adapters/primaries/territory.presenter'; import { TerritoryPresenter } from '../adapters/primaries/territory.presenter';
import { TerritoryPresenter as SecondaryTerritoryPresenter } from '../adapters/secondaries/territory.presenter';
import { TerritoryLoggingPresenter } from '../adapters/secondaries/territory-logging.presenter'; import { TerritoryLoggingPresenter } from '../adapters/secondaries/territory-logging.presenter';
import { CreateTerritoryRequest } from '../domain/dtos/create-territory.request'; import { CreateTerritoryRequest } from '../domain/dtos/create-territory.request';
import { UpdateTerritoryRequest } from '../domain/dtos/update-territory.request'; import { UpdateTerritoryRequest } from '../domain/dtos/update-territory.request';
@ -16,6 +17,7 @@ export class TerritoryProfile extends AutomapperProfile {
override get profile() { override get profile() {
return (mapper) => { return (mapper) => {
createMap(mapper, Territory, TerritoryPresenter); createMap(mapper, Territory, TerritoryPresenter);
createMap(mapper, Territory, SecondaryTerritoryPresenter);
createMap(mapper, Territory, TerritoryLoggingPresenter); createMap(mapper, Territory, TerritoryLoggingPresenter);
createMap(mapper, CreateTerritoryRequest, Territory); createMap(mapper, CreateTerritoryRequest, Territory);
@ -27,6 +29,7 @@ export class TerritoryProfile extends AutomapperProfile {
forMember((dest) => dest.uuid, ignore()), forMember((dest) => dest.uuid, ignore()),
); );
createMap(mapper, UpdateTerritoryRequest, SecondaryTerritoryPresenter);
createMap(mapper, UpdateTerritoryRequest, TerritoryLoggingPresenter); createMap(mapper, UpdateTerritoryRequest, TerritoryLoggingPresenter);
}; };
} }

View File

@ -6,9 +6,8 @@ import { CqrsModule } from '@nestjs/cqrs';
import { redisStore } from 'cache-manager-ioredis-yet'; import { redisStore } from 'cache-manager-ioredis-yet';
import { DatabaseModule } from '../database/database.module'; import { DatabaseModule } from '../database/database.module';
import { TerritoriesController } from './adapters/primaries/territories.controller'; import { TerritoriesController } from './adapters/primaries/territories.controller';
import { LoggingMessager } from './adapters/secondaries/logging.messager';
import { TerritoriesRepository } from './adapters/secondaries/territories.repository'; import { TerritoriesRepository } from './adapters/secondaries/territories.repository';
import { TerritoryMessager } from './adapters/secondaries/territory.messager'; import { Messager } from './adapters/secondaries/messager';
import { CreateTerritoryUseCase } from './domain/usecases/create-territory.usecase'; import { CreateTerritoryUseCase } from './domain/usecases/create-territory.usecase';
import { DeleteTerritoryUseCase } from './domain/usecases/delete-territory.usecase'; import { DeleteTerritoryUseCase } from './domain/usecases/delete-territory.usecase';
import { FindAllTerritoriesForPointUseCase } from './domain/usecases/find-all-territories-for-point.usecase'; import { FindAllTerritoriesForPointUseCase } from './domain/usecases/find-all-territories-for-point.usecase';
@ -27,11 +26,7 @@ import { TerritoryProfile } from './mappers/territory.profile';
useFactory: async (configService: ConfigService) => ({ useFactory: async (configService: ConfigService) => ({
exchanges: [ exchanges: [
{ {
name: 'territory', name: configService.get<string>('RMQ_EXCHANGE'),
type: 'topic',
},
{
name: 'logging',
type: 'topic', type: 'topic',
}, },
], ],
@ -56,8 +51,7 @@ import { TerritoryProfile } from './mappers/territory.profile';
providers: [ providers: [
TerritoryProfile, TerritoryProfile,
TerritoriesRepository, TerritoriesRepository,
TerritoryMessager, Messager,
LoggingMessager,
FindAllTerritoriesForPointUseCase, FindAllTerritoriesForPointUseCase,
FindAllTerritoriesForPointsUseCase, FindAllTerritoriesForPointsUseCase,
FindAllTerritoriesUseCase, FindAllTerritoriesUseCase,

View File

@ -1,9 +1,8 @@
import { classes } from '@automapper/classes'; import { classes } from '@automapper/classes';
import { AutomapperModule } from '@automapper/nestjs'; import { AutomapperModule } from '@automapper/nestjs';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { TerritoryMessager } from '../../adapters/secondaries/territory.messager'; import { Messager } from '../../adapters/secondaries/messager';
import { CreateTerritoryCommand } from '../../commands/create-territory.command'; import { CreateTerritoryCommand } from '../../commands/create-territory.command';
import { CreateTerritoryRequest } from '../../domain/dtos/create-territory.request'; import { CreateTerritoryRequest } from '../../domain/dtos/create-territory.request';
import { Territory } from '../../domain/entities/territory'; import { Territory } from '../../domain/entities/territory';
@ -51,11 +50,7 @@ describe('CreateTerritoryUseCase', () => {
CreateTerritoryUseCase, CreateTerritoryUseCase,
TerritoryProfile, TerritoryProfile,
{ {
provide: TerritoryMessager, provide: Messager,
useValue: mockMessager,
},
{
provide: LoggingMessager,
useValue: mockMessager, useValue: mockMessager,
}, },
], ],

View File

@ -1,7 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { TerritoryMessager } from '../../adapters/secondaries/territory.messager'; import { Messager } from '../../adapters/secondaries/messager';
import { DeleteTerritoryCommand } from '../../commands/delete-territory.command'; import { DeleteTerritoryCommand } from '../../commands/delete-territory.command';
import { DeleteTerritoryUseCase } from '../../domain/usecases/delete-territory.usecase'; import { DeleteTerritoryUseCase } from '../../domain/usecases/delete-territory.usecase';
@ -60,11 +59,7 @@ describe('DeleteTerritoryUseCase', () => {
}, },
DeleteTerritoryUseCase, DeleteTerritoryUseCase,
{ {
provide: TerritoryMessager, provide: Messager,
useValue: mockMessager,
},
{
provide: LoggingMessager,
useValue: mockMessager, useValue: mockMessager,
}, },
], ],

View File

@ -1,6 +1,6 @@
import { NotFoundException } from '@nestjs/common'; import { NotFoundException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager'; import { Messager } from '../../adapters/secondaries/messager';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { FindTerritoryByUuidRequest } from '../../domain/dtos/find-territory-by-uuid.request'; import { FindTerritoryByUuidRequest } from '../../domain/dtos/find-territory-by-uuid.request';
import { FindTerritoryByUuidUseCase } from '../../domain/usecases/find-territory-by-uuid.usecase'; import { FindTerritoryByUuidUseCase } from '../../domain/usecases/find-territory-by-uuid.usecase';
@ -41,7 +41,7 @@ describe('FindTerritoryByUuidUseCase', () => {
useValue: mockTerritoriesRepository, useValue: mockTerritoriesRepository,
}, },
{ {
provide: LoggingMessager, provide: Messager,
useValue: mockMessager, useValue: mockMessager,
}, },
FindTerritoryByUuidUseCase, FindTerritoryByUuidUseCase,

View File

@ -1,36 +1,47 @@
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq'; import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager'; import { Messager } from '../../adapters/secondaries/messager';
const mockAmqpConnection = { const mockAmqpConnection = {
publish: jest.fn().mockImplementation(), publish: jest.fn().mockImplementation(),
}; };
describe('LoggingMessager', () => { const mockConfigService = {
let loggingMessager: LoggingMessager; get: jest.fn().mockResolvedValue({
RMQ_EXCHANGE: 'mobicoop',
}),
};
describe('Messager', () => {
let messager: Messager;
beforeAll(async () => { beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
imports: [], imports: [],
providers: [ providers: [
LoggingMessager, Messager,
{ {
provide: AmqpConnection, provide: AmqpConnection,
useValue: mockAmqpConnection, useValue: mockAmqpConnection,
}, },
{
provide: ConfigService,
useValue: mockConfigService,
},
], ],
}).compile(); }).compile();
loggingMessager = module.get<LoggingMessager>(LoggingMessager); messager = module.get<Messager>(Messager);
}); });
it('should be defined', () => { it('should be defined', () => {
expect(LoggingMessager).toBeDefined(); expect(messager).toBeDefined();
}); });
it('should publish a message', async () => { it('should publish a message', async () => {
jest.spyOn(mockAmqpConnection, 'publish'); jest.spyOn(mockAmqpConnection, 'publish');
await loggingMessager.publish('territory.create.info', 'my-test'); messager.publish('territory.create.info', 'my-test');
expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1); expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1);
}); });
}); });

View File

@ -1,36 +0,0 @@
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { Test, TestingModule } from '@nestjs/testing';
import { TerritoryMessager } from '../../adapters/secondaries/territory.messager';
const mockAmqpConnection = {
publish: jest.fn().mockImplementation(),
};
describe('TerritoryMessager', () => {
let territoryMessager: TerritoryMessager;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [],
providers: [
TerritoryMessager,
{
provide: AmqpConnection,
useValue: mockAmqpConnection,
},
],
}).compile();
territoryMessager = module.get<TerritoryMessager>(TerritoryMessager);
});
it('should be defined', () => {
expect(territoryMessager).toBeDefined();
});
it('should publish a message', async () => {
jest.spyOn(mockAmqpConnection, 'publish');
await territoryMessager.publish('territory.create.info', 'my-test');
expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1);
});
});

View File

@ -1,9 +1,8 @@
import { classes } from '@automapper/classes'; import { classes } from '@automapper/classes';
import { AutomapperModule } from '@automapper/nestjs'; import { AutomapperModule } from '@automapper/nestjs';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { TerritoryMessager } from '../../adapters/secondaries/territory.messager'; import { Messager } from '../../adapters/secondaries/messager';
import { UpdateTerritoryCommand } from '../../commands/update-territory.command'; import { UpdateTerritoryCommand } from '../../commands/update-territory.command';
import { UpdateTerritoryRequest } from '../../domain/dtos/update-territory.request'; import { UpdateTerritoryRequest } from '../../domain/dtos/update-territory.request';
import { Territory } from '../../domain/entities/territory'; import { Territory } from '../../domain/entities/territory';
@ -54,11 +53,7 @@ describe('UpdateTerritoryUseCase', () => {
UpdateTerritoryUseCase, UpdateTerritoryUseCase,
TerritoryProfile, TerritoryProfile,
{ {
provide: TerritoryMessager, provide: Messager,
useValue: mockMessager,
},
{
provide: LoggingMessager,
useValue: mockMessager, useValue: mockMessager,
}, },
], ],