Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
HindujaB committed Jan 16, 2025
1 parent 5bc3d35 commit 024101e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.runtime.api.values.BTypedesc;
import io.ballerina.runtime.internal.JsonGenerator;
import io.ballerina.runtime.internal.values.ErrorValue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import java.io.IOException;
import static io.ballerina.runtime.api.utils.JsonUtils.convertJSON;
import static io.ballerina.runtime.api.utils.JsonUtils.convertJSONToRecord;

Expand Down Expand Up @@ -99,4 +101,16 @@ public static Object testParsingNullString(BString str) {
public static Object testBStringParsingWithProcessingMode(BString str) {
return JsonUtils.parse(str, JsonUtils.NonStringValueProcessingMode.FROM_JSON_STRING);
}

public static Object testJsonSerializeExtern(Object jsonObject) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (JsonGenerator gen = new JsonGenerator(out)) {
gen.serialize(jsonObject);
gen.flush();
out.close();
} catch (IOException e) {
throw new ErrorValue(StringUtils.fromString(e.getMessage()), e);
}
return StringUtils.fromString(out.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public void testRuntimeAPIs(String packageName) {
@DataProvider
public Object[] packageNameProvider() {
return new String[]{
"values",
"errors",
"types",
"invalid_values",
"async",
"utils",
"identifier_utils",
"environment",
"stream",
// "values",
// "errors",
// "types",
// "invalid_values",
// "async",
// "utils",
// "identifier_utils",
// "environment",
// "stream",
"json"
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,33 @@ public function main() {

result = testStringParsingWithProcessingMode(jsonStr);
test:assertTrue(result is json, "Invalid json");

testJsonSerialize();
}

const string[] CONST_ROLES = ["Admin"];

type Profile record {|
string name;
int age;
|};

function testJsonSerialize() {
Profile profile = {name: "John", age: 30};
json j = {
roles: CONST_ROLES,
ID: "User1",
profile: profile
};
string expected = "{\"roles\":[\"Admin\"], \"ID\":\"User1\", \"profile\":{\"name\":\"John\", \"age\":30}}";
string result = checkpanic testJsonSerializeExtern(j);
test:assertEquals(result, expected, "Invalid json serialization");
}

public isolated function testJsonSerializeExtern(json j) returns string|error = @java:Method {
'class: "org.ballerinalang.nativeimpl.jvm.runtime.api.tests.JsonValues"
} external;

public isolated function testParsingWrongCharset(string str) returns json|error = @java:Method {
'class: "org.ballerinalang.nativeimpl.jvm.runtime.api.tests.JsonValues"
} external;
Expand Down

0 comments on commit 024101e

Please sign in to comment.