Skip to content

Commit

Permalink
Merge pull request #2204 from lnash94/integrate-data.json-2
Browse files Browse the repository at this point in the history
[master] Use `toJson` method from data.jsondata module in http client
  • Loading branch information
lnash94 authored Nov 6, 2024
2 parents 2267e56 + 4153e6f commit d375c3b
Show file tree
Hide file tree
Showing 30 changed files with 333 additions and 69 deletions.
13 changes: 12 additions & 1 deletion ballerina-tests/http-advanced-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.10.0"
distribution-version = "2201.11.0-20241008-112400-81975006"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -54,6 +54,16 @@ modules = [
{org = "ballerina", packageName = "crypto", moduleName = "crypto"}
]

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

[[package]]
org = "ballerina"
name = "file"
Expand All @@ -79,6 +89,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
17 changes: 16 additions & 1 deletion ballerina-tests/http-client-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.10.0"
distribution-version = "2201.11.0-20241008-112400-81975006"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -54,6 +54,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 @@ -76,6 +89,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 @@ -105,6 +119,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"});
}
13 changes: 12 additions & 1 deletion ballerina-tests/http-dispatching-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.10.0"
distribution-version = "2201.11.0-20241008-112400-81975006"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -54,6 +54,16 @@ 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"}
]

[[package]]
org = "ballerina"
name = "file"
Expand All @@ -76,6 +86,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
Loading

0 comments on commit d375c3b

Please sign in to comment.