1- import { INestApplication } from '@nestjs/common' ;
2- import { Transport } from '@nestjs/microservices' ;
1+ import {
2+ ArgumentsHost ,
3+ Catch ,
4+ ExceptionFilter ,
5+ HttpException ,
6+ INestApplication ,
7+ RpcExceptionFilter ,
8+ } from '@nestjs/common' ;
9+ import { RpcException , Transport } from '@nestjs/microservices' ;
310import { Test } from '@nestjs/testing' ;
411import { expect } from 'chai' ;
512import * as request from 'supertest' ;
613import { KafkaController } from '../src/kafka/kafka.controller' ;
14+ import { APP_FILTER } from '@nestjs/core' ;
15+ import { Observable , throwError } from 'rxjs' ;
16+ import { KafkaMessagesController } from '../src/kafka/kafka.messages.controller' ;
17+ import { UserDto } from '../src/kafka/dtos/user.dto' ;
18+ import { UserEntity } from '../src/kafka/entities/user.entity' ;
19+ import { BusinessDto } from '../src/kafka/dtos/business.dto' ;
20+ import { BusinessEntity } from '../src/kafka/entities/business.entity' ;
21+
22+ @Catch ( )
23+ class KafkaExceptionFilter implements ExceptionFilter {
24+ catch ( exception : HttpException , host : ArgumentsHost ) : any {
25+ console . log ( exception ) ;
26+ }
27+ }
28+ @Catch ( )
29+ class RpcErrorFilter implements RpcExceptionFilter {
30+ catch ( exception : RpcException ) : Observable < any > {
31+ console . log ( exception ) ;
32+ return throwError ( exception ) ;
33+ }
34+ }
735
836describe ( 'Kafka transport' , ( ) => {
937 let server ;
1038 let app : INestApplication ;
1139
1240 it ( `Start Kafka app` , async ( ) => {
1341 const module = await Test . createTestingModule ( {
14- controllers : [ KafkaController ] ,
42+ controllers : [
43+ KafkaController ,
44+ KafkaMessagesController ,
45+ ] ,
46+ providers : [
47+ {
48+ provide : APP_FILTER ,
49+ useClass : RpcErrorFilter ,
50+ } ,
51+ {
52+ provide : APP_FILTER ,
53+ useClass : KafkaExceptionFilter ,
54+ } ,
55+ ] ,
1556 } ) . compile ( ) ;
1657
1758 app = module . createNestApplication ( ) ;
@@ -37,30 +78,43 @@ describe('Kafka transport', () => {
3778 . expect ( 200 , '15' ) ;
3879 } ) . timeout ( 50000 ) ;
3980
40- // it(`/POST (async command sum)`, done => {
41- // request(server)
42- // .post('/?command=math.sum')
43- // .send([1, 2, 3, 4, 5])
44- // .end(() => {
45- // setTimeout(() => {
46- // expect(KafkaController.MATH_SUM).to.eq(15);
47- // done();
48- // }, 4000);
49- // });
50- // }).timeout(5000);
51-
5281 it ( `/POST (async event notification)` , done => {
5382 request ( server )
5483 . post ( '/notify' )
55- . send ( [ 1 , 2 , 3 , 4 , 5 ] )
84+ . send ( )
5685 . end ( ( ) => {
57- setTimeout ( ( ) => {
58- expect ( KafkaController . IS_NOTIFIED ) . to . be . true ;
59- done ( ) ;
60- } , 4000 ) ;
86+ expect ( KafkaController . IS_NOTIFIED ) . to . be . true ;
87+ done ( ) ;
6188 } ) ;
6289 } ) . timeout ( 5000 ) ;
6390
91+ const userDto : UserDto = {
92+ email : 'enriquebenavidesm@gmail.com' ,
93+ name : 'Ben' ,
94+ phone : '1112223331' ,
95+ years : 33 ,
96+ } ;
97+ const newUser : UserEntity = new UserEntity ( userDto ) ;
98+ const businessDto : BusinessDto = {
99+ name : 'Example' ,
100+ phone : '2233441122' ,
101+ user : newUser ,
102+ } ;
103+ it ( `/POST (async command create user)` , ( ) => {
104+ return request ( server )
105+ . post ( '/user' )
106+ . send ( userDto )
107+ . expect ( 200 ) ;
108+ } ) . timeout ( 50000 ) ;
109+
110+ it ( `/POST (async command create business` , ( ) => {
111+ const newBusiness : BusinessEntity = new BusinessEntity ( businessDto ) ;
112+ return request ( server )
113+ . post ( '/business' )
114+ . send ( businessDto )
115+ . expect ( 200 ) ;
116+ } ) . timeout ( 50000 ) ;
117+
64118 after ( `Stopping Kafka app` , async ( ) => {
65119 await app . close ( ) ;
66120 } ) ;
0 commit comments