data-modelling-core 2.4.0

Core SDK library for model operations across platforms
Documentation
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
//! Tests for ODPS (Open Data Product Standard) import/export

use data_modelling_core::export::odps::ODPSExporter;
use data_modelling_core::import::odps::ODPSImporter;
use data_modelling_core::models::Tag;
use data_modelling_core::models::odps::*;

#[test]
fn test_odps_import_basic() {
    let yaml = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
name: customer-data-product
version: 1.0.0
status: active
domain: customer-service
tenant: acme-corp
tags:
  - customer
  - data-product
description:
  purpose: Customer data product for analytics
  usage: Use for customer analytics and reporting
  limitations: Data is updated daily
"#;

    let importer = ODPSImporter::new();
    let product = importer.import(yaml).unwrap();

    assert_eq!(product.api_version, "v1.0.0");
    assert_eq!(product.kind, "DataProduct");
    assert_eq!(product.id, "550e8400-e29b-41d4-a716-446655440000");
    assert_eq!(product.name, Some("customer-data-product".to_string()));
    assert_eq!(product.version, Some("1.0.0".to_string()));
    assert_eq!(product.status, ODPSStatus::Active);
    assert_eq!(product.domain, Some("customer-service".to_string()));
    assert_eq!(product.tenant, Some("acme-corp".to_string()));
    assert_eq!(product.tags.len(), 2);

    if let Some(description) = &product.description {
        assert_eq!(
            description.purpose,
            Some("Customer data product for analytics".to_string())
        );
        assert_eq!(
            description.usage,
            Some("Use for customer analytics and reporting".to_string())
        );
        assert_eq!(
            description.limitations,
            Some("Data is updated daily".to_string())
        );
    } else {
        panic!("Description should be present");
    }
}

#[test]
fn test_odps_import_with_ports() {
    let yaml = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
name: customer-data-product
version: 1.0.0
status: active
inputPorts:
  - name: customer-input
    version: 1.0.0
    contractId: 660e8400-e29b-41d4-a716-446655440001
outputPorts:
  - name: customer-output
    version: 1.0.0
    contractId: 660e8400-e29b-41d4-a716-446655440002
    description: Processed customer data
"#;

    let importer = ODPSImporter::new();
    let product = importer.import(yaml).unwrap();

    assert!(product.input_ports.is_some());
    assert_eq!(product.input_ports.as_ref().unwrap().len(), 1);
    assert_eq!(
        product.input_ports.as_ref().unwrap()[0].name,
        "customer-input"
    );
    assert_eq!(
        product.input_ports.as_ref().unwrap()[0].contract_id,
        "660e8400-e29b-41d4-a716-446655440001"
    );

    assert!(product.output_ports.is_some());
    assert_eq!(product.output_ports.as_ref().unwrap().len(), 1);
    assert_eq!(
        product.output_ports.as_ref().unwrap()[0].name,
        "customer-output"
    );
    assert_eq!(
        product.output_ports.as_ref().unwrap()[0].contract_id,
        Some("660e8400-e29b-41d4-a716-446655440002".to_string())
    );
    assert_eq!(
        product.output_ports.as_ref().unwrap()[0].description,
        Some("Processed customer data".to_string())
    );
}

#[test]
fn test_odps_contractid_validation() {
    let yaml = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
name: customer-data-product
version: 1.0.0
status: active
inputPorts:
  - name: customer-input
    version: 1.0.0
    contractId: unknown-contract-id
"#;

    // Without validation, should succeed
    let importer = ODPSImporter::new();
    let product = importer.import(yaml).unwrap();
    assert_eq!(
        product.input_ports.as_ref().unwrap()[0].contract_id,
        "unknown-contract-id"
    );

    // With validation, should fail
    let importer = ODPSImporter::with_table_ids(vec!["known-contract-id".to_string()]);
    let result = importer.import(yaml);
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("unknown contractId")
    );
}

#[test]
fn test_odps_export_basic() {
    let product = ODPSDataProduct {
        api_version: "v1.0.0".to_string(),
        kind: "DataProduct".to_string(),
        id: "550e8400-e29b-41d4-a716-446655440000".to_string(),
        name: Some("customer-data-product".to_string()),
        version: Some("1.0.0".to_string()),
        status: ODPSStatus::Active,
        domain: Some("customer-service".to_string()),
        tenant: Some("acme-corp".to_string()),
        authoritative_definitions: None,
        description: Some(ODPSDescription {
            purpose: Some("Customer data product for analytics".to_string()),
            limitations: Some("Data is updated daily".to_string()),
            usage: Some("Use for customer analytics and reporting".to_string()),
            authoritative_definitions: None,
            custom_properties: None,
        }),
        custom_properties: None,
        tags: vec![
            Tag::Simple("customer".to_string()),
            Tag::Simple("data-product".to_string()),
        ],
        input_ports: None,
        output_ports: None,
        management_ports: None,
        support: None,
        team: None,
        product_created_ts: None,
        created_at: None,
        updated_at: None,
    };

    let yaml = ODPSExporter::export_product(&product);

    assert!(yaml.contains("apiVersion: v1.0.0"));
    assert!(yaml.contains("kind: DataProduct"));
    assert!(yaml.contains("id: 550e8400-e29b-41d4-a716-446655440000"));
    assert!(yaml.contains("name: customer-data-product"));
    assert!(yaml.contains("version: 1.0.0"));
    assert!(yaml.contains("status: active"));
    assert!(yaml.contains("domain: customer-service"));
    assert!(yaml.contains("tenant: acme-corp"));
    assert!(yaml.contains("tags:"));
    assert!(yaml.contains("description:"));
}

#[test]
fn test_odps_round_trip() {
    let original_yaml = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
name: customer-data-product
version: 1.0.0
status: active
domain: customer-service
inputPorts:
  - name: customer-input
    version: 1.0.0
    contractId: 660e8400-e29b-41d4-a716-446655440001
    tags:
      - input
      - customer
outputPorts:
  - name: customer-output
    version: 1.0.0
    contractId: 660e8400-e29b-41d4-a716-446655440002
    description: Processed customer data
    type: batch
managementPorts:
  - name: api
    content: discoverability
    type: rest
    url: https://api.example.com/products/customer
support:
  - channel: slack
    url: https://slack.example.com/channels/customer-support
    tool: slack
    scope: interactive
team:
  name: Customer Data Team
  description: Team responsible for customer data products
  members:
    - username: john.doe@example.com
      name: John Doe
      role: Data Product Owner
"#;

    // Import
    let importer = ODPSImporter::new();
    let product = importer.import(original_yaml).unwrap();

    // Verify import
    assert_eq!(product.name, Some("customer-data-product".to_string()));
    assert!(product.input_ports.is_some());
    assert!(product.output_ports.is_some());
    assert!(product.management_ports.is_some());
    assert!(product.support.is_some());
    assert!(product.team.is_some());

    // Export
    let exported_yaml = ODPSExporter::export_product(&product);

    // Import again
    let product2 = importer.import(&exported_yaml).unwrap();

    // Verify round-trip
    assert_eq!(product.id, product2.id);
    assert_eq!(product.name, product2.name);
    assert_eq!(product.version, product2.version);
    assert_eq!(product.status, product2.status);
    assert_eq!(product.domain, product2.domain);
    assert_eq!(
        product.input_ports.as_ref().map(|p| p.len()),
        product2.input_ports.as_ref().map(|p| p.len())
    );
    assert_eq!(
        product.output_ports.as_ref().map(|p| p.len()),
        product2.output_ports.as_ref().map(|p| p.len())
    );
}

#[test]
fn test_odps_enhanced_tags() {
    let yaml = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
name: test-product
version: 1.0.0
status: active
tags:
  - simple-tag
  - Environment:Production
  - SecondaryDomains:[finance, operations]
inputPorts:
  - name: input
    version: 1.0.0
    contractId: test-contract-id
    tags:
      - input-tag
      - Type:DataContract
"#;

    let importer = ODPSImporter::new();
    let product = importer.import(yaml).unwrap();

    assert_eq!(product.tags.len(), 3);
    assert_eq!(product.tags[0], Tag::Simple("simple-tag".to_string()));
    assert_eq!(
        product.tags[1],
        Tag::Pair("Environment".to_string(), "Production".to_string())
    );
    assert_eq!(
        product.tags[2],
        Tag::List(
            "SecondaryDomains".to_string(),
            vec!["finance".to_string(), "operations".to_string()]
        )
    );

    // Check input port tags
    if let Some(input_ports) = &product.input_ports {
        assert_eq!(input_ports[0].tags.len(), 2);
        assert_eq!(input_ports[0].tags[0], Tag::Simple("input-tag".to_string()));
        assert_eq!(
            input_ports[0].tags[1],
            Tag::Pair("Type".to_string(), "DataContract".to_string())
        );
    }
}

#[test]
fn test_odps_all_statuses() {
    let statuses = vec![
        ("proposed", ODPSStatus::Proposed),
        ("draft", ODPSStatus::Draft),
        ("active", ODPSStatus::Active),
        ("deprecated", ODPSStatus::Deprecated),
        ("retired", ODPSStatus::Retired),
    ];

    for (status_str, status_enum) in statuses {
        let yaml = format!(
            r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
name: test-product
version: 1.0.0
status: {}
"#,
            status_str
        );

        let importer = ODPSImporter::new();
        let product = importer.import(&yaml).unwrap();
        assert_eq!(product.status, status_enum);
    }
}

#[cfg(feature = "odps-validation")]
mod odps_validation_integration_tests {
    use data_modelling_core::import::odps::ODPSImporter;

    #[test]
    fn test_odps_validation_valid_file() {
        let valid_odps = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
status: active
name: test-product
version: 1.0.0
"#;

        let importer = ODPSImporter::new();
        let result = importer.import(valid_odps);
        assert!(
            result.is_ok(),
            "Valid ODPS file should pass validation and import"
        );
    }

    #[test]
    fn test_odps_validation_invalid_missing_required_field() {
        let invalid_odps = r#"
apiVersion: v1.0.0
kind: DataProduct
# Missing 'id' field
status: active
"#;

        let importer = ODPSImporter::new();
        let result = importer.import(invalid_odps);
        assert!(
            result.is_err(),
            "ODPS file missing required field should fail validation"
        );
        if let Err(e) = result {
            let error_msg = e.to_string();
            assert!(
                error_msg.contains("validation") || error_msg.contains("id"),
                "Error message should indicate validation failure or missing id"
            );
        }
    }

    #[test]
    fn test_odps_validation_invalid_enum_value() {
        let invalid_odps = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
status: invalid-status-value
"#;

        let importer = ODPSImporter::new();
        let result = importer.import(invalid_odps);
        assert!(
            result.is_err(),
            "ODPS file with invalid enum value should fail validation"
        );
        if let Err(e) = result {
            let error_msg = e.to_string();
            assert!(
                error_msg.contains("validation") || error_msg.contains("status"),
                "Error message should indicate validation failure"
            );
        }
    }

    #[test]
    fn test_odps_validation_invalid_url_format() {
        // Note: JSON Schema format validation may be lenient depending on validator implementation
        // This test verifies validation runs, but URL format validation may not be strictly enforced
        let invalid_odps = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
status: active
support:
  - channel: email
    url: not-a-valid-url-format
"#;

        let importer = ODPSImporter::new();
        let result = importer.import(invalid_odps);
        // URL format validation may pass if jsonschema doesn't strictly validate format
        // The important thing is that validation runs and other validations work
        // If validation fails, that's good; if it passes, format validation may be lenient
        if let Err(err) = result {
            let error_msg = err.to_string();
            assert!(
                error_msg.contains("validation") || error_msg.contains("url"),
                "Error message should indicate validation failure"
            );
        }
        // If validation passes, that's acceptable - format validation may be lenient
    }

    #[test]
    fn test_odps_validation_missing_nested_required_field() {
        let invalid_odps = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
status: active
support:
  - channel: email
    # Missing 'url' field
"#;

        let importer = ODPSImporter::new();
        let result = importer.import(invalid_odps);
        assert!(
            result.is_err(),
            "ODPS file with missing nested required field should fail validation"
        );
        if let Err(e) = result {
            let error_msg = e.to_string();
            assert!(
                error_msg.contains("validation") || error_msg.contains("url"),
                "Error message should indicate validation failure"
            );
        }
    }
}

#[cfg(feature = "odps-validation")]
mod odps_field_preservation_tests {
    use super::*;

    #[test]
    fn test_odps_field_preservation_all_optional_fields() {
        let yaml = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
name: test-product
version: 1.0.0
status: active
domain: test-domain
tenant: test-tenant
tags:
  - tag1
  - tag2
description:
  purpose: Test purpose
  usage: Test usage
  limitations: Test limitations
customProperties:
  - property: custom1
    value: value1
  - property: custom2
    value: value2
authoritativeDefinitions:
  - type: businessDefinition
    url: https://example.com/def1
    description: Definition 1
  - type: tutorial
    url: https://example.com/tut1
support:
  - channel: email
    url: https://example.com/support
  - channel: slack
    url: https://example.com/slack
team:
  name: Test Team
  members:
    - username: user1
      name: User One
      role: Developer
inputPorts:
  - name: input1
    version: 1.0.0
    contractId: 660e8400-e29b-41d4-a716-446655440001
    tags:
      - input-tag
    customProperties:
      - property: port-prop
        value: port-value
outputPorts:
  - name: output1
    version: 1.0.0
    contractId: 660e8400-e29b-41d4-a716-446655440002
    description: Output description
    tags:
      - output-tag
    customProperties:
      - property: output-prop
        value: output-value
productCreatedTs: 2024-01-01T00:00:00Z
"#;

        let importer = ODPSImporter::new();
        let product = importer.import(yaml).unwrap();

        // Export back
        let exporter = ODPSExporter;
        let exported_yaml = exporter.export(&product).unwrap();

        // Import again to verify round-trip
        let product2 = importer.import(&exported_yaml).unwrap();

        // Verify all fields are preserved
        assert_eq!(product.id, product2.id);
        assert_eq!(product.name, product2.name);
        assert_eq!(product.version, product2.version);
        assert_eq!(product.status, product2.status);
        assert_eq!(product.domain, product2.domain);
        assert_eq!(product.tenant, product2.tenant);
        assert_eq!(product.tags.len(), product2.tags.len());
        assert_eq!(product.tags, product2.tags);

        // Verify description
        assert_eq!(
            product.description.is_some(),
            product2.description.is_some()
        );
        if let (Some(desc1), Some(desc2)) = (&product.description, &product2.description) {
            assert_eq!(desc1.purpose, desc2.purpose);
            assert_eq!(desc1.usage, desc2.usage);
            assert_eq!(desc1.limitations, desc2.limitations);
        }

        // Verify custom properties
        assert_eq!(
            product.custom_properties.is_some(),
            product2.custom_properties.is_some()
        );
        if let (Some(props1), Some(props2)) =
            (&product.custom_properties, &product2.custom_properties)
        {
            assert_eq!(props1.len(), props2.len());
            for (p1, p2) in props1.iter().zip(props2.iter()) {
                assert_eq!(p1.property, p2.property);
                assert_eq!(p1.value, p2.value);
            }
        }

        // Verify authoritative definitions
        assert_eq!(
            product.authoritative_definitions.is_some(),
            product2.authoritative_definitions.is_some()
        );
        if let (Some(defs1), Some(defs2)) = (
            &product.authoritative_definitions,
            &product2.authoritative_definitions,
        ) {
            assert_eq!(defs1.len(), defs2.len());
            for (d1, d2) in defs1.iter().zip(defs2.iter()) {
                assert_eq!(d1.r#type, d2.r#type);
                assert_eq!(d1.url, d2.url);
                assert_eq!(d1.description, d2.description);
            }
        }

        // Verify support
        assert_eq!(product.support.is_some(), product2.support.is_some());
        if let (Some(sup1), Some(sup2)) = (&product.support, &product2.support) {
            assert_eq!(sup1.len(), sup2.len());
            for (s1, s2) in sup1.iter().zip(sup2.iter()) {
                assert_eq!(s1.channel, s2.channel);
                assert_eq!(s1.url, s2.url);
            }
        }

        // Verify team
        assert_eq!(product.team.is_some(), product2.team.is_some());
        if let (Some(team1), Some(team2)) = (&product.team, &product2.team) {
            assert_eq!(team1.name, team2.name);
            assert_eq!(team1.members.is_some(), team2.members.is_some());
            if let (Some(mem1), Some(mem2)) = (&team1.members, &team2.members) {
                assert_eq!(mem1.len(), mem2.len());
                for (m1, m2) in mem1.iter().zip(mem2.iter()) {
                    assert_eq!(m1.username, m2.username);
                    assert_eq!(m1.name, m2.name);
                    assert_eq!(m1.role, m2.role);
                }
            }
        }

        // Verify input ports with nested structures
        assert_eq!(
            product.input_ports.is_some(),
            product2.input_ports.is_some()
        );
        if let (Some(ports1), Some(ports2)) = (&product.input_ports, &product2.input_ports) {
            assert_eq!(ports1.len(), ports2.len());
            for (p1, p2) in ports1.iter().zip(ports2.iter()) {
                assert_eq!(p1.name, p2.name);
                assert_eq!(p1.tags.len(), p2.tags.len());
                assert_eq!(p1.tags, p2.tags);
                assert_eq!(
                    p1.custom_properties.is_some(),
                    p2.custom_properties.is_some()
                );
                if let (Some(cp1), Some(cp2)) = (&p1.custom_properties, &p2.custom_properties) {
                    assert_eq!(cp1.len(), cp2.len());
                }
            }
        }

        // Verify output ports with nested structures
        assert_eq!(
            product.output_ports.is_some(),
            product2.output_ports.is_some()
        );
        if let (Some(ports1), Some(ports2)) = (&product.output_ports, &product2.output_ports) {
            assert_eq!(ports1.len(), ports2.len());
            for (p1, p2) in ports1.iter().zip(ports2.iter()) {
                assert_eq!(p1.name, p2.name);
                assert_eq!(p1.description, p2.description);
                assert_eq!(p1.tags.len(), p2.tags.len());
                assert_eq!(p1.tags, p2.tags);
                assert_eq!(
                    p1.custom_properties.is_some(),
                    p2.custom_properties.is_some()
                );
            }
        }

        assert_eq!(product.product_created_ts, product2.product_created_ts);
    }

    #[test]
    fn test_odps_field_preservation_nested_structures() {
        let yaml = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
status: active
inputPorts:
  - name: input1
    version: 1.0.0
    contractId: 660e8400-e29b-41d4-a716-446655440001
    tags:
      - nested-tag-1
      - nested-tag-2
    customProperties:
      - property: nested-prop-1
        value: nested-value-1
      - property: nested-prop-2
        value: nested-value-2
    authoritativeDefinitions:
      - type: businessDefinition
        url: https://example.com/nested-def
        description: Nested definition
outputPorts:
  - name: output1
    version: 1.0.0
    contractId: 660e8400-e29b-41d4-a716-446655440002
    tags:
      - output-nested-tag
    customProperties:
      - property: output-nested-prop
        value: output-nested-value
"#;

        let importer = ODPSImporter::new();
        let product = importer.import(yaml).unwrap();

        // Verify nested structures are imported correctly
        assert!(product.input_ports.is_some());
        let input_port = &product.input_ports.as_ref().unwrap()[0];
        assert_eq!(input_port.tags.len(), 2);
        assert!(input_port.custom_properties.is_some());
        assert_eq!(input_port.custom_properties.as_ref().unwrap().len(), 2);
        assert!(input_port.authoritative_definitions.is_some());
        assert_eq!(
            input_port.authoritative_definitions.as_ref().unwrap().len(),
            1
        );

        // Export and re-import
        let exporter = ODPSExporter;
        let exported_yaml = exporter.export(&product).unwrap();
        let product2 = importer.import(&exported_yaml).unwrap();

        // Verify nested structures are preserved
        assert!(product2.input_ports.is_some());
        let input_port2 = &product2.input_ports.as_ref().unwrap()[0];
        assert_eq!(input_port.tags, input_port2.tags);
        assert_eq!(
            input_port.custom_properties.as_ref().unwrap().len(),
            input_port2.custom_properties.as_ref().unwrap().len()
        );
        assert_eq!(
            input_port.authoritative_definitions.as_ref().unwrap().len(),
            input_port2
                .authoritative_definitions
                .as_ref()
                .unwrap()
                .len()
        );

        // Verify output port nested structures
        assert!(product2.output_ports.is_some());
        let output_port2 = &product2.output_ports.as_ref().unwrap()[0];
        assert_eq!(
            product.output_ports.as_ref().unwrap()[0].tags,
            output_port2.tags
        );
        assert_eq!(
            product.output_ports.as_ref().unwrap()[0]
                .custom_properties
                .as_ref()
                .unwrap()
                .len(),
            output_port2.custom_properties.as_ref().unwrap().len()
        );
    }

    #[test]
    fn test_odps_field_preservation_empty_optional_arrays() {
        let yaml = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
status: active
tags: []
inputPorts: []
outputPorts: []
support: []
customProperties: []
authoritativeDefinitions: []
"#;

        let importer = ODPSImporter::new();
        let product = importer.import(yaml).unwrap();

        // Verify empty arrays are preserved
        assert_eq!(product.tags.len(), 0);
        assert!(product.input_ports.is_some());
        assert_eq!(product.input_ports.as_ref().unwrap().len(), 0);
        assert!(product.output_ports.is_some());
        assert_eq!(product.output_ports.as_ref().unwrap().len(), 0);
        assert!(product.support.is_some());
        assert_eq!(product.support.as_ref().unwrap().len(), 0);
        assert!(product.custom_properties.is_some());
        assert_eq!(product.custom_properties.as_ref().unwrap().len(), 0);
        assert!(product.authoritative_definitions.is_some());
        assert_eq!(product.authoritative_definitions.as_ref().unwrap().len(), 0);

        // Export and verify empty arrays are preserved in YAML
        let exporter = ODPSExporter;
        let exported_yaml = exporter.export(&product).unwrap();

        // Re-import to verify round-trip
        let product2 = importer.import(&exported_yaml).unwrap();
        assert_eq!(product2.tags.len(), 0);
        assert!(product2.input_ports.is_some());
        assert_eq!(product2.input_ports.as_ref().unwrap().len(), 0);
        assert!(product2.output_ports.is_some());
        assert_eq!(product2.output_ports.as_ref().unwrap().len(), 0);
    }
}

#[cfg(feature = "odps-validation")]
mod odps_feature_flag_tests {
    use super::*;

    #[test]
    fn test_odps_import_with_validation_enabled() {
        let valid_odps = r#"
apiVersion: v1.0.0
kind: DataProduct
id: 550e8400-e29b-41d4-a716-446655440000
status: active
"#;

        let importer = ODPSImporter::new();
        let result = importer.import(valid_odps);
        // With validation enabled, valid file should succeed
        assert!(
            result.is_ok(),
            "Valid ODPS file should import successfully with validation enabled"
        );
    }

    #[test]
    fn test_odps_import_validation_error_when_enabled() {
        let invalid_odps = r#"
apiVersion: v1.0.0
kind: DataProduct
# Missing 'id' field
status: active
"#;

        let importer = ODPSImporter::new();
        let result = importer.import(invalid_odps);
        // With validation enabled, invalid file should fail
        assert!(
            result.is_err(),
            "Invalid ODPS file should fail validation when feature enabled"
        );
        if let Err(e) = result {
            let error_msg = e.to_string();
            assert!(
                error_msg.contains("validation") || error_msg.contains("id"),
                "Error should indicate validation failure or missing id"
            );
        }
    }

    #[test]
    fn test_odps_export_with_validation_enabled() {
        use data_modelling_core::models::odps::{ODPSDataProduct, ODPSStatus};

        let product = ODPSDataProduct {
            api_version: "v1.0.0".to_string(),
            kind: "DataProduct".to_string(),
            id: "550e8400-e29b-41d4-a716-446655440000".to_string(),
            name: Some("test-product".to_string()),
            version: Some("1.0.0".to_string()),
            status: ODPSStatus::Active,
            domain: None,
            tenant: None,
            tags: vec![],
            description: None,
            authoritative_definitions: None,
            custom_properties: None,
            input_ports: None,
            output_ports: None,
            management_ports: None,
            support: None,
            team: None,
            product_created_ts: None,
            created_at: None,
            updated_at: None,
        };

        let exporter = ODPSExporter;
        let result = exporter.export(&product);
        // With validation enabled, valid product should export successfully
        assert!(
            result.is_ok(),
            "Valid ODPS product should export successfully with validation enabled"
        );
    }

    #[test]
    fn test_odps_export_validation_runs_when_enabled() {
        use data_modelling_core::models::odps::{ODPSDataProduct, ODPSStatus};

        let product = ODPSDataProduct {
            api_version: "v1.0.0".to_string(),
            kind: "DataProduct".to_string(),
            id: "550e8400-e29b-41d4-a716-446655440000".to_string(),
            name: Some("test-product".to_string()),
            version: Some("1.0.0".to_string()),
            status: ODPSStatus::Active,
            domain: None,
            tenant: None,
            tags: vec![],
            description: None,
            authoritative_definitions: None,
            custom_properties: None,
            input_ports: None,
            output_ports: None,
            management_ports: None,
            support: None,
            team: None,
            product_created_ts: None,
            created_at: None,
            updated_at: None,
        };

        let exporter = ODPSExporter;
        let result = exporter.export(&product);
        // With validation enabled, valid product should export successfully
        assert!(
            result.is_ok(),
            "Valid ODPS product should export successfully with validation enabled"
        );

        // Verify the exported YAML is valid by importing it back
        let exported_yaml = result.unwrap();
        let importer = ODPSImporter::new();
        let round_trip_result = importer.import(&exported_yaml);
        assert!(
            round_trip_result.is_ok(),
            "Exported YAML should be valid and importable"
        );
    }
}