Skip to content

Commit

Permalink
Add unit tests for constraint record types
Browse files Browse the repository at this point in the history
  • Loading branch information
SachinAkash01 committed Sep 26, 2023
1 parent 4c924c3 commit c3a756d
Show file tree
Hide file tree
Showing 13 changed files with 753 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,27 @@ public void testMapFiled() throws IOException {
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/record_field.yaml");
}

@Test(description = "When the record field has constraint type with records")
public void testMapRecFiled() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("constraint/record_fieldRec.bal");
//Compare generated yaml file with expected yaml content
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/record_fieldRec.yaml");
}

@Test(description = "When the record field has array type")
public void testArrayType() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("constraint/array.bal");
//Compare generated yaml file with expected yaml content
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/array.yaml");
}

@Test(description = "When the record field has array record type")
public void testArrayRecType() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("constraint/arrayRec.bal");
//Compare generated yaml file with expected yaml content
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/arrayRec.yaml");
}

@Test(description = "When the record field has integer (minValueExclusive) type")
public void testIntegerMinType() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("constraint/integerMin.bal");
Expand All @@ -59,6 +73,13 @@ public void testIntegerMaxType() throws IOException {
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/integerMax.yaml");
}

@Test(description = "When the record field has integer record type")
public void testIntegerRecType() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("constraint/integerRec.bal");
//Compare generated yaml file with expected yaml content
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/integerRec.yaml");
}

@Test(description = "When the record field has float (minValueExclusive) type")
public void testFloatMinType() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("constraint/floatMin.bal");
Expand All @@ -73,6 +94,13 @@ public void testFloatMaxType() throws IOException {
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/floatMax.yaml");
}

@Test(description = "When the record field has float record type")
public void testFloatRecType() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("constraint/floatRec.bal");
//Compare generated yaml file with expected yaml content
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/floatRec.yaml");
}

@Test(description = "When the record field has decimal (minValueExclusive) type")
public void testDecimalMinType() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("constraint/decimalMin.bal");
Expand All @@ -87,10 +115,24 @@ public void testDecimalMaxType() throws IOException {
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/decimalMax.yaml");
}

@Test(description = "When the record field has decimal record type")
public void testDecimalRecType() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("constraint/decimalRec.bal");
//Compare generated yaml file with expected yaml content
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/decimalRec.yaml");
}

@Test(description = "When the record field has string type")
public void testStringType() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("constraint/string.bal");
//Compare generated yaml file with expected yaml content
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/string.yaml");
}

@Test(description = "When the record field has string record type")
public void testStringRecType() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("constraint/stringRec.bal");
//Compare generated yaml file with expected yaml content
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "constraint/stringRec.yaml");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
//
// 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/http;
import ballerina/constraint;

@constraint:String {maxLength: 23}
public type HobbyItemsString string;

@constraint:String {minLength: 7}
public type PersonDetailsItemsString string;

@constraint:Float {maxValue: 445.4}
public type PersonFeeItemsNumber float;

@constraint:Int {maxValue: 67 }
public type PersonLimitItemsInteger int;

@constraint:Array {maxLength: 5, minLength: { value: 2, message: "Min Length Exceeded!"}}
public type Hobby HobbyItemsString[];

public type Person record {
Hobby hobby?;
@constraint:Array {maxLength: 5}
PersonDetailsItemsString[] Details?;
int id;
PersonFeeItemsNumber[] fee?;
# The maximum number of items in the response (as set in the query or by default).
PersonLimitItemsInteger[] 'limit?;
};

service /payloadV on new http:Listener(9090) {
resource function post pet(@http:Payload Person body) returns error? {
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
//
// 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/http;
import ballerina/constraint;

@constraint:Number {
minValueExclusive: {
value: 2.55,
message: "Min Value Exceeded!"
},
maxValue: 5.55
}
public type Marks decimal;

public type School record {
Marks marks;
};

service /payloadV on new http:Listener(9090) {
resource function post pet(@http:Payload School body) returns error? {
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
//
// 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/http;
import ballerina/constraint;

@constraint:Float {
maxValueExclusive: {
value: 10.5,
message: "Max Value Exceeded!"
},
minValue: 2.5
}
public type Rating float;

public type Hotel record {
Rating rate;
};

service /payloadV on new http:Listener(9090) {
resource function post pet(@http:Payload Hotel body) returns error? {
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
//
// 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/http;
import ballerina/constraint;

@constraint:Int{
minValueExclusive: 0,
maxValue: {
value: 50,
message: "Max Value Exceeded!"
}
}
public type Position int;

public type Child record {
Position position;
};

service /payloadV on new http:Listener(9090) {
resource function post pet(@http:Payload Child body) returns error? {
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
//
// 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/http;
import ballerina/constraint;

@constraint:String {minLength: 5}
public type Address string;

public type Person record {
@constraint:String {
maxLength: {
value: 14,
message: "Max Length Exceeded!"
}
}
string name?;
@constraint:Array {
maxLength: 5,
minLength: {
value: 2,
message: "Min Length Exceeded!"
}
}
string[] hobby?;
@constraint:Int {
maxValueExclusive: {
value: 5,
message: "Max Value Exceeded!"
},
minValue: 0
}
int id;
Address address?;
@constraint:Float {
maxValueExclusive: {
value: 100000,
message: "Min Value Exceeded!"
},
minValue: 1000
}
float salary?;
@constraint:Number {
minValueExclusive: 500000,
maxValue: {
value: 1000000,
message: "Max Value Exceeded!"
}
}
decimal net?;
};

service /payloadV on new http:Listener(9090) {
resource function post pet(@http:Payload Person body) returns error? {
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
//
// 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/http;
import ballerina/constraint;

@constraint:String {
length: {
value: 10,
message: "Length Exceeded!"
},
pattern: re `^[a-zA-Z0-9_]+$`
}
public type St_ID string;

public type StudentRecord record {
St_ID id;
@constraint:String { pattern: re `^[a-zA-Z]+$`}
string name?;
};

service /payloadV on new http:Listener(9090) {
resource function post pet(@http:Payload StudentRecord body) returns error? {
return;
}
}
Loading

0 comments on commit c3a756d

Please sign in to comment.