/
ElSpec.scala
808 lines (673 loc) · 30 KB
/
ElSpec.scala
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
/*
* Copyright 2011-2024 GatlingCorp (https://gatling.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gatling.core.session.el
import java.{ util => ju }
import java.util.UUID
import io.gatling.{ BaseSpec, ValidationValues }
import io.gatling.commons.validation.Success
import io.gatling.core.EmptySession
import io.gatling.core.config.GatlingConfiguration
import io.gatling.core.session.el
private final case class Foo(bar: String)
private final case class Bar(baz: Baz)
private final case class Baz(qix: String)
final case class Primitives(
boolean: Boolean,
byte: Byte,
short: Short,
int: Int,
long: Long,
float: Float,
double: Double,
char: Char
)
class ElSpec extends BaseSpec with ValidationValues with EmptySession {
private implicit val configuration: GatlingConfiguration = GatlingConfiguration.loadForTest()
private def newSession(attributes: Map[String, Any]) =
emptySession.copy(attributes = attributes)
"Static String" should "return itself" in {
val session = newSession(Map.empty)
val expression = "bar".el[String]
expression(session).succeeded shouldBe "bar"
}
it should "return empty when empty" in {
val session = newSession(Map.empty)
val expression = "".el[String]
expression(session).succeeded shouldBe ""
}
"One monovalued Expression" should "return expected result when the variable is the whole string" in {
val session = newSession(Map("bar" -> "BAR"))
val expression = "#{bar}".el[String]
expression(session).succeeded shouldBe "BAR"
}
it should "return expected result when the variable is at the end of the string" in {
val session = newSession(Map("bar" -> "BAR"))
val expression = "foo#{bar}".el[String]
expression(session).succeeded shouldBe "fooBAR"
}
it should "return expected result when the variable is at the beginning of the string" in {
val session = newSession(Map("bar" -> "BAR"))
val expression = "#{bar}baz".el[String]
expression(session).succeeded shouldBe "BARbaz"
}
it should "return expected result when the variable is in the middle of the string" in {
val session = newSession(Map("bar" -> "BAR"))
val expression = "foo#{bar}baz".el[String]
expression(session).succeeded shouldBe "fooBARbaz"
}
it should "handle gracefully when an attribute is missing" in {
val session = newSession(Map("foo" -> "FOO"))
val expression = "foo#{bar}".el[String]
expression(session).failed shouldBe ElMessages.undefinedSessionAttribute("bar").message
}
it should "properly handle JsonPath expression" in {
val session = newSession(Map("foo" -> "FOO"))
val expression = "$.foo.bar".el[String]
expression(session).succeeded shouldBe "$.foo.bar"
}
it should "properly handle JsonPath expression template" in {
val session = newSession(Map("foo" -> "FOO"))
val expression = "$.#{foo}.bar".el[String]
expression(session).succeeded shouldBe "$.FOO.bar"
}
it should "properly handle multiline JSON template" in {
val session = newSession(Map("foo" -> "FOO"))
val expression = """{
"foo": #{foo},
"bar": 1
}""".el[String]
expression(session).succeeded shouldBe """{
"foo": FOO,
"bar": 1
}"""
}
"Multivalued Expression" should "return expected result with 2 monovalued expressions" in {
val session = newSession(Map("foo" -> "FOO", "bar" -> "BAR"))
val expression = "#{foo} #{bar}".el[String]
expression(session).succeeded shouldBe "FOO BAR"
}
it should "return expected result when used with a static index" in {
val session = newSession(Map("bar" -> List("BAR1", "BAR2")))
val expression = "foo#{bar(1)}".el[String]
expression(session).succeeded shouldBe "fooBAR2"
}
"index access" should "return n-th element of a Seq when n is a static number" in {
val session = newSession(Map("bar" -> List("BAR1", "BAR2")))
val expression = "#{bar(0)}".el[String]
expression(session).succeeded shouldBe "BAR1"
}
it should "return n-th element of a Seq when n is a variable" in {
val session = newSession(Map("bar" -> List("BAR1", "BAR2"), "baz" -> 1))
val expression = "{foo#{bar(baz)}}".el[String]
expression(session).succeeded shouldBe "{fooBAR2}"
}
it should "return n-th element of an Array" in {
val session = newSession(Map("arr" -> Array(1, 2)))
val expression = "#{arr(0)}".el[Int]
expression(session).succeeded shouldBe 1
}
it should "return n-th element of JList" in {
val lst = new ju.LinkedList[Int]
lst.add(1)
lst.add(2)
val session = newSession(Map("lst" -> lst))
val expression = "#{lst(0)}".el[Int]
expression(session).succeeded shouldBe 1
}
it should "return (length + n)-th element of a Seq when n is a negative number" in {
val session = newSession(Map("bar" -> List("BAR1", "BAR2")))
val expression = "#{bar(-1)}".el[String]
expression(session).succeeded shouldBe "BAR2"
}
it should "return (length + n)-th element of an Array when n is a negative number" in {
val session = newSession(Map("arr" -> Array(1, 2)))
val expression = "#{arr(1)}".el[Int]
expression(session).succeeded shouldBe 2
}
it should "return (length + n)-th element of a JList when n is a negative number" in {
val lst = new ju.LinkedList[Int]
lst.add(1)
lst.add(2)
val session = newSession(Map("lst" -> lst))
val expression = "#{lst(-1)}".el[Int]
expression(session).succeeded shouldBe 2
}
it should "handle gracefully when index in an Array is out of range" in {
val session = newSession(Map("arr" -> Array(1, 2)))
val expression = "#{arr(2)}".el[Int]
expression(session).failed shouldBe ElMessages.undefinedSeqIndex("arr", 2).message
}
it should "handle gracefully when index in an JList is out of range" in {
val lst = new ju.LinkedList[Int]
lst.add(1)
lst.add(2)
val session = newSession(Map("lst" -> lst))
val expression = "#{lst(2)}".el[Int]
expression(session).failed shouldBe ElMessages.undefinedSeqIndex("lst", 2).message
}
it should "handle gracefully when used with static index and missing attribute" in {
val session = newSession(Map.empty)
val expression = "foo#{bar(1)}".el[String]
expression(session).failed shouldBe ElMessages.undefinedSessionAttribute("bar").message
}
it should "handle gracefully when used with static index and empty attribute" in {
val session = newSession(Map("bar" -> Nil))
val expression = "foo#{bar(1)}".el[String]
expression(session).failed shouldBe ElMessages.undefinedSeqIndex("bar", 1).message
}
it should "handle gracefully when used with static index and missing index" in {
val session = newSession(Map("bar" -> List("BAR1")))
val expression = "foo#{bar(1)}".el[String]
expression(session).failed shouldBe ElMessages.undefinedSeqIndex("bar", 1).message
}
it should "handle gracefully when used with missing resolved index attribute" in {
val session = newSession(Map("bar" -> List("BAR1", "BAR2")))
val expression = "{foo#{bar(baz)}}".el[String]
expression(session).failed shouldBe ElMessages.undefinedSessionAttribute("baz").message
}
it should "handle gracefully value of unsupported type" in {
val session = newSession(Map("i" -> 1))
val expression = "#{i(0)}".el[Int]
expression(session).failed shouldBe ElMessages.indexAccessNotSupported(1, "i").message
}
it should "support tuples" in {
val session = newSession(Map("tuple" -> ("foo", "bar")))
val expression = "#{tuple(0)}".el[String]
expression(session).succeeded shouldBe "foo"
}
"'size' function in Expression" should "return correct size for non empty seq" in {
val session = newSession(Map("bar" -> List("BAR1", "BAR2")))
val expression = "#{bar.size()}".el[Int]
expression(session).succeeded shouldBe 2
}
it should "return correct size for empty seq" in {
val session = newSession(Map("bar" -> List()))
val expression = "#{bar.size()}".el[Int]
expression(session).succeeded shouldBe 0
}
it should "return 0 size for missing attribute" in {
val session = newSession(Map.empty)
val expression = "#{bar.size()}".el[Int]
expression(session).failed shouldBe ElMessages.undefinedSessionAttribute("bar").message
}
it should "return correct size for a non empty Array" in {
val session = newSession(Map("arr" -> Array(1, 2)))
val expression = "#{arr.size()}".el[Int]
expression(session).succeeded shouldBe 2
}
it should "return correct size for a non empty JList" in {
val lst = new java.util.LinkedList[Int]
lst.add(1)
lst.add(2)
val session = newSession(Map("lst" -> lst))
val expression = "#{lst.size()}".el[Int]
expression(session).succeeded shouldBe 2
}
it should "return correct size for a non empty map" in {
val session = newSession(Map("map" -> Map("key1" -> "val1", "key2" -> "val2")))
val expression = "#{map.size()}".el[Int]
expression(session).succeeded shouldBe 2
}
it should "return correct size for a non empty JSet" in {
val set = new java.util.HashSet[Int]
set.add(1)
set.add(2)
val session = newSession(Map("set" -> set))
val expression = "#{set.size()}".el[Int]
expression(session).succeeded shouldBe 2
}
it should "return correct size for a non empty JMap" in {
val map = new ju.HashMap[Int, Int]
map.put(1, 1)
map.put(2, 2)
val session = newSession(Map("map" -> map))
val expression = "#{map.size()}".el[Int]
expression(session).succeeded shouldBe 2
}
it should "handle gracefully unsupported type" in {
val session = newSession(Map("i" -> 10))
val expression = "#{i.size()}".el[Int]
expression(session).failed shouldBe ElMessages.sizeNotSupported(10, "i").message
}
"'random' function in Expression" should "return one of elements of List" in {
val elements = List("BAR1", "BAR2")
val session = newSession(Map("bar" -> elements))
val expression = "#{bar.random()}".el[String]
expression(session).succeeded should (be("BAR1") or be("BAR2"))
}
it should "return one of elements of JList" in {
val list = new ju.ArrayList[Int]
list.add(1)
list.add(2)
val session = newSession(Map("lst" -> list))
val expression = "#{lst.random()}".el[Int]
expression(session).succeeded should (be(1) or be(2))
}
it should "return one of elements of Array" in {
val session = newSession(Map("arr" -> Array(1, 2)))
val expression = "#{arr.random()}".el[Int]
expression(session).succeeded should (be(1) or be(2))
}
it should "handle unsupported type" in {
val session = newSession(Map("i" -> 10))
val expression = "#{i.random()}".el[Int]
expression(session).failed shouldBe el.ElMessages.randomNotSupported(10, "i").message
}
"'exists' function in Expression" should "validate that a value is in the session" in {
val session = newSession(Map("key1" -> "val1"))
val expression = "#{key1.exists()}".el[Boolean]
expression(session).succeeded shouldBe true
}
it should "validate that a value is not in the session" in {
val session = newSession(Map.empty)
val expression = "#{key1.exists()}".el[Boolean]
expression(session).succeeded shouldBe false
}
"access map in Expression" should "return value by key" in {
val map = Map("key1" -> "val1", "key2" -> "val2")
val session = newSession(Map("map" -> map))
val expression = "#{map.key1}".el[String]
expression(session).succeeded shouldBe "val1"
}
it should "return value by key from JMap" in {
val map = new ju.HashMap[String, Int]
map.put("key1", 1)
map.put("key2", 2)
val session = newSession(Map("map" -> map))
val expression = "#{map.key1}".el[Int]
expression(session).succeeded shouldBe 1
}
it should "handle missing map correctly" in {
val session = newSession(Map.empty)
val expression = "#{map.key1}".el[String]
expression(session).failed shouldBe ElMessages.undefinedSessionAttribute("map").message
}
it should "handle nested map access" in {
val session = newSession(Map("map" -> Map("key" -> Map("subKey" -> "val"))))
val expression = "#{map.key.subKey}".el[String]
expression(session).succeeded shouldBe "val"
}
it should "handle missing value correctly" in {
val map = Map("key" -> "val")
val session = newSession(Map("map" -> map))
val expression = "#{map.nonexisting}".el[String]
expression(session).failed shouldBe ElMessages.undefinedMapKey("map", "nonexisting").message
}
it should "handle missing value in JMap correctly" in {
val map = new ju.HashMap[String, Int]
map.put("key1", 1)
val session = newSession(Map("map" -> map))
val expression = "#{map.nonexisting}".el[Int]
expression(session).failed shouldBe ElMessages.undefinedMapKey("map", "nonexisting").message
}
it should "handle wrong type correctly" in {
val session = newSession(Map("i" -> 1))
val expression = "#{i.key}".el[Int]
expression(session).failed shouldBe ElMessages.accessByKeyNotSupported(1, "i").message
}
it should "support case classes String attribute" in {
val session = newSession(Map("foo" -> Foo("hello")))
val expression = "#{foo.bar}".el[String]
expression(session).succeeded shouldBe "hello"
}
it should "extract case classes attributes with primitive types" in {
val session = emptySession.set("foo", Primitives(boolean = true, 1, 1, 1, 1L, 1.1.toFloat, 1.1, 'a'))
"#{foo.boolean}".el[Boolean].apply(session).succeeded shouldBe true
"#{foo.byte}".el[Byte].apply(session).succeeded shouldBe 1
"#{foo.short}".el[Short].apply(session).succeeded shouldBe 1
"#{foo.int}".el[Int].apply(session).succeeded shouldBe 1
"#{foo.long}".el[Long].apply(session).succeeded shouldBe 1L
"#{foo.float}".el[Float].apply(session).succeeded shouldBe 1.1.toFloat
"#{foo.double}".el[Double].apply(session).succeeded shouldBe 1.1
"#{foo.char}".el[Char].apply(session).succeeded shouldBe 'a'
}
it should "support POJO String attributes" in {
val pojo = new MyPojo
pojo.setMyString("hello")
val session = newSession(Map("foo" -> pojo))
val expression = "#{foo.myString}".el[String]
expression(session).succeeded shouldBe "hello"
}
it should "support POJO boolean attributes" in {
val pojo = new MyPojo
pojo.setMyBoolean(true)
val session = newSession(Map("foo" -> pojo))
val expression = "#{foo.myBoolean}".el[Boolean]
expression(session).succeeded shouldBe true
}
it should "fail on POJO null attributes" in {
val pojo = new MyPojo
val session = newSession(Map("foo" -> pojo))
val expression = "#{foo.myString}".el[String]
expression(session).failed shouldBe "Value is null"
}
it should "handle wrong key" in {
val session = newSession(Map("foo" -> Foo("hello")))
val expression = "#{foo.qix}".el[String]
expression(session).failed shouldBe ElMessages.undefinedMapKey("foo", "qix").message
}
it should "handle deeply nested case classes" in {
val session = newSession(Map("bar" -> Bar(Baz("fux"))))
val expression = "#{bar.baz.qix}".el[String]
expression(session).succeeded shouldBe "fux"
}
"multiple level access" should "return an element of a sub-list" in {
val lst = List(List(1, 2), List(3, 4))
val session = newSession(Map("lst" -> lst))
val expression = "#{lst(0)(0)}".el[Int]
expression(session).succeeded shouldBe 1
}
it should "return key of a map in a list" in {
val lst = List(Map("key" -> "val"))
val session = newSession(Map("lst" -> lst))
val expression = "#{lst(0).key}".el[String]
expression(session).succeeded shouldBe "val"
}
it should "return an element of a list from a map" in {
val map = Map("lst" -> List(1, 2))
val session = newSession(Map("map" -> map))
val expression = "#{map.lst(0)}".el[Int]
expression(session).succeeded shouldBe 1
}
it should "return a value from a sub-map" in {
val map = Map("subMap" -> Map("key" -> "val"))
val session = newSession(Map("map" -> map))
val expression = "#{map.subMap.key}".el[String]
expression(session).succeeded shouldBe "val"
}
it should "return a value from a random list" in {
val lst = List(List(1, 2), List(3, 4))
val session = newSession(Map("lst" -> lst))
val expression = "#{lst.random()(0)}".el[Int]
expression(session).succeeded should (be(1) or be(3))
}
it should "return size of a sub-list" in {
val lst = List(List(1, 2), List(3, 4, 5))
val session = newSession(Map("lst" -> lst))
val expression = "#{lst(1).size()}".el[Int]
expression(session).succeeded shouldBe 3
}
it should "name of a value is correct" in {
val lst = List(Map("key" -> "value"))
val session = newSession(Map("lst" -> lst))
val expression = "#{lst(0).key.nonexisting}".el[Int]
expression(session).failed shouldBe ElMessages.accessByKeyNotSupported("value", "lst(0).key").message
}
"tuples access" should "return size of Tuple2" in {
val session = newSession(Map("tuple" -> Tuple2(1, 1)))
val expression = "#{tuple.size()}".el[Int]
expression(session).succeeded shouldBe 2
}
it should "return size of Tuple3" in {
val session = newSession(Map("tuple" -> Tuple3(1, 1, 1)))
val expression = "#{tuple.size()}".el[Int]
expression(session).succeeded shouldBe 3
}
it should "return first element of a Tuple" in {
val session = newSession(Map("tuple" -> Tuple3(1, 2, 3)))
val expression = "#{tuple._1}".el[Int]
expression(session).succeeded shouldBe 1
}
it should "return last element of a Tuple" in {
val session = newSession(Map("tuple" -> Tuple3(1, 2, 3)))
val expression = "#{tuple._3}".el[Int]
expression(session).succeeded shouldBe 3
}
it should "return a random element of a Tuple" in {
val session = newSession(Map("tuple" -> Tuple3(1, 2, 3)))
val expression = "#{tuple.random()}".el[Int]
expression(session).succeeded should (be(1) or be(2) or be(3))
}
it should "return element with zero index in a Tuple" in {
val tuple = Tuple3(1, 2, 3)
val session = newSession(Map("tuple" -> tuple))
val expression = "#{tuple._0}".el[Int]
expression(session).failed shouldBe ElMessages.outOfRangeAccess("tuple", tuple, 0).message
}
it should "return element of range" in {
val tuple = Tuple3(1, 2, 3)
val session = newSession(Map("tuple" -> tuple))
val expression = "#{tuple._4}".el[Int]
expression(session).failed shouldBe ElMessages.outOfRangeAccess("tuple", tuple, 4).message
}
it should "handle correctly if object do not support tuple access" in {
val int = 5
val session = newSession(Map("i" -> int))
val expression = "#{i._3}".el[Int]
expression(session).failed shouldBe ElMessages.tupleAccessNotSupported("i", int).message
}
"pairs access" should "return size of a Pair" in {
val session = newSession(Map("pair" -> (1 -> 2)))
val expression = "#{pair.size()}".el[Int]
expression(session).succeeded shouldBe 2
}
it should "return first element of a pair" in {
val session = newSession(Map("pair" -> (1 -> 2)))
val expression = "#{pair._1}".el[Int]
expression(session).succeeded shouldBe 1
}
it should "return second element of a pair" in {
val session = newSession(Map("pair" -> (1 -> 2)))
val expression = "#{pair._2}".el[Int]
expression(session).succeeded shouldBe 2
}
it should "return random element of a pair" in {
val session = newSession(Map("pair" -> (1 -> 2)))
val expression = "#{pair.random()}".el[Int]
expression(session).succeeded should (be(1) or be(2))
}
it should "return zero element of a pair" in {
val pair = 1 -> 2
val session = newSession(Map("pair" -> pair))
val expression = "#{pair._0}".el[Int]
expression(session).failed shouldBe ElMessages.outOfRangeAccess("pair", pair, 0).message
}
it should "return out of range element of a pair" in {
val pair = 1 -> 2
val session = newSession(Map("pair" -> pair))
val expression = "#{pair._3}".el[Int]
expression(session).failed shouldBe ElMessages.outOfRangeAccess("pair", pair, 3).message
}
"Malformed Expression" should "be handled correctly when an attribute name is missing" in {
a[ElParserException] should be thrownBy "foo#{}bar".el[String]
}
it should "be handled when #{ is not closed" in {
a[ElParserException] should be thrownBy "#{foo".el[String]
}
"'isUndefined' function in Expression" should "validate that a value is not in the session" in {
val session = newSession(Map.empty)
val expression = "#{key1.isUndefined()}".el[Boolean]
expression(session).succeeded shouldBe true
}
it should "validate that a value is in the session" in {
val session = newSession(Map("key1" -> "value1"))
val expression = "#{key1.isUndefined()}".el[Boolean]
expression(session).succeeded shouldBe false
}
it should "be handled correctly when there is a nested attribute definition with string before nested attribute" in {
a[ElParserException] should be thrownBy "#{foo#{bar}}".el[String]
}
it should "be handled correctly when there is a nested attribute definition with string after nested attribute" in {
a[ElParserException] should be thrownBy "#{#{bar}foo}".el[String]
}
it should "be handled correctly when there is a nested attribute definition with string before and after nested attribute" in {
a[ElParserException] should be thrownBy "#{foo#{bar}foo}".el[String]
}
it should "be handled correctly when there is a nested attribute definition" in {
a[ElParserException] should be thrownBy "#{#{bar}}".el[String]
}
it should "be handled correctly when there are several nested attributes" in {
a[ElParserException] should be thrownBy "#{#{bar}#{bar}}".el[String]
}
"jsonStringify" should "support String value" in {
val session = newSession(Map("value" -> "VALUE"))
val expression = """"name": #{value.jsonStringify()}""".el[String]
expression(session).succeeded shouldBe """"name": "VALUE""""
}
it should "support number value" in {
val session = newSession(Map("value" -> 5.0))
val expression = """"name": #{value.jsonStringify()}""".el[String]
expression(session).succeeded shouldBe """"name": 5.0"""
}
it should "support null value" in {
val session = newSession(Map("value" -> null))
val expression = """"name": #{value.jsonStringify()}""".el[String]
expression(session).succeeded shouldBe """"name": null"""
}
it should "support arrays" in {
val session = newSession(Map("value" -> Array("a", 1)))
val expression = "#{value.jsonStringify()}".el[String]
expression(session).succeeded shouldBe """["a",1]"""
}
it should "support maps/objects" in {
val session = newSession(Map("value" -> Map("foo" -> "bar", "baz" -> 1)))
val expression = "#{value.jsonStringify()}".el[String]
expression(session).succeeded shouldBe """{"foo":"bar","baz":1}"""
}
it should "support case classes" in {
val session = newSession(Map("bar" -> Bar(Baz("fux"))))
val expression = "#{bar.jsonStringify()}".el[String]
expression(session).succeeded shouldBe """{"baz":{"qix":"fux"}}"""
}
it should "support POJOs" in {
val pojo = new MyPojo
pojo.setMyString("hello")
val session = newSession(Map("pojo" -> pojo))
val expression = "#{pojo.jsonStringify()}".el[String]
expression(session).succeeded shouldBe """{"myString":"hello","myBoolean":false}"""
}
it should "support key access" in {
val json = Map("bar" -> Map("baz" -> "qix"))
val session = newSession(Map("foo" -> json))
val expression = "#{foo.bar.jsonStringify()}".el[String]
expression(session).succeeded shouldBe """{"baz":"qix"}"""
}
it should "return the original failure when failing" in {
val session = newSession(Map("foo" -> "bar"))
val failedKeyAccessExpression = "#{foo.bar}".el[String]
val failedJsonStringifyExpression = "#{foo.bar.jsonStringify()}".el[String]
failedJsonStringifyExpression(session).failed shouldBe failedKeyAccessExpression(session).failed
}
"currentTimeMillis" should "generate a long" in {
val session = newSession(Map("foo" -> "bar"))
val currentTimeMillisExpression = "#{currentTimeMillis()}".el[Long]
currentTimeMillisExpression(session).succeeded shouldBe a[Long]
}
"currentDate" should "generate a String" in {
val currentDateExpression = "#{currentDate(yyyy-MM-dd HH:mm:ss)}".el[String]
currentDateExpression(emptySession).succeeded.length shouldBe 19
}
it should "support patterns with a dot" in {
val currentDateExpression = """#{currentDate("YYYY-MM-dd'T'HH:mm:ss.SSSMs'Z'")}""".el[String]
currentDateExpression(emptySession) shouldBe a[Success[_]]
}
"htmlUnescape" should "escape HTML entities" in {
val expression = "#{foo.htmlUnescape()}".el[String]
val session = newSession(Map("foo" -> "foo é bar"))
expression(session).succeeded shouldBe "foo é bar"
}
"Escaping" should "turn \\#{ into #{" in {
val session = newSession(Map("foo" -> "FOO"))
val expression = "\\#{foo}".el[String]
expression(session).succeeded shouldBe "#{foo}"
}
"randomUuid" should "generate uuid" in {
val randomUuid = "#{randomUuid()}".el[String]
UUID.fromString(randomUuid(emptySession).succeeded)
}
"randomSecureUuid" should "generate uuid" in {
val randomSecureUuid = "#{randomSecureUuid()}".el[String]
UUID.fromString(randomSecureUuid(emptySession).succeeded)
}
"randomInt" should "generate random Int full range" in {
val randomInt = "#{randomInt()}".el[Int]
randomInt(emptySession).succeeded should (be >= Int.MinValue and be <= Int.MaxValue)
}
"randomIntRange" should "generate random Int within range (left inclusive and right exclusive)" in {
val randomInt = "#{randomInt(0,10)}".el[Int]
val actual = Set.fill(1000)(randomInt(emptySession).succeeded)
val expected = (0 until 10).toSet
actual should contain theSameElementsAs expected
}
"randomIntRange" should "generate random Int with negative numbers" in {
val randomInt = "#{randomInt(-10,-5)}".el[Int]
randomInt(emptySession).succeeded should (be >= -10 and be < -5)
}
"randomIntRange" should "throw exception with 'max' less than 'min'" in {
an[ElParserException] should be thrownBy "#{randomInt(20,1)}".el[Int]
}
"randomLong" should "generate random Long with full range" in {
val randomLong = "#{randomLong()}".el[Long]
randomLong(emptySession).succeeded should (be >= Long.MinValue and be <= Long.MaxValue)
}
"randomLongRange" should "generate random Long with range (left inclusive and right exclusive)" in {
val randomLong = "#{randomLong(2147483648,2147483658)}".el[Long]
val actual = Set.fill(1000)(randomLong(emptySession).succeeded)
val expected = (Int.MaxValue + 1L until Int.MaxValue + 11L).toSet
actual should contain theSameElementsAs expected
}
"randomLongRange" should "generate random Long with negative numbers" in {
val randomInt = "#{randomLong(-2147483658,-2147483648)}".el[Long]
randomInt(emptySession).succeeded should (be >= -2147483658L and be < -2147483648L)
}
"randomLongRange" should "throw exception with 'max' less than 'min'" in {
an[ElParserException] should be thrownBy "#{randomLong(2147483658,2147483648)}".el[Long]
}
"randomDoubleRange" should "throw an ELParse exception when first parameter is digit.digit.digit" in {
a[ElParserException] should be thrownBy "#{randomDouble(0.42.1,42.42)}".el[Double]
}
it should "throw an ELParse exception when second parameter is not digit.digit.digit" in {
a[ElParserException] should be thrownBy "#{randomDouble(0.42,42.42.42)}".el[Double]
}
it should "throw an ELParse exception when parameter has .digit" in {
a[ElParserException] should be thrownBy "#{randomDouble(.42,1.42)}".el[Double]
}
it should "throw an ELParse exception when parameter has digit." in {
a[ElParserException] should be thrownBy "#{randomDouble(0.42,1.)}".el[Double]
}
it should "throw an ELParse exception when second parameter is a string" in {
a[ElParserException] should be thrownBy "#{randomDouble(0.42,a)}".el[Double]
}
it should "throw an ELParse exception when first parameter is a character" in {
a[ElParserException] should be thrownBy "#{randomDouble(abc,42.42)}".el[Double]
}
it should "generate random Double" in {
val randomDouble = "#{randomDouble(-0.42,42.42)}".el[Double]
all(List.fill(420)(randomDouble(emptySession).succeeded)) should (be >= -.42 and be < 42.42)
}
"randomDoubleRangeDigits" should "generate random Double with limited fractional digits" in {
val randomDouble = "#{randomDouble(1111.11,2222.22,4)}".el[Double]
val foo = List.fill(420)(randomDouble(emptySession).succeeded)
all(foo.map(s => s.toString.split("\\.")(1).length)) should be <= 9
}
it should "generate random Double with 0 fractional digits sets to 1 fractional digit with digits.0" in {
val randomDouble = "#{randomDouble(-3.14,3.14,0)}".el[Double]
all(List.fill(420)(randomDouble(emptySession).succeeded.toString.split("\\.")(1)).map(s => (s.length, s))) should be <= (1, "0")
}
it should "throw exception when max <= min" in {
a[ElParserException] should be thrownBy "#{randomDouble(42.42,42.42)}".el[Double]
}
"randomDoubleRangeDigits" should "throw exception num of digits is less than 0" in {
a[ElParserException] should be thrownBy "#{randomDouble(4.242,42.42,-42)}".el[Double]
}
it should "throw exception when max <= min given a valid fractionalDigits" in {
a[ElParserException] should be thrownBy "#{randomDouble(42.42,42.42,42)}".el[Double]
}
"randomAlphanumeric" should "generate random alphanum by length" in {
val randomAlphanumeric = "#{randomAlphanumeric(10)}".el[String]
randomAlphanumeric(emptySession).succeeded.length shouldBe 10
}
}