From f590da4b7262bc741ead190ba20288b6c8a537e8 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Wed, 20 Nov 2024 22:51:37 +0530 Subject: [PATCH] Add tests for xsd with namespaces --- ...h_namespace_annotation_with_parse_type.bal | 16 +- ballerina/tests/xsd_test_toxml_tests.bal | 176 ++++++++++++ .../tests/xsd_test_toxml_with_namespaces.bal | 272 +++++++++++++++++- 3 files changed, 460 insertions(+), 4 deletions(-) diff --git a/ballerina/tests/xsd_sequence_test_with_namespace_annotation_with_parse_type.bal b/ballerina/tests/xsd_sequence_test_with_namespace_annotation_with_parse_type.bal index 03ee89d4..d256b73f 100644 --- a/ballerina/tests/xsd_sequence_test_with_namespace_annotation_with_parse_type.bal +++ b/ballerina/tests/xsd_sequence_test_with_namespace_annotation_with_parse_type.bal @@ -16,6 +16,9 @@ import ballerina/test; +@Name { + value: "Root" +} type XsdSequenceWithNamespaceAnnotationWithXmlValue record { @Sequence { minOccurs: 0, @@ -70,15 +73,21 @@ type Seq_EA1_NamespaceAnnotationWithXmlValue record { function testXsdSequenceWithNamespaceAnnotationWithXmlValue() returns error? { xml xmlStr; XsdSequenceWithNamespaceAnnotationWithXmlValue|Error v; + xml|Error toXmlResult; xmlStr = xml `ABCABC`; v = parseAsType(xmlStr); test:assertTrue(v is Error); test:assertEquals((v).message(), "Element(s) 'EA3' is not found in 'seq_EA1_NamespaceAnnotationWithXmlValue'"); + toXmlResult = toXml({seq_EA1_NamespaceAnnotationWithXmlValue: {EA1: "ABC", EA2: "ABC", EA3: []}}); + test:assertTrue(toXmlResult is Error); + test:assertEquals((toXmlResult).message(), "'EA3' occurs less than the min required times"); xmlStr = xml `ABCABCABABAB`; v = parseAsType(xmlStr); test:assertEquals(v, {seq_EA1_NamespaceAnnotationWithXmlValue: {EA1: "ABC", EA2: "ABC", EA3: ["AB", "AB", "AB"]}}); + // // BUG: https://github.com/ballerina-platform/ballerina-library/issues/7389 + // test:assertEquals(toXml(check v), xml `ABCABCABABAB`); xmlStr = xml `ABCABAB`; v = parseAsType(xmlStr); @@ -96,6 +105,9 @@ function testXsdSequenceWithNamespaceAnnotationWithXmlValue() returns error? { v = parseAsType(xmlStr); test:assertTrue(v is Error); test:assertEquals((v).message(), "'EA3' occurs more than the max allowed times"); + toXmlResult = toXml({seq_EA1_NamespaceAnnotationWithXmlValue: {EA3: ["AB", "AB", "AB", "AB", "AB", "AB"]}}); + test:assertTrue(toXmlResult is Error); + test:assertEquals((toXmlResult).message(), (v).message()); xmlStr = xml `ABCABAB`; v = parseAsType(xmlStr); @@ -110,6 +122,9 @@ function testXsdSequenceWithNamespaceAnnotationWithXmlValue() returns error? { v = parseAsType(xmlStr); test:assertTrue(v is Error); test:assertEquals((v).message(), "'EA3' occurs less than the min required times"); + toXmlResult = toXml({seq_EA1_NamespaceAnnotationWithXmlValue: {EA3: ["AB"]}}); + test:assertTrue(toXmlResult is Error); + test:assertEquals((toXmlResult).message(), (v).message()); xmlStr = xml `ABCAB`; v = parseAsType(xmlStr); @@ -170,7 +185,6 @@ type Seq_EA2_NamespaceAnnotationWithXmlValue record { maxOccurs: 4, minOccurs: 2 } - @Namespace { uri: "example3.com", prefix: "ea3" diff --git a/ballerina/tests/xsd_test_toxml_tests.bal b/ballerina/tests/xsd_test_toxml_tests.bal index ee0ec5b9..f9d3fcb2 100644 --- a/ballerina/tests/xsd_test_toxml_tests.bal +++ b/ballerina/tests/xsd_test_toxml_tests.bal @@ -210,6 +210,17 @@ type ToXml6 record { Seq_XSDSequenceRecord13_2 seq_XSDSequenceRecord13_2; }; +@Name { + value: "A" +} +type ToXml7 record { + @Sequence { + minOccurs: 1, + maxOccurs: 2 + } + Seq_A_toXml[] seq_a; +}; + @test:Config {groups: ["xsd", "to_xml"], dataProvider: testToXmlWithXsdProvider} function testToXmlWithXsd(typedesc recordType, record{} value, xml expected) returns error?{ xml|Error xmlResult = toXml(check value.ensureType(recordType), {}); @@ -246,6 +257,171 @@ function testToXmlWithXsdProvider() returns [typedesc, record{}, xml][ ToXml6, {seq_XSDSequenceRecord13_1: {field1: {value1: {a: "1", b: "2", c: "3"}}, field2: {value2: {d: "1", e: "2", f: "3"}}, field3: {value3: {g: "1", h: "2", i: "3"}}}, seq_XSDSequenceRecord13_2: {field4: {value1: {a: "1", b: "2", c: "3"}}, field5: {value2: {d: "1", e: "2", f: "3"}}, field6: {value3: {g: "1", h: "2", i: "3"}}}}, xml `123123123123123123` + ], + [ + ToXml7, + {seq_a: [{b: "B", a: "A", c: "C"}, {b: "B", a: "A", c: "C"}]}, + xml `ABCABC` + ] + ]; +} + +@Name { + value: "A" +} +type ToXmlChoice1 record { + @Choice { + minOccurs: 1, + maxOccurs: 1 + } + Choice_A_toXml choice_a; +}; + +type Choice_A_toXml record { + string c?; + string a?; + string b?; +}; + +@Name { + value: "A" +} +type ToXmlChoice2 record { + @Choice { + minOccurs: 1, + maxOccurs: 1 + } + Choice_A_toXml2 choice_a; + + @Choice { + minOccurs: 1, + maxOccurs: 3 + } + Choice_A_toXml3 choice_b; +}; + +type Choice_A_toXml2 record { + @Element { + minOccurs: 2, + maxOccurs: 3 + } + string c?; + + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string a?; + + @Element { + minOccurs: 1, + maxOccurs: 5 + } + string b?; +}; + +type Choice_A_toXml3 record { + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string c?; + + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string a?; + + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string b?; +}; + +@Name { + value: "A" +} +type ToXmlChoice4 record { + record { + @Choice { + minOccurs: 2, + maxOccurs: 3 + } + Choice_A_toXml4 choice_a; + } nestedName; + + record { + @Choice { + minOccurs: 2, + maxOccurs: 2 + } + Choice_A_toXml4 choice_a; + } nestedName2; +}; + +type Choice_A_toXml4 record { + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string c?; + + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string a?; + + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string b?; +}; + +type Choice_A_toXml5 record { + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string c?; + + @Element { + minOccurs: 1, + maxOccurs: 2 + } + string a?; + + @Element { + minOccurs: 2, + maxOccurs: 3 + } + string b?; +}; + +@test:Config {groups: ["xsd", "to_xml"], dataProvider: testToXmlWithXsdProvider2} +function testToXmlWithXsd2(typedesc recordType, record{} value, xml expected) returns error?{ + xml|Error xmlResult = toXml(check value.ensureType(recordType), {}); + test:assertEquals(xmlResult, expected); +} + +function testToXmlWithXsdProvider2() returns [typedesc, record{}, xml][] { + return [[ + ToXmlChoice1, + {choice_a: {b: "B"}}, + xml `B` + ], + [ + ToXmlChoice2, + {choice_a: {c: "C"}, choice_b: {b: "B", a: "A", c: "C"}}, + xml `CCAB` + ], + [ + ToXmlChoice4, + {nestedName: {choice_a: {b: "B", a: "A"}}, nestedName2: {choice_a: {b: "B", a: "A"}}}, + xml `ABAB` ] ]; } diff --git a/ballerina/tests/xsd_test_toxml_with_namespaces.bal b/ballerina/tests/xsd_test_toxml_with_namespaces.bal index 81f57556..9e3cd6e9 100644 --- a/ballerina/tests/xsd_test_toxml_with_namespaces.bal +++ b/ballerina/tests/xsd_test_toxml_with_namespaces.bal @@ -16,6 +16,8 @@ import ballerina/test; +type EmptyRec record {}; + @Name { value: "A" } @@ -41,7 +43,6 @@ type Seq_A_toXmlWithNamespaces record { } string c; - @Namespace { uri: "http://example2.com", prefix: "ex2" @@ -298,6 +299,46 @@ type Seq_XSDSequenceRecordWithNamespace13_2 record { Seq_F_13 field6; }; +@Name { + value: "A" +} +type ToXmlWithNamespaces7 record { + @Sequence { + minOccurs: 1, + maxOccurs: 2 + } + @Namespace { + uri: "http://example.com", + prefix: "ex" + } + Seq_A_toXmlWithNamespaces[] seq_a; +}; + +@Name { + value: "A" +} +type ToXmlWithNamespaces8 record { + @Sequence { + minOccurs: 1, + maxOccurs: 2 + } + @Namespace { + uri: "http://example.com", + prefix: "ex" + } + Seq_A_toXmlWithNamespaces[] seq_a; + + @Sequence { + minOccurs: 1, + maxOccurs: 2 + } + @Namespace { + uri: "http://example2.com", + prefix: "ex2" + } + Seq_A_toXmlWithNamespaces[] seq_a2; +}; + @test:Config {groups: ["xsd", "to_xml"], dataProvider: testToXmlWithXsdProviderWithNamespaces} function testToXmlWithXsdWithNamespaces(typedesc recordType, record{} value, xml expected) returns error?{ xml|Error xmlResult = toXml(check value.ensureType(recordType), {}); @@ -329,12 +370,237 @@ function testToXmlWithXsdProviderWithNamespaces() returns [typedesc, r ToXmlWithNamespaces5, {a: {n: 1, name: {b: "B", a: "A", c: "C"}, name3: {name: {n: 1}}, name2: {b2: "B", a2: "A", c2: "C"}}, b: {n: 1, name: {b: "B", a: "A", c: "C"}, name3: {name: {n: 1}}, name2: {b2: "B", a2: "A", c2: "C"}}, c: "A"}, xml `1ABCABC1A1ABCABC1` - ] + ], // TODO: Values are exact equals but assertion is failing. Need to check. // [ // ToXmlWithNamespaces6, // {seq_XSDSequenceRecord13_1: {field1: {value1: {a: "1", b: "2", c: "3"}}, field2: {value2: {d: "1", e: "2", f: "3"}}, field3: {value3: {g: "1", h: "2", i: "3"}}}, seq_XSDSequenceRecord13_2: {field4: {value1: {a: "1", b: "2", c: "3"}}, field5: {value2: {d: "1", e: "2", f: "3"}}, field6: {value3: {g: "1", h: "2", i: "3"}}}}, // xml `123123123123123123` - // ] + // ], + [ + EmptyRec, + {}, + xml `` + ], + [ + ToXmlWithNamespaces7, + {seq_a: [{b: "B", a: "A", c: "C"}, {b: "B", a: "A", c: "C"}]}, + xml `ABCABC` + ], + [ + ToXmlWithNamespaces8, + {seq_a: [{b: "B", a: "A", c: "C"}, {b: "B", a: "A", c: "C"}], seq_a2: [{b: "B", a: "A", c: "C"}, {b: "B", a: "A", c: "C"}]}, + xml `ABCABCABCABC` + ] + ]; +} + +@Name { + value: "A" +} +type ToXmlChoiceWithNamespace1 record { + @Choice { + minOccurs: 1, + maxOccurs: 1 + } + Choice_A_toXmlWithNamespace choice_a; +}; + +type Choice_A_toXmlWithNamespace record { + @Namespace { + uri: "http://examplec.com", + prefix: "c" + } + string c?; + + @Namespace { + uri: "http://examplea.com", + prefix: "a" + } + string a?; + + @Namespace { + uri: "http://exampleb.com", + prefix: "b" + } + string b?; +}; + +@Name { + value: "A" +} +type ToXmlChoiceWithNamespace2 record { + @Choice { + minOccurs: 1, + maxOccurs: 1 + } + Choice_A_toXmlWithNamespace2 choice_a; + + @Choice { + minOccurs: 1, + maxOccurs: 3 + } + Choice_A_toXmlWithNamespace3 choice_b; +}; + +type Choice_A_toXmlWithNamespace2 record { + @Element { + minOccurs: 2, + maxOccurs: 3 + } + @Namespace { + uri: "http://examplea.com", + prefix: "c" + } + string c?; + + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string a?; + + @Element { + minOccurs: 1, + maxOccurs: 5 + } + @Namespace { + uri: "http://examplea.com", + prefix: "b" + } + string b?; +}; + +type Choice_A_toXmlWithNamespace3 record { + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string c?; + + @Namespace { + uri: "http://examplea.com", + prefix: "a" + } + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string a?; + + @Element { + minOccurs: 1, + maxOccurs: 1 + } + string b?; +}; + +@Name { + value: "A" +} +type ToXmlChoiceWithNamespace4 record { + record { + @Choice { + minOccurs: 2, + maxOccurs: 3 + } + Choice_A_toXmlWithNamespace4 choice_a; + } nestedName; + + record { + @Choice { + minOccurs: 2, + maxOccurs: 2 + } + Choice_A_toXmlWithNamespace3 choice_a; + } nestedName2; +}; + +type Choice_A_toXmlWithNamespace4 record { + @Element { + minOccurs: 1, + maxOccurs: 1 + } + @Namespace { + uri: "http://examplec.com", + prefix: "c" + } + string c?; + + @Element { + minOccurs: 1, + maxOccurs: 1 + } + @Namespace { + uri: "http://exampleb.com", + prefix: "a" + } + string a?; + + @Element { + minOccurs: 1, + maxOccurs: 1 + } + @Namespace { + uri: "http://exampleb.com", + prefix: "b" + } + string b?; +}; + +type Choice_A_toXmlWithNamespace5 record { + @Element { + minOccurs: 1, + maxOccurs: 1 + } + @Namespace { + uri: "http://examplea.com", + prefix: "c" + } + string c?; + + @Namespace { + uri: "http://examplea.com", + prefix: "a" + } + @Element { + minOccurs: 1, + maxOccurs: 2 + } + string a?; + + @Element { + minOccurs: 2, + maxOccurs: 3 + } + @Namespace { + uri: "http://examplea.com", + prefix: "b" + } + string b?; +}; + +@test:Config {groups: ["xsd", "to_xml"], dataProvider: testToXmlDataProviderWithNamespaces2} +function testToXmlWithXsdWithNamespace(typedesc recordType, record{} value, xml expected) returns error?{ + xml|Error xmlResult = toXml(check value.ensureType(recordType), {}); + test:assertEquals(xmlResult, expected); +} + +function testToXmlDataProviderWithNamespaces2() returns [typedesc, record{}, xml][] { + return [[ + ToXmlChoiceWithNamespace1, + {choice_a: {b: "B"}}, + xml `B` + ], + [ + ToXmlChoiceWithNamespace2, + {choice_a: {c: "C"}, choice_b: {b: "B", a: "A", c: "C"}}, + xml `CCAB` + ], + [ + ToXmlChoiceWithNamespace4, + {nestedName: {choice_a: {b: "B", a: "A"}}, nestedName2: {choice_a: {b: "B", a: "A"}}}, + xml `ABAB` + ] ]; }