1- package com .relogiclabs .jschema .test .external ;
1+ package com .relogiclabs .jschema .test .extension ;
22
33import 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 ;
56import com .relogiclabs .jschema .function .FutureFunction ;
6- import com .relogiclabs .jschema .internal .function .IPAddress ;
77import com .relogiclabs .jschema .message .ActualDetail ;
88import com .relogiclabs .jschema .message .ErrorDetail ;
99import com .relogiclabs .jschema .message .ExpectedDetail ;
1313import com .relogiclabs .jschema .node .JReceiver ;
1414import com .relogiclabs .jschema .node .JString ;
1515
16- import java .util .Arrays ;
17-
1816import 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 ;
0 commit comments