From e84edb4d78f0cb1018441b29df45dce7edb13a95 Mon Sep 17 00:00:00 2001
From: prakanth <50439067+prakanth97@users.noreply.github.com>
Date: Fri, 22 Mar 2024 19:26:24 +0530
Subject: [PATCH] Add tests for file read and convert xml to record
---
ballerina/tests/fromXml_test.bal | 8 +-
ballerina/tests/resources/source_1.xml | 20 ++
ballerina/tests/resources/source_2.xml | 20 ++
ballerina/tests/resources/source_3.xml | 29 +++
ballerina/tests/resources/source_4.xml | 70 ++++++
ballerina/tests/test_read_from_file.bal | 316 ++++++++++++++++++++++++
6 files changed, 459 insertions(+), 4 deletions(-)
create mode 100644 ballerina/tests/resources/source_1.xml
create mode 100644 ballerina/tests/resources/source_2.xml
create mode 100644 ballerina/tests/resources/source_3.xml
create mode 100644 ballerina/tests/resources/source_4.xml
create mode 100644 ballerina/tests/test_read_from_file.bal
diff --git a/ballerina/tests/fromXml_test.bal b/ballerina/tests/fromXml_test.bal
index 8bda736e..d7979fbb 100644
--- a/ballerina/tests/fromXml_test.bal
+++ b/ballerina/tests/fromXml_test.bal
@@ -41,7 +41,7 @@ function testXmlStringToRecord1() returns error? {
5
`;
- Data rec1 = check parseString(xmlStr);
+ Data rec1 = check parseString(xmlStr, {});
test:assertEquals(rec1.A.length(), 2);
test:assertEquals(rec1.A[0].B.length(), 2);
@@ -62,7 +62,7 @@ function testXmlStringToRecord1() returns error? {
}
function testXmlToRecord1() returns error? {
xml xmlVal = xml `1265345`;
- Data rec1 = check parseAsType(xmlVal);
+ Data rec1 = check parseAsType(xmlVal, {});
test:assertEquals(rec1.A.length(), 2);
test:assertEquals(rec1.A[0].B.length(), 2);
@@ -93,7 +93,7 @@ type Data2 record {|
}
function testXmlStringToRecord2() returns error? {
string xmlStr1 = "1";
- Data1 rec1 = check parseString(xmlStr1);
+ Data1 rec1 = check parseString(xmlStr1, {});
test:assertEquals(rec1.A.\#content, "1");
string xmlStr2 = "1";
@@ -142,7 +142,7 @@ type Data5 record {|
}
function testXmlStringToRecord3() returns error? {
string xmlStr1 = "12";
- Data3 rec1 = check parseString(xmlStr1);
+ Data3 rec1 = check parseString(xmlStr1, {});
test:assertEquals(rec1.A.\#content, "1");
test:assertEquals(rec1.B.\#content, 2);
diff --git a/ballerina/tests/resources/source_1.xml b/ballerina/tests/resources/source_1.xml
new file mode 100644
index 00000000..048d104c
--- /dev/null
+++ b/ballerina/tests/resources/source_1.xml
@@ -0,0 +1,20 @@
+
+
+ Harry Potter and the Philosopher's Stone
+ J.K. Rowling
+ Fantasy
+ 1997
+
+
+ The Great Gatsby
+ F. Scott Fitzgerald
+ Classic
+ 1925
+
+
+ To Kill a Mockingbird
+ Harper Lee
+ Fiction
+ 1960
+
+
diff --git a/ballerina/tests/resources/source_2.xml b/ballerina/tests/resources/source_2.xml
new file mode 100644
index 00000000..e436c0e0
--- /dev/null
+++ b/ballerina/tests/resources/source_2.xml
@@ -0,0 +1,20 @@
+
+
+ American Beauty
+ Sam Mendes
+ Drama
+ 1999
+
+
+ The Shawshank Redemption
+ Frank Darabont
+ Drama
+ 1994
+
+
+ Forrest Gump
+ Robert Zemeckis
+ Drama
+ 1994
+
+
diff --git a/ballerina/tests/resources/source_3.xml b/ballerina/tests/resources/source_3.xml
new file mode 100644
index 00000000..20d0b264
--- /dev/null
+++ b/ballerina/tests/resources/source_3.xml
@@ -0,0 +1,29 @@
+
+
+ The Catcher in the Rye
+
+ J.D.
+ Salinger
+
+ Fiction
+ 1951
+
+
+ 1984
+
+ George
+ Orwell
+
+ Dystopian
+ 1949
+
+
+ The Lord of the Rings
+
+ J.R.R.
+ Tolkien
+
+ Fantasy
+ 1954
+
+
diff --git a/ballerina/tests/resources/source_4.xml b/ballerina/tests/resources/source_4.xml
new file mode 100644
index 00000000..591fd2cf
--- /dev/null
+++ b/ballerina/tests/resources/source_4.xml
@@ -0,0 +1,70 @@
+
+
+
+ John Doe
+ john.doe@example.com
+
+ 123 Main St
+ Anytown
+ CA
+ 12345
+ USA
+
+
+
+
+ Laptop
+ 999.99
+
+
+ Printer
+ 199.99
+
+
+
+ credit_card
+ 2199.97
+
+ VISA
+ 1234 5678 9012 3456
+ 2026-12
+
+
+ completed
+
+
+
+ Jane Smith
+ jane.smith@example.com
+
+ 456 Oak Ave
+ Smalltown
+ NY
+ 54321
+ USA
+
+
+
+
+ Smartphone
+ 799.99
+
+
+ Tablet
+ 499.99
+
+
+ Smart Watch
+ 299.99
+
+
+
+ paypal
+ 3499.94
+
+ jane.smith@example.com
+
+
+ pending
+
+
diff --git a/ballerina/tests/test_read_from_file.bal b/ballerina/tests/test_read_from_file.bal
new file mode 100644
index 00000000..50816803
--- /dev/null
+++ b/ballerina/tests/test_read_from_file.bal
@@ -0,0 +1,316 @@
+// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+import ballerina/io;
+import ballerina/test;
+
+const FILE_PATH = "tests/resources/";
+
+type LibraryS1 record {
+ BookS1[] book;
+};
+
+type BookS1 record {|
+ string title;
+ string author;
+ string genre;
+ int publication_year;
+|};
+
+type MoviesS2 record {|
+ MovieS2[] movie;
+|};
+
+type MovieS2 record {|
+ string id;
+ string title;
+ string genre;
+ @Name {
+ value: "release_year"
+ }
+ int year;
+|};
+
+type OpenRecord record {
+};
+
+type OrdersS4 record {|
+ OrderS4[] 'order;
+|};
+
+type OrderS4 record {|
+ @Name {
+ value: "order_id"
+ }
+ int id;
+ record {|
+ ProductS4[1] product;
+ |} products;
+|};
+
+type ProductS4 record {|
+ @Name {
+ value: "product_id"
+ }
+ string id;
+ string name;
+|};
+
+@test:Config {
+ dataProvider: dataProviderForFileReadTest
+}
+function testParseString(string filePath, typedesc expectedType, record {} expectedData) returns error? {
+ string content = check io:fileReadString(FILE_PATH + filePath);
+ record {} data = check parseString(content, {}, expectedType);
+ test:assertEquals(data, expectedData, "Data mismatched");
+}
+
+@test:Config {
+ dataProvider: dataProviderForFileReadTest
+}
+function testParseBytes(string filePath, typedesc expectedType, record {} expectedData) returns error? {
+ byte[] content = check io:fileReadBytes(FILE_PATH + filePath);
+ record {} data = check parseBytes(content, {}, expectedType);
+ test:assertEquals(data, expectedData, "Data mismatched");
+}
+
+@test:Config {
+ dataProvider: dataProviderForFileReadTest
+}
+function testParseStrema(string filePath, typedesc expectedType, record {} expectedData) returns error? {
+ stream content = check io:fileReadBlocksAsStream(FILE_PATH + filePath);
+ record {} data = check parseStream(content, {}, expectedType);
+ test:assertEquals(data, expectedData, "Data mismatched");
+}
+
+function dataProviderForFileReadTest() returns [string, typedesc, record {}][] {
+ return [
+ [
+ "source_1.xml",
+ LibraryS1,
+ {
+ "book": [
+ {
+ title: "Harry Potter and the Philosopher's Stone",
+ author: "J.K. Rowling",
+ genre: "Fantasy",
+ publication_year: 1997
+ },
+ {
+ title: "The Great Gatsby",
+ author: "F. Scott Fitzgerald",
+ genre: "Classic",
+ publication_year: 1925
+ },
+ {
+ title: "To Kill a Mockingbird",
+ author: "Harper Lee",
+ genre: "Fiction",
+ publication_year: 1960
+ }
+ ]
+ }
+ ],
+ [
+ "source_2.xml",
+ MoviesS2,
+ {
+ "movie": [
+ {
+ id: "1",
+ title: "American Beauty",
+ genre: "Drama",
+ "year": 1999
+ },
+ {
+ id: "2",
+ title: "The Shawshank Redemption",
+ genre: "Drama",
+ year: 1994
+ },
+ {
+ id: "3",
+ title: "Forrest Gump",
+ genre: "Drama",
+ year: 1994
+ }
+ ]
+ }
+ ],
+ [
+ "source_3.xml",
+ OpenRecord,
+ {
+ "book": [
+ {
+ "id": 1,
+ "title": "The Catcher in the Rye",
+ "author": {
+ "id": 101,
+ "nationality": "American",
+ "first_name": "J.D.",
+ "last_name": "Salinger"
+ },
+ "genre": "Fiction",
+ "publication_year": 1951
+ },
+ {
+ "id": 2,
+ "title": 1984,
+ "author": {
+ "id": 102,
+ "nationality": "British",
+ "first_name": "George",
+ "last_name": "Orwell"
+ },
+ "genre": "Dystopian",
+ "publication_year": 1949
+ },
+ {
+ "id": 3,
+ "title": "The Lord of the Rings",
+ "author": {
+ "id": 103,
+ "nationality": "British",
+ "first_name": "J.R.R.",
+ "last_name": "Tolkien"
+ },
+ "genre": "Fantasy",
+ "publication_year": 1954
+ }
+ ]
+ }
+ ],
+ [
+ "source_4.xml",
+ OpenRecord,
+ {
+ "order": [
+ {
+ "order_id": 123456,
+ "date": "2024-03-22T10:30:00",
+ "customer_id": 987654,
+ "customer_info": {
+ "name": "John Doe",
+ "email": "john.doe@example.com",
+ "address": {
+ "street": "123 Main St",
+ "city": "Anytown",
+ "state": "CA",
+ "zip": 12345,
+ "country": "USA"
+ }
+ },
+ "products": {
+ "product": [
+ {
+ "product_id": 789,
+ "quantity": 2,
+ "name": "Laptop",
+ "price":
+ {"currency": "USD", "#content": 999.99}
+ },
+ {
+ "product_id": 456,
+ "quantity": 1,
+ "name": "Printer",
+ "price":
+ {"currency": "USD", "#content": 199.99}
+ }
+ ]
+ },
+ "payment": {
+ "method": "credit_card",
+ "amount": {"currency": "USD", "#content": 2199.97},
+ "card": {
+ "card_type": "VISA",
+ "card_number": "1234 5678 9012 3456",
+ "expiration_date": "2026-12"
+ }
+ },
+ "status": "completed"
+ },
+ {
+ "order_id": 789012,
+ "date": "2024-03-22T11:45:00",
+ "customer_id": 543210,
+ "customer_info": {
+ "name": "Jane Smith",
+ "email": "jane.smith@example.com",
+ "address": {
+ "street": "456 Oak Ave",
+ "city": "Smalltown",
+ "state": "NY",
+ "zip": 54321,
+ "country": "USA"
+ }
+ },
+ "products": {
+ "product": [
+ {
+ "product_id": 123,
+ "quantity": 3,
+ "name": "Smartphone",
+ "price":
+ {"currency": "USD", "#content": 799.99}
+ },
+ {
+ "product_id": 234,
+ "quantity": 2,
+ "name": "Tablet",
+ "price":
+ {"currency": "USD", "#content": 499.99}
+ },
+ {
+ "product_id": 345,
+ "quantity": 1,
+ "name": "Smart Watch",
+ "price":
+ {"currency": "USD", "#content": 299.99}
+ }
+ ]
+ },
+ "payment": {
+ "method": "paypal",
+ "amount": {"currency": "USD", "#content": 3499.94},
+ "paypal": {"email": "jane.smith@example.com"}
+ },
+ "status": "pending"
+ }
+ ]
+ }
+ ],
+ [
+ "source_4.xml",
+ OrdersS4,
+ {
+ "order": [
+ {
+ "id": 123456,
+ "products": {
+ "product": [{"id": "789", "name": "Laptop"}]
+ }
+ },
+ {
+ "id": 789012,
+ "products": {
+ "product": [{"id": "123", "name": "Smartphone"}]
+ }
+ }
+ ]
+ }
+ ]
+ ];
+}