From 48d0e3b8e7cf4980b40604950ba874b5da855de9 Mon Sep 17 00:00:00 2001 From: Hasitha Aravinda Date: Wed, 1 Nov 2023 10:15:30 +0530 Subject: [PATCH] Add large file test cases --- .../tests/stream_default_namspace_test.bal | 117 +++++++++++++ ballerina/tests/stream_large_file_test.bal | 161 ++++++++++++++++++ ballerina/tests/stream_namespace_test.bal | 77 ++++++++- 3 files changed, 354 insertions(+), 1 deletion(-) create mode 100644 ballerina/tests/stream_large_file_test.bal diff --git a/ballerina/tests/stream_default_namspace_test.bal b/ballerina/tests/stream_default_namspace_test.bal index dde58dc..6ecd25b 100644 --- a/ballerina/tests/stream_default_namspace_test.bal +++ b/ballerina/tests/stream_default_namspace_test.bal @@ -110,42 +110,159 @@ type InvoiceFull record { CustomersFull customers; }; +# Test default namespace with a record including all fields using annotations. +# +# + return - return error on failure, otherwise nil. @test:Config function testDefaultNamespaceInvoiceFull() returns error? { stream dataStream = check io:fileReadBlocksAsStream("tests/resources/default_namespaced_invoice.xml"); InvoiceFull invoice = check fromXmlStringWithType(dataStream); + test:assertEquals(invoice.length(), 2, "Invoice count mismatched"); + test:assertEquals(invoice.products.length(), 1, "Products count mismatched"); + test:assertEquals(invoice.products.product.length(), 2, "Products/product count mismatched"); + test:assertEquals(invoice.customers.length(), 1, "Customers count mismatched"); + test:assertEquals(invoice.customers.customer.length(), 2, "Customers/customer count mismatched"); + + test:assertEquals(invoice.products.product[0].length(), 5, "Product 1 field count mismatched"); + test:assertEquals(invoice.products.product[0].id, 1); + test:assertEquals(invoice.products.product[0].name, "Product 1"); + test:assertEquals(invoice.products.product[0].description, string `This is the description for + Product 1. + `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); + test:assertEquals(invoice.products.product[0].price.\#content, 57.70d); + test:assertEquals(invoice.products.product[0].price.currency, "USD"); + test:assertEquals(invoice.products.product[0].category, "Home and Garden"); + + test:assertEquals(invoice.products.product[1].length(), 5, "Product 2 field count mismatched"); + test:assertEquals(invoice.products.product[1].id, 2); + test:assertEquals(invoice.products.product[1].name, "Product 2"); + test:assertEquals(invoice.products.product[1].description, string `This is the description for + Product 2. + `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); + test:assertEquals(invoice.products.product[1].price.\#content, 6312.36d); + test:assertEquals(invoice.products.product[1].price.currency, "LKR"); + test:assertEquals(invoice.products.product[1].category, "Books"); + + test:assertEquals(invoice.customers.customer[0].length(), 5, "Customer 1 field count mismatched"); + test:assertEquals(invoice.customers.customer[0].id, "C001"); + test:assertEquals(invoice.customers.customer[0].name, "John Doe"); + test:assertEquals(invoice.customers.customer[0].email, "john@example.com"); + test:assertEquals(invoice.customers.customer[0].phone, "569-5052"); + test:assertEquals(invoice.customers.customer[0].address.length(), 4, "Customer 1 address field count mismatched"); + test:assertEquals(invoice.customers.customer[0].address.street, "MZ738SI4DV St."); + test:assertEquals(invoice.customers.customer[0].address.city, "Newport"); + test:assertEquals(invoice.customers.customer[0].address.state, "NY"); + test:assertEquals(invoice.customers.customer[0].address.zip, 19140); + + test:assertEquals(invoice.customers.customer[1].length(), 5, "Customer 2 field count mismatched"); + test:assertEquals(invoice.customers.customer[1].id, "C002"); + test:assertEquals(invoice.customers.customer[1].name, "Jane Doe"); + test:assertEquals(invoice.customers.customer[1].email, "jane@example.com"); + test:assertEquals(invoice.customers.customer[1].phone, "674-2864"); + test:assertEquals(invoice.customers.customer[1].address.length(), 4, "Customer 2 address field count mismatched"); + test:assertEquals(invoice.customers.customer[1].address.street, "ZI0TGK3BKG St."); + test:assertEquals(invoice.customers.customer[1].address.city, "Otherville"); + test:assertEquals(invoice.customers.customer[1].address.state, "CA"); + test:assertEquals(invoice.customers.customer[1].address.zip, 77855); +} + +type ProductFullPlain record { + string description; + PriceFullPlain price; + string category; + int id; + string name; +}; + +type PriceFullPlain record { + decimal \#content; + string currency; +}; + +type ProductsFullPlain record { + ProductFullPlain[] product; +}; + +type AddressFullPlain record { + string street; + string city; + string state; + int zip; +}; + +type CustomerFullPlain record { + string name; + string email; + string phone; + AddressFullPlain address; + string id; +}; + +type CustomersFullPlain record { + CustomerFullPlain[] customer; +}; + +type InvoiceFullPlain record { + ProductsFullPlain products; + CustomersFullPlain customers; +}; + +# Test default namespace with a record including all fields without using any annotations. +# +# + return - return error on failure, otherwise nil. +@test:Config +function testDefaultNamespaceInvoiceFullPlain() returns error? { + stream dataStream = check io:fileReadBlocksAsStream("tests/resources/default_namespaced_invoice.xml"); + InvoiceFullPlain invoice = check fromXmlStringWithType(dataStream); + + test:assertEquals(invoice.length(), 2, "Invoice count mismatched"); + test:assertEquals(invoice.products.length(), 1, "Products count mismatched"); + test:assertEquals(invoice.products.product.length(), 2, "Products/product count mismatched"); + test:assertEquals(invoice.customers.length(), 1, "Customers count mismatched"); + test:assertEquals(invoice.customers.customer.length(), 2, "Customers/customer count mismatched"); + + test:assertEquals(invoice.products.product[0].length(), 5, "Product 1 field count mismatched"); test:assertEquals(invoice.products.product[0].id, 1); test:assertEquals(invoice.products.product[0].name, "Product 1"); test:assertEquals(invoice.products.product[0].description, string `This is the description for Product 1. `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); test:assertEquals(invoice.products.product[0].price.\#content, 57.70d); test:assertEquals(invoice.products.product[0].price.currency, "USD"); test:assertEquals(invoice.products.product[0].category, "Home and Garden"); + test:assertEquals(invoice.products.product[1].length(), 5, "Product 2 field count mismatched"); test:assertEquals(invoice.products.product[1].id, 2); test:assertEquals(invoice.products.product[1].name, "Product 2"); test:assertEquals(invoice.products.product[1].description, string `This is the description for Product 2. `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); test:assertEquals(invoice.products.product[1].price.\#content, 6312.36d); test:assertEquals(invoice.products.product[1].price.currency, "LKR"); test:assertEquals(invoice.products.product[1].category, "Books"); + test:assertEquals(invoice.customers.customer[0].length(), 5, "Customer 1 field count mismatched"); test:assertEquals(invoice.customers.customer[0].id, "C001"); test:assertEquals(invoice.customers.customer[0].name, "John Doe"); test:assertEquals(invoice.customers.customer[0].email, "john@example.com"); test:assertEquals(invoice.customers.customer[0].phone, "569-5052"); + test:assertEquals(invoice.customers.customer[0].address.length(), 4, "Customer 1 address field count mismatched"); test:assertEquals(invoice.customers.customer[0].address.street, "MZ738SI4DV St."); test:assertEquals(invoice.customers.customer[0].address.city, "Newport"); test:assertEquals(invoice.customers.customer[0].address.state, "NY"); test:assertEquals(invoice.customers.customer[0].address.zip, 19140); + test:assertEquals(invoice.customers.customer[1].length(), 5, "Customer 2 field count mismatched"); test:assertEquals(invoice.customers.customer[1].id, "C002"); test:assertEquals(invoice.customers.customer[1].name, "Jane Doe"); test:assertEquals(invoice.customers.customer[1].email, "jane@example.com"); test:assertEquals(invoice.customers.customer[1].phone, "674-2864"); + test:assertEquals(invoice.customers.customer[1].address.length(), 4, "Customer 2 address field count mismatched"); test:assertEquals(invoice.customers.customer[1].address.street, "ZI0TGK3BKG St."); test:assertEquals(invoice.customers.customer[1].address.city, "Otherville"); test:assertEquals(invoice.customers.customer[1].address.state, "CA"); diff --git a/ballerina/tests/stream_large_file_test.bal b/ballerina/tests/stream_large_file_test.bal new file mode 100644 index 0000000..41ea903 --- /dev/null +++ b/ballerina/tests/stream_large_file_test.bal @@ -0,0 +1,161 @@ +// Copyright (c) 2023, 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; + +@Namespace { + uri: "http://www.example.com/invoice", + prefix: "inv" +} +@Name { + value: "invoice" +} +type SimpleInvoiceNS record {| + @Namespace { + uri: "http://www.example.com/invoice", + prefix: "inv" + } + SimpleCustomersNS customers; +|}; + +type SimpleCustomersNS record {| + @Namespace { + uri: "http://www.example.com/customers", + prefix: "customers" + } + @Name { + value: "customer" + } + SimpleCustomerNS[10] cust; +|}; + +type SimpleCustomerNS record {| + int id; + string name; + @Namespace { + uri: "http://www.example.com/customers", + prefix: "customers" + } + string city; +|}; + +const LARGE_XML_FILE = "build//resources//large_data.xml"; + +# Test reading large xml file with namespaces. +# + return - return error on failure, else nil. +@test:Config { + enable: false +} +function testLargeFileStreamWithNamespace() returns error? { + stream dataStream = check io:fileReadBlocksAsStream(LARGE_XML_FILE); + SimpleInvoiceNS invoice = check fromXmlStringWithType(dataStream); + + test:assertEquals(invoice.customers.length(), 1, "Invalid number of customers fields"); + test:assertEquals(invoice.customers.cust.length(), 10, "Invalid customers/customer length"); + test:assertEquals(invoice.customers.cust[0].length(), 3, "Invalid customers/customer/[0] fields"); + + test:assertEquals(invoice.customers.cust[0].id, 0, "Invalid customers/customer/[0]/id"); + test:assertEquals(invoice.customers.cust[0].name, "Customer0", "Invalid customers/customer/[0]/name"); + test:assertEquals(invoice.customers.cust[0].city, "City0", "Invalid customers/customer/[0]/city"); + + test:assertEquals(invoice.customers.cust[9].id, 9, "Invalid customers/customer/[9]/id"); + test:assertEquals(invoice.customers.cust[9].name, "Customer9", "Invalid customers/customer/[9]/name"); + test:assertEquals(invoice.customers.cust[9].city, "City9", "Invalid customers/customer/[9]/city"); +} + +type SimpleInvoice record {| + SimpleCustomers customers; +|}; + +type SimpleCustomers record {| + SimpleCustomer[] customer; +|}; + +type SimpleCustomer record {| + int id; + string name; + string city; +|}; + +# Test reading large xml file without considering namespaces. +# + return - return error on failure, else nil. +@test:Config { + enable: false +} +function testLargeFileStream() returns error? { + stream dataStream = check io:fileReadBlocksAsStream(LARGE_XML_FILE); + SimpleInvoice invoice = check fromXmlStringWithType(dataStream); + + test:assertEquals(invoice.customers.length(), 1, "Invalid number of customers fields"); + test:assertEquals(invoice.customers.customer.length(), 100000, "Invalid customers/customer length"); + test:assertEquals(invoice.customers.customer[0].length(), 3, "Invalid customers/customer/[0] fields"); + + test:assertEquals(invoice.customers.customer[0].id, 0, "Invalid customers/customer/[0]/id"); + test:assertEquals(invoice.customers.customer[0].name, "Customer0", "Invalid customers/customer/[0]/name"); + test:assertEquals(invoice.customers.customer[0].city, "City0", "Invalid customers/customer/[0]/city"); + + test:assertEquals(invoice.customers.customer[9].id, 9, "Invalid customers/customer/[9]/id"); + test:assertEquals(invoice.customers.customer[9].name, "Customer9", "Invalid customers/customer/[9]/name"); + test:assertEquals(invoice.customers.customer[9].city, "City9", "Invalid customers/customer/[9]/city"); +} + +@test:BeforeSuite +function createLargeFile() returns error? { + + io:WritableByteChannel wbc = check io:openWritableFile(LARGE_XML_FILE); + _ = check wbc.write(string ``.toBytes(), 0); + _ = check wbc.write("\n".toBytes(), 0); + foreach int i in 0 ... 100000 { + _ = check wbc.write(createProduct(i).toString().toBytes(), 0); + } + _ = check wbc.write("\n".toBytes(), 0); + _ = check wbc.write("\n".toBytes(), 0); + foreach int i in 0 ... 100000 { + _ = check wbc.write(createCustomer(i).toString().toBytes(), 0); + } + _ = check wbc.write("\n".toBytes(), 0); + _ = check wbc.write("\n".toBytes(), 0); +} + +xmlns "http://www.example.com/products" as pro; +xmlns "http://www.example.com/customers" as customers; + +function createProduct(int id) returns xml { + string name = "Product" + id.toString(); + string description = "This is a " + name; + int price = 100 + id; + string category = "category" + id.toString(); + xml product = xml ` + ${description} + ${price} + ${category} + `; + return product; +} + +function createCustomer(int id) returns xml { + string name = "Customer" + id.toString(); + string address = "Address" + id.toString(); + string city = "City" + id.toString(); + string country = "Country" + id.toString(); + xml customer = xml ` + ${address} + ${name}@example.com + ${city} + ${country} + `; + return customer; +} diff --git a/ballerina/tests/stream_namespace_test.bal b/ballerina/tests/stream_namespace_test.bal index c4dc298..f62f4a0 100644 --- a/ballerina/tests/stream_namespace_test.bal +++ b/ballerina/tests/stream_namespace_test.bal @@ -126,45 +126,120 @@ type InvoiceFullNS record { CustomersFull customers; }; +# Test namespaces & prefixes with a record including all fields with annotations. +# +# + return - return error on failure, otherwise nil. @test:Config function testNamespaceInvoiceFull() returns error? { stream dataStream = check io:fileReadBlocksAsStream("tests/resources/default_namespaced_invoice.xml"); - InvoiceFull invoice = check fromXmlStringWithType(dataStream); + InvoiceFullNS invoice = check fromXmlStringWithType(dataStream); + test:assertEquals(invoice.length(), 2, "Invoice count mismatched"); + test:assertEquals(invoice.products.length(), 1, "Products count mismatched"); + test:assertEquals(invoice.products.product.length(), 2, "Products/product count mismatched"); + test:assertEquals(invoice.customers.length(), 1, "Customers count mismatched"); + test:assertEquals(invoice.customers.customer.length(), 2, "Customers/customer count mismatched"); + + test:assertEquals(invoice.products.product[0].length(), 5, "Product 1 field count mismatched"); test:assertEquals(invoice.products.product[0].id, 1); test:assertEquals(invoice.products.product[0].name, "Product 1"); test:assertEquals(invoice.products.product[0].description, string `This is the description for Product 1. `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); test:assertEquals(invoice.products.product[0].price.\#content, 57.70d); test:assertEquals(invoice.products.product[0].price.currency, "USD"); test:assertEquals(invoice.products.product[0].category, "Home and Garden"); + test:assertEquals(invoice.products.product[1].length(), 5, "Product 2 field count mismatched"); test:assertEquals(invoice.products.product[1].id, 2); test:assertEquals(invoice.products.product[1].name, "Product 2"); test:assertEquals(invoice.products.product[1].description, string `This is the description for Product 2. `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); test:assertEquals(invoice.products.product[1].price.\#content, 6312.36d); test:assertEquals(invoice.products.product[1].price.currency, "LKR"); test:assertEquals(invoice.products.product[1].category, "Books"); + test:assertEquals(invoice.customers.customer[0].length(), 5, "Customer 1 field count mismatched"); test:assertEquals(invoice.customers.customer[0].id, "C001"); test:assertEquals(invoice.customers.customer[0].name, "John Doe"); test:assertEquals(invoice.customers.customer[0].email, "john@example.com"); test:assertEquals(invoice.customers.customer[0].phone, "569-5052"); + test:assertEquals(invoice.customers.customer[0].address.length(), 4, "Customer 1 address field count mismatched"); test:assertEquals(invoice.customers.customer[0].address.street, "MZ738SI4DV St."); test:assertEquals(invoice.customers.customer[0].address.city, "Newport"); test:assertEquals(invoice.customers.customer[0].address.state, "NY"); test:assertEquals(invoice.customers.customer[0].address.zip, 19140); + test:assertEquals(invoice.customers.customer[1].length(), 5, "Customer 2 field count mismatched"); test:assertEquals(invoice.customers.customer[1].id, "C002"); test:assertEquals(invoice.customers.customer[1].name, "Jane Doe"); test:assertEquals(invoice.customers.customer[1].email, "jane@example.com"); test:assertEquals(invoice.customers.customer[1].phone, "674-2864"); + test:assertEquals(invoice.customers.customer[1].address.length(), 4, "Customer 2 address field count mismatched"); test:assertEquals(invoice.customers.customer[1].address.street, "ZI0TGK3BKG St."); test:assertEquals(invoice.customers.customer[1].address.city, "Otherville"); test:assertEquals(invoice.customers.customer[1].address.state, "CA"); test:assertEquals(invoice.customers.customer[1].address.zip, 77855); } +# Test namespaces & prefixes with a record including all fields without using any annotations. +# +# + return - return error on failure, otherwise nil. +@test:Config +function testNamespaceInvoiceFullPlain() returns error? { + stream dataStream = check io:fileReadBlocksAsStream("tests/resources/default_namespaced_invoice.xml"); + InvoiceFullPlain invoice = check fromXmlStringWithType(dataStream); + + test:assertEquals(invoice.length(), 2, "Invoice count mismatched"); + test:assertEquals(invoice.products.length(), 1, "Products count mismatched"); + test:assertEquals(invoice.products.product.length(), 2, "Products/product count mismatched"); + test:assertEquals(invoice.customers.length(), 1, "Customers count mismatched"); + test:assertEquals(invoice.customers.customer.length(), 2, "Customers/customer count mismatched"); + + test:assertEquals(invoice.products.product[0].length(), 5, "Product 1 field count mismatched"); + test:assertEquals(invoice.products.product[0].id, 1); + test:assertEquals(invoice.products.product[0].name, "Product 1"); + test:assertEquals(invoice.products.product[0].description, string `This is the description for + Product 1. + `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); + test:assertEquals(invoice.products.product[0].price.\#content, 57.70d); + test:assertEquals(invoice.products.product[0].price.currency, "USD"); + test:assertEquals(invoice.products.product[0].category, "Home and Garden"); + + test:assertEquals(invoice.products.product[1].length(), 5, "Product 2 field count mismatched"); + test:assertEquals(invoice.products.product[1].id, 2); + test:assertEquals(invoice.products.product[1].name, "Product 2"); + test:assertEquals(invoice.products.product[1].description, string `This is the description for + Product 2. + `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); + test:assertEquals(invoice.products.product[1].price.\#content, 6312.36d); + test:assertEquals(invoice.products.product[1].price.currency, "LKR"); + test:assertEquals(invoice.products.product[1].category, "Books"); + + test:assertEquals(invoice.customers.customer[0].length(), 5, "Customer 1 field count mismatched"); + test:assertEquals(invoice.customers.customer[0].id, "C001"); + test:assertEquals(invoice.customers.customer[0].name, "John Doe"); + test:assertEquals(invoice.customers.customer[0].email, "john@example.com"); + test:assertEquals(invoice.customers.customer[0].phone, "569-5052"); + test:assertEquals(invoice.customers.customer[0].address.length(), 4, "Customer 1 address field count mismatched"); + test:assertEquals(invoice.customers.customer[0].address.street, "MZ738SI4DV St."); + test:assertEquals(invoice.customers.customer[0].address.city, "Newport"); + test:assertEquals(invoice.customers.customer[0].address.state, "NY"); + test:assertEquals(invoice.customers.customer[0].address.zip, 19140); + + test:assertEquals(invoice.customers.customer[1].length(), 5, "Customer 2 field count mismatched"); + test:assertEquals(invoice.customers.customer[1].id, "C002"); + test:assertEquals(invoice.customers.customer[1].name, "Jane Doe"); + test:assertEquals(invoice.customers.customer[1].email, "jane@example.com"); + test:assertEquals(invoice.customers.customer[1].phone, "674-2864"); + test:assertEquals(invoice.customers.customer[1].address.length(), 4, "Customer 2 address field count mismatched"); + test:assertEquals(invoice.customers.customer[1].address.street, "ZI0TGK3BKG St."); + test:assertEquals(invoice.customers.customer[1].address.city, "Otherville"); + test:assertEquals(invoice.customers.customer[1].address.state, "CA"); + test:assertEquals(invoice.customers.customer[1].address.zip, 77855); +}