Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
chamil321 committed Apr 21, 2021
1 parent bb455d2 commit f5928e5
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class CompilerPluginTest {
private static final String HTTP_111 = "HTTP_111";
private static final String HTTP_112 = "HTTP_112";
private static final String HTTP_113 = "HTTP_113";
private static final String HTTP_114 = "HTTP_114";

private static final String REMOTE_METHODS_NOT_ALLOWED = "remote methods are not allowed in http:Service";

Expand Down Expand Up @@ -211,4 +212,52 @@ public void testListenerTypes() {
err -> err.diagnosticInfo().messageFormat().contains(
"invalid resource parameter type: 'ballerina/http")).forEach(Assert::assertTrue);
}

// @Test
public void testCallerInfoAnnotation() {
Package currentPackage = loadPackage("sample_package_10");
PackageCompilation compilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = compilation.diagnosticResult();
Assert.assertEquals(diagnosticResult.diagnosticCount(), 6);
assertError(diagnosticResult, 0, "incompatible respond method argument type : expected " +
"'int' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 1, "incompatible respond method argument type : expected " +
"'decimal' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 2, "incompatible respond method argument type : expected " +
"'Person' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 3, "incompatible respond method argument type : expected " +
"'string' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 4, "incompatible respond method argument type : expected " +
"'Person' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 5, "incompatible respond method argument type : expected " +
"'Person' according to the 'http:CallerInfo' annotation", HTTP_114);
}

@Test
public void testCallerInfoTypes() {
Package currentPackage = loadPackage("sample_package_11");
PackageCompilation compilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = compilation.diagnosticResult();
Assert.assertEquals(diagnosticResult.diagnosticCount(), 10);
assertError(diagnosticResult, 0, "incompatible respond method argument type : expected " +
"'http:Response' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 1, "incompatible respond method argument type : expected " +
"'Xml' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 2, "incompatible respond method argument type : expected " +
"'json' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 3, "incompatible respond method argument type : expected " +
"'ByteArr' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 4, "incompatible respond method argument type : expected " +
"'MapJson' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 5, "incompatible respond method argument type : expected " +
"'PersonTable' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 6, "incompatible respond method argument type : expected " +
"'MapJsonArr' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 7, "incompatible respond method argument type : expected " +
"'PersonTableArr' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 8, "incompatible respond method argument type : expected " +
"'EntityArr' according to the 'http:CallerInfo' annotation", HTTP_114);
assertError(diagnosticResult, 9, "incompatible respond method argument type : expected " +
"'ByteStream' according to the 'http:CallerInfo' annotation", HTTP_114);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ service http:Service on new http:Listener(9090) {
}

resource function get callerInfo17(@http:CallerInfo {respondType: Person}http:Caller abc) returns error? {
error? a = abc->respond({school:1.23}); // This getting passed as map
error? a = abc->respond({school:1.23}); // This getting passed as map<json> and this is a limitation
}

resource function get callerInfo18(@http:CallerInfo {respondType: Person}http:Caller abc) returns error? {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "http_test"
name = "sample_10"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import ballerina/http;
import ballerina/mime;
import ballerina/io;

type RetEmployee record {|
readonly int id;
string name;
|};

type PersonTable table<RetEmployee> key(id);
type Xml xml;
type ByteArr byte[];
type MapJson map<json>;
type MapJsonArr map<json>[];
type EntityArr mime:Entity[];
type PersonTableArr PersonTable[];
type ByteStream stream<byte[], io:Error?>;

service http:Service on new http:Listener(9090) {
resource function get callerInfo17(int xyz, @http:CallerInfo {respondType: http:Response} http:Caller abc) {
http:Response res = new;
checkpanic abc->respond(res);
}

resource function get callerInfo18(int xyz, @http:CallerInfo {respondType: http:Response} http:Caller abc) {
checkpanic abc->respond("res"); // error
}

resource function get callerInfo20(int xyz, @http:CallerInfo {respondType: Xml} http:Caller abc) {
xml x = xml `<book>Hello World</book>`;
checkpanic abc->respond(x);
}

resource function get callerInfo21(int xyz, @http:CallerInfo {respondType: Xml} http:Caller abc) {
checkpanic abc->respond("res"); // error
}

resource function get callerInfo2_2(int xyz, @http:CallerInfo {respondType: json} http:Caller abc) {
json j = {hello: "world"};
checkpanic abc->respond(j);
}

resource function get callerInfo22(int xyz, @http:CallerInfo {respondType: json} http:Caller abc) {
checkpanic abc->respond({hello: "world"}); // this is also fails and a limitation for inline json
}

resource function get callerInfo23(int xyz, @http:CallerInfo {respondType: json} http:Caller abc) {
http:Response res = new;
checkpanic abc->respond(res); // error
}

resource function get callerInfo24(int xyz, @http:CallerInfo {respondType: ByteArr} http:Caller abc) {
checkpanic abc->respond("Sample Text".toBytes());
}

resource function get callerInfo25(int xyz, @http:CallerInfo {respondType: ByteArr} http:Caller abc) {
http:Response res = new;
checkpanic abc->respond(res); // error
}

resource function get callerInfo26(int xyz, @http:CallerInfo {respondType: MapJson} http:Caller abc) {
map<json> jj = {sam: {hello:"world"}, jon: {no:56}};
checkpanic abc->respond(jj);
}

resource function get callerInfo27(int xyz, @http:CallerInfo {respondType: MapJson} http:Caller abc) {
http:Response res = new;
checkpanic abc->respond(res); // error
}

resource function get callerInfo28(int xyz, @http:CallerInfo {respondType: PersonTable} http:Caller abc) {
PersonTable tbPerson = table [
{id: 1, name: "John"},
{id: 2, name: "Bella"}
];
checkpanic abc->respond(tbPerson);
}

resource function get callerInfo29(int xyz, @http:CallerInfo {respondType: PersonTable} http:Caller abc) {
checkpanic abc->respond("res"); // error
}

resource function get callerInfo30(int xyz, @http:CallerInfo {respondType: MapJsonArr} http:Caller abc) {
map<json> jj = {sam: {hello:"world"}, jon: {no:56}};
map<json>[] arr = [jj, jj];
checkpanic abc->respond(arr);
}

resource function get callerInfo31(int xyz, @http:CallerInfo {respondType: MapJsonArr} http:Caller abc) {
http:Response res = new;
checkpanic abc->respond(res); // error
}

resource function get callerInfo32(int xyz, @http:CallerInfo {respondType: PersonTableArr} http:Caller abc) {
PersonTable tbPerson = table [
{id: 1, name: "John"},
{id: 2, name: "Bella"}
];
PersonTableArr arr = [tbPerson, tbPerson];
checkpanic abc->respond(arr);
}

resource function get callerInfo33(int xyz, @http:CallerInfo {respondType: PersonTableArr} http:Caller abc) {
checkpanic abc->respond("res"); // error
}

resource function get callerInfo34(int xyz, @http:CallerInfo {respondType: EntityArr} http:Caller abc) {
mime:Entity bodyPart = new;
bodyPart.setJson({"bodyPart":"jsonPart"});
checkpanic abc->respond([bodyPart, bodyPart]);
}

resource function get callerInfo35(int xyz, @http:CallerInfo {respondType: EntityArr} http:Caller abc) {
checkpanic abc->respond("res"); // error
}

resource function get callerInfo36(http:Request request,
@http:CallerInfo {respondType: ByteStream} http:Caller abc) {
var str = request.getByteStream();
if (str is stream<byte[], io:Error?>) {
error? err = abc->respond(str);
}
}

resource function get callerInfo37(@http:CallerInfo {respondType: ByteStream} http:Caller abc) {
checkpanic abc->respond("res"); // error
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ private static void extractInputParamTypeAndValidate(SyntaxNodeAnalysisContext c
continue;
}
String typeName = typeNameOptional.get();
if (CALLER_OBJ_NAME.equals(typeName) || REQUEST_OBJ_NAME.equals(typeName) ||
HEADER_OBJ_NAME.equals(typeName)) {

} else {
if (!CALLER_OBJ_NAME.equals(typeName) && !REQUEST_OBJ_NAME.equals(typeName) &&
!HEADER_OBJ_NAME.equals(typeName)) {
reportInvalidParameterType(ctx, member, paramType);
}
} else {
Expand Down Expand Up @@ -397,7 +395,7 @@ private static void extractCallerInfoValueAndValidate(SyntaxNodeAnalysisContext
TypeSymbol argTypeSymbol = ctx.semanticModel().type(argumentNode.expression()).get();
TypeSymbol annotValueSymbol =
(TypeSymbol) ctx.semanticModel().symbol(specificFieldNode.valueExpr().get()).get();
if (!annotValueSymbol.assignableTo(argTypeSymbol)) {
if (!argTypeSymbol.assignableTo(annotValueSymbol)) {
reportInCompatibleCallerInfoType(ctx, argumentNode, expectedType);
}
}
Expand Down

0 comments on commit f5928e5

Please sign in to comment.