Skip to content

Commit

Permalink
Merge pull request #50 from SasinduDilshara/xsd
Browse files Browse the repository at this point in the history
Implement XSD validation for Ballerina `data.xmldata` module
  • Loading branch information
SasinduDilshara authored Nov 22, 2024
2 parents f5e06e7 + cb0f04f commit 53fc30c
Show file tree
Hide file tree
Showing 79 changed files with 12,600 additions and 136 deletions.
3 changes: 3 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_1.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Book" type="xs:string"/>
</xs:schema>
16 changes: 16 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_2.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Library">
<xs:complexType>
<xs:sequence>
<xs:element name="Book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Author" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
11 changes: 11 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_3.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Age" type="xs:int"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
10 changes: 10 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_4.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Vehicle">
<xs:complexType>
<xs:choice>
<xs:element name="Car" type="xs:string"/>
<xs:element name="Bike" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
10 changes: 10 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_5.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Order">
<xs:complexType>
<xs:all>
<xs:element name="OrderID" type="xs:int"/>
<xs:element name="Product" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Book><Title>Sample</Title></Book>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Book>Sample</Book>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Library>
<Book>
<Title>Sample Title</Title>
</Book>
</Library>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Library>
<Book>
<Title>Sample Title</Title>
<Author>Sample Author</Author>
</Book>
</Library>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Person>
<Name>John Doe</Name>
<Age>30</Age>
</Person>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Person id="123">
<Name>John Doe</Name>
<Age>30</Age>
</Person>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Vehicle>
<Car>Toyota</Car>
<Bike>Yamaha</Bike>
</Vehicle>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Vehicle>
<Car>Toyota</Car>
</Vehicle>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Order>
<OrderID>12345</OrderID>
</Order>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Order>
<OrderID>12345</OrderID>
<Product>Laptop</Product>
</Order>
38 changes: 38 additions & 0 deletions ballerina/tests/test_from_json.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import ballerina/test;

@test:Config {groups: ["xsd", "to_xml"], dataProvider: fromJsonDataProvider}
function testFromJson(json value, xml expected) returns error?{
xml|Error xmlResult = fromJson(value);
test:assertEquals(xmlResult, expected);
}

function fromJsonDataProvider() returns [json, xml][] {
return [
[{a: {b: 2, c: 3}}, xml `<a><b>2</b><c>3</c></a>`],
[{a: {a: 1}}, xml `<a><a>1</a></a>`],
[{a: {d: 4, e: {f: 5, g: "text"}}}, xml `<a><d>4</d><e><f>5</f><g>text</g></e></a>`],
[{root: {nested: {value1: "example", value2: 10}}}, xml `<root><nested><value1>example</value1><value2>10</value2></nested></root>`],
[{book: {title: "XML Guide", author: "John Doe", year: 2024}}, xml `<book><title>XML Guide</title><author>John Doe</author><year>2024</year></book>`],
[{library: {section: {book1: "Book A", book2: "Book B"}}}, xml `<library><section><book1>Book A</book1><book2>Book B</book2></section></library>`],
[{person: {name: "Alice", details: {age: 30, city: "Wonderland"}}}, xml `<person><name>Alice</name><details><age>30</age><city>Wonderland</city></details></person>`],
[{catalog: {item: [{id: 1, name: "Item 1"}, {id: 2, name: "Item 2"}]}}, xml `<catalog><item><id>1</id><name>Item 1</name></item><item><id>2</id><name>Item 2</name></item></catalog>`],
[{company: {employee: {id: 1001, name: "Bob", department: "Engineering"}}}, xml `<company><employee><id>1001</id><name>Bob</name><department>Engineering</department></employee></company>`],
[{'order: {orderId: 5001, items: {item1: "Widget", item2: "Gadget"}}}, xml `<order><orderId>5001</orderId><items><item1>Widget</item1><item2>Gadget</item2></items></order>`],
[{menu: {dish: [{name: "Pasta", price: 12.5}, {name: "Salad", price: 8.0}]}}, xml `<menu><dish><name>Pasta</name><price>12.5</price></dish><dish><name>Salad</name><price>8.0</price></dish></menu>`],
[{report: {entries: [{date: "2024-10-01", detail: "Entry 1"}, {date: "2024-10-02", detail: "Entry 2"}]}}, xml `<report><entries><date>2024-10-01</date><detail>Entry 1</detail></entries><entries><date>2024-10-02</date><detail>Entry 2</detail></entries></report>`],
[{shoppingList: {items: [{item: "Apples", quantity: 5}, {item: "Bananas", quantity: 3}]}}, xml `<shoppingList><items><item>Apples</item><quantity>5</quantity></items><items><item>Bananas</item><quantity>3</quantity></items></shoppingList>`],
[{conference: {session: [{topic: "AI Trends", speaker: "Dr. Smith"}, {topic: "Web 3.0", speaker: "Jane Doe"}]}}, xml `<conference><session><topic>AI Trends</topic><speaker>Dr. Smith</speaker></session><session><topic>Web 3.0</topic><speaker>Jane Doe</speaker></session></conference>`],
[{project: {tasks: [{title: "Setup Environment", status: "Completed"}, {title: "Develop Module", status: "In Progress"}]}}, xml `<project><tasks><title>Setup Environment</title><status>Completed</status></tasks><tasks><title>Develop Module</title><status>In Progress</status></tasks></project>`],
[{school: {students: [{name: "Emily", grade: "A"}, {name: "Michael", grade: "B"}]}}, xml `<school><students><name>Emily</name><grade>A</grade></students><students><name>Michael</name><grade>B</grade></students></school>`],
[{portfolio: {stocks: [{symbol: "AAPL", shares: 50}, {symbol: "TSLA", shares: 30}]}}, xml `<portfolio><stocks><symbol>AAPL</symbol><shares>50</shares></stocks><stocks><symbol>TSLA</symbol><shares>30</shares></stocks></portfolio>`],
[{university: {course: [{name: "Mathematics", credits: 4}, {name: "History", credits: 3}]}}, xml `<university><course><name>Mathematics</name><credits>4</credits></course><course><name>History</name><credits>3</credits></course></university>`],
[{research: {papers: [{title: "Quantum Computing", author: "Alice Cooper"}, {title: "Blockchain Advances", author: "John Smith"}]}}, xml `<research><papers><title>Quantum Computing</title><author>Alice Cooper</author></papers><papers><title>Blockchain Advances</title><author>John Smith</author></papers></research>`],
[{movieCollection: {movies: [{title: "Inception", director: "Nolan"}, {title: "Interstellar", director: "Nolan"}]}}, xml `<movieCollection><movies><title>Inception</title><director>Nolan</director></movies><movies><title>Interstellar</title><director>Nolan</director></movies></movieCollection>`],
[{library: {books: [{title: "XML Guide", author: "John Doe", year: 2024}, {title: "JSON Primer", author: "Jane Smith", year: 2023}]}}, xml `<library><books><title>XML Guide</title><author>John Doe</author><year>2024</year></books><books><title>JSON Primer</title><author>Jane Smith</author><year>2023</year></books></library>`],
[{shoppingList: {items: [{item: "Apples", quantity: 5}, {item: "Bananas", quantity: 3}]}}, xml `<shoppingList><items><item>Apples</item><quantity>5</quantity></items><items><item>Bananas</item><quantity>3</quantity></items></shoppingList>`],
[{project: {tasks: [{title: "Setup Environment", status: "Completed"}, {title: "Develop Module", status: "In Progress"}]}}, xml `<project><tasks><title>Setup Environment</title><status>Completed</status></tasks><tasks><title>Develop Module</title><status>In Progress</status></tasks></project>`],
[{school: {students: [{name: "Emily", grade: "A"}, {name: "Michael", grade: "B"}]}}, xml `<school><students><name>Emily</name><grade>A</grade></students><students><name>Michael</name><grade>B</grade></students></school>`],
[{conference: {sessions: [{topic: "AI Trends", speaker: "Dr. Smith"}, {topic: "Web 3.0", speaker: "Jane Doe"}]}}, xml `<conference><sessions><topic>AI Trends</topic><speaker>Dr. Smith</speaker></sessions><sessions><topic>Web 3.0</topic><speaker>Jane Doe</speaker></sessions></conference>`],
[{catalog: {items: [{id: 1, name: "Item 1"}, {id: 2, name: "Item 2"}]}}, xml `<catalog><items><id>1</id><name>Item 1</name></items><items><id>2</id><name>Item 2</name></items></catalog>`]
];
}
2 changes: 1 addition & 1 deletion ballerina/tests/toXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function testToXmlWithNameAnnotation() returns error? {
xml xmlVal1 = xml `<Data8><A><D><C><D>1</D><D>2</D></C><C><D>3</D><D>4</D></C></D></A></Data8>`;
Rec2 rec1 = check parseAsType(xmlVal1);
xml result = check toXml(rec1);
test:assertTrue(result == xmlVal1);
test:assertEquals(result, xmlVal1);
}

@test:Config {
Expand Down
Loading

0 comments on commit 53fc30c

Please sign in to comment.