mx20022-cli 0.2.0

CLI tool for inspecting, validating, and translating ISO 20022 and SWIFT MT financial messages
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
//! Integration tests for `mx20022-cli`.
//!
//! These tests compile and invoke the binary directly via `std::process::Command`
//! so that real argument parsing, I/O, and exit-code behaviour is exercised.

use std::path::PathBuf;
use std::process::Command;

fn bin_path() -> PathBuf {
    // The integration-test binary is always compiled to the same target tree as
    // the crate under test.  `CARGO_BIN_EXE_mx20022-cli` is set by Cargo when
    // running integration tests.
    PathBuf::from(env!("CARGO_BIN_EXE_mx20022-cli"))
}

fn testdata(rel: &str) -> PathBuf {
    // Cargo sets CARGO_MANIFEST_DIR to the crate root.
    let crate_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    crate_root
        .join("../../testdata")
        .join(rel)
        .canonicalize()
        .unwrap_or_else(|_| panic!("testdata path not found: {rel}"))
}

// ---------------------------------------------------------------------------
// inspect
// ---------------------------------------------------------------------------

#[test]
fn inspect_head_xml_prints_message_type() {
    let output = Command::new(bin_path())
        .args([
            "inspect",
            &testdata("xml/head/head_001_001_04_minimal.xml").to_string_lossy(),
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        output.status.success(),
        "inspect should exit 0, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("head.001.001.04"),
        "expected message type in output, got:\n{stdout}"
    );
    assert!(stdout.contains("head"), "expected family in output");
}

#[test]
fn inspect_pacs_008_xml_prints_message_type() {
    let output = Command::new(bin_path())
        .args([
            "inspect",
            &testdata("xml/pacs/pacs_008_001_13_minimal.xml").to_string_lossy(),
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("pacs.008.001.13"), "stdout: {stdout}");
}

#[test]
fn inspect_pacs_002_xml_prints_message_type() {
    let output = Command::new(bin_path())
        .args([
            "inspect",
            &testdata("xml/pacs/pacs_002_001_14_minimal.xml").to_string_lossy(),
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("pacs.002.001.14"), "stdout: {stdout}");
}

#[test]
fn inspect_nonexistent_file_exits_nonzero() {
    let output = Command::new(bin_path())
        .args(["inspect", "/nonexistent/path/message.xml"])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        !output.status.success(),
        "inspect on missing file should exit non-zero"
    );
}

#[test]
fn inspect_rejects_oversized_file() {
    let big = std::env::temp_dir().join("mx20022_test_oversized_inspect.xml");
    std::fs::write(&big, vec![b'x'; 11 * 1024 * 1024]).unwrap();
    let out = Command::new(bin_path())
        .args(["inspect", &big.to_string_lossy()])
        .output()
        .unwrap();
    let _ = std::fs::remove_file(&big);
    assert!(!out.status.success());
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("too large"),
        "expected 'too large' in: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// validate
// ---------------------------------------------------------------------------

#[test]
fn validate_valid_pacs_exits_zero() {
    let output = Command::new(bin_path())
        .args([
            "validate",
            &testdata("xml/pacs/pacs_008_001_13_minimal.xml").to_string_lossy(),
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        output.status.success(),
        "validate on valid message should exit 0, stderr: {}\nstdout: {}",
        String::from_utf8_lossy(&output.stderr),
        String::from_utf8_lossy(&output.stdout)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("OK"),
        "expected OK in output, got:\n{stdout}"
    );
}

#[test]
fn validate_valid_pacs_002_exits_zero() {
    let output = Command::new(bin_path())
        .args([
            "validate",
            &testdata("xml/pacs/pacs_002_001_14_minimal.xml").to_string_lossy(),
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        output.status.success(),
        "validate on valid pacs.002 should exit 0, stderr: {}\nstdout: {}",
        String::from_utf8_lossy(&output.stderr),
        String::from_utf8_lossy(&output.stdout)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("OK"),
        "expected OK in output, got:\n{stdout}"
    );
}

#[test]
fn validate_invalid_bic_exits_nonzero() {
    let output = Command::new(bin_path())
        .args([
            "validate",
            &testdata("xml/pacs/pacs_008_invalid_bic.xml").to_string_lossy(),
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        !output.status.success(),
        "validate on invalid message should exit non-zero"
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("ERROR"),
        "expected ERROR findings in output, got:\n{stdout}"
    );
    assert!(
        stdout.contains("BIC_CHECK"),
        "expected BIC_CHECK rule ID in output, got:\n{stdout}"
    );
}

#[test]
fn validate_head_xml_exits_zero() {
    let output = Command::new(bin_path())
        .args([
            "validate",
            &testdata("xml/head/head_001_001_04_minimal.xml").to_string_lossy(),
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        output.status.success(),
        "validate on valid head message should exit 0, stdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

// ---------------------------------------------------------------------------
// codegen
// ---------------------------------------------------------------------------

#[test]
fn codegen_head_xsd_produces_rust() {
    let xsd_path = {
        let crate_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        crate_root
            .join("../../schemas/head/head.001.001.04.xsd")
            .canonicalize()
            .expect("head.001.001.04.xsd not found")
    };

    let output = Command::new(bin_path())
        .args(["codegen", &xsd_path.to_string_lossy()])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        output.status.success(),
        "codegen should exit 0, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("pub struct"),
        "expected generated structs in output, got:\n{stdout}"
    );
    assert!(
        stdout.contains("BusinessApplicationHeaderV04"),
        "expected BusinessApplicationHeaderV04 in generated code"
    );
}

#[test]
fn codegen_writes_to_output_file() {
    let xsd_path = {
        let crate_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        crate_root
            .join("../../schemas/head/head.001.001.04.xsd")
            .canonicalize()
            .expect("head.001.001.04.xsd not found")
    };

    let out_file = std::env::temp_dir().join("mx20022_cli_codegen_test.rs");

    let output = Command::new(bin_path())
        .args([
            "codegen",
            &xsd_path.to_string_lossy(),
            "--output",
            &out_file.to_string_lossy(),
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        output.status.success(),
        "codegen with --output should exit 0, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let written = std::fs::read_to_string(&out_file).expect("output file not written");
    assert!(written.contains("pub struct BusinessApplicationHeaderV04"));

    // Clean up.
    let _ = std::fs::remove_file(&out_file);
}

#[test]
fn codegen_nonexistent_xsd_exits_nonzero() {
    let output = Command::new(bin_path())
        .args(["codegen", "/nonexistent/schema.xsd"])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(!output.status.success());
}

#[test]
fn codegen_rejects_oversized_file() {
    let big = std::env::temp_dir().join("mx20022_test_oversized_codegen.xsd");
    std::fs::write(&big, vec![b'x'; 11 * 1024 * 1024]).unwrap();
    let out = Command::new(bin_path())
        .args(["codegen", &big.to_string_lossy()])
        .output()
        .unwrap();
    let _ = std::fs::remove_file(&big);
    assert!(!out.status.success());
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("too large"),
        "expected 'too large' in: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// validate --scheme
// ---------------------------------------------------------------------------

fn scheme_testdata(rel: &str) -> PathBuf {
    let crate_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    crate_root
        .join("../../testdata/schemes")
        .join(rel)
        .canonicalize()
        .unwrap_or_else(|_| panic!("scheme testdata path not found: {rel}"))
}

#[test]
fn validate_with_scheme_fednow_valid_exits_zero() {
    let output = Command::new(bin_path())
        .args([
            "validate",
            &scheme_testdata("fednow/valid_pacs008.xml").to_string_lossy(),
            "--scheme",
            "fednow",
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        output.status.success(),
        "validate --scheme fednow should exit 0 for valid input, stderr: {}\nstdout: {}",
        String::from_utf8_lossy(&output.stderr),
        String::from_utf8_lossy(&output.stdout)
    );
}

#[test]
fn validate_with_scheme_sepa_valid_exits_zero() {
    let output = Command::new(bin_path())
        .args([
            "validate",
            &scheme_testdata("sepa/valid_pacs008.xml").to_string_lossy(),
            "--scheme",
            "sepa",
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        output.status.success(),
        "validate --scheme sepa should exit 0 for valid input, stderr: {}\nstdout: {}",
        String::from_utf8_lossy(&output.stderr),
        String::from_utf8_lossy(&output.stdout)
    );
}

#[test]
fn validate_with_scheme_cbpr_valid_exits_zero() {
    let output = Command::new(bin_path())
        .args([
            "validate",
            &scheme_testdata("cbpr/valid_pacs008.xml").to_string_lossy(),
            "--scheme",
            "cbpr",
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        output.status.success(),
        "validate --scheme cbpr should exit 0 for valid input, stderr: {}\nstdout: {}",
        String::from_utf8_lossy(&output.stderr),
        String::from_utf8_lossy(&output.stdout)
    );
}

#[test]
fn validate_with_scheme_fednow_invalid_catches_error() {
    let output = Command::new(bin_path())
        .args([
            "validate",
            &scheme_testdata("fednow/invalid_eur.xml").to_string_lossy(),
            "--scheme",
            "fednow",
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        !output.status.success(),
        "validate --scheme fednow should exit non-zero for invalid input"
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("FEDNOW_CURRENCY"),
        "expected FEDNOW_CURRENCY in output, got:\n{stdout}"
    );
}

#[test]
fn validate_rejects_oversized_file() {
    let big = std::env::temp_dir().join("mx20022_test_oversized.xml");
    std::fs::write(&big, vec![b'x'; 11 * 1024 * 1024]).unwrap();
    let out = Command::new(bin_path())
        .args(["validate", &big.to_string_lossy()])
        .output()
        .unwrap();
    let _ = std::fs::remove_file(&big);
    assert!(!out.status.success());
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("too large"),
        "expected 'too large' in: {stderr}"
    );
}

#[test]
fn validate_with_scheme_sepa_invalid_catches_error() {
    let out = Command::new(bin_path())
        .args([
            "validate",
            &scheme_testdata("sepa/invalid_usd.xml").to_string_lossy(),
            "--scheme",
            "sepa",
        ])
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "validate --scheme sepa should exit non-zero for invalid input"
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("SEPA_CURRENCY"),
        "expected SEPA_CURRENCY in: {stdout}"
    );
}

#[test]
fn validate_with_scheme_cbpr_invalid_catches_error() {
    let out = Command::new(bin_path())
        .args([
            "validate",
            &scheme_testdata("cbpr/invalid_missing_uetr.xml").to_string_lossy(),
            "--scheme",
            "cbpr",
        ])
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "validate --scheme cbpr should exit non-zero for invalid input"
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("CBPR_UETR_REQUIRED"),
        "expected CBPR_UETR_REQUIRED in: {stdout}"
    );
}

#[test]
fn validate_with_unknown_scheme_exits_nonzero() {
    let output = Command::new(bin_path())
        .args([
            "validate",
            &testdata("xml/pacs/pacs_008_001_13_minimal.xml").to_string_lossy(),
            "--scheme",
            "nonexistent",
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        !output.status.success(),
        "validate with unknown scheme should exit non-zero"
    );
}

// ---------------------------------------------------------------------------
// translate
// ---------------------------------------------------------------------------

#[test]
fn translate_mt103_to_pacs008_exits_zero() {
    let output = Command::new(bin_path())
        .args([
            "translate",
            &testdata("mt/mt103.txt").to_string_lossy(),
            "--to",
            "pacs008",
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        output.status.success(),
        "translate mt103 -> pacs008 should exit 0, stderr: {}\nstdout: {}",
        String::from_utf8_lossy(&output.stderr),
        String::from_utf8_lossy(&output.stdout)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("FIToFICstmrCdtTrf") || stdout.contains("pacs.008"),
        "expected pacs.008 content in output, got:\n{stdout}"
    );
}

#[test]
fn translate_nonexistent_file_exits_nonzero() {
    let output = Command::new(bin_path())
        .args([
            "translate",
            "/nonexistent/path/message.txt",
            "--to",
            "pacs008",
        ])
        .output()
        .expect("failed to run mx20022-cli");

    assert!(
        !output.status.success(),
        "translate on missing file should exit non-zero"
    );
}