/
CheckBuilder.scala
281 lines (242 loc) · 12.1 KB
/
CheckBuilder.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
/*
* 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.check
import java.util.concurrent.ThreadLocalRandom
import io.gatling.commons.util.{ Arrays, Equality }
import io.gatling.commons.validation._
import io.gatling.core.session._
trait CheckBuilder[T, P] {
def build[C <: Check[R], R](materializer: CheckMaterializer[T, C, R, P]): C
}
object CheckBuilder {
// T: Check type, only used for CheckMaterializer Type Class
// P: Prepared
// X: Extracted
trait Find[T, P, X] {
def find: Validate[T, P, X]
}
object Find {
class Default[T, P, X](extractor: Expression[Extractor[P, X]], displayActualValue: Boolean) extends Find[T, P, X] {
override def find: Validate[T, P, X] = Validate.Default(extractor, displayActualValue)
}
}
trait MultipleFind[T, P, X] extends Find[T, P, X] {
override def find: Validate[T, P, X]
def find(occurrence: Int): Validate[T, P, X]
def findAll: Validate[T, P, Seq[X]]
def findRandom: Validate[T, P, X]
@SuppressWarnings(Array("org.wartremover.warts.DefaultArguments"))
def findRandom(num: Int, failIfLess: Boolean = false): Validate[T, P, Seq[X]]
def count: Validate[T, P, Int]
}
object MultipleFind {
abstract class Default[T, P, X](displayActualValue: Boolean) extends MultipleFind[T, P, X] {
protected def findExtractor(occurrence: Int): Expression[Extractor[P, X]]
protected def findAllExtractor: Expression[Extractor[P, Seq[X]]]
private def findRandomExtractor: Expression[Extractor[P, X]] = findAllExtractor.map { fae =>
new Extractor[P, X] {
override def name: String = fae.name
override def arity: String = "findRandom"
@SuppressWarnings(Array("org.wartremover.warts.SeqApply"))
override def apply(prepared: P): Validation[Option[X]] =
fae(prepared)
.map(_.collect { case seq if seq.nonEmpty => seq(ThreadLocalRandom.current.nextInt(seq.size)) })
}
}
private def findManyRandomExtractor(num: Int, failIfLess: Boolean): Expression[Extractor[P, Seq[X]]] = findAllExtractor.map { fae =>
new Extractor[P, Seq[X]] {
override def name: String = fae.name
override def arity: String = s"findRandom($num, $failIfLess)"
@SuppressWarnings(Array("org.wartremover.warts.SeqApply"))
override def apply(prepared: P): Validation[Option[Seq[X]]] =
fae(prepared)
.flatMap {
case Some(seq) =>
if (failIfLess && seq.sizeIs < num) {
s"Failed to collect $num matches".failure
} else if (seq.isEmpty) {
Validation.NoneSuccess
} else {
val randomSeq =
if (seq.sizeIs <= num) {
seq
} else {
val shuffledIndices = Arrays.shuffle(seq.indices.toArray)
for (i <- 0 until num) yield seq(shuffledIndices(i))
}
Some(randomSeq).success
}
case _ => Validation.NoneSuccess
}
}
}
protected def countExtractor: Expression[Extractor[P, Int]]
override def find: Validate[T, P, X] = find(0)
override def find(occurrence: Int): Validate[T, P, X] = Validate.Default(findExtractor(occurrence), displayActualValue)
override def findAll: Validate[T, P, Seq[X]] = Validate.Default(findAllExtractor, displayActualValue)
override def findRandom: Validate[T, P, X] = Validate.Default(findRandomExtractor, displayActualValue)
override def findRandom(num: Int, failIfLess: Boolean): Validate[T, P, Seq[X]] =
Validate.Default(findManyRandomExtractor(num, failIfLess), displayActualValue)
override def count: Validate[T, P, Int] = Validate.Default(countExtractor, displayActualValue)
}
}
trait Validate[T, P, X] {
private[gatling] def transform0[X2](
extractedF: Validation[Option[X]] => Validation[Option[X2]],
arityF: String => String
): Validate[T, P, X2]
def transform[X2](transformation: X => X2): Validate[T, P, X2]
def transformWithSession[X2](transformation: (X, Session) => X2): Validate[T, P, X2]
def transformOption[X2](transformation: Option[X] => Validation[Option[X2]]): Validate[T, P, X2]
def transformOptionWithSession[X2](transformation: (Option[X], Session) => Validation[Option[X2]]): Validate[T, P, X2]
def withDefault(defaultValue: Expression[X]): Validate[T, P, X]
def validate(validator: Expression[Validator[X]]): Final[T, P]
def validate(opName: String, validator: (Option[X], Session) => Validation[Option[X]]): Final[T, P]
def is(expected: Expression[X])(implicit equality: Equality[X]): Final[T, P]
def isNull: Final[T, P]
def not(expected: Expression[X])(implicit equality: Equality[X]): Final[T, P]
def notNull: Final[T, P]
def in(expected: X*): Final[T, P]
def in(expected: Expression[Seq[X]]): Final[T, P]
def exists: Final[T, P]
def notExists: Final[T, P]
def optional: Final[T, P]
def lt(expected: Expression[X])(implicit ordering: Ordering[X]): Final[T, P]
def lte(expected: Expression[X])(implicit ordering: Ordering[X]): Final[T, P]
def gt(expected: Expression[X])(implicit ordering: Ordering[X]): Final[T, P]
def gte(expected: Expression[X])(implicit ordering: Ordering[X]): Final[T, P]
}
object Validate {
private val TransformErrorMapper: String => String = "transform crashed: " + _
private val TransformOptionErrorMapper: String => String = "transformOption crashed: " + _
private val TransformArity: String => String = _ + ".transform"
private val TransformOptionArity: String => String = _ + ".transformOption"
final case class Default[T, P, X](extractor: Expression[Extractor[P, X]], displayActualValue: Boolean) extends Validate[T, P, X] {
private def mapExtractor[X2](
extractedF: Validation[Option[X]] => Validation[Option[X2]],
arityF: String => String
)(extractor: Extractor[P, X]) =
new Extractor[P, X2] {
override def name: String = extractor.name
override def arity: String = arityF(extractor.arity)
override def apply(prepared: P): Validation[Option[X2]] = extractedF(extractor(prepared))
}
override private[gatling] def transform0[X2](
extractedF: Validation[Option[X]] => Validation[Option[X2]],
arityF: String => String
): Validate[T, P, X2] =
copy(extractor = extractor.map(mapExtractor(extractedF, arityF)))
private def transformWithSession0[X2](
extractedF: (Session, Validation[Option[X]]) => Validation[Option[X2]],
arityF: String => String
): Validate[T, P, X2] =
copy(extractor =
session =>
extractor(session).map(
mapExtractor(
extractedF(session, _),
arityF
)
)
)
override def transform[X2](transformation: X => X2): Validate[T, P, X2] =
transform0(
extracted =>
safely(TransformErrorMapper) {
extracted.map(_.map(transformation))
},
TransformArity
)
override def transformWithSession[X2](transformation: (X, Session) => X2): Validate[T, P, X2] =
transformWithSession0(
(session, extracted) =>
safely(TransformErrorMapper) {
extracted.map(_.map(transformation(_, session)))
},
TransformArity
)
override def transformOption[X2](transformation: Option[X] => Validation[Option[X2]]): Validate[T, P, X2] =
transform0(
extracted =>
safely(TransformOptionErrorMapper) {
extracted.flatMap(transformation)
},
TransformOptionArity
)
override def transformOptionWithSession[X2](transformation: (Option[X], Session) => Validation[Option[X2]]): Validate[T, P, X2] =
transformWithSession0(
(session, extracted) =>
safely(TransformOptionErrorMapper) {
extracted.flatMap(transformation(_, session))
},
TransformOptionArity
)
override def withDefault(defaultValue: Expression[X]): Validate[T, P, X] =
transformOptionWithSession((actual, session) =>
actual match {
case None => defaultValue(session).map(Option(_))
case _ => actual.success
}
)
override def validate(validator: Expression[Validator[X]]): Final[T, P] =
new Final.Default[T, P, X](this.extractor, validator, displayActualValue, None, None)
override def validate(opName: String, validator: (Option[X], Session) => Validation[Option[X]]): Final[T, P] =
validate(session =>
new Validator[X] {
override val name: String = opName
override def apply(actual: Option[X], displayActualValue: Boolean): Validation[Option[X]] = validator(actual, session)
}.success
)
override def is(expected: Expression[X])(implicit equality: Equality[X]): Final[T, P] =
validate(expected.map(new Matcher.Is(_, equality)))
override def isNull: Final[T, P] = validate(new Matcher.IsNull[X].expressionSuccess)
override def not(expected: Expression[X])(implicit equality: Equality[X]): Final[T, P] =
validate(expected.map(new Matcher.Not(_, equality)))
override def notNull: Final[T, P] = validate(new Matcher.NotNull[X].expressionSuccess)
override def in(expected: X*): Final[T, P] = validate(expected.expressionSuccess.map(new Matcher.In(_)))
override def in(expected: Expression[Seq[X]]): Final[T, P] = validate(expected.map(new Matcher.In(_)))
override def exists: Final[T, P] = validate(new Validator.Exists[X]().expressionSuccess)
override def notExists: Final[T, P] = validate(new Validator.NotExists[X]().expressionSuccess)
override def optional: Final[T, P] = validate(new Validator.Optional[X]().expressionSuccess)
override def lt(expected: Expression[X])(implicit ordering: Ordering[X]): Final[T, P] =
validate(expected.map(new Matcher.Compare("lessThan", "less than", ordering.lt, _)))
override def lte(expected: Expression[X])(implicit ordering: Ordering[X]): Final[T, P] =
validate(expected.map(new Matcher.Compare("lessThanOrEqual", "less than or equal to", ordering.lteq, _)))
override def gt(expected: Expression[X])(implicit ordering: Ordering[X]): Final[T, P] =
validate(expected.map(new Matcher.Compare("greaterThan", "greater than", ordering.gt, _)))
override def gte(expected: Expression[X])(implicit ordering: Ordering[X]): Final[T, P] =
validate(expected.map(new Matcher.Compare("greaterThanOrEqual", "greater than or equal to", ordering.gteq, _)))
}
}
trait Final[T, P] extends CheckBuilder[T, P] {
def name(n: String): Final[T, P]
def saveAs(key: String): Final[T, P]
}
object Final {
final case class Default[T, P, X](
extractor: Expression[Extractor[P, X]],
validator: Expression[Validator[X]],
displayActualValue: Boolean,
customName: Option[String],
saveAs: Option[String]
) extends Final[T, P] {
override def name(n: String): Final[T, P] = copy(customName = Some(n))
override def saveAs(key: String): Final[T, P] = copy(saveAs = Some(key))
override def build[C <: Check[R], R](materializer: CheckMaterializer[T, C, R, P]): C =
materializer.materialize(Check.Default(_, extractor, validator, displayActualValue, customName, None, saveAs))
}
}
}