From b5075a1c1ee4b90a6a9b1a16b84ba104b10baf7a Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:23:30 +0530 Subject: [PATCH] Improve code coverage --- .../tests/from_json_string_file_read_test.bal | 313 ++++++++++++++++++ ballerina/tests/from_json_string_test.bal | 71 +++- ballerina/tests/resources/source_1.json | 22 ++ ballerina/tests/resources/source_2.json | 25 ++ ballerina/tests/resources/source_3.json | 40 +++ ballerina/tests/resources/source_4.json | 113 +++++++ .../lib/data/jsondata/FromString.java | 11 - .../lib/data/jsondata/json/JsonTraverse.java | 2 - 8 files changed, 583 insertions(+), 14 deletions(-) create mode 100644 ballerina/tests/from_json_string_file_read_test.bal create mode 100644 ballerina/tests/resources/source_1.json create mode 100644 ballerina/tests/resources/source_2.json create mode 100644 ballerina/tests/resources/source_3.json create mode 100644 ballerina/tests/resources/source_4.json diff --git a/ballerina/tests/from_json_string_file_read_test.bal b/ballerina/tests/from_json_string_file_read_test.bal new file mode 100644 index 0000000..4469373 --- /dev/null +++ b/ballerina/tests/from_json_string_file_read_test.bal @@ -0,0 +1,313 @@ +// 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 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" + } + int 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.json", + 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.json", + 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.json", + 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.json", + 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.json", + OrdersS4, + { + "order": [ + { + "id": 123456, + "products": { + "product": [{"id": 789, "name": "Laptop"}] + } + }, + { + "id": 789012, + "products": { + "product": [{"id": 123, "name": "Smartphone"}] + } + } + ] + } + ] + ]; +} diff --git a/ballerina/tests/from_json_string_test.bal b/ballerina/tests/from_json_string_test.bal index b5d0e3d..1010b1b 100644 --- a/ballerina/tests/from_json_string_test.bal +++ b/ballerina/tests/from_json_string_test.bal @@ -30,7 +30,10 @@ function basicTypeDataProviderForParseString() returns [string, typedescerr6).message(), "incompatible expected type '()' for value '()'"); } + +@test:Config +isolated function testConvertStringValueNegative() { + string:Char|Error err1 = parseString("\"abc\""); + test:assertTrue(err1 is Error); + test:assertEquals((err1).message(), "incompatible expected type 'lang.string:Char' for value 'abc'"); + + ()|Error err2 = parseString("\"abc\""); + test:assertTrue(err2 is Error); + test:assertEquals((err2).message(), "incompatible expected type '()' for value 'abc'"); + + "a"|Error err3 = parseString("\"abc\""); + test:assertTrue(err3 is Error); + test:assertEquals((err3).message(), "incompatible expected type '\"a\"' for value 'abc'"); + + boolean|Error err4 = parseString("\"abc\""); + test:assertTrue(err4 is Error); + test:assertEquals((err4).message(), "incompatible expected type 'boolean' for value 'abc'"); + + int|float|Error err5 = parseString("\"abc\""); + test:assertTrue(err5 is Error); + test:assertEquals((err5).message(), "incompatible expected type '(int|float)' for value 'abc'"); + + float|Error err6 = parseString(string `1f`); + test:assertTrue(err6 is Error); + test:assertEquals((err6).message(), "incompatible expected type 'float' for value '1f'"); + + float|Error err7 = parseString(string `1F`); + test:assertTrue(err7 is Error); + test:assertEquals((err7).message(), "incompatible expected type 'float' for value '1F'"); + + float|Error err8 = parseString(string `1d`); + test:assertTrue(err8 is Error); + test:assertEquals((err8).message(), "incompatible expected type 'float' for value '1d'"); + + float|Error err9 = parseString(string `1D`); + test:assertTrue(err9 is Error); + test:assertEquals((err9).message(), "incompatible expected type 'float' for value '1D'"); +} diff --git a/ballerina/tests/resources/source_1.json b/ballerina/tests/resources/source_1.json new file mode 100644 index 0000000..170fc22 --- /dev/null +++ b/ballerina/tests/resources/source_1.json @@ -0,0 +1,22 @@ +{ + "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 + } + ] +} diff --git a/ballerina/tests/resources/source_2.json b/ballerina/tests/resources/source_2.json new file mode 100644 index 0000000..46a5c99 --- /dev/null +++ b/ballerina/tests/resources/source_2.json @@ -0,0 +1,25 @@ +{ + "movie": [ + { + "title": "American Beauty", + "director": "Sam Mendes", + "genre": "Drama", + "release_year": 1999, + "id": "1" + }, + { + "title": "The Shawshank Redemption", + "director": "Frank Darabont", + "genre": "Drama", + "release_year": 1994, + "id": "2" + }, + { + "title": "Forrest Gump", + "director": "Robert Zemeckis", + "genre": "Drama", + "release_year": 1994, + "id": "3" + } + ] +} diff --git a/ballerina/tests/resources/source_3.json b/ballerina/tests/resources/source_3.json new file mode 100644 index 0000000..ec0650a --- /dev/null +++ b/ballerina/tests/resources/source_3.json @@ -0,0 +1,40 @@ +{ + "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 + } + ] +} diff --git a/ballerina/tests/resources/source_4.json b/ballerina/tests/resources/source_4.json new file mode 100644 index 0000000..d42cda1 --- /dev/null +++ b/ballerina/tests/resources/source_4.json @@ -0,0 +1,113 @@ +{ + "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" + } + ] +} diff --git a/native/src/main/java/io/ballerina/lib/data/jsondata/FromString.java b/native/src/main/java/io/ballerina/lib/data/jsondata/FromString.java index c4befa2..1c13762 100644 --- a/native/src/main/java/io/ballerina/lib/data/jsondata/FromString.java +++ b/native/src/main/java/io/ballerina/lib/data/jsondata/FromString.java @@ -34,7 +34,6 @@ import io.ballerina.runtime.api.values.BDecimal; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BString; -import io.ballerina.runtime.api.values.BTypedesc; import java.util.ArrayList; import java.util.Comparator; @@ -78,16 +77,6 @@ public class FromString { public static final Integer UNSIGNED16_MAX_VALUE = 65535; public static final Integer UNSIGNED8_MAX_VALUE = 255; - public static Object fromStringWithType(BString string, BTypedesc typed) { - Type expType = typed.getDescribingType(); - - try { - return fromStringWithType(string, expType); - } catch (NumberFormatException e) { - return returnError(string.getValue(), expType.toString()); - } - } - public static Object fromStringWithType(BString string, Type expType) { String value = string.getValue(); try { diff --git a/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java b/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java index 127ded3..58bd9ba 100644 --- a/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java +++ b/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java @@ -71,8 +71,6 @@ public static Object traverse(Object json, BMap options, Type t (Boolean) ((BMap) allowDataProjection).get(Constants.NIL_AS_OPTIONAL_FIELD); } return jsonTree.traverseJson(json, type); - } catch (BError e) { - return e; } finally { jsonTree.reset(); }