-
Notifications
You must be signed in to change notification settings - Fork 156
/
ScriptingTest.java
6484 lines (4700 loc) · 365 KB
/
ScriptingTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010-2024 Structr GmbH
*
* This file is part of Structr <http://structr.org>.
*
* Structr is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Structr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Structr. If not, see <http://www.gnu.org/licenses/>.
*/
package org.structr.test.core.script;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.structr.api.config.Settings;
import org.structr.api.graph.Cardinality;
import org.structr.api.schema.*;
import org.structr.api.util.Iterables;
import org.structr.common.AccessControllable;
import org.structr.common.AccessMode;
import org.structr.common.Permission;
import org.structr.common.SecurityContext;
import org.structr.common.error.FrameworkException;
import org.structr.common.error.UnlicensedScriptException;
import org.structr.common.geo.GeoCodingResult;
import org.structr.common.geo.GeoHelper;
import org.structr.core.GraphObject;
import org.structr.core.app.App;
import org.structr.core.app.StructrApp;
import org.structr.core.entity.*;
import org.structr.core.function.DateFormatFunction;
import org.structr.core.function.FindFunction;
import org.structr.core.function.NumberFormatFunction;
import org.structr.core.graph.NodeAttribute;
import org.structr.core.graph.NodeInterface;
import org.structr.core.graph.Tx;
import org.structr.core.property.EnumProperty;
import org.structr.core.property.PropertyKey;
import org.structr.core.property.PropertyMap;
import org.structr.core.script.ScriptTestHelper;
import org.structr.core.script.Scripting;
import org.structr.schema.ConfigurationProvider;
import org.structr.schema.action.ActionContext;
import org.structr.schema.action.Actions;
import org.structr.schema.action.EvaluationHints;
import org.structr.schema.export.StructrSchema;
import org.structr.test.common.StructrTest;
import org.structr.test.core.entity.*;
import org.structr.test.core.entity.TestOne.Status;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.net.URISyntaxException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.*;
import org.structr.core.api.AbstractMethod;
import org.structr.core.api.Arguments;
import org.structr.core.api.Methods;
import static org.testng.AssertJUnit.*;
/**
*
*
*/
public class ScriptingTest extends StructrTest {
private static final Logger logger = LoggerFactory.getLogger(ScriptingTest.class.getName());
@Test
public void testSetPropertyWithDynamicNodes() {
/**
* This test creates two connected SchemaNodes and tests the script-based
* association of one instance with several others in the onCreate method.
*/
final long currentTimeMillis = System.currentTimeMillis();
Class sourceType = null;
Class targetType = null;
PropertyKey targetsProperty = null;
EnumProperty testEnumProperty = null;
PropertyKey testBooleanProperty = null;
PropertyKey testIntegerProperty = null;
PropertyKey testStringProperty = null;
PropertyKey testDoubleProperty = null;
PropertyKey testDateProperty = null;
Class testEnumType = null;
// setup phase: create schema nodes
try (final Tx tx = app.tx()) {
// create two nodes and associate them with each other
final SchemaNode sourceNode = createTestNode(SchemaNode.class, "TestSource");
final SchemaNode targetNode = createTestNode(SchemaNode.class, "TestTarget");
final List<SchemaProperty> properties = new LinkedList<>();
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testBoolean"), new NodeAttribute(SchemaProperty.propertyType, "Boolean")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testInteger"), new NodeAttribute(SchemaProperty.propertyType, "Integer")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testString"), new NodeAttribute(SchemaProperty.propertyType, "String")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testDouble"), new NodeAttribute(SchemaProperty.propertyType, "Double")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testEnum"), new NodeAttribute(SchemaProperty.propertyType, "Enum"), new NodeAttribute(SchemaProperty.format, "OPEN, CLOSED, TEST")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testDate"), new NodeAttribute(SchemaProperty.propertyType, "Date")));
sourceNode.setProperty(SchemaNode.schemaProperties, properties);
final List<SchemaMethod> methods = new LinkedList<>();
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "onCreate"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testtargets = Structr.find('TestTarget'); }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest01"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'OPEN'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest02"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'CLOSED'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest03"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'TEST'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest04"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'INVALID'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest05"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testBoolean = true; e.testInteger = 123; e.testString = 'testing..'; e.testDouble = 1.2345; e.testDate = new Date(" + currentTimeMillis + "); }")));
sourceNode.setProperty(SchemaNode.schemaMethods, methods);
final PropertyMap propertyMap = new PropertyMap();
propertyMap.put(SchemaRelationshipNode.sourceId, sourceNode.getUuid());
propertyMap.put(SchemaRelationshipNode.targetId, targetNode.getUuid());
propertyMap.put(SchemaRelationshipNode.sourceJsonName, "testsource");
propertyMap.put(SchemaRelationshipNode.targetJsonName, "testtargets");
propertyMap.put(SchemaRelationshipNode.sourceMultiplicity, "*");
propertyMap.put(SchemaRelationshipNode.targetMultiplicity, "*");
propertyMap.put(SchemaRelationshipNode.relationshipType, "HAS");
app.create(SchemaRelationshipNode.class, propertyMap);
tx.success();
} catch(FrameworkException t) {
t.printStackTrace();
fail("Unexpected exception.");
}
try (final Tx tx = app.tx()) {
final ConfigurationProvider config = StructrApp.getConfiguration();
sourceType = config.getNodeEntityClass("TestSource");
targetType = config.getNodeEntityClass("TestTarget");
targetsProperty = StructrApp.key(sourceType, "testtargets");
// we need to cast to EnumProperty in order to obtain the dynamic enum type
testEnumProperty = (EnumProperty)StructrApp.key(sourceType, "testEnum");
testEnumType = testEnumProperty.getEnumType();
testBooleanProperty = StructrApp.key(sourceType, "testBoolean");
testIntegerProperty = StructrApp.key(sourceType, "testInteger");
testStringProperty = StructrApp.key(sourceType, "testString");
testDoubleProperty = StructrApp.key(sourceType, "testDouble");
testDateProperty = StructrApp.key(sourceType, "testDate");
assertNotNull(sourceType);
assertNotNull(targetType);
assertNotNull(targetsProperty);
// create 5 target nodes
createTestNodes(targetType, 5);
// create source node
createTestNodes(sourceType, 5);
tx.success();
} catch(FrameworkException t) {
t.printStackTrace();
fail("Unexpected exception.");
}
// check phase: source node should have all five target nodes associated with HAS
try (final Tx tx = app.tx()) {
// check all source nodes
for (final Object obj : app.nodeQuery(sourceType).getAsList()) {
assertNotNull("Invalid nodeQuery result", obj);
final GraphObject sourceNode = (GraphObject)obj;
// test contents of "targets" property
final Object targetNodesObject = sourceNode.getProperty(targetsProperty);
assertTrue("Invalid getProperty result for scripted association", targetNodesObject instanceof Iterable);
final Iterable iterable = (Iterable)targetNodesObject;
assertEquals("Invalid getProperty result for scripted association", 5, Iterables.count(iterable));
}
final AbstractNode sourceNode = (AbstractNode)app.nodeQuery(sourceType).getFirst();
final EvaluationHints hints = new EvaluationHints();
// set testEnum property to OPEN via doTest01 function call, check result
invokeMethod(securityContext, sourceNode, "doTest01", Collections.EMPTY_MAP, true, hints);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[0], sourceNode.getProperty(testEnumProperty));
// set testEnum property to CLOSED via doTest02 function call, check result
invokeMethod(securityContext, sourceNode, "doTest02", Collections.EMPTY_MAP, true, hints);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[1], sourceNode.getProperty(testEnumProperty));
// set testEnum property to TEST via doTest03 function call, check result
invokeMethod(securityContext, sourceNode, "doTest03", Collections.EMPTY_MAP, true, hints);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[2], sourceNode.getProperty(testEnumProperty));
// set testEnum property to INVALID via doTest03 function call, expect previous value & error
try {
invokeMethod(securityContext, sourceNode, "doTest04", Collections.EMPTY_MAP, true, hints);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[2], sourceNode.getProperty(testEnumProperty));
fail("Setting EnumProperty to invalid value should result in an Exception!");
} catch (FrameworkException fx) {}
// test other property types
invokeMethod(securityContext, sourceNode, "doTest05", Collections.EMPTY_MAP, true, hints);
assertEquals("Invalid setProperty result for BooleanProperty", true, sourceNode.getProperty(testBooleanProperty));
assertEquals("Invalid setProperty result for IntegerProperty", 123, sourceNode.getProperty(testIntegerProperty));
assertEquals("Invalid setProperty result for StringProperty", "testing..", sourceNode.getProperty(testStringProperty));
assertEquals("Invalid setProperty result for DoubleProperty", 1.2345, sourceNode.getProperty(testDoubleProperty));
assertEquals("Invalid setProperty result for DateProperty", new Date(currentTimeMillis), sourceNode.getProperty(testDateProperty));
tx.success();
} catch(FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
}
@Test
public void testGrantViaScripting() {
// setup phase: create schema nodes
try (final Tx tx = app.tx()) {
// create two nodes and associate them with each other
final SchemaNode sourceNode = createTestNode(SchemaNode.class, "TestSource");
final SchemaMethod method = createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest01"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.grant(Structr.find('Principal')[0], 'read', 'write'); }"));
sourceNode.setProperty(SchemaNode.schemaMethods, Arrays.asList(new SchemaMethod[] { method } ));
tx.success();
} catch(FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class sourceType = config.getNodeEntityClass("TestSource");
Principal testUser = null;
// create test node as superuser
try (final Tx tx = app.tx()) {
app.create(sourceType);
tx.success();
} catch(FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// create test user
try (final Tx tx = app.tx()) {
testUser = app.create(Principal.class,
new NodeAttribute<>(Principal.name, "test"),
new NodeAttribute<>(StructrApp.key(Principal.class, "password"), "test")
);
tx.success();
} catch(FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final App userApp = StructrApp.getInstance(SecurityContext.getInstance(testUser, AccessMode.Backend));
// first test without grant, expect no test object to be found using the user context
try (final Tx tx = userApp.tx()) { assertEquals("Invalid grant() scripting result", 0, userApp.nodeQuery(sourceType).getAsList().size()); tx.success(); } catch(FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// grant read access to test user
try (final Tx tx = app.tx()) {
final AbstractNode node = (AbstractNode)app.nodeQuery(sourceType).getFirst();
final AbstractMethod method = Methods.resolveMethod(node.getClass(), "doTest01");
if (method != null) {
method.execute(securityContext, node, new Arguments(), new EvaluationHints());
}
tx.success();
} catch(FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
// first test without grant, expect no test object to be found using the user context
try (final Tx tx = userApp.tx()) { assertEquals("Invalid grant() scripting result", 1, userApp.nodeQuery(sourceType).getAsList().size()); tx.success(); } catch(FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
@Test
public void testScriptedFindWithJSONObject() {
final Random random = new Random();
final long long1 = 13475233523455L;
final long long2 = 327326252322L;
final double double1 = 1234.56789;
final double double2 = 5678.975321;
List<TestSix> testSixs = null;
TestOne testOne1 = null;
TestOne testOne2 = null;
TestTwo testTwo1 = null;
TestTwo testTwo2 = null;
TestThree testThree1 = null;
TestThree testThree2 = null;
TestFour testFour1 = null;
TestFour testFour2 = null;
Date date1 = null;
Date date2 = null;
// setup phase
try (final Tx tx = app.tx()) {
testSixs = createTestNodes(TestSix.class, 10);
testOne1 = app.create(TestOne.class);
testOne2 = app.create(TestOne.class);
testTwo1 = app.create(TestTwo.class);
testTwo2 = app.create(TestTwo.class);
testThree1 = app.create(TestThree.class);
testThree2 = app.create(TestThree.class);
testFour1 = app.create(TestFour.class);
testFour2 = app.create(TestFour.class);
date1 = new Date(random.nextLong());
date2 = new Date();
testOne1.setProperty(TestOne.anInt , 42);
testOne1.setProperty(TestOne.aLong , long1);
testOne1.setProperty(TestOne.aDouble , double1);
testOne1.setProperty(TestOne.aDate , date1);
testOne1.setProperty(TestOne.anEnum , Status.One);
testOne1.setProperty(TestOne.aString , "aString1");
testOne1.setProperty(TestOne.aBoolean , true);
testOne1.setProperty(TestOne.testTwo , testTwo1);
testOne1.setProperty(TestOne.testThree , testThree1);
testOne1.setProperty(TestOne.testFour , testFour1);
testOne1.setProperty(TestOne.manyToManyTestSixs, testSixs.subList(0, 5));
testOne2.setProperty(TestOne.anInt , 33);
testOne2.setProperty(TestOne.aLong , long2);
testOne2.setProperty(TestOne.aDouble , double2);
testOne2.setProperty(TestOne.aDate , date2);
testOne2.setProperty(TestOne.anEnum , Status.Two);
testOne2.setProperty(TestOne.aString , "aString2");
testOne2.setProperty(TestOne.aBoolean , false);
testOne2.setProperty(TestOne.testTwo , testTwo2);
testOne2.setProperty(TestOne.testThree , testThree2);
testOne2.setProperty(TestOne.testFour , testFour2);
testOne2.setProperty(TestOne.manyToManyTestSixs, testSixs.subList(5, 10));
tx.success();
} catch(FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// test phase, find all the things using scripting
try (final Tx tx = app.tx()) {
final ActionContext actionContext = new ActionContext(securityContext);
assertEquals("Invalid scripted find() result", testOne1, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { anInt: 42 })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne2, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { anInt: 33 })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne1, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aLong: " + long1 + " })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne2, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aLong: " + long2 + " })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne1, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aDouble: " + double1 + " })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne2, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aDouble: " + double2 + " })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne1, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { anEnum: 'One' })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne2, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { anEnum: 'Two' })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne1, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aBoolean: true })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne2, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aBoolean: false })[0]; }}", "test"));
tx.success();
} catch(UnlicensedScriptException |FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
@Test
public void testWrappingUnwrapping() {
// setup phase
try (final Tx tx = app.tx()) {
final ActionContext actionContext = new ActionContext(securityContext);
final TestOne context = app.create(TestOne.class);
Scripting.evaluate(actionContext, context, "${{ Structr.create('Group', { name: 'Group1' } ); }}", "test");
Scripting.evaluate(actionContext, context, "${{ Structr.create('Group', 'name', 'Group2'); }}", "test");
assertEquals("Invalid unwrapping result", 2, app.nodeQuery(Group.class).getAsList().size());
tx.success();
} catch(UnlicensedScriptException |FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
@Test
public void testEnumPropertyGet() {
// setup phase
try (final Tx tx = app.tx()) {
final ActionContext actionContext = new ActionContext(securityContext);
final TestOne context = app.create(TestOne.class);
Scripting.evaluate(actionContext, context, "${{ var e = Structr.get('this'); e.anEnum = 'One'; }}", "test");
assertEquals("Invalid enum get result", "One", Scripting.evaluate(actionContext, context, "${{ var e = Structr.get('this'); return e.anEnum; }}", "test"));
assertEquals("Invalid Javascript enum comparison result", true, Scripting.evaluate(actionContext, context, "${{ var e = Structr.get('this'); return e.anEnum == 'One'; }}", "test"));
tx.success();
} catch(UnlicensedScriptException |FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
@Test
public void testCollectionOperations() {
final Class groupType = StructrApp.getConfiguration().getNodeEntityClass("Group");
final PropertyKey<Iterable<Principal>> members = StructrApp.key(groupType, "members");
Group group = null;
Principal user1 = null;
Principal user2 = null;
TestOne testOne = null;
// setup phase
try (final Tx tx = app.tx()) {
group = app.create(Group.class, "Group");
user1 = app.create(Principal.class, "Tester1");
user2 = app.create(Principal.class, "Tester2");
group.setProperty(members, Arrays.asList(new Principal[] { user1 } ));
testOne = app.create(TestOne.class);
createTestNodes(TestSix.class, 10);
tx.success();
} catch(FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// test phase, find all the things using scripting
try (final Tx tx = app.tx()) {
final ActionContext actionContext = new ActionContext(securityContext);
// test prerequisites
assertEquals("Invalid prerequisite", 1, Iterables.count(group.getProperty(members)));
assertEquals("Invalid prerequisite", user2, Scripting.evaluate(actionContext, group, "${{ return Structr.find('Principal', { name: 'Tester2' })[0]; }}", "test"));
// test scripting association
Scripting.evaluate(actionContext, group, "${{ var group = Structr.find('Group')[0]; var users = group.members; users.push(Structr.find('Principal', { name: 'Tester2' })[0]); }}", "test");
assertEquals("Invalid scripted array operation result", 2, Iterables.count(group.getProperty(members)));
// reset group
group.setProperty(members, Arrays.asList(new Principal[] { user1 } ));
// test prerequisites
assertEquals("Invalid prerequisite", 1, Iterables.count(group.getProperty(members)));
// test direct push on member property
Scripting.evaluate(actionContext, group, "${{ var group = Structr.find('Group')[0]; group.members.push(Structr.find('Principal', { name: 'Tester2' })[0]); }}", "test");
assertEquals("Invalid scripted array operation result", 2, Iterables.count(group.getProperty(members)));
// test scripting association
Scripting.evaluate(actionContext, group, "${{ var test = Structr.find('TestOne')[0]; var testSixs = test.manyToManyTestSixs; testSixs.push(Structr.find('TestSix')[0]); }}", "test");
assertEquals("Invalid scripted array operation result", 1, Iterables.count(testOne.getProperty(TestOne.manyToManyTestSixs)));
// test direct push on member property
Scripting.evaluate(actionContext, group, "${{ var test = Structr.find('TestOne')[0]; var testSixs = test.manyToManyTestSixs.push(Structr.find('TestSix')[1]); }}", "test");
assertEquals("Invalid scripted array operation result", 2, Iterables.count(testOne.getProperty(TestOne.manyToManyTestSixs)));
tx.success();
} catch(UnlicensedScriptException |FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
@Test
public void testPropertyConversion() {
TestOne testOne = null;
// setup phase
try (final Tx tx = app.tx()) {
testOne = app.create(TestOne.class);
tx.success();
} catch(FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// test phase, check value conversion
try (final Tx tx = app.tx()) {
final ActionContext actionContext = new ActionContext(securityContext);
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aString = 12; }}", "test");
assertEquals("Invalid scripted property conversion result", "12", testOne.getProperty(TestOne.aString));
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.anInt = '12'; }}", "test");
assertEquals("Invalid scripted property conversion result", 12L, (long)testOne.getProperty(TestOne.anInt));
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aDouble = '12.2342'; }}", "test");
assertEquals("Invalid scripted property conversion result", 12.2342, (double)testOne.getProperty(TestOne.aDouble), 0.0);
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aDouble = 2; }}", "test");
assertEquals("Invalid scripted property conversion result", 2.0, (double)testOne.getProperty(TestOne.aDouble), 0.0);
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aLong = 2352343457252; }}", "test");
assertEquals("Invalid scripted property conversion result", 2352343457252L, (long)testOne.getProperty(TestOne.aLong));
Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aBoolean = true; }}", "test");
assertEquals("Invalid scripted property conversion result", true, (boolean)testOne.getProperty(TestOne.aBoolean));
tx.success();
} catch(UnlicensedScriptException |FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
@Test
public void testQuotes() {
try (final Tx tx = app.tx()) {
final ActionContext actionContext = new ActionContext(securityContext);
Scripting.evaluate(actionContext, app.create(TestOne.class), "${{\n // \"test\n}}", "test");
tx.success();
} catch(UnlicensedScriptException |FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
@Test
public void testVariableReplacement() {
final Date now = new Date();
final SimpleDateFormat format1 = new SimpleDateFormat("dd.MM.yyyy");
final SimpleDateFormat format2 = new SimpleDateFormat("HH:mm:ss");
final SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
final String nowString1 = format1.format(now);
final String nowString2 = format2.format(now);
final String nowString3 = format3.format(now);
final DecimalFormat numberFormat1 = new DecimalFormat("###0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
final DecimalFormat numberFormat2 = new DecimalFormat("0000.0000", DecimalFormatSymbols.getInstance(Locale.GERMAN));
final DecimalFormat numberFormat3 = new DecimalFormat("####", DecimalFormatSymbols.getInstance(Locale.SIMPLIFIED_CHINESE));
final String numberString1 = numberFormat1.format(2.234);
final String numberString2 = numberFormat2.format(2.234);
final String numberString3 = numberFormat3.format(2.234);
final List<String> testSixNames = new LinkedList<>();
NodeInterface template = null;
NodeInterface template2 = null;
TestOne testOne = null;
TestTwo testTwo = null;
TestThree testThree = null;
TestFour testFour = null;
List<TestSix> testSixs = null;
int index = 0;
try (final Tx tx = app.tx()) {
testOne = createTestNode(TestOne.class);
testTwo = createTestNode(TestTwo.class);
testThree = createTestNode(TestThree.class);
testFour = createTestNode(TestFour.class);
testSixs = createTestNodes(TestSix.class, 20, 1);
// set string array on test four
testFour.setProperty(TestFour.stringArrayProperty, new String[] { "one", "two", "three", "four" } );
final Calendar cal = GregorianCalendar.getInstance();
// set calendar to 2018-01-01T00:00:00+0000
cal.set(2018, 0, 1, 0, 0, 0);
for (final TestSix testSix : testSixs) {
final String name = "TestSix" + StringUtils.leftPad(Integer.toString(index), 2, "0");
testSix.setProperty(TestSix.name, name);
testSix.setProperty(TestSix.index, index);
testSix.setProperty(TestSix.date, cal.getTime());
index++;
cal.add(Calendar.DAY_OF_YEAR, 3);
// build list of names
testSixNames.add(name);
}
// create mail template
template = createTestNode(getType("MailTemplate"));
template.setProperty(getKey("MailTemplate", "name"), "TEST");
template.setProperty(getKey("MailTemplate", "locale"), "en_EN");
template.setProperty(getKey("MailTemplate", "text"), "This is a template for ${this.name}");
// create mail template
template2 = createTestNode(getType("MailTemplate"));
template2.setProperty(getKey("MailTemplate", "name"), "TEST2");
template2.setProperty(getKey("MailTemplate", "locale"), "en_EN");
template2.setProperty(getKey("MailTemplate", "text"), "${this.aDouble}");
// check existance
assertNotNull(testOne);
testOne.setProperty(TestOne.name, "A-nice-little-name-for-my-test-object");
testOne.setProperty(TestOne.anInt, 1);
testOne.setProperty(TestOne.aString, "String");
testOne.setProperty(TestOne.anotherString, "{\n\ttest: test,\n\tnum: 3\n}");
testOne.setProperty(TestOne.replaceString, "${this.name}");
testOne.setProperty(TestOne.aLong, 235242522552L);
testOne.setProperty(TestOne.aDouble, 2.234);
testOne.setProperty(TestOne.aDate, now);
testOne.setProperty(TestOne.anEnum, TestOne.Status.One);
testOne.setProperty(TestOne.aBoolean, true);
testOne.setProperty(TestOne.testTwo, testTwo);
testOne.setProperty(TestOne.testThree, testThree);
testOne.setProperty(TestOne.testFour, testFour);
testOne.setProperty(TestOne.manyToManyTestSixs, testSixs);
testOne.setProperty(TestOne.cleanTestString, "a<b>c.d'e?f(g)h{i}j[k]l+m/n–o\\p\\q|r's!t,u-v_w`x-y-zöäüßABCDEFGH");
testOne.setProperty(TestOne.stringWithQuotes, "A'B\"C");
testOne.setProperty(TestOne.aStringArray, new String[] { "a", "b", "c" });
testTwo.setProperty(TestTwo.name, "testTwo_name");
testThree.setProperty(TestThree.name, "testThree_name");
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
final ActionContext ctx = new ActionContext(securityContext, null);
// test quotes etc.
assertEquals("Invalid result for quoted template expression", "''", Scripting.replaceVariables(ctx, testOne, "'${err}'"));
assertEquals("Invalid result for quoted template expression", " '' ", Scripting.replaceVariables(ctx, testOne, " '${err}' "));
assertEquals("Invalid result for quoted template expression", "\"\"", Scripting.replaceVariables(ctx, testOne, "\"${this.error}\""));
assertEquals("Invalid result for quoted template expression", "''''''", Scripting.replaceVariables(ctx, testOne, "'''${this.this.this.error}'''"));
assertEquals("Invalid result for quoted template expression", "''", Scripting.replaceVariables(ctx, testOne, "'${parent.error}'"));
assertEquals("Invalid result for quoted template expression", "''", Scripting.replaceVariables(ctx, testOne, "'${this.owner}'"));
assertEquals("Invalid result for quoted template expression", "''", Scripting.replaceVariables(ctx, testOne, "'${this.alwaysNull}'"));
assertEquals("Invalid result for quoted template expression", "''", Scripting.replaceVariables(ctx, testOne, "'${parent.owner}'"));
// test for "empty" return value
assertEquals("Invalid expressions should yield an empty result", "", Scripting.replaceVariables(ctx, testOne, "${err}"));
assertEquals("Invalid expressions should yield an empty result", "", Scripting.replaceVariables(ctx, testOne, "${this.error}"));
assertEquals("Invalid expressions should yield an empty result", "", Scripting.replaceVariables(ctx, testOne, "${this.this.this.error}"));
assertEquals("Invalid expressions should yield an empty result", "", Scripting.replaceVariables(ctx, testOne, "${parent.error}"));
assertEquals("Invalid expressions should yield an empty result", "", Scripting.replaceVariables(ctx, testOne, "${this.owner}"));
assertEquals("Invalid expressions should yield an empty result", "", Scripting.replaceVariables(ctx, testOne, "${this.alwaysNull}"));
assertEquals("Invalid expressions should yield an empty result", "", Scripting.replaceVariables(ctx, testOne, "${parent.owner}"));
assertEquals("${this} should evaluate to the current node", testOne.toString(), Scripting.replaceVariables(ctx, testOne, "${this}"));
//assertEquals("${parent} should evaluate to the context parent node", testOne.toString(), Scripting.replaceVariables(ctx, testOne, "${parent}"));
assertEquals("${this} should evaluate to the current node", testTwo.toString(), Scripting.replaceVariables(ctx, testTwo, "${this}"));
//assertEquals("${parent} should evaluate to the context parent node", testOne.toString(), Scripting.replaceVariables(ctx, testOne, "${parent}"));
assertEquals("Invalid variable reference", testTwo.toString(), Scripting.replaceVariables(ctx, testOne, "${this.testTwo}"));
assertEquals("Invalid variable reference", testThree.toString(), Scripting.replaceVariables(ctx, testOne, "${this.testThree}"));
assertEquals("Invalid variable reference", testFour.toString(), Scripting.replaceVariables(ctx, testOne, "${this.testFour}"));
assertEquals("Invalid variable reference", testTwo.getUuid(), Scripting.replaceVariables(ctx, testOne, "${this.testTwo.id}"));
assertEquals("Invalid variable reference", testThree.getUuid(), Scripting.replaceVariables(ctx, testOne, "${this.testThree.id}"));
assertEquals("Invalid variable reference", testFour.getUuid(), Scripting.replaceVariables(ctx, testOne, "${this.testFour.id}"));
assertEquals("Invalid size result", "20", Scripting.replaceVariables(ctx, testOne, "${this.manyToManyTestSixs.size}"));
assertEquals("Invalid size result", "", Scripting.replaceVariables(ctx, testOne, "${(this.alwaysNull.size}"));
assertEquals("Invalid size result", "", Scripting.replaceVariables(ctx, testOne, "${this.alwaysNull.size}"));
assertEquals("Invalid variable reference", "1", Scripting.replaceVariables(ctx, testOne, "${this.anInt}"));
assertEquals("Invalid variable reference", "String", Scripting.replaceVariables(ctx, testOne, "${this.aString}"));
assertEquals("Invalid variable reference", "235242522552", Scripting.replaceVariables(ctx, testOne, "${this.aLong}"));
assertEquals("Invalid variable reference", "2.234", Scripting.replaceVariables(ctx, testOne, "${this.aDouble}"));
// test with property
assertEquals("Invalid md5() result", "27118326006d3829667a400ad23d5d98", Scripting.replaceVariables(ctx, testOne, "${md5(this.aString)}"));
assertEquals("Invalid upper() result", "27118326006D3829667A400AD23D5D98", Scripting.replaceVariables(ctx, testOne, "${upper(md5(this.aString))}"));
assertEquals("Invalid upper(lower() result", "27118326006D3829667A400AD23D5D98", Scripting.replaceVariables(ctx, testOne, "${upper(lower(upper(md5(this.aString))))}"));
assertEquals("Invalid md5() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${md5(this.alwaysNull)}"));
assertEquals("Invalid upper() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${upper(this.alwaysNull)}"));
assertEquals("Invalid lower() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${lower(this.alwaysNull)}"));
// test literal value as well
assertEquals("Invalid md5() result", "cc03e747a6afbbcbf8be7668acfebee5", Scripting.replaceVariables(ctx, testOne, "${md5(\"test123\")}"));
assertEquals("Invalid lower() result", "string", Scripting.replaceVariables(ctx, testOne, "${lower(this.aString)}"));
assertEquals("Invalid upper() result", "STRING", Scripting.replaceVariables(ctx, testOne, "${upper(this.aString)}"));
// merge
assertEquals("Invalid merge() result", "[one, two, three]", Scripting.replaceVariables(ctx, testOne, "${merge('one', 'two', 'three')}"));
assertEquals("Invalid merge() result", "[one, two, three, two, one, two, three]", Scripting.replaceVariables(ctx, testOne, "${merge(merge('one', 'two', 'three'), 'two', merge('one', 'two', 'three'))}"));
assertEquals("Invalid merge() result", "[1, 2, 3, 4, 5, 6, 7, 8]", Scripting.replaceVariables(ctx, testOne, "${merge(merge('1', '2', '3'), merge('4', '5', merge('6', '7', '8')))}"));
assertEquals("Invalid merge() result", "[1, 2, 3, 4, 5, 6, 1, 2, 3, 8]", Scripting.replaceVariables(ctx, testOne, "${ ( store('list', merge('1', '2', '3')), merge(retrieve('list'), merge('4', '5', merge('6', retrieve('list'), '8'))) )}"));
// merge_unique
assertEquals("Invalid merge_unique() result", "[one, two, three]", Scripting.replaceVariables(ctx, testOne, "${merge_unique('one', 'two', 'three', 'two')}"));
assertEquals("Invalid merge_unique() result", "[one, two, three]", Scripting.replaceVariables(ctx, testOne, "${merge_unique(merge_unique('one', 'two', 'three'), 'two', merge_unique('one', 'two', 'three'))}"));
assertEquals("Invalid merge_unique() result", "[1, 2, 3, 4, 5, 6, 7, 8]", Scripting.replaceVariables(ctx, testOne, "${merge_unique(merge_unique('1', '2', '3'), merge_unique('4', '5', merge_unique('6', '7', '8')))}"));
assertEquals("Invalid merge_unique() result", "[1, 2, 3, 4, 5, 6, 8]", Scripting.replaceVariables(ctx, testOne, "${ ( store('list', merge_unique('1', '2', '3')), merge_unique(retrieve('list'), merge_unique('4', '5', merge_unique('6', retrieve('list'), '8'))) )}"));
// complement
assertEquals("Invalid complement() result", "[]", Scripting.replaceVariables(ctx, testOne, "${complement(merge('one', 'two', 'three'), 'one', merge('two', 'three', 'four'))}"));
assertEquals("Invalid complement() result", "[two]", Scripting.replaceVariables(ctx, testOne, "${complement(merge('one', 'two', 'three'), merge('one', 'four', 'three'))}"));
assertEquals("Invalid complement() result", "[two, two]", Scripting.replaceVariables(ctx, testOne, "${complement(merge('one', 'two', 'three', 'two'), merge('one', 'four', 'three'))}"));
assertEquals("Invalid complement() result", "[one]", Scripting.replaceVariables(ctx, testOne, "${complement(merge('one', 'two', 'three', 'two'), merge('two', 'four', 'three'))}"));
assertEquals("Invalid complement() result", "[one, three]", Scripting.replaceVariables(ctx, testOne, "${complement(merge('one', 'two', 'three', 'two'), 'two')}"));
// join
assertEquals("Invalid join() result", "one,two,three", Scripting.replaceVariables(ctx, testOne, "${join(merge(\"one\", \"two\", \"three\"), \",\")}"));
// concat
assertEquals("Invalid concat() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(\"one\", \"two\", \"three\")}"));
assertEquals("Invalid concat() result", "oneStringthree", Scripting.replaceVariables(ctx, testOne, "${concat(\"one\", this.aString, \"three\")}"));
assertEquals("Invalid concat() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${concat(this.alwaysNull, this.alwaysNull)}"));
// split
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one,two,three\"))}"));
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one;two;three\"))}"));
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one two three\"))}"));
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one two three\"))}"));
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one;two;three\", \";\"))}"));
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one,two,three\", \",\"))}"));
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one.two.three\", \".\"))}"));
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one two three\", \" \"))}"));
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one+two+three\", \"+\"))}"));
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one|two|three\", \"|\"))}"));
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one::two::three\", \"::\"))}"));
assertEquals("Invalid split() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one-two-three\", \"-\"))}"));
assertEquals("Invalid split() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${split(this.alwaysNull)}"));
// split_regex
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one,two,three\"))}"));
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one;two;three\"))}"));
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one two three\"))}"));
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one two three\"))}"));
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one;two;three\", \";\"))}"));
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one,two,three\", \",\"))}"));
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one.two.three\", \"\\.\"))}"));
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one two three\", \" \"))}"));
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one+two+three\", \"+\"))}"));
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one|two|three\", \"|\"))}"));
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one::two::three\", \"::\"))}"));
assertEquals("Invalid split_regex() result", "onetwothree", Scripting.replaceVariables(ctx, testOne, "${concat(split(\"one-two-three\", \"-\"))}"));
assertEquals("Invalid split_regex() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${split(this.alwaysNull)}"));
// abbr
assertEquals("Invalid abbr() result", "oneStringt…", Scripting.replaceVariables(ctx, testOne, "${abbr(concat(\"one\", this.aString, \"three\"), 10)}"));
assertEquals("Invalid abbr() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${abbr(this.alwaysNull, 10)}"));
// capitalize..
assertEquals("Invalid capitalize() result", "One_two_three", Scripting.replaceVariables(ctx, testOne, "${capitalize(concat(\"one_\", \"two_\", \"three\"))}"));
assertEquals("Invalid capitalize() result", "One_Stringthree", Scripting.replaceVariables(ctx, testOne, "${capitalize(concat(\"one_\", this.aString, \"three\"))}"));
assertEquals("Invalid capitalize() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${capitalize(this.alwaysNull)}"));
// titleize
assertEquals("Invalid titleize() result", "One Two Three", Scripting.replaceVariables(ctx, testOne, "${titleize(concat(\"one_\", \"two_\", \"three\"), \"_\")}"));
assertEquals("Invalid titleize() result", "One Stringthree", Scripting.replaceVariables(ctx, testOne, "${titleize(concat(\"one_\", this.aString, \"three\"), \"_\")}"));
assertEquals("Invalid titleize() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${titleize(this.alwaysNull)}"));
// num (explicit number conversion)
assertEquals("Invalid num() result", "2.234", Scripting.replaceVariables(ctx, testOne, "${num(2.234)}"));
assertEquals("Invalid num() result", "2.234", Scripting.replaceVariables(ctx, testOne, "${num(this.aDouble)}"));
assertEquals("Invalid num() result", "1.0", Scripting.replaceVariables(ctx, testOne, "${num(this.anInt)}"));
assertEquals("Invalid num() result", "", Scripting.replaceVariables(ctx, testOne, "${num(\"abc\")}"));
assertEquals("Invalid num() result", "", Scripting.replaceVariables(ctx, testOne, "${num(this.aString)}"));
assertEquals("Invalid num() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${num(this.alwaysNull)}"));
// index_of
assertEquals("Invalid index_of() result", "19", Scripting.replaceVariables(ctx, testOne, "${index_of(this.name, 'for')}"));
assertEquals("Invalid index_of() result", "-1", Scripting.replaceVariables(ctx, testOne, "${index_of(this.name, 'entity')}"));
assertEquals("Invalid index_of() result", "19", Scripting.replaceVariables(ctx, testOne, "${index_of('a-nice-little-name-for-my-test-object', 'for')}"));
assertEquals("Invalid index_of() result", "-1", Scripting.replaceVariables(ctx, testOne, "${index_of('a-nice-little-name-for-my-test-object', 'entity')}"));
// contains
assertEquals("Invalid contains() result", "true", Scripting.replaceVariables(ctx, testOne, "${contains(this.name, 'for')}"));
assertEquals("Invalid contains() result", "false", Scripting.replaceVariables(ctx, testOne, "${contains(this.name, 'entity')}"));
assertEquals("Invalid contains() result", "true", Scripting.replaceVariables(ctx, testOne, "${contains('a-nice-little-name-for-my-test-object', 'for')}"));
assertEquals("Invalid contains() result", "false", Scripting.replaceVariables(ctx, testOne, "${contains('a-nice-little-name-for-my-test-object', 'entity')}"));
// contains with collection / entity
assertEquals("Invalid contains() result", "true", Scripting.replaceVariables(ctx, testOne, "${contains(this.manyToManyTestSixs, first(find('TestSix')))}"));
assertEquals("Invalid contains() result", "false", Scripting.replaceVariables(ctx, testOne, "${contains(this.manyToManyTestSixs, first(find('TestFive')))}"));
// starts_with
assertEquals("Invalid starts_with() result", "true", Scripting.replaceVariables(ctx, testOne, "${starts_with(null, null)}"));
assertEquals("Invalid starts_with() result", "false", Scripting.replaceVariables(ctx, testOne, "${starts_with(null, 'abc')}"));
assertEquals("Invalid starts_with() result", "false", Scripting.replaceVariables(ctx, testOne, "${starts_with('abcdef', null)}"));
assertEquals("Invalid starts_with() result", "true", Scripting.replaceVariables(ctx, testOne, "${starts_with('abcdef', 'abc')}"));
assertEquals("Invalid starts_with() result", "false", Scripting.replaceVariables(ctx, testOne, "${starts_with('ABCDEF', 'abc')}"));
assertEquals("Invalid starts_with() result", "true", Scripting.replaceVariables(ctx, testOne, "${starts_with(merge('a', 'b'), 'a')}"));
assertEquals("Invalid starts_with() result", "false", Scripting.replaceVariables(ctx, testOne, "${starts_with(merge('c', 'a', 'b'), 'a')}"));
assertEquals("Invalid starts_with() result", "false", Scripting.replaceVariables(ctx, testOne, "${starts_with(merge('abc', 'b'), 'a')}"));
// substring
assertEquals("Invalid substring() result", "for", Scripting.replaceVariables(ctx, testOne, "${substring(this.name, 19, 3)}"));
assertEquals("Invalid substring() result", "", Scripting.replaceVariables(ctx, testOne, "${substring(this.name, -1, -1)}"));
assertEquals("Invalid substring() result", "", Scripting.replaceVariables(ctx, testOne, "${substring(this.name, 100, -1)}"));
assertEquals("Invalid substring() result", "", Scripting.replaceVariables(ctx, testOne, "${substring(this.name, 5, -2)}"));
assertEquals("Invalid substring() result", "for", Scripting.replaceVariables(ctx, testOne, "${substring('a-nice-little-name-for-my-test-object', 19, 3)}"));
assertEquals("Invalid substring() result", "ice-little-name-for-my-test-object", Scripting.replaceVariables(ctx, testOne, "${substring('a-nice-little-name-for-my-test-object', 3)}"));
assertEquals("Invalid substring() result", "ice", Scripting.replaceVariables(ctx, testOne, "${substring('a-nice-little-name-for-my-test-object', 3, 3)}"));
assertEquals("Invalid substring() result", "", Scripting.replaceVariables(ctx, testOne, "${substring('a-nice-little-name-for-my-test-object', -1, -1)}"));
assertEquals("Invalid substring() result", "", Scripting.replaceVariables(ctx, testOne, "${substring('a-nice-little-name-for-my-test-object', 100, -1)}"));
assertEquals("Invalid substring() result", "", Scripting.replaceVariables(ctx, testOne, "${substring('a-nice-little-name-for-my-test-object', 5, -2)}"));
assertEquals("Invalid substring() result", "y-short", Scripting.replaceVariables(ctx, testOne, "${substring('very-short', 3, 200)}"));
// length
assertEquals("Invalid length() result", "37", Scripting.replaceVariables(ctx, testOne, "${length(this.name)}"));
assertEquals("Invalid length() result", "37", Scripting.replaceVariables(ctx, testOne, "${length('a-nice-little-name-for-my-test-object')}"));
assertEquals("Invalid length() result", "4", Scripting.replaceVariables(ctx, testOne, "${length('test')}"));
assertEquals("Invalid length() result", "", Scripting.replaceVariables(ctx, testOne, "${length(this.alwaysNull)}"));
// clean ("a<b>c.d'e?f(g)h{i}j[k]l+m/n–o\\p\\q|r's!t,u-v_w`x-y-zöäüßABCDEFGH")
assertEquals("Invalid clean() result", "abcd-efghijkl-m-n-o-p-q-r-stu-v-w-x-y-zoeaeuessabcdefgh", Scripting.replaceVariables(ctx, testOne, "${clean(this.cleanTestString)}"));
assertEquals("Invalid clean() result", "abcd-efghijkl-m-n-o-p-q-r-stu-v-w-x-y-zoeaeuessabcdefgh", Scripting.replaceVariables(ctx, testOne, "${clean(get(this, \"cleanTestString\"))}"));
assertEquals("Invalid clean() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${clean(this.alwaysNull)}"));
// trim
assertEquals("Invalid trim() result", "test", Scripting.replaceVariables(ctx, testOne, "${trim(' \t\t\t\r\r\r\n\n\ntest')}"));
assertEquals("Invalid trim() result", "test", Scripting.replaceVariables(ctx, testOne, "${trim('test \t\t\t\r\r\r\n\n\n')}"));
assertEquals("Invalid trim() result", "test", Scripting.replaceVariables(ctx, testOne, "${trim(' \t\t\t\r\r\r\n\n\ntest \t\t\t\r\r\r\n\n\n')}"));
assertEquals("Invalid trim() result", "test", Scripting.replaceVariables(ctx, testOne, "${trim(' test ')}"));
assertEquals("Invalid trim() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${trim(null)}"));
assertEquals("Invalid trim() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${trim(this.alwaysNull)}"));
// urlencode
assertEquals("Invalid urlencode() result", "a%3Cb%3Ec.d%27e%3Ff%28g%29h%7Bi%7Dj%5Bk%5Dl%2Bm%2Fn%E2%80%93o%5Cp%5Cq%7Cr%27s%21t%2Cu-v_w%60x-y-z%C3%B6%C3%A4%C3%BC%C3%9FABCDEFGH", Scripting.replaceVariables(ctx, testOne, "${urlencode(this.cleanTestString)}"));
assertEquals("Invalid urlencode() result", "a%3Cb%3Ec.d%27e%3Ff%28g%29h%7Bi%7Dj%5Bk%5Dl%2Bm%2Fn%E2%80%93o%5Cp%5Cq%7Cr%27s%21t%2Cu-v_w%60x-y-z%C3%B6%C3%A4%C3%BC%C3%9FABCDEFGH", Scripting.replaceVariables(ctx, testOne, "${urlencode(get(this, \"cleanTestString\"))}"));
assertEquals("Invalid urlencode() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${urlencode(this.alwaysNull)}"));
// escape_javascript
assertEquals("Invalid escape_javascript() result", "A\\'B\\\"C", Scripting.replaceVariables(ctx, testOne, "${escape_javascript(this.stringWithQuotes)}"));
assertEquals("Invalid escape_javascript() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${escape_javascript(this.alwaysNull)}"));
// escape_json
assertEquals("Invalid escape_json() result", "A'B\\\"C", Scripting.replaceVariables(ctx, testOne, "${escape_json(this.stringWithQuotes)}"));
assertEquals("Invalid escape_json() result with null value", "", Scripting.replaceVariables(ctx, testOne, "${escape_json(this.alwaysNull)}"));
// is
assertEquals("Invalid is() result", "true", Scripting.replaceVariables(ctx, testOne, "${is(\"true\", \"true\")}"));
assertEquals("Invalid is() result", "", Scripting.replaceVariables(ctx, testOne, "${is(\"false\", \"true\")}"));
// is + equal
assertEquals("Invalid is(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${is(equal(this.id, this.id), \"true\")}"));
assertEquals("Invalid is(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${is(equal(\"abc\", \"abc\"), \"true\")}"));
assertEquals("Invalid is(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${is(equal(3, 3), \"true\")}"));
assertEquals("Invalid is(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${is(equal(\"3\", \"3\"), \"true\")}"));
assertEquals("Invalid is(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${is(equal(3.1414, 3.1414), \"true\")}"));
assertEquals("Invalid is(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${is(equal(\"3.1414\", \"3.1414\"), \"true\")}"));
assertEquals("Invalid is(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${is(equal(23.44242222243633337234623462, 23.44242222243633337234623462), \"true\")}"));
assertEquals("Invalid is(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${is(equal(\"23.44242222243633337234623462\", \"23.44242222243633337234623462\"), \"true\")}"));
assertEquals("Invalid is(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${is(equal(13, 013), \"true\")}"));
assertEquals("Invalid is(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${is(equal(13, \"013\"), \"true\")}"));
assertEquals("Invalid is(equal()) result", "", Scripting.replaceVariables(ctx, testOne, "${is(equal(\"13\", \"013\"), \"true\")}"));
assertEquals("Invalid is(equal()) result", "", Scripting.replaceVariables(ctx, testOne, "${is(equal(\"13\", \"00013\"), \"true\")}"));
// if etc.
assertEquals("Invalid if() result", "true", Scripting.replaceVariables(ctx, testOne, "${if(\"true\", \"true\", \"false\")}"));
assertEquals("Invalid if() result", "false", Scripting.replaceVariables(ctx, testOne, "${if(\"false\", \"true\", \"false\")}"));
// empty
assertEquals("Invalid empty() result", "true", Scripting.replaceVariables(ctx, testOne, "${empty(\"\")}"));
assertEquals("Invalid empty() result", "false", Scripting.replaceVariables(ctx, testOne, "${empty(\" \")}"));
assertEquals("Invalid empty() result", "false", Scripting.replaceVariables(ctx, testOne, "${empty(\" \")}"));
assertEquals("Invalid empty() result", "false", Scripting.replaceVariables(ctx, testOne, "${empty(\"xyz\")}"));
assertEquals("Invalid empty() result with null value", "true", Scripting.replaceVariables(ctx, testOne, "${empty(this.alwaysNull)}"));
assertEquals("Invalid if(empty()) result", "false", Scripting.replaceVariables(ctx, testOne, "${if(empty(\"test\"), true, false)}"));
assertEquals("Invalid if(empty()) result", "false", Scripting.replaceVariables(ctx, testOne, "${if(empty(\"test\n\"), true, false)}"));
// functions can NOT handle literal strings containing newlines (disabled for now, because literal strings pose problems in the matching process)
assertEquals("Invalid if(empty()) result", "false", Scripting.replaceVariables(ctx, testOne, "${if(empty(\"\n\"), true, false)}"));
assertEquals("Invalid if(empty()) result", "false", Scripting.replaceVariables(ctx, testOne, "${if(empty(\"\n\"), \"true\", \"false\")}"));
// functions CAN handle variable values with newlines!
assertEquals("Invalid if(empty()) result", "false", Scripting.replaceVariables(ctx, testOne, "${if(empty(this.anotherString), \"true\", \"false\")}"));
// empty in JavaScript
assertEquals("Invalid empty() result", "true", Scripting.replaceVariables(ctx, testOne, "${{return $.empty(\"\")}}"));
assertEquals("Invalid empty() result", "false", Scripting.replaceVariables(ctx, testOne, "${{return $.empty(\" \")}}"));
assertEquals("Invalid empty() result", "false", Scripting.replaceVariables(ctx, testOne, "${{return $.empty(\" \")}}"));
assertEquals("Invalid empty() result", "false", Scripting.replaceVariables(ctx, testOne, "${{return $.empty(\"xyz\")}}"));
assertEquals("Invalid empty() result", "true", Scripting.replaceVariables(ctx, testOne, "${{return $.empty([])}}"));
assertEquals("Invalid empty() result", "true", Scripting.replaceVariables(ctx, testOne, "${{return $.empty({})}}"));
// equal
assertEquals("Invalid equal() result", "true", Scripting.replaceVariables(ctx, testOne, "${equal(this.id, this.id)}"));
assertEquals("Invalid equal() result", "true", Scripting.replaceVariables(ctx, testOne, "${equal(\"1\", this.anInt)}"));
assertEquals("Invalid equal() result", "true", Scripting.replaceVariables(ctx, testOne, "${equal(1, this.anInt)}"));
assertEquals("Invalid equal() result", "true", Scripting.replaceVariables(ctx, testOne, "${equal(1.0, this.anInt)}"));
assertEquals("Invalid equal() result", "true", Scripting.replaceVariables(ctx, testOne, "${equal(this.anInt, \"1\")}"));
assertEquals("Invalid equal() result", "true", Scripting.replaceVariables(ctx, testOne, "${equal(this.anInt, 1)}"));
assertEquals("Invalid equal() result", "true", Scripting.replaceVariables(ctx, testOne, "${equal(this.anInt, 1.0)}"));
assertEquals("Invalid equal() result", "true", Scripting.replaceVariables(ctx, testOne, "${equal(this.aBoolean, \"true\")}"));
assertEquals("Invalid equal() result", "false", Scripting.replaceVariables(ctx, testOne, "${equal(this.aBoolean, \"false\")}"));
assertEquals("Invalid equal() result", "true", Scripting.replaceVariables(ctx, testOne, "${equal(this.aBoolean, true)}"));
assertEquals("Invalid equal() result", "false", Scripting.replaceVariables(ctx, testOne, "${equal(this.aBoolean, false)}"));
assertEquals("Invalid equal() result", "true", Scripting.replaceVariables(ctx, testOne, "${equal(this.anEnum, 'One')}"));
// if + equal
assertEquals("Invalid if(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${if(equal(this.id, this.id), \"true\", \"false\")}"));
assertEquals("Invalid if(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${if(equal(\"abc\", \"abc\"), \"true\", \"false\")}"));
assertEquals("Invalid if(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${if(equal(3, 3), \"true\", \"false\")}"));
assertEquals("Invalid if(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${if(equal(\"3\", \"3\"), \"true\", \"false\")}"));
assertEquals("Invalid if(equal()) result", "true", Scripting.replaceVariables(ctx, testOne, "${if(equal(3.1414, 3.1414), \"true\", \"false\")}"));