-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessStart-input.test.ts
More file actions
714 lines (626 loc) · 22.6 KB
/
processStart-input.test.ts
File metadata and controls
714 lines (626 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/* eslint-disable @typescript-eslint/no-explicit-any */
import cds from '@sap/cds';
const { join } = cds.utils.path;
const app = join(__dirname, '../../bookshop');
const { test, POST } = cds.test(app);
describe('Integration tests for START annotation with inputs array', () => {
let foundMessages: any[] = [];
beforeAll(async () => {
const db = await cds.connect.to('db');
db.before('*', (req) => {
if (req.event === 'CREATE' && req.target?.name === 'cds.outbox.Messages') {
const msg = JSON.parse(req.query?.INSERT?.entries[0].msg);
foundMessages.push(msg);
}
});
});
beforeEach(async () => {
await test.data.reset();
foundMessages = [];
});
// Helper to get the start message context
const getStartContext = () => {
const startMsg = foundMessages.find((msg) => msg.event === 'start');
return startMsg?.data?.context;
};
// ================================================
// Test 1: No inputs array specified, but ProcessInputs type exists
// Only entity fields matching ProcessInputs should be included
// ================================================
describe('Test 1: No inputs array (filtered by ProcessInputs)', () => {
it('should include only entity fields matching ProcessInputs element names', async () => {
const shipment = {
ID: '550e8400-e29b-41d4-a716-446655440000',
status: 'PENDING',
shipmentDate: '2026-01-15',
expectedDelivery: '2026-01-25',
origin: 'Berlin, Germany',
destination: 'Rome, Italy',
totalValue: 2500.0,
notes: 'Handle with care',
};
const response = await POST('/odata/v4/annotation/StartNoInput', shipment);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// Only fields matching ProcessInputs (status, origin) should be present
expect(context).toEqual({
status: shipment.status,
origin: shipment.origin,
});
});
});
// ================================================
// Test 2: With inputs array on selected fields
// Only specified fields should be included in context
// ================================================
describe('Test 2: inputs array with selected fields', () => {
it('should include only specified fields in process context', async () => {
const shipment = {
ID: '550e8400-e29b-41d4-a716-446655440001',
status: 'PENDING',
shipmentDate: '2026-01-15',
expectedDelivery: '2026-01-25',
origin: 'Berlin, Germany',
destination: 'Rome, Italy',
totalValue: 2500.0,
};
const response = await POST('/odata/v4/annotation/StartSelectedInput', shipment);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// Only specified fields should be present: ID, shipmentDate, origin
expect(context).toEqual({
ID: shipment.ID,
shipmentDate: shipment.shipmentDate,
origin: shipment.origin,
});
});
});
// ================================================
// Test 3: With inputs array with custom aliases
// Field should be renamed in context using { path: $self.field, as: 'Alias' }
// ================================================
describe('Test 3: inputs array with custom alias', () => {
it('should rename fields according to alias in process context', async () => {
const shipment = {
ID: '550e8400-e29b-41d4-a716-446655440002',
status: 'PENDING',
shipmentDate: '2026-01-15',
expectedDelivery: '2026-01-25',
origin: 'Berlin, Germany',
destination: 'Rome, Italy',
totalValue: 2500.0,
};
const response = await POST('/odata/v4/annotation/StartAliasInput', shipment);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// ID should remain as ID (no alias)
expect(context).toEqual({
ID: shipment.ID,
ProcessStartDate: shipment.shipmentDate, // alias
SourceLocation: shipment.origin, // alias
TargetLocation: shipment.destination, // alias
Amount: shipment.totalValue, // alias
});
});
});
// ================================================
// Test 4: With nested Composition in inputs (all child fields)
// Include composition items with all their fields using $self.items
// ================================================
describe('Test 4: Nested Composition in inputs (all child fields)', () => {
it('should include composition items with all their fields', async () => {
const order = {
ID: '550e8400-e29b-41d4-a716-446655440003',
status: 'PENDING',
shipmentDate: '2026-01-15',
items: [
{
ID: 'item-001',
title: 'Product A',
quantity: 5,
price: 100.0,
parentID: '550e8400-e29b-41d4-a716-446655440003',
},
{
ID: 'item-002',
title: 'Product B',
quantity: 3,
price: 50.0,
parentID: '550e8400-e29b-41d4-a716-446655440003',
},
],
};
const response = await POST('/odata/v4/annotation/StartNestedComposition', order);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// Specified fields in inputs array
expect(context).toEqual({
ID: order.ID,
shipmentDate: order.shipmentDate,
items: order.items, // entire composition included with all fields
});
});
});
// ================================================
// Test 5: With nested Composition (selected child fields)
// Include only selected fields from composition items using $self.items.field
// ================================================
describe('Test 5: Nested Composition with selected child fields', () => {
it('should include only specified fields from composition items', async () => {
const order = {
ID: '550e8400-e29b-41d4-a716-446655440004',
status: 'PENDING',
shipmentDate: '2026-01-15',
items: [
{
ID: 'item-001',
title: 'Product A',
quantity: 5,
price: 100.0,
},
{
ID: 'item-002',
title: 'Product B',
quantity: 3,
price: 50.0,
},
],
};
const response = await POST('/odata/v4/annotation/StartNestedSelected', order);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// Parent specified fields
expect(context).toEqual({
ID: order.ID,
shipmentDate: order.shipmentDate,
items: [
// composition included but only with specified fields: ID, title, price
{
ID: order.items[0].ID,
title: order.items[0].title,
price: order.items[0].price,
},
{
ID: order.items[1].ID,
title: order.items[1].title,
price: order.items[1].price,
},
],
});
});
});
// ================================================
// Test 6: Nested Composition with aliases
// Child fields should be renamed in context using { path: $self.items.field, as: 'Alias' }
// ================================================
describe('Test 6: Nested Composition with aliases', () => {
it('should rename fields according to aliases in nested items', async () => {
const order = {
ID: '550e8400-e29b-41d4-a716-446655440005',
status: 'PENDING',
orderDate: '2026-01-15',
items: [
{
ID: 'item-001',
productName: 'Widget',
quantity: 10,
unitPrice: 25.0,
},
],
};
const response = await POST('/odata/v4/annotation/StartNestedAlias', order);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// Parent fields with aliases
expect(context).toEqual({
ID: order.ID,
ProcessDate: order.orderDate, // alias
OrderLines: [
{
ID: order.items[0].ID,
Product: order.items[0].productName, // alias
Qty: order.items[0].quantity, // alias
Price: order.items[0].unitPrice, // alias
},
],
});
});
});
// ================================================
// Test 7: Deep cyclic path in inputs array
// Demonstrates that explicit paths work with cyclic relationships
// e.g. $self.items.shipment.items.shipment.ID
// ================================================
describe('Test 7: Deep cyclic path (items.shipment.items.shipment.ID)', () => {
it('should handle deep cyclic paths without infinite loops', async () => {
const shipment = {
ID: '550e8400-e29b-41d4-a716-446655440006',
status: 'PENDING',
items: [
{
ID: 'item-001',
title: 'Product A',
},
{
ID: 'item-002',
title: 'Product B',
},
],
};
const response = await POST('/odata/v4/annotation/StartCyclicPath', shipment);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
expect(context).toEqual({
ID: shipment.ID,
status: shipment.status,
items: [
{
ID: shipment.items[0].ID,
title: shipment.items[0].title,
shipment: {
ID: shipment.ID,
status: shipment.status,
items: [
{
ID: shipment.items[0].ID,
title: shipment.items[0].title,
shipment: { ID: shipment.ID },
},
{
ID: shipment.items[1].ID,
title: shipment.items[1].title,
shipment: { ID: shipment.ID },
},
],
},
},
{
ID: shipment.items[1].ID,
title: shipment.items[1].title,
shipment: {
ID: shipment.ID,
status: shipment.status,
items: [
{
ID: shipment.items[0].ID,
title: shipment.items[0].title,
shipment: { ID: shipment.ID },
},
{
ID: shipment.items[1].ID,
title: shipment.items[1].title,
shipment: { ID: shipment.ID },
},
],
},
},
],
});
});
});
// ================================================
// Test 8: $self wildcard - all scalar fields
// Using $self alone to include all scalar fields plus composition
// ================================================
describe('Test 8: $self wildcard (all scalar fields + composition)', () => {
it('should include all scalar fields and composition when using $self', async () => {
const order = {
ID: '550e8400-e29b-41d4-a716-446655440008',
status: 'PENDING',
shipmentDate: '2026-01-15',
totalValue: 2500.0,
items: [
{
ID: 'item-001',
title: 'Product A',
quantity: 5,
},
{
ID: 'item-002',
title: 'Product B',
quantity: 3,
},
],
};
const response = await POST('/odata/v4/annotation/StartSelfWildcard', order);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
expect(context).toEqual({
ID: order.ID,
status: order.status,
shipmentDate: order.shipmentDate,
totalValue: order.totalValue,
items: [
{
ID: order.items[0].ID,
title: order.items[0].title,
quantity: order.items[0].quantity,
parent_ID: order.ID,
},
{
ID: order.items[1].ID,
title: order.items[1].title,
quantity: order.items[1].quantity,
parent_ID: order.ID,
},
],
});
});
});
describe('Test 9: $self wildcard with field alias override', () => {
it('should include all scalar fields AND the aliased field (ID appears twice: as ID and as OrderId)', async () => {
const order = {
ID: '550e8400-e29b-41d4-a716-446655440009',
status: 'NEW',
shipmentDate: '2026-02-20',
totalValue: 999.99,
items: [{ ID: 'item-a01', title: 'Widget', quantity: 10 }],
};
const response = await POST('/odata/v4/annotation/StartSelfWildcardAlias', order);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
expect(context).toEqual({
ID: order.ID,
OrderId: order.ID,
status: order.status,
shipmentDate: order.shipmentDate,
totalValue: order.totalValue,
items: [
{
ID: order.items[0].ID,
title: order.items[0].title,
quantity: order.items[0].quantity,
parent_ID: order.ID,
},
],
});
});
});
describe('Test 10: $self.items (composition wildcard) with child field alias', () => {
it('should include all composition fields AND the aliased field (ID appears twice in items)', async () => {
const order = {
ID: '550e8400-e29b-41d4-a716-446655440010',
status: 'PROCESSING',
shipmentDate: '2026-03-15',
totalValue: 1500.0,
items: [
{ ID: 'item-b01', title: 'Gadget', quantity: 7 },
{ ID: 'item-b02', title: 'Gizmo', quantity: 3 },
],
};
const response = await POST('/odata/v4/annotation/StartCompositionWildcardAlias', order);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
expect(context).toEqual({
ID: order.ID,
status: order.status,
items: [
{
ID: order.items[0].ID,
ItemId: order.items[0].ID,
title: order.items[0].title,
quantity: order.items[0].quantity,
parent_ID: order.ID,
},
{
ID: order.items[1].ID,
ItemId: order.items[1].ID,
title: order.items[1].title,
quantity: order.items[1].quantity,
parent_ID: order.ID,
},
],
});
});
});
// ================================================
// Test 11: Multiple aliases on same scalar field
// Same field (ID) should appear under two different names
// ================================================
describe('Test 11: Multiple aliases on same scalar field', () => {
it('should include same scalar field under multiple aliases', async () => {
const order = {
ID: '550e8400-e29b-41d4-a716-446655440011',
status: 'NEW',
shipmentDate: '2026-04-01',
totalValue: 1234.56,
};
const response = await POST('/odata/v4/annotation/StartMultipleAliasScalar', order);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// ID should appear under both aliases: OrderId and ReferenceId
expect(context).toEqual({
OrderId: order.ID,
ReferenceId: order.ID,
});
});
});
// ================================================
// Test 12: Multiple aliases on same composition
// Same composition (items) should appear under two different names
// ================================================
describe('Test 12: Multiple aliases on same composition', () => {
it('should include same composition under multiple aliases', async () => {
const order = {
ID: '550e8400-e29b-41d4-a716-446655440012',
status: 'PROCESSING',
items: [
{ ID: 'item-c01', title: 'Alpha', quantity: 5 },
{ ID: 'item-c02', title: 'Beta', quantity: 10 },
],
};
const response = await POST('/odata/v4/annotation/StartMultipleAliasComposition', order);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// items should appear under both aliases: Orders and LineItems
expect(context).toEqual({
ID: order.ID,
Orders: [
{
ID: order.items[0].ID,
title: order.items[0].title,
quantity: order.items[0].quantity,
parent_ID: order.ID,
},
{
ID: order.items[1].ID,
title: order.items[1].title,
quantity: order.items[1].quantity,
parent_ID: order.ID,
},
],
LineItems: [
{
ID: order.items[0].ID,
title: order.items[0].title,
quantity: order.items[0].quantity,
parent_ID: order.ID,
},
{
ID: order.items[1].ID,
title: order.items[1].title,
quantity: order.items[1].quantity,
parent_ID: order.ID,
},
],
});
});
});
// ================================================
// Test 13: $self with Composition and Association
// $self should only include scalar fields, NOT compositions or associations
// ================================================
describe('Test 13: $self with Composition and Association', () => {
it('should include only scalar fields, NOT compositions or associations', async () => {
const entity = {
ID: '550e8400-e29b-41d4-a716-446655440013',
status: 'PENDING',
author_ID: '550e8400-e29b-41d4-a716-446655440099',
};
const response = await POST('/odata/v4/annotation/StartSelfWithAssoc', entity);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// $self should only include scalar fields: ID, status
// Should NOT include: items (composition), author (association)
expect(context).toEqual({
ID: entity.ID,
status: entity.status,
author_ID: entity.author_ID,
});
});
});
// ================================================
// Test 14: No inputs with Composition and Association
// ProcessInputs type matches all scalar fields, so all should be included
// ================================================
describe('Test 14: No inputs with Composition and Association', () => {
it('should include all scalar fields matching ProcessInputs', async () => {
const author = {
ID: '550e8400-e29b-41d4-a716-446655440099',
};
const entity = {
ID: '550e8400-e29b-41d4-a716-446655440014',
status: 'PENDING',
author_ID: author.ID,
};
// Create author first
await POST('/odata/v4/annotation/StartNoInputWithAssocAuthors', author);
const response = await POST('/odata/v4/annotation/StartNoInputWithAssoc', entity);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// ProcessInputs has {ID, status, author_ID} — all match entity scalar fields
expect(context).toEqual({
ID: entity.ID,
status: entity.status,
author_ID: author.ID,
});
});
});
// ================================================
// Test 15: $self.author - explicitly include association
// Should expand the author association with all its fields
// ================================================
describe('Test 15: $self.author - explicitly include association', () => {
it('should include the author association expanded with all its fields', async () => {
const author = {
ID: '550e8400-e29b-41d4-a716-446655440098',
name: 'John Doe',
};
const entity = {
ID: '550e8400-e29b-41d4-a716-446655440015',
status: 'PENDING',
items: [{ ID: 'item-f01', title: 'Item A', quantity: 5 }],
author_ID: author.ID,
};
// Create author first
await POST('/odata/v4/annotation/StartWithAuthorInputAuthors', author);
const response = await POST('/odata/v4/annotation/StartWithAuthorInput', entity);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// inputs: [$self.ID, $self.status, $self.author]
// Should include ID, status, and author expanded (NOT items, NOT author_ID)
expect(context).toEqual({
ID: entity.ID,
status: entity.status,
author_ID: author.ID,
author: {
ID: author.ID,
name: author.name,
},
});
});
});
// ================================================
// Test 16: No inputs, ProcessInputs exists but zero entity fields match
// Should send empty context {}
// ================================================
describe('Test 16: No inputs, ProcessInputs exists but zero fields match', () => {
it('should send empty context when no entity fields match ProcessInputs', async () => {
const entity = {
ID: '550e8400-e29b-41d4-a716-44665544ff01',
shipmentDate: '2026-01-15',
expectedDelivery: '2026-01-25',
totalValue: 2500.0,
notes: 'Handle with care',
};
const response = await POST('/odata/v4/annotation/StartNoInputZeroMatch', entity);
expect(response.status).toBe(201);
expect(foundMessages.length).toBe(1);
const context = getStartContext();
expect(context).toBeDefined();
// ProcessInputs has {status, origin} but entity has {ID, shipmentDate, expectedDelivery, totalValue, notes}
// No field names match, so context should be empty
expect(context).toEqual({});
});
});
});