33const cds = require ( "@sap/cds" ) ;
44const eventQueue = require ( "@cap-js-community/event-queue" ) ;
55
6- const { GET , POST , expect , axios } = cds . test ( __dirname + "/.." ) ;
6+ const { GET , POST , axios } = cds . test ( __dirname + "/.." ) ;
77axios . defaults . auth = { username : "alice" , password : "alice" } ;
88
99describe ( "ProcessService OData APIs" , ( ) => {
10+ let log = cds . test . log ( ) ;
11+
12+ beforeEach ( async ( ) => {
13+ await DELETE . from ( "sap.eventqueue.Event" ) ;
14+ } ) ;
15+
1016 it ( "serves ProcessService.C_ClosingTask" , async ( ) => {
1117 const { data } = await GET `/odata/v4/process/C_ClosingTask ${ { params : { $select : "ID,description" } } } ` ;
12- expect ( data . value ) . to . be . empty ;
18+ expect ( data . value ) . toHaveLength ( 0 ) ;
1319 } ) ;
1420
1521 it ( "emit and process event via odata" , async ( ) => {
16- let { data } = await POST ( `/odata/v4/process/C_ClosingTask` , {
22+ const { data } = await POST ( `/odata/v4/process/C_ClosingTask` , {
1723 description : "Demo Task for processing" ,
1824 } ) ;
19-
20- await POST ( `/odata/v4/process/C_ClosingTask(${ data . ID } )/process` ) ;
25+ await POST ( `/odata/v4/process/C_ClosingTask(${ data . ID } )/trigger` ) ;
2126
2227 let events = await SELECT . from ( "sap.eventqueue.Event" ) ;
23- expect ( events ) . to . have . lengthOf ( 1 ) ;
24- expect ( events [ 0 ] ) . to . nested . include ( {
28+ expect ( events ) . toHaveLength ( 1 ) ;
29+ expect ( events [ 0 ] ) . toMatchObject ( {
2530 type : "CAP_OUTBOX" ,
2631 subType : "task-service" ,
2732 status : 0 ,
@@ -30,17 +35,106 @@ describe("ProcessService OData APIs", () => {
3035 await eventQueue . processEventQueue ( { } , "CAP_OUTBOX" , "task-service" ) ;
3136
3237 events = await SELECT . from ( "sap.eventqueue.Event" ) . orderBy ( "createdAt" ) ;
33- expect ( events ) . to . have . lengthOf ( 2 ) ;
34- expect ( events [ 0 ] ) . to . nested . include ( {
38+ expect ( events ) . toHaveLength ( 2 ) ;
39+ expect ( events [ 0 ] ) . toMatchObject ( {
3540 type : "CAP_OUTBOX" ,
3641 subType : "task-service" ,
3742 status : 2 ,
3843 } ) ;
3944
40- expect ( events [ 1 ] ) . to . nested . include ( {
45+ expect ( events [ 1 ] ) . toMatchObject ( {
4146 type : "CAP_OUTBOX" ,
4247 subType : "mail-service" ,
4348 status : 0 ,
4449 } ) ;
4550 } ) ;
51+
52+ it ( "multiple tasks are clustered by 'to' property" , async ( ) => {
53+ const { data : task1 } = await POST ( `/odata/v4/process/C_ClosingTask` , {
54+ description : "Demo Task for processing I" ,
55+ } ) ;
56+ const { data : task2 } = await POST ( `/odata/v4/process/C_ClosingTask` , {
57+ description : "Demo Task for processing II" ,
58+ } ) ;
59+ await POST ( `/odata/v4/process/C_ClosingTask(${ task1 . ID } )/trigger` ) ;
60+ await POST ( `/odata/v4/process/C_ClosingTask(${ task2 . ID } )/trigger` ) ;
61+
62+ let events = await SELECT . from ( "sap.eventqueue.Event" ) ;
63+ expect ( events ) . toHaveLength ( 2 ) ;
64+
65+ await eventQueue . processEventQueue ( { } , "CAP_OUTBOX" , "task-service" ) ;
66+
67+ events = await SELECT . from ( "sap.eventqueue.Event" ) . orderBy ( "createdAt" ) ;
68+ expect ( events ) . toHaveLength ( 4 ) ;
69+
70+ await eventQueue . processEventQueue ( { } , "CAP_OUTBOX" , "mail-service" ) ;
71+
72+ const logs = parseLogEntries ( log . output ) ;
73+ const mailLogs = logs . filter ( ( entry ) => entry . includes ( "sending e-mail" ) ) ;
74+ expect ( mailLogs ) . toHaveLength ( 1 ) ;
75+ expect ( mailLogs [ 0 ] ) . toMatchSnapshot ( ) ;
76+ } ) ;
77+
78+ it ( "trigger special task (own cds env configuration) with exceeded handling" , async ( ) => {
79+ const { data } = await POST ( `/odata/v4/process/C_ClosingTask` , {
80+ description : "Demo Task for processing" ,
81+ } ) ;
82+ await POST ( `/odata/v4/process/C_ClosingTask(${ data . ID } )/triggerSpecial` ) ;
83+
84+ let events = await SELECT . from ( "sap.eventqueue.Event" ) ;
85+ expect ( events ) . toHaveLength ( 1 ) ;
86+ expect ( events [ 0 ] ) . toMatchObject ( {
87+ type : "CAP_OUTBOX" ,
88+ subType : "task-service.processSpecial" ,
89+ status : 0 ,
90+ } ) ;
91+
92+ await eventQueue . processEventQueue ( { } , "CAP_OUTBOX" , "task-service.processSpecial" ) ;
93+
94+ events = await SELECT . from ( "sap.eventqueue.Event" ) . orderBy ( "createdAt" ) ;
95+ expect ( events ) . toHaveLength ( 1 ) ;
96+ expect ( events [ 0 ] ) . toMatchObject ( {
97+ type : "CAP_OUTBOX" ,
98+ subType : "task-service.processSpecial" ,
99+ status : 3 ,
100+ error : expect . stringContaining ( "Special tasks cannot be processed at the moment." ) ,
101+ } ) ;
102+
103+ await eventQueue . processEventQueue ( { } , "CAP_OUTBOX" , "task-service.processSpecial" ) ;
104+
105+ events = await SELECT . from ( "sap.eventqueue.Event" ) . orderBy ( "createdAt" ) ;
106+ expect ( events ) . toHaveLength ( 1 ) ;
107+ expect ( events [ 0 ] ) . toMatchObject ( {
108+ type : "CAP_OUTBOX" ,
109+ subType : "task-service.processSpecial" ,
110+ status : 4 ,
111+ error : expect . stringContaining ( "Special tasks cannot be processed at the moment." ) ,
112+ } ) ;
113+ } ) ;
46114} ) ;
115+
116+ function parseLogEntries ( logText ) {
117+ const lines = logText . split ( / \r ? \n / ) ;
118+ const entries = [ ] ;
119+ let current = [ ] ;
120+
121+ for ( const line of lines ) {
122+ if ( / ^ \[ [ ^ \] ] + ] \s + - / . test ( line ) ) {
123+ // Start of a new entry
124+ if ( current . length > 0 ) {
125+ entries . push ( current . join ( "\n" ) ) ;
126+ }
127+ current = [ line ] ;
128+ } else {
129+ // Continuation of previous entry
130+ current . push ( line ) ;
131+ }
132+ }
133+
134+ // Push the final entry
135+ if ( current . length > 0 ) {
136+ entries . push ( current . join ( "\n" ) ) ;
137+ }
138+
139+ return entries ;
140+ }
0 commit comments