Skip to content

Commit ff78fb7

Browse files
committed
Add test cases for external functions
1 parent 4b4f579 commit ff78fb7

File tree

8 files changed

+302
-0
lines changed

8 files changed

+302
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.relogiclabs.json.schema.negative;
2+
3+
import com.relogiclabs.json.schema.JsonAssert;
4+
import com.relogiclabs.json.schema.exception.ClassInstantiationException;
5+
import com.relogiclabs.json.schema.exception.DuplicateIncludeException;
6+
import com.relogiclabs.json.schema.exception.FunctionNotFoundException;
7+
import com.relogiclabs.json.schema.exception.InvalidFunctionException;
8+
import com.relogiclabs.json.schema.exception.InvalidIncludeException;
9+
import com.relogiclabs.json.schema.exception.NotFoundClassException;
10+
import org.junit.jupiter.api.Test;
11+
12+
import static com.relogiclabs.json.schema.message.ErrorCode.CLAS01;
13+
import static com.relogiclabs.json.schema.message.ErrorCode.CLAS02;
14+
import static com.relogiclabs.json.schema.message.ErrorCode.CLAS03;
15+
import static com.relogiclabs.json.schema.message.ErrorCode.CLAS04;
16+
import static com.relogiclabs.json.schema.message.ErrorCode.FUNC01;
17+
import static com.relogiclabs.json.schema.message.ErrorCode.FUNC02;
18+
import static com.relogiclabs.json.schema.message.ErrorCode.FUNC05;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
22+
public class FunctionTests {
23+
24+
@Test
25+
public void When_ExternalIncludeNotInheritBaseClass_ExceptionThrown() {
26+
var schema =
27+
"""
28+
%include: com.relogiclabs.json.schema.negative.function.ExternalFunctions1
29+
%schema: @odd #integer
30+
""";
31+
var json = "10";
32+
33+
//JsonSchema.IsValid(schema, json);
34+
var exception = assertThrows(InvalidIncludeException.class,
35+
() -> JsonAssert.isValid(schema, json));
36+
assertEquals(CLAS03, exception.getCode());
37+
exception.printStackTrace();
38+
}
39+
40+
@Test
41+
public void When_ExternalIncludeNotExisting_ExceptionThrown() {
42+
var schema =
43+
"""
44+
%include: com.relogiclabs.json.schema.negative.function.NotExisting
45+
%schema: @odd #integer
46+
""";
47+
var json = "10";
48+
49+
//JsonSchema.IsValid(schema, json);
50+
var exception = assertThrows(NotFoundClassException.class,
51+
() -> JsonAssert.isValid(schema, json));
52+
assertEquals(CLAS02, exception.getCode());
53+
exception.printStackTrace();
54+
}
55+
56+
@Test
57+
public void When_ExternalIncludeDuplicationOccurred_ExceptionThrown() {
58+
var schema =
59+
"""
60+
%include: com.relogiclabs.json.schema.positive.ExternalFunctions
61+
%include: com.relogiclabs.json.schema.positive.ExternalFunctions
62+
%schema: @odd #integer
63+
""";
64+
var json = "10";
65+
66+
//JsonSchema.IsValid(schema, json);
67+
var exception = assertThrows(DuplicateIncludeException.class,
68+
() -> JsonAssert.isValid(schema, json));
69+
assertEquals(CLAS01, exception.getCode());
70+
exception.printStackTrace();
71+
}
72+
73+
@Test
74+
public void When_ExternalIncludeInstantiationNotCompleted_ExceptionThrown() {
75+
var schema =
76+
"""
77+
%include: com.relogiclabs.json.schema.negative.function.ExternalFunctions5
78+
%schema: @odd #integer
79+
""";
80+
var json = "10";
81+
82+
//JsonSchema.IsValid(schema, json);
83+
var exception = assertThrows(ClassInstantiationException.class,
84+
() -> JsonAssert.isValid(schema, json));
85+
assertEquals(CLAS04, exception.getCode());
86+
exception.printStackTrace();
87+
}
88+
89+
@Test
90+
public void When_ExternalFunctionWrongReturnType_ExceptionThrown() {
91+
var schema =
92+
"""
93+
%include: com.relogiclabs.json.schema.negative.function.ExternalFunctions2
94+
%schema: @odd #integer
95+
""";
96+
var json = "10";
97+
98+
//JsonSchema.IsValid(schema, json);
99+
var exception = assertThrows(InvalidFunctionException.class,
100+
() -> JsonAssert.isValid(schema, json));
101+
assertEquals(FUNC01, exception.getCode());
102+
exception.printStackTrace();
103+
}
104+
105+
@Test
106+
public void When_ExternalFunctionWrongParameterNumber_ExceptionThrown() {
107+
var schema =
108+
"""
109+
%include: com.relogiclabs.json.schema.negative.function.ExternalFunctions3
110+
%schema: @odd #integer
111+
""";
112+
var json = "10";
113+
114+
//JsonSchema.IsValid(schema, json);
115+
var exception = assertThrows(InvalidFunctionException.class,
116+
() -> JsonAssert.isValid(schema, json));
117+
assertEquals(FUNC02, exception.getCode());
118+
exception.printStackTrace();
119+
}
120+
121+
@Test
122+
public void When_ExternalFunctionNotExists_ExceptionThrown() {
123+
var schema =
124+
"""
125+
%include: com.relogiclabs.json.schema.negative.function.ExternalFunctions4
126+
%schema: @odd #integer
127+
""";
128+
var json = "10";
129+
130+
//JsonSchema.IsValid(schema, json);
131+
var exception = assertThrows(FunctionNotFoundException.class,
132+
() -> JsonAssert.isValid(schema, json));
133+
assertEquals(FUNC05, exception.getCode());
134+
exception.printStackTrace();
135+
}
136+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.relogiclabs.json.schema.negative.function;
2+
3+
import com.relogiclabs.json.schema.types.JNumber;
4+
5+
public class ExternalFunctions1 {
6+
public boolean odd(JNumber target) {
7+
boolean result = (target.toDouble() % 2 != 0);
8+
if(!result) throw new RuntimeException("Not an odd number");
9+
return true;
10+
}
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.relogiclabs.json.schema.negative.function;
2+
3+
import com.relogiclabs.json.schema.function.FunctionBase;
4+
import com.relogiclabs.json.schema.tree.RuntimeContext;
5+
import com.relogiclabs.json.schema.types.JNumber;
6+
7+
public class ExternalFunctions2 extends FunctionBase {
8+
public ExternalFunctions2(RuntimeContext runtime) {
9+
super(runtime);
10+
}
11+
12+
public void odd(JNumber source) {
13+
boolean result = source.toDouble() % 2 != 0;
14+
if(!result) throw new RuntimeException("Not an odd number");
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.relogiclabs.json.schema.negative.function;
2+
3+
import com.relogiclabs.json.schema.function.FunctionBase;
4+
import com.relogiclabs.json.schema.tree.RuntimeContext;
5+
6+
public class ExternalFunctions3 extends FunctionBase {
7+
public ExternalFunctions3(RuntimeContext runtime) {
8+
super(runtime);
9+
}
10+
11+
public boolean odd() {
12+
throw new UnsupportedOperationException();
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.relogiclabs.json.schema.negative.function;
2+
3+
import com.relogiclabs.json.schema.function.FunctionBase;
4+
import com.relogiclabs.json.schema.tree.RuntimeContext;
5+
6+
public class ExternalFunctions4 extends FunctionBase {
7+
public ExternalFunctions4(RuntimeContext runtime) {
8+
super(runtime);
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.relogiclabs.json.schema.negative.function;
2+
3+
import com.relogiclabs.json.schema.function.FunctionBase;
4+
5+
public class ExternalFunctions5 extends FunctionBase {
6+
public ExternalFunctions5() {
7+
super(null);
8+
}
9+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.relogiclabs.json.schema.positive;
2+
3+
import com.relogiclabs.json.schema.exception.JsonSchemaException;
4+
import com.relogiclabs.json.schema.function.FunctionBase;
5+
import com.relogiclabs.json.schema.message.ActualDetail;
6+
import com.relogiclabs.json.schema.message.ErrorDetail;
7+
import com.relogiclabs.json.schema.message.ExpectedDetail;
8+
import com.relogiclabs.json.schema.tree.RuntimeContext;
9+
import com.relogiclabs.json.schema.types.JBoolean;
10+
import com.relogiclabs.json.schema.types.JNumber;
11+
import com.relogiclabs.json.schema.types.JString;
12+
13+
public class ExternalFunctions extends FunctionBase {
14+
public static final String EVENFUNC01 = "EVENFUNC01";
15+
16+
public ExternalFunctions(RuntimeContext runtime) {
17+
super(runtime);
18+
}
19+
20+
public boolean even(JNumber target) {
21+
boolean result = (target.toDouble() % 2 == 0);
22+
if(!result) failWith(new JsonSchemaException(
23+
new ErrorDetail(EVENFUNC01, "Number is not even"),
24+
new ExpectedDetail(target, "even number"),
25+
new ActualDetail(target, "number ", target, " is odd")));
26+
return true;
27+
}
28+
29+
public boolean canTest(JNumber target, JString str1, JBoolean bool1, JNumber... args) {
30+
return true;
31+
}
32+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.relogiclabs.json.schema.positive;
2+
3+
import com.relogiclabs.json.schema.JsonAssert;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class FunctionTests {
7+
@Test
8+
public void When_ExternalFunctionExecute_ValidTrue() {
9+
var schema =
10+
"""
11+
%include: com.relogiclabs.json.schema.positive.ExternalFunctions
12+
%schema: @even #integer
13+
""";
14+
var json = "10";
15+
JsonAssert.isValid(schema, json);
16+
}
17+
18+
@Test
19+
public void When_ExternalFunctionExecute2_ValidTrue() {
20+
var schema =
21+
"""
22+
%include: com.relogiclabs.json.schema.positive.ExternalFunctions
23+
%schema: @canTest("test", true, 1, 2, 3) #integer
24+
""";
25+
var json = "10";
26+
JsonAssert.isValid(schema, json);
27+
}
28+
29+
@Test
30+
public void When_ExternalFunctionWithoutDataType_ValidTrue() {
31+
var schema =
32+
"""
33+
%include: com.relogiclabs.json.schema.positive.ExternalFunctions
34+
%schema: @even
35+
""";
36+
var json = "10";
37+
JsonAssert.isValid(schema, json);
38+
}
39+
40+
@Test
41+
public void When_ExternalFunctionInObject_ValidTrue() {
42+
var schema =
43+
"""
44+
%include: com.relogiclabs.json.schema.positive.ExternalFunctions
45+
%schema:
46+
{
47+
"key1": @even #integer,
48+
"key2": @even #integer
49+
}
50+
""";
51+
var json =
52+
"""
53+
{
54+
"key1": 10,
55+
"key2": 12
56+
}
57+
""";
58+
JsonAssert.isValid(schema, json);
59+
}
60+
61+
@Test
62+
public void When_ExternalFunctionInArray_ValidTrue() {
63+
var schema =
64+
"""
65+
%include: com.relogiclabs.json.schema.positive.ExternalFunctions
66+
%schema: [
67+
@even #integer,
68+
@even #integer
69+
]
70+
""";
71+
var json = "[100, 200]";
72+
JsonAssert.isValid(schema, json);
73+
}
74+
}

0 commit comments

Comments
 (0)