Skip to content

Commit

Permalink
Merge pull request #2216 from HindujaB/java21
Browse files Browse the repository at this point in the history
[WIP] Migrate runtime APIs for Java 21
  • Loading branch information
warunalakshitha authored Nov 14, 2024
2 parents 30ed05b + e5f8d8c commit 7e9f66a
Show file tree
Hide file tree
Showing 94 changed files with 1,754 additions and 233 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ load-tests/**/Dependencies.toml
# Ballerina related ignores
Ballerina.lock
velocity.log*

compiler-plugin-tests/**/target
15 changes: 15 additions & 0 deletions ballerina-tests/http-client-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ dependencies = [
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "data.jsondata"
version = "0.3.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.object"}
]
modules = [
{org = "ballerina", packageName = "data.jsondata", moduleName = "data.jsondata"}
]

[[package]]
org = "ballerina"
name = "file"
Expand All @@ -75,6 +88,7 @@ dependencies = [
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "data.jsondata"},
{org = "ballerina", name = "file"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand Down Expand Up @@ -104,6 +118,7 @@ name = "http_client_tests"
version = "2.13.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "data.jsondata"},
{org = "ballerina", name = "http"},
{org = "ballerina", name = "http_test_common"},
{org = "ballerina", name = "io"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;
import ballerina/mime;
import ballerina/test;
import ballerina/data.jsondata;

service /api on new http:Listener(resBindingAdvancedPort) {

Expand All @@ -41,6 +43,31 @@ service /api on new http:Listener(resBindingAdvancedPort) {
resource function get byteArray() returns byte[] {
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
}

resource function get overwriteNames/jsont(boolean y) returns json|TPerson {
if y {
return {"name": "John", "age": "23"};
}
TPerson t = {firstName: "Potter", personAge: "30"};
return t;
}

resource function post overwriteNames/jsont(TPerson payload) returns TPerson {
return payload;
}

resource function get status/code() returns OKPerson {
return {body: {firstName: "Potter", personAge: "40"}};
}

resource function get projection/tests() returns json {
json v = {
a: "a",
b: "b",
c: "c"
};
return v;
}
}

final http:Client clientEP = check new (string `localhost:${resBindingAdvancedPort}/api`);
Expand Down Expand Up @@ -101,3 +128,52 @@ function testResponseWithAnydataResBinding() returns error? {
test:assertFail("Invalid response type");
}
}

public type TPerson record {
@jsondata:Name {
value: "name"
}
string firstName;
@jsondata:Name {
value: "age"
}
string personAge;
};

public type OKPerson record {|
*http:Ok;
TPerson body;
|};

@test:Config {}
function clientoverwriteResponseJsonName() returns error? {
TPerson res1 = check clientEP->/overwriteNames/jsont(y = true);
test:assertEquals(res1, {firstName: "John", personAge: "23"});

json res2 = check clientEP->/overwriteNames/jsont(y = false);
test:assertEquals(res2, {"name": "Potter", "age": "30"});

json j = {
name: "Sumudu",
age: "29"
};

TPerson res3 = check clientEP->/overwriteNames/jsont.post(j);
test:assertEquals(res3, {firstName: "Sumudu", personAge: "29"});

json res4 = check clientEP->/status/code;
test:assertEquals(res4, {name: "Potter", age: "40"});
}

public type AB record {|
string a;
string b;
|};

@test:Config {}
function projectionTestWithClient() {
AB|error res = clientEP->/projection/tests();
if res is error {
test:assertEquals(res.message(), "Payload binding failed: undefined field 'c'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,9 @@ function testAllBindingErrorsWithNillableTypes() returns error? {
test:assertEquals(response.statusCode, 200, msg = "Found unexpected output");
common:assertHeaderValue(check response.getHeader(common:CONTENT_TYPE), common:TEXT_PLAIN);
common:assertTextPayload(response.getTextPayload(),
"Payload binding failed: 'map<json>' value cannot be converted to " +
"'xml<(lang.xml:Element|lang.xml:Comment|lang.xml:ProcessingInstruction|lang.xml:Text)>?'|" +
"incompatible typedesc int? found for 'text/plain' mime type");
"Payload binding failed: incompatible expected type 'xml<(lang.xml:Element|lang.xml:Comment|" +
"lang.xml:ProcessingInstruction|lang.xml:Text)>?' for value " +
"'{\"id\":\"chamil\",\"values\":{\"a\":2,\"b\":45,\"c\":{\"x\":\"mnb\",\"y\":\"uio\"}}}'|incompatible typedesc int? found for 'text/plain' mime type");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}
Expand Down Expand Up @@ -815,9 +815,7 @@ function testDBRecordErrorNegative() {
ClientDBErrorPerson|error response = clientDBBackendClient->post("/backend/getRecord", "want record");
if (response is error) {
common:assertTrueTextPayload(response.message(),
"Payload binding failed: 'map<json>' value cannot be converted to 'http_client_tests:ClientDBErrorPerson'");
common:assertTrueTextPayload(response.message(),
"missing required field 'weight' of type 'float' in record 'http_client_tests:ClientDBErrorPerson'");
"Payload binding failed: required field 'weight' not present in JSON");
} else {
test:assertFail(msg = "Found unexpected output type: ClientDBErrorPerson");
}
Expand All @@ -828,7 +826,7 @@ function testDBRecordArrayNegative() {
ClientDBErrorPerson[]|error response = clientDBBackendClient->post("/backend/getRecordArr", "want record arr");
if (response is error) {
common:assertTrueTextPayload(response.message(),
"Payload binding failed: 'json[]' value cannot be converted to 'http_client_tests:ClientDBErrorPerson[]'");
"Payload binding failed: required field 'weight' not present in JSON");
} else {
test:assertFail(msg = "Found unexpected output type: ClientDBErrorPerson[]");
}
Expand All @@ -852,7 +850,7 @@ function testMapOfStringDataBindingWithJsonPayload() {
map<string>|error response = clientDBBackendClient->get("/backend/getJson");
if (response is error) {
common:assertTrueTextPayload(response.message(),
"Payload binding failed: 'map<json>' value cannot be converted to 'map<string>'");
"Payload binding failed: incompatible expected type 'string' for value '{\"a\":2,\"b\":45,\"c\":{\"x\":\"mnb\",\"y\":\"uio\"}}'");
} else {
test:assertFail(msg = "Found unexpected output type: map<string>");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ function testIntMapDatabindingByType() returns error? {
test:assertEquals(response, {"name": 11, "team": 22}, msg = "Found unexpected output");
}

@test:Config {}
@test:Config {enable:false}
function testIntTableDatabinding() returns error? {
table<map<int>> tbl = check clientDBBackendClient->get("/anydataTest/intTableType");
object {
Expand All @@ -355,7 +355,7 @@ function testIntTableDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable:false}
function testIntTableOrMapofIntArrayDatabinding() returns error? {
map<int>[]|table<map<int>> response = check clientDBBackendClient->get("/anydataTest/intTableType");
if response is map<int>[] {
Expand All @@ -369,7 +369,7 @@ function testIntTableOrMapofIntArrayDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable:false}
function testIntTableOrXmlArrayDatabinding() returns error? {
table<map<int>>|xml tbl = check clientDBBackendClient->get("/anydataTest/intTableType");
if tbl is table<map<int>> {
Expand All @@ -387,7 +387,7 @@ function testIntTableOrXmlArrayDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable:false}
function testIntTableDatabindingByType() returns error? {
table<map<int>> tbl = check clientDBBackendClient->get("/anydataTest/intTableTypeWithInvalidMimeType");
object {
Expand Down Expand Up @@ -448,7 +448,7 @@ function testStringMapDatabindingByType() returns error? {
test:assertEquals(response, {name: "hello", team: "ballerina"}, msg = "Found unexpected output");
}

@test:Config {}
@test:Config {enable:false}
function testStringTableDatabinding() returns error? {
table<map<string>> tbl = check clientDBBackendClient->get("/anydataTest/stringTableType");
object {
Expand All @@ -462,7 +462,7 @@ function testStringTableDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable:false}
function testStringTableDatabindingByType() returns error? {
table<map<string>> tbl = check clientDBBackendClient->get("/anydataTest/stringTableTypeWithInvalidMimeType");
object {
Expand Down Expand Up @@ -508,7 +508,7 @@ function testRecordMapDatabindingByType() returns error? {
test:assertEquals(response.get("1"), {name: "hello", age: 23}, msg = "Found unexpected output");
}

@test:Config {}
@test:Config {enable:false}
function testRecordTableDatabinding() returns error? {
table<ClientAnydataDBPerson> tbl = check clientDBBackendClient->get("/anydataTest/recordTableType");
object {
Expand All @@ -522,7 +522,7 @@ function testRecordTableDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable: false}
function testRecordTableDatabindingByType() returns error? {
table<ClientAnydataDBPerson> tbl = check clientDBBackendClient->get("/anydataTest/recordTableTypeWithInvalidMimeType");
object {
Expand Down Expand Up @@ -574,7 +574,7 @@ function testByteArrMapDatabindingByType() returns error? {
test:assertEquals(check strings:fromBytes(val), "STDLIB", msg = "Found unexpected output");
}

@test:Config {}
@test:Config {enable: false}
function testByteArrTableDatabinding() returns error? {
table<map<byte[]>> response = check clientDBBackendClient->get("/anydataTest/byteArrTableType");
object {
Expand All @@ -589,7 +589,7 @@ function testByteArrTableDatabinding() returns error? {
}
}

@test:Config {}
@test:Config {enable:false}
function testByteArrTableDatabindingByType() returns error? {
table<map<byte[]>> response = check clientDBBackendClient->get("/anydataTest/byteArrTableTypeWithInvalidMimeType");
object {
Expand All @@ -609,7 +609,7 @@ function testXmlArrDatabinding() {
xml[]|error response = clientDBBackendClient->get("/anydataTest/xmlArrType");
if response is error {
common:assertTrueTextPayload(response.message(),
"Payload binding failed: 'json[]' value cannot be converted to 'xml<");
"Payload binding failed: invalid type 'xml<(lang.xml:Element|lang.xml:Comment|lang.xml:ProcessingInstruction|lang.xml:Text)>' expected 'anydata'");
} else {
test:assertEquals(response[0], xml `<name>WSO2</name>`, msg = "Found unexpected output");
}
Expand All @@ -620,7 +620,7 @@ function testXmlArrDatabindingByType() {
xml[]|error response = clientDBBackendClient->get("/anydataTest/xmlArrTypeWithInvalidMimeType");
if response is error {
common:assertTrueTextPayload(response.message(),
"Payload binding failed: 'json[]' value cannot be converted to 'xml");
"Payload binding failed: invalid type 'xml<(lang.xml:Element|lang.xml:Comment|lang.xml:ProcessingInstruction|lang.xml:Text)>' expected 'anydata'");
} else {
test:assertEquals(response[0], xml `<name>WSO2</name>`, msg = "Found unexpected output");
}
Expand Down
14 changes: 12 additions & 2 deletions ballerina-tests/http-client-tests/tests/sc_res_binding_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ service /api on new http:Listener(statusCodeBindingPort2) {
headers: {userId: "user-1", reqId: 1}
};
}

resource function get album/auther() returns OKPerson {
return {body: {firstName: "Potter", personAge: "40"}};
}
}

final http:StatusCodeClient albumClient = check new (string `localhost:${statusCodeBindingPort2}/api`);
Expand Down Expand Up @@ -467,8 +471,8 @@ function testUnionPayloadBindingWithStatusCodeResponse() returns error? {
AlbumFoundInvalid|AlbumFound|AlbumNotFound|error res5 = albumClient->/albums/'1;
if res5 is error {
test:assertTrue(res5 is http:PayloadBindingError);
test:assertTrue(res5.message().includes("Payload binding failed: 'map<json>' value cannot be" +
" converted to 'http_client_tests:AlbumInvalid"), "Invalid error message");
test:assertTrue(res5.message().includes("Payload binding failed: required field 'invalidField' not present in JSON"),
"Invalid error message");
} else {
test:assertFail("Invalid response type");
}
Expand Down Expand Up @@ -670,3 +674,9 @@ function testStatusCodeBindingWithNamedHeaders() returns error? {
test:assertFail("Invalid response type");
}
}

@test:Config {}
function testOverwriteName() returns error? {
OKPerson res = check albumClient->/album/auther;
test:assertEquals(res.body, {firstName: "Potter", personAge: "40"});
}
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,7 @@ function testDataBindingStructWithNoMatchingContent() returns error? {
http:Response|error response = dataBindingClient->post("/dataBinding/body6", req);
if response is http:Response {
test:assertEquals(response.statusCode, 400, msg = "Found unexpected output");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "data binding failed: {ballerina");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "}ConversionError, {\"message\":\"'map<json>' ");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "value cannot be converted to 'http_dispatching_tests:Person':");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "missing required field 'age' of type 'int' in record 'http_dispatching_tests:Person'");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "field 'team' cannot be added to the closed record 'http_dispatching_tests:Person'\"");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "data binding failed: undefined field 'team'");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}
Expand All @@ -411,11 +407,7 @@ function testDataBindingStructWithInvalidTypes() returns error? {
http:Response|error response = dataBindingClient->post("/dataBinding/body7", req);
if response is http:Response {
test:assertEquals(response.statusCode, 400, msg = "Found unexpected output");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "'map<json>' value cannot be converted to 'http_dispatching_tests:Stock'");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "missing required field 'price' of type 'float' in record 'http_dispatching_tests:Stock'");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "missing required field 'id' of type 'int' in record 'http_dispatching_tests:Stock'");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "field 'name' cannot be added to the closed record 'http_dispatching_tests:Stock'");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "field 'team' cannot be added to the closed record 'http_dispatching_tests:Stock'");
check common:assertJsonErrorPayloadPartialMessage(check response.getJsonPayload(), "data binding failed: undefined field 'name'");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public function testSendingMap() returns error? {
common:assertJsonPayload(resp.getJsonPayload(), {sam: 50, jhon: 60});
}

@test:Config {}
@test:Config {
enable: false
}
public function testSendingMapArray() returns error? {
map<json> jj = {sam: {hello: "world"}, jhon: {no: 56}};
map<json>[] val = [jj, jj];
Expand All @@ -102,7 +104,9 @@ public function testSendingTable() returns error? {
]);
}

@test:Config {}
@test:Config {
enable: false
}
public function testSendingTableArray() returns error? {
CustomerTable customerTab = table [
{id: 13, fname: "Dan", lname: "Bing"}
Expand Down Expand Up @@ -236,7 +240,7 @@ public function testSendingClosedRecordTable() returns error? {
]);
}

@test:Config {}
@test:Config { enable: false}
public function testRequestAnydataNegative() returns error? {
json[] x = [];
x.push(x);
Expand Down Expand Up @@ -472,7 +476,7 @@ public function testGettingClosedRecordArray() returns error? {
]);
}

@test:Config {}
@test:Config {enable: false}
public function testResponseAnydataNegative() returns error? {
http:Response resp = check outRequestClient->get("/mytest/anydataNegative");
test:assertEquals(resp.statusCode, 500, msg = "Found unexpected output");
Expand Down
Loading

0 comments on commit 7e9f66a

Please sign in to comment.