Skip to content

Commit

Permalink
Merge pull request ballerina-platform#2 from hasithaa/main
Browse files Browse the repository at this point in the history
Fix gradle build and update tests
  • Loading branch information
prakanth97 authored Nov 1, 2023
2 parents fa8f481 + 240a44d commit 79ab095
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 5 deletions.
6 changes: 2 additions & 4 deletions ballerina/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ ballerina {
langVersion = ballerinaLangVersion
}

configurations {
externalJars
}

task updateTomlFiles {
doLast {
def newConfig = ballerinaTomlFilePlaceHolder.text.replace("@project.version@", project.version)
Expand Down Expand Up @@ -113,6 +109,8 @@ task deleteDependencyTomlFiles {
}
}


updateTomlFiles.dependsOn unpackStdLibs
build.dependsOn "generatePomFileForMavenPublication"
build.dependsOn ":${packageName}-native:build"
build.dependsOn deleteDependencyTomlFiles
Expand Down
117 changes: 117 additions & 0 deletions ballerina/tests/stream_default_namspace_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte[], error?> 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, "[email protected]");
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, "[email protected]");
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<byte[], error?> 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, "[email protected]");
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, "[email protected]");
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");
Expand Down
161 changes: 161 additions & 0 deletions ballerina/tests/stream_large_file_test.bal
Original file line number Diff line number Diff line change
@@ -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<byte[], error?> 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<byte[], error?> 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 `<inv:invoice xmlns:inv="http://www.example.com/invoice">`.toBytes(), 0);
_ = check wbc.write("<inv:products>\n".toBytes(), 0);
foreach int i in 0 ... 100000 {
_ = check wbc.write(createProduct(i).toString().toBytes(), 0);
}
_ = check wbc.write("</inv:products>\n".toBytes(), 0);
_ = check wbc.write("<inv:customers>\n".toBytes(), 0);
foreach int i in 0 ... 100000 {
_ = check wbc.write(createCustomer(i).toString().toBytes(), 0);
}
_ = check wbc.write("</inv:customers>\n".toBytes(), 0);
_ = check wbc.write("</inv:invoice>\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 `<pro:product id="${id}" name="${name}">
<pro:description>${description}</pro:description>
<pro:price currency="USD">${price}</pro:price>
<pro:category>${category}</pro:category>
</pro:product>`;
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 `<customers:customer id="${id}" name="${name}">
<customers:address>${address}</customers:address>
<customers:email>${name}@example.com</customers:email>
<customers:city>${city}</customers:city>
<customers:country>${country}</customers:country>
</customers:customer>`;
return customer;
}
Loading

0 comments on commit 79ab095

Please sign in to comment.