Skip to content

Commit 046feed

Browse files
committed
Add and update test cases
1 parent ac093a9 commit 046feed

28 files changed

+600
-234
lines changed

src/test/java/com/relogiclabs/jschema/test/external/ExternalFunctions.java renamed to src/test/java/com/relogiclabs/jschema/test/extension/ConstraintExtension1.java

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package com.relogiclabs.jschema.test.external;
1+
package com.relogiclabs.jschema.test.extension;
22

33
import com.relogiclabs.jschema.exception.FunctionValidationException;
4-
import com.relogiclabs.jschema.function.FunctionProvider;
4+
import com.relogiclabs.jschema.extension.ConstraintExtension;
5+
import com.relogiclabs.jschema.extension.ConstraintFunction;
56
import com.relogiclabs.jschema.function.FutureFunction;
6-
import com.relogiclabs.jschema.internal.function.IPAddress;
77
import com.relogiclabs.jschema.message.ActualDetail;
88
import com.relogiclabs.jschema.message.ErrorDetail;
99
import com.relogiclabs.jschema.message.ExpectedDetail;
@@ -13,93 +13,67 @@
1313
import com.relogiclabs.jschema.node.JReceiver;
1414
import com.relogiclabs.jschema.node.JString;
1515

16-
import java.util.Arrays;
17-
1816
import static com.relogiclabs.jschema.internal.util.StringHelper.join;
17+
import static java.util.Arrays.stream;
1918

2019
// Functions for positive (valid) test cases
21-
public class ExternalFunctions extends FunctionProvider {
20+
public class ConstraintExtension1 extends ConstraintExtension {
2221
public static final String EX_EVENFUNC01 = "EX_EVENFUNC01";
23-
public static final String EX_ERRACCESS01 = "EX_ERRACCESS01";
24-
public static final String EX_ERRORIP01 = "EX_ERRORIP01";
25-
public static final String EX_ERRORIP02 = "EX_ERRORIP02";
2622
public static final String EX_CONDFUNC01 = "EX_CONDFUNC01";
2723
public static final String EX_CONDFUNC02 = "EX_CONDFUNC02";
2824
public static final String EX_SUMEQUAL01 = "EX_SUMEQUAL01";
2925
public static final String EX_MINMAX01 = "EX_MINMAX01";
3026

31-
private static final String INTERNAL_IP_PREFIX = "0.";
32-
27+
@ConstraintFunction
3328
public boolean even(JNumber target) {
3429
// Precision loss is not considered here
3530
if(target.toDouble() % 2 == 0) return true;
3631
return fail(new FunctionValidationException(
3732
new ErrorDetail(EX_EVENFUNC01, "Target number is not even"),
38-
new ExpectedDetail(caller, "an even number"),
33+
new ExpectedDetail(getInvoker(), "an even number"),
3934
new ActualDetail(target, "target " + target + " is odd")));
4035
}
4136

37+
@ConstraintFunction
4238
public boolean canTest(JNumber target, JString str1, JBoolean bool1, JNumber... args) {
4339
System.out.println("Target: " + target);
4440
System.out.println("String Parameter: " + str1);
4541
System.out.println("Boolean Parameter: " + bool1);
46-
System.out.println("Params Numbers: " + join(Arrays.asList(args), ",", "[", "]"));
47-
return true;
48-
}
49-
50-
public boolean checkDataAccess(JInteger target, JReceiver userRole) {
51-
var role = userRole.<JString>getValueNode().getValue();
52-
if(role.equals("user") && target.getValue() > 5)
53-
return fail(new FunctionValidationException(
54-
new ErrorDetail(EX_ERRACCESS01, "Data access incompatible with 'user' role"),
55-
new ExpectedDetail(caller, "an access at most 5 for 'user' role"),
56-
new ActualDetail(target, "found access " + target + " that is greater than 5")));
57-
return true;
58-
}
59-
60-
public boolean checkIPAddress(JString target) {
61-
if(!IPAddress.isValidIPv4(target.getValue()))
62-
return fail(new FunctionValidationException(
63-
new ErrorDetail(EX_ERRORIP01, "Target IP address invalid"),
64-
new ExpectedDetail(caller, "a valid IP address"),
65-
new ActualDetail(target, "found invalid target " + target)));
66-
// Might have any IP restrictions depending on requirements
67-
if(target.getValue().startsWith(INTERNAL_IP_PREFIX))
68-
return fail(new FunctionValidationException(
69-
new ErrorDetail(EX_ERRORIP02, "Target IP address must not be in 0.0.0.0/8"),
70-
new ExpectedDetail(caller, "a valid IP address not in 0.0.0.0/8"),
71-
new ActualDetail(target, "found invalid target " + target)));
42+
System.out.println("Params Numbers: " + join(stream(args), ",", "[", "]"));
7243
return true;
7344
}
7445

46+
@ConstraintFunction
7547
public boolean condition(JInteger target, JReceiver receiver) {
76-
var threshold = receiver.<JInteger>getValueNode().getValue();
48+
var threshold = receiver.getValueNode(JInteger.class).getValue();
7749
System.out.println("Received integer: " + threshold);
7850
if(threshold < target.getValue()) return true;
7951
return fail(new FunctionValidationException(
8052
new ErrorDetail(EX_CONDFUNC01, "Target number does not satisfy the condition"),
81-
new ExpectedDetail(caller, "a number > " + threshold + " of " + receiver),
53+
new ExpectedDetail(getInvoker(), "a number > " + threshold + " of " + receiver),
8254
new ActualDetail(target, "found target " + target + " <= " + threshold)));
8355
}
8456

57+
@ConstraintFunction
8558
public boolean conditionMany(JInteger target, JReceiver receiver) {
86-
var list = receiver.<JInteger>getValueNodes();
59+
var list = receiver.getValueNodes(JInteger.class);
8760
var values = join(list, ",", "[", "]");
8861
System.out.println("Target: " + target);
8962
System.out.println("Received integers: " + values);
9063
boolean result = list.stream().allMatch(i -> i.getValue() < target.getValue());
9164
if(result) return true;
9265
return fail(new FunctionValidationException(
9366
new ErrorDetail(EX_CONDFUNC02, "Target number does not satisfy the condition"),
94-
new ExpectedDetail(caller, "a number > any of " + values + " in " + receiver),
67+
new ExpectedDetail(getInvoker(), "a number > any of " + values + " in " + receiver),
9568
new ActualDetail(target, "found target " + target + " <= some of " + values)));
9669
}
9770

71+
@ConstraintFunction
9872
public FutureFunction sumEqual(JInteger target, JReceiver receiver) {
99-
// Capture the current value of the caller
100-
var current = caller;
73+
// Capture the current value of the invoker
74+
var current = getInvoker();
10175
return () -> {
102-
var values = receiver.<JInteger>getValueNodes();
76+
var values = receiver.getValueNodes(JInteger.class);
10377
System.out.println("Target: " + target);
10478
long sum = values.stream().mapToLong(JInteger::getValue).sum();
10579
var expression = join(values, " + ") + " = " + sum;
@@ -112,12 +86,13 @@ public FutureFunction sumEqual(JInteger target, JReceiver receiver) {
11286
};
11387
}
11488

89+
@ConstraintFunction
11590
public FutureFunction minmax(JInteger target, JReceiver min, JReceiver max) {
116-
// Capture the current value of the caller
117-
var current = caller;
91+
// Capture the current value of the invoker
92+
var current = getInvoker();
11893
return () -> {
119-
var intMin = min.<JInteger>getValueNode().getValue();
120-
var intMax = max.<JInteger>getValueNode().getValue();
94+
var intMin = min.getValueNode(JInteger.class).getValue();
95+
var intMax = max.getValueNode(JInteger.class).getValue();
12196
System.out.println("Target: " + target);
12297
System.out.println("Received min: " + intMin + ", max: " + intMax);
12398
if(target.getValue() >= intMin && target.getValue() <= intMax) return true;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.relogiclabs.jschema.test.extension;
2+
3+
import com.relogiclabs.jschema.extension.ConstraintExtension;
4+
import com.relogiclabs.jschema.extension.ConstraintFunction;
5+
import com.relogiclabs.jschema.node.JNumber;
6+
7+
// Functions for negative (error) test cases
8+
public class ConstraintExtension2 extends ConstraintExtension {
9+
@ConstraintFunction
10+
public void odd(JNumber target) {
11+
// Return type of constraint function must not be void
12+
// Precision loss is not considered here
13+
if(target.toDouble() % 2 != 0) return;
14+
throw new RuntimeException("Not an odd number");
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.relogiclabs.jschema.test.extension;
2+
3+
import com.relogiclabs.jschema.extension.ConstraintFunction;
4+
import com.relogiclabs.jschema.node.JNumber;
5+
6+
// Functions for negative (error) test cases
7+
public class ConstraintExtension3 {
8+
// To use @ConstraintFunction, interface ConstraintFunctions must be implemented
9+
// ConstraintExtension and GeneralExtension are available built-in implementations
10+
@ConstraintFunction
11+
public boolean odd(JNumber target) {
12+
// Precision loss is not considered here
13+
if(target.toDouble() % 2 != 0) return true;
14+
throw new RuntimeException("Not an odd number");
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.relogiclabs.jschema.test.extension;
2+
3+
import com.relogiclabs.jschema.extension.ConstraintExtension;
4+
5+
// Functions for negative (error) test cases
6+
public class ConstraintExtension4 extends ConstraintExtension {
7+
public ConstraintExtension4() {
8+
throw new RuntimeException("Error occurred");
9+
}
10+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.relogiclabs.jschema.test.extension;
2+
3+
import com.relogiclabs.jschema.exception.FunctionValidationException;
4+
import com.relogiclabs.jschema.extension.ConstraintFunction;
5+
import com.relogiclabs.jschema.extension.GeneralExtension;
6+
import com.relogiclabs.jschema.function.SchemaFunctions;
7+
import com.relogiclabs.jschema.message.ActualDetail;
8+
import com.relogiclabs.jschema.message.ErrorDetail;
9+
import com.relogiclabs.jschema.message.ExpectedDetail;
10+
import com.relogiclabs.jschema.node.JInteger;
11+
import com.relogiclabs.jschema.node.JReceiver;
12+
import com.relogiclabs.jschema.node.JString;
13+
14+
// Functions for positive (valid) test cases
15+
public class GeneralExtension1 extends GeneralExtension {
16+
public static final String EX_ERRACCESS01 = "EX_ERRACCESS01";
17+
public static final String EX_ERRORIP01 = "EX_ERRORIP01";
18+
19+
private static final String INTERNAL_IP_PREFIX = "0.";
20+
21+
@ConstraintFunction
22+
public boolean checkDataAccess(JInteger target, JReceiver role) {
23+
var roleValue = role.getValueNode(JString.class).getValue();
24+
if(roleValue.equals("user") && target.getValue() > 5)
25+
return fail(new FunctionValidationException(
26+
new ErrorDetail(EX_ERRACCESS01, "Data access incompatible with 'user' role"),
27+
new ExpectedDetail(getInvoker(), "an access at most 5 for 'user' role"),
28+
new ActualDetail(target, "found access " + target + " that is greater than 5")));
29+
return true;
30+
}
31+
32+
@ConstraintFunction
33+
public boolean checkIPAddress(JString target) {
34+
var functions = SchemaFunctions.getInstance(this);
35+
if(!functions.ipv4(target)) return false;
36+
// Below is an example of different IP restrictions
37+
if(target.getValue().startsWith(INTERNAL_IP_PREFIX))
38+
return fail(new FunctionValidationException(
39+
new ErrorDetail(EX_ERRORIP01, "Target IP address must not be in 0.0.0.0/8"),
40+
new ExpectedDetail(getInvoker(), "a valid IP address not in 0.0.0.0/8"),
41+
new ActualDetail(target, "found invalid target " + target)));
42+
return true;
43+
}
44+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.relogiclabs.jschema.test.extension;
2+
3+
import com.relogiclabs.jschema.extension.ConstraintFunction;
4+
import com.relogiclabs.jschema.extension.GeneralExtension;
5+
import com.relogiclabs.jschema.extension.ScriptFunction;
6+
import com.relogiclabs.jschema.extension.ScriptMethod;
7+
import com.relogiclabs.jschema.internal.script.GBoolean;
8+
import com.relogiclabs.jschema.internal.script.GString;
9+
import com.relogiclabs.jschema.library.CommonMethods;
10+
import com.relogiclabs.jschema.type.EArray;
11+
import com.relogiclabs.jschema.type.EBoolean;
12+
import com.relogiclabs.jschema.type.EInteger;
13+
import com.relogiclabs.jschema.type.ENumber;
14+
import com.relogiclabs.jschema.type.EObject;
15+
import com.relogiclabs.jschema.type.EValue;
16+
17+
import static com.relogiclabs.jschema.type.EType.ANY;
18+
import static com.relogiclabs.jschema.type.EType.COMPOSITE;
19+
import static com.relogiclabs.jschema.type.EType.NUMBER;
20+
import static java.util.Objects.requireNonNull;
21+
22+
// GeneralExtension support all types of extensions
23+
public class GeneralExtension2 extends GeneralExtension {
24+
@ConstraintFunction
25+
public static boolean checkPrime(EInteger number) {
26+
var num = number.getValue();
27+
var sqrt = Math.sqrt(num);
28+
for (int i = 2; i <= sqrt; i++) {
29+
if(num % i == 0) return false;
30+
}
31+
return true;
32+
}
33+
34+
@ScriptFunction
35+
public static ENumber maxNum(ENumber value, ENumber... values) {
36+
var max = value;
37+
for(var v : values) if(max.toDouble() < v.toDouble()) max = v;
38+
return max;
39+
}
40+
41+
@ScriptMethod(ANY)
42+
public EValue getTypeName() {
43+
var common = requireNonNull(CommonMethods.getInstance(this));
44+
return GString.from(common.type().getValue().substring(1));
45+
}
46+
47+
@ScriptMethod(NUMBER)
48+
public EBoolean isEvenCheck() {
49+
var tolerance = getRuntime().getPragmas().getFloatingPointTolerance();
50+
return GBoolean.from(Math.abs(getSelf(ENumber.class).toDouble() % 2) < tolerance);
51+
}
52+
53+
@ScriptMethod(COMPOSITE)
54+
public EBoolean isEmptyCheck() {
55+
if(getSelf() instanceof EArray a) return GBoolean.from(a.size() == 0);
56+
if(getSelf() instanceof EObject o) return GBoolean.from(o.size() == 0);
57+
throw new IllegalStateException("Invalid instance type");
58+
}
59+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.relogiclabs.jschema.test.extension;
2+
3+
import com.relogiclabs.jschema.extension.ConstraintFunction;
4+
import com.relogiclabs.jschema.extension.GeneralExtension;
5+
6+
// Functions for negative (error) test cases
7+
public class GeneralExtension3 extends GeneralExtension {
8+
@ConstraintFunction
9+
public boolean odd() {
10+
// Not register due to invalid signature
11+
// Missing mandatory target parameter
12+
return false;
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.relogiclabs.jschema.test.extension;
2+
3+
import com.relogiclabs.jschema.extension.ConstraintFunction;
4+
import com.relogiclabs.jschema.extension.GeneralExtension;
5+
import com.relogiclabs.jschema.node.JNumber;
6+
7+
// Functions for negative (error) test cases
8+
public class GeneralExtension4 extends GeneralExtension {
9+
@ConstraintFunction
10+
public boolean odd(JNumber target) {
11+
// Any RuntimeException can be thrown without details
12+
return fail(new RuntimeException("something went wrong"));
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.relogiclabs.jschema.test.extension;
2+
3+
import com.relogiclabs.jschema.extension.ConstraintFunction;
4+
import com.relogiclabs.jschema.extension.GeneralExtension;
5+
import com.relogiclabs.jschema.node.JNumber;
6+
7+
import java.io.FileNotFoundException;
8+
9+
// Functions for negative (error) test cases
10+
public class GeneralExtension5 extends GeneralExtension {
11+
@ConstraintFunction
12+
public boolean odd(JNumber target) throws FileNotFoundException {
13+
// Avoid checked exception where possible
14+
throw new FileNotFoundException("file not found");
15+
}
16+
}

src/test/java/com/relogiclabs/jschema/test/external/ExternalFunctions1.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)