From b1d2903ea8ca82f09b502bdde56c071805d221c2 Mon Sep 17 00:00:00 2001 From: John Axe Date: Wed, 11 Dec 2024 05:00:43 -0300 Subject: [PATCH] Add support for negative date ranges in feeders Refactored `RandomDateRangeFeeder` to handle negative offset dates, added `negativeDateRange` feeder example, and updated related utilities and tests. This improves flexibility in generating dynamic date ranges. --- .../performance/example/GatlingRunner.scala | 26 ++ .../performance/example/feeders/Feeders.scala | 13 +- .../example/scenarios/SampleScenario.scala | 1 + .../feeders/RandomDateRangeFeeder.scala | 27 +- .../gatling/utils/RandomDataGenerators.scala | 387 +++++++++--------- .../utils/RandomDataGeneratorsTest.scala | 18 +- 6 files changed, 263 insertions(+), 209 deletions(-) create mode 100644 example/src/test/scala/org/galaxio/performance/example/GatlingRunner.scala diff --git a/example/src/test/scala/org/galaxio/performance/example/GatlingRunner.scala b/example/src/test/scala/org/galaxio/performance/example/GatlingRunner.scala new file mode 100644 index 0000000..b0094f6 --- /dev/null +++ b/example/src/test/scala/org/galaxio/performance/example/GatlingRunner.scala @@ -0,0 +1,26 @@ +package org.galaxio.performance.example + +import io.gatling.app.Gatling +import io.gatling.shared.cli.GatlingCliOptions + +object GatlingRunner { + + def main(args: Array[String]): Unit = { + + // this is where you specify the class you want to run + val simulationClass = classOf[SampleSimulation].getName + + Gatling.main( + args ++ + Array( + GatlingCliOptions.Simulation.shortOption, + simulationClass, + GatlingCliOptions.ResultsFolder.shortOption, + "results", + GatlingCliOptions.Launcher.shortOption, + "sbt", + ), + ) + } + +} diff --git a/example/src/test/scala/org/galaxio/performance/example/feeders/Feeders.scala b/example/src/test/scala/org/galaxio/performance/example/feeders/Feeders.scala index 7b0d9f4..4222f27 100644 --- a/example/src/test/scala/org/galaxio/performance/example/feeders/Feeders.scala +++ b/example/src/test/scala/org/galaxio/performance/example/feeders/Feeders.scala @@ -8,7 +8,7 @@ import org.galaxio.gatling.utils.phone._ import java.time.format.DateTimeFormatter import java.time.temporal.ChronoUnit -import java.time.{LocalDateTime, ZoneId} +import java.time.{LocalDateTime, ZoneId, ZoneOffset, ZonedDateTime} object Feeders { @@ -42,6 +42,17 @@ object Feeders { val vacationDate: Feeder[String] = RandomDateRangeFeeder("startOfVacation", "endOfVacation", 14, "yyyy-MM-dd", LocalDateTime.now(), ChronoUnit.DAYS) + val negativeDateRange: Feeder[String] = { + RandomDateRangeFeeder( + "from", + "to", + -1, + "YYYY-MM-dd", + ZonedDateTime.now(ZoneOffset.UTC).toLocalDateTime, + ChronoUnit.MONTHS, + ) + } + // random Int val randomDigit: Feeder[Int] = RandomDigitFeeder("randomDigit") val randomRangeInt: Feeder[Int] = CustomFeeder[Int]("randomRangeInt", RandomDataGenerators.randomValue(1, 50)) diff --git a/example/src/test/scala/org/galaxio/performance/example/scenarios/SampleScenario.scala b/example/src/test/scala/org/galaxio/performance/example/scenarios/SampleScenario.scala index 4aa4b75..56f1301 100644 --- a/example/src/test/scala/org/galaxio/performance/example/scenarios/SampleScenario.scala +++ b/example/src/test/scala/org/galaxio/performance/example/scenarios/SampleScenario.scala @@ -45,6 +45,7 @@ class SampleScenario { .feed(randomDigit) .feed(randomPhone) .feed(randomRangeString) + .feed(negativeDateRange) .feed(randomString) .feed(randomUsaPhone) .feed(randomUuid) diff --git a/src/main/scala/org/galaxio/gatling/feeders/RandomDateRangeFeeder.scala b/src/main/scala/org/galaxio/gatling/feeders/RandomDateRangeFeeder.scala index 701dd77..7a7431e 100644 --- a/src/main/scala/org/galaxio/gatling/feeders/RandomDateRangeFeeder.scala +++ b/src/main/scala/org/galaxio/gatling/feeders/RandomDateRangeFeeder.scala @@ -6,18 +6,25 @@ import java.time.temporal.{ChronoUnit, TemporalUnit} import io.gatling.core.feeder.Feeder import org.galaxio.gatling.utils.RandomDataGenerators + object RandomDateRangeFeeder { def apply( - paramNameFrom: String, - paramNameTo: String, - offsetDate: Long, - datePattern: String = "yyyy-MM-dd", - dateFrom: LocalDateTime = LocalDateTime.now(), - unit: TemporalUnit = ChronoUnit.DAYS, - timezone: ZoneId = ZoneId.systemDefault(), - ): Feeder[String] = - feeder[String](paramNameFrom)(dateFrom.format(DateTimeFormatter.ofPattern(datePattern))) - .map(m => m + (paramNameTo -> RandomDataGenerators.randomDate(offsetDate, datePattern, dateFrom, unit, timezone))) + paramNameFrom: String, + paramNameTo: String, + offsetDate: Long, + datePattern: String = "yyyy-MM-dd", + dateFrom: LocalDateTime = LocalDateTime.now(), + unit: TemporalUnit = ChronoUnit.DAYS, + timezone: ZoneId = ZoneId.systemDefault(), + ): Feeder[String] = { + val dateFormatter = DateTimeFormatter.ofPattern(datePattern) + + def addRandomDateToMap(originalMap: Map[String, String]): Map[String, String] = + originalMap + (paramNameTo -> RandomDataGenerators.randomDate(offsetDate, datePattern, dateFrom, unit, timezone)) + + feeder[String](paramNameFrom)(dateFrom.format(dateFormatter)).map(addRandomDateToMap) + } + } diff --git a/src/main/scala/org/galaxio/gatling/utils/RandomDataGenerators.scala b/src/main/scala/org/galaxio/gatling/utils/RandomDataGenerators.scala index 166a5aa..426483b 100644 --- a/src/main/scala/org/galaxio/gatling/utils/RandomDataGenerators.scala +++ b/src/main/scala/org/galaxio/gatling/utils/RandomDataGenerators.scala @@ -11,8 +11,8 @@ import scala.util.Random object RandomDataGenerators { - private val Digits = "0123456789" - private val HexDigits = "0123456789abcdef" + private val Digits = "0123456789" + private val HexDigits = "0123456789abcdef" private val CyrillicLetters = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя" private def validateLength(stringLength: Int): Unit = @@ -24,20 +24,20 @@ object RandomDataGenerators { } private def validateRange[T](min: T, max: T)(implicit ordering: Ordering[T]): Unit = { - require(ordering.lt(min, max), s"Min value ($min) must be less than max value ($max)") + require(ordering.lteq(min, max), s"Min value ($min) must be less than max value ($max)") } /** Generates a random string of the specified length using the provided alphabet of characters. - * - * @param alphabet - * the set of characters to use when generating the random string; must be non-empty - * @param length - * the desired length of the random string; must be greater than 0 - * @return - * a random string of specified length composed of characters from the provided alphabet - * @throws IllegalArgumentException - * if `alphabet` is empty or `length` is less than or equal to 0 - */ + * + * @param alphabet + * the set of characters to use when generating the random string; must be non-empty + * @param length + * the desired length of the random string; must be greater than 0 + * @return + * a random string of specified length composed of characters from the provided alphabet + * @throws IllegalArgumentException + * if `alphabet` is empty or `length` is less than or equal to 0 + */ def randomString(alphabet: String)(length: Int): String = { require(alphabet.nonEmpty, "Alphabet must be non-empty") require(length > 0, s"String length must be > 0, but got $length") @@ -46,146 +46,149 @@ object RandomDataGenerators { } /** Generates a random string of digits with the specified length. - * - * @param length - * the desired length of the digit string; must be greater than 0 - * @return - * a random string consisting only of numeric digits of the specified length - * @throws IllegalArgumentException - * if `length` is less than or equal to 0 - */ + * + * @param length + * the desired length of the digit string; must be greater than 0 + * @return + * a random string consisting only of numeric digits of the specified length + * @throws IllegalArgumentException + * if `length` is less than or equal to 0 + */ def digitString(length: Int): String = randomStringFromAlphabet(Digits, length) /** Generates a random hexadecimal string of the specified length. - * - * @param length - * the desired length of the hexadecimal string; must be greater than 0 - * @return - * a randomly generated string of the specified length containing only hexadecimal characters (0-9, a-f) - * @throws IllegalArgumentException - * if the `length` is less than or equal to 0 - */ + * + * @param length + * the desired length of the hexadecimal string; must be greater than 0 + * @return + * a randomly generated string of the specified length containing only hexadecimal characters (0-9, a-f) + * @throws IllegalArgumentException + * if the `length` is less than or equal to 0 + */ def hexString(length: Int): String = randomStringFromAlphabet(HexDigits, length) /** Generates a random alphanumeric string of the specified length. - * - * @param length - * the desired length of the random string; must be greater than 0 - * @return - * a random string of the specified length composed of alphanumeric characters - * @throws IllegalArgumentException - * if `length` is less than or equal to 0 - */ + * + * @param length + * the desired length of the random string; must be greater than 0 + * @return + * a random string of the specified length composed of alphanumeric characters + * @throws IllegalArgumentException + * if `length` is less than or equal to 0 + */ def alphanumericString(length: Int): String = Random.alphanumeric.take(length).mkString /** Generates a random string of letters with the specified length. - * - * @param length - * the desired length of the string; must be greater than 0 - * @return - * a random string consisting only of alphabetic characters of the specified length - * @throws IllegalArgumentException - * if `length` is less than or equal to 0 - */ + * + * @param length + * the desired length of the string; must be greater than 0 + * @return + * a random string consisting only of alphabetic characters of the specified length + * @throws IllegalArgumentException + * if `length` is less than or equal to 0 + */ def lettersString(length: Int): String = { validateLength(length) Random.alphanumeric.filter(_.isLetter).take(length).mkString } /** Generates a random string of the specified length containing only Cyrillic characters. - * - * @param length - * the desired length of the generated Cyrillic string; must be greater than 0 - * @return - * a random string of the specified length composed of Cyrillic characters - * @throws IllegalArgumentException - * if `length` is less than or equal to 0 - */ + * + * @param length + * the desired length of the generated Cyrillic string; must be greater than 0 + * @return + * a random string of the specified length composed of Cyrillic characters + * @throws IllegalArgumentException + * if `length` is less than or equal to 0 + */ def cyrillicString(length: Int): String = randomStringFromAlphabet(CyrillicLetters, length) /** Generates a random UUID as a string. - * - * @return - * a randomly generated UUID in string format - */ + * + * @return + * a randomly generated UUID in string format + */ def randomUUID: String = FastUUID.toString(UUID.randomUUID) /** Generates a random value of type `T` using the provided implicit `RandomProvider`. - * - * @tparam T - * the type of the random value to generate - * @param rng - * the implicit `RandomProvider` instance used to generate the random value - * @return - * a random value of type `T` - */ + * + * @tparam T + * the type of the random value to generate + * @param rng + * the implicit `RandomProvider` instance used to generate the random value + * @return + * a random value of type `T` + */ def randomValue[T]()(implicit rng: RandomProvider[T]): T = rng.random() /** Generates a random value of type `T` within the specified upper bound. - * - * @param max - * the maximum value for the random generation - * @param rng - * an implicit `RandomProvider` instance used to generate the random value - * @return - * a randomly generated value of type `T` that is less than or equal to `max` - */ + * + * @param max + * the maximum value for the random generation + * @param rng + * an implicit `RandomProvider` instance used to generate the random value + * @return + * a randomly generated value of type `T` that is less than or equal to `max` + */ def randomValue[T](max: T)(implicit rng: RandomProvider[T]): T = rng.random(max) /** Generates a random value of a specified type within a given range. - * - * @param min - * the minimum value of the range (inclusive) - * @param max - * the maximum value of the range (inclusive) - * @param rng - * an implicit RandomProvider to generate random values of type T - * @param ord - * an implicit Ordering to compare the minimum and maximum values - * @return - * a randomly generated value of type T within the specified range - * @throws IllegalArgumentException - * if the minimum value is not less than the maximum value - */ + * + * @param min + * the minimum value of the range (inclusive) + * @param max + * the maximum value of the range (inclusive) + * @param rng + * an implicit RandomProvider to generate random values of type T + * @param ord + * an implicit Ordering to compare the minimum and maximum values + * @return + * a randomly generated value of type T within the specified range + * @throws IllegalArgumentException + * if the minimum value is not less than the maximum value + */ def randomValue[T](min: T, max: T)(implicit rng: RandomProvider[T], ord: Ordering[T]): T = { validateRange(min, max) - rng.random(min, max) + if (min != max) + rng.random(min, max) + else + max } /** Selects a random element from the provided list of integers. If the list is empty, a random integer of the specified - * length is generated and returned. - * - * @param items - * a list of integers to select a random element from; can be empty - * @param intLength - * the number of digits for generating a random integer if the list is empty - * @return - * a random integer from the list if it is non-empty, or a randomly generated integer of the specified length if the list - * is empty - */ + * length is generated and returned. + * + * @param items + * a list of integers to select a random element from; can be empty + * @param intLength + * the number of digits for generating a random integer if the list is empty + * @return + * a random integer from the list if it is non-empty, or a randomly generated integer of the specified length if the list + * is empty + */ private def getRandomElement(items: List[Int], intLength: Int): Int = items match { case Nil => randomValue(intLength) - case _ => items(randomValue(items.length)) + case _ => items(randomValue(items.length)) } /** Selects a random element from the given list of strings or generates a random string if the list is empty. - * - * @param items - * the list of strings to select a random element from; can be empty - * @param stringLength - * the length of the random string to generate if the list is empty; must be greater than 0 - * @return - * a randomly selected string from the list or a randomly generated string of the specified length - * @throws IllegalArgumentException - * if `stringLength` is less than or equal to 0 - */ + * + * @param items + * the list of strings to select a random element from; can be empty + * @param stringLength + * the length of the random string to generate if the list is empty; must be greater than 0 + * @return + * a randomly selected string from the list or a randomly generated string of the specified length + * @throws IllegalArgumentException + * if `stringLength` is less than or equal to 0 + */ private def getRandomElement(items: List[String], stringLength: Int): String = items match { case Nil => lettersString(stringLength) - case _ => items(randomValue(items.length)) + case _ => items(randomValue(items.length)) } def randomPAN(bins: String*): String = { @@ -193,26 +196,26 @@ object RandomDataGenerators { def fifteenDigits(bins: List[String]): List[Char] = bins match { case Nil => s"""${digitString(6)}$idNum""".toList - case _ => s"""${getRandomElement(bins, 6)}$idNum""".toList + case _ => s"""${getRandomElement(bins, 6)}$idNum""".toList } val results: List[Int] = fifteenDigits(bins.toList).flatMap(_.toString.toIntOption) - val evenPosSum: Int = results.indices.collect { + val evenPosSum: Int = results.indices.collect { case i if i % 2 == 0 => results(i) }.fold(0)((x, y) => x + (if (y * 2 > 9) y * 2 - 9 else y * 2)) - val oddPosSum: Int = results.indices.collect { case i if i % 2 != 0 => results(i) }.sum - val controlNum: Int = 10 - (if ((oddPosSum + evenPosSum) % 10 == 0) 10 else (oddPosSum + evenPosSum) % 10) + val oddPosSum: Int = results.indices.collect { case i if i % 2 != 0 => results(i) }.sum + val controlNum: Int = 10 - (if ((oddPosSum + evenPosSum) % 10 == 0) 10 else (oddPosSum + evenPosSum) % 10) s"""${results.mkString("")}$controlNum""" } def randomOGRN(): String = { - val indicatorOGRN: Int = getRandomElement(List(1, 5), 1) - val year: String = String.format("%02d", randomValue(2, 21)) + val indicatorOGRN: Int = getRandomElement(List(1, 5), 1) + val year: String = String.format("%02d", randomValue(2, 21)) val ruSubjectNum: String = String.format("%02d", randomValue(1, 90)) - val idNum: String = digitString(7) - val result: String = s"""$indicatorOGRN$year$ruSubjectNum$idNum""" - val rem: Long = result.toLong % 11 + val idNum: String = digitString(7) + val result: String = s"""$indicatorOGRN$year$ruSubjectNum$idNum""" + val rem: Long = result.toLong % 11 if (rem == 10) s"""${result}0""" @@ -222,11 +225,11 @@ object RandomDataGenerators { def randomPSRNSP(): String = { val indicatorPSRNSP: Int = 3 - val year: String = String.format("%02d", randomValue(2, 21)) + val year: String = String.format("%02d", randomValue(2, 21)) val ruSubjectNum: String = String.format("%02d", randomValue(1, 90)) - val idNum: String = digitString(9) - val result: String = s"""$indicatorPSRNSP$year$ruSubjectNum$idNum""" - val rem: Long = result.toLong % 13 % 10 + val idNum: String = digitString(9) + val result: String = s"""$indicatorPSRNSP$year$ruSubjectNum$idNum""" + val rem: Long = result.toLong % 13 % 10 if (rem == 10) s"""${result}0""" @@ -236,8 +239,8 @@ object RandomDataGenerators { def randomKPP(): String = { val revenueServiceCode: String = String.format("%04d", randomValue(1, 10000)) - val reasonForReg: String = String.format("%02d", randomValue(1, 100)) - val idNum: String = String.format("%03d", randomValue(1, 1000)) + val reasonForReg: String = String.format("%02d", randomValue(1, 100)) + val idNum: String = String.format("%03d", randomValue(1, 1000)) s"""$revenueServiceCode$reasonForReg$idNum""" } @@ -248,7 +251,7 @@ object RandomDataGenerators { def itnNatRecursion(n: Int, sum: Int, results: List[Int]): String = { val rnd: Int = results match { case 0 :: Nil => randomValue(1, 10) - case _ => randomValue(0, 10) + case _ => randomValue(0, 10) } val factors: List[Int] = List(2, 4, 10, 3, 5, 9, 4, 6, 8) @@ -268,8 +271,8 @@ object RandomDataGenerators { @tailrec def itnJurRecursion(n: Int, sum1: Int, sum2: Int, results: List[Int]): String = { - val rnd: Int = randomValue(0, 10) - val firstFactors: List[Int] = List(7, 2, 4, 10, 3, 5, 9, 4, 6, 8) + val rnd: Int = randomValue(0, 10) + val firstFactors: List[Int] = List(7, 2, 4, 10, 3, 5, 9, 4, 6, 8) val secondFactors: List[Int] = List(3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8) def checkSum1: Int = sum1 + rnd * firstFactors(11 - n) @@ -279,16 +282,16 @@ object RandomDataGenerators { n match { case 1 => (results :+ (if ((sum2 + results.last * secondFactors(11 - n)) % 11 == 10) 0 - else - (sum2 + results.last * secondFactors(11 - n)) % 11)).mkString("") + else + (sum2 + results.last * secondFactors(11 - n)) % 11)).mkString("") case 2 => itnJurRecursion( n - 1, checkSum1, checkSum2, results :+ rnd :+ (if (checkSum1 % 11 == 10) 0 - else - checkSum1 % 11), + else + checkSum1 % 11), ) case _ => itnJurRecursion(n - 1, checkSum1, checkSum2, results :+ rnd) } @@ -308,9 +311,9 @@ object RandomDataGenerators { @tailrec def defineCheckSum(checkSum: Int): String = checkSum match { case x if x >= 10 && x < 100 => checkSum.toString - case x if x < 10 => s"0$checkSum" - case 100 | 101 => "00" - case _ => defineCheckSum(checkSum % 101) + case x if x < 10 => s"0$checkSum" + case 100 | 101 => "00" + case _ => defineCheckSum(checkSum % 101) } n match { @@ -324,8 +327,8 @@ object RandomDataGenerators { def randomRusPassport(): String = { val ruSubjectNum: String = String.format("%02d", randomValue(1, 90)) - val year: String = String.format("%02d", randomValue(0, 21)) - val idNum: String = digitString(6) + val year: String = String.format("%02d", randomValue(0, 21)) + val idNum: String = digitString(6) s"""$ruSubjectNum$year$idNum""" } @@ -338,32 +341,32 @@ object RandomDataGenerators { } /** Generates a random date string based on the provided parameters. - * - * @param positiveDelta - * the maximum positive offset in `unit` from the base date; must be non-negative - * @param negativeDelta - * the maximum negative offset in `unit` from the base date; must be non-negative - * @param datePattern - * the format pattern for the resulting date string - * @param dateFrom - * the base date and time from which the random date is calculated - * @param unit - * the temporal unit for the offset (e.g., days, hours, etc.) - * @param timezone - * the timezone used when formatting the resulting date - * @return - * a string representation of the randomly generated date formatted according to `datePattern` - * @throws IllegalArgumentException - * if `positiveDelta` or `negativeDelta` is negative - */ + * + * @param positiveDelta + * the maximum positive offset in `unit` from the base date; must be non-negative + * @param negativeDelta + * the maximum negative offset in `unit` from the base date; must be non-negative + * @param datePattern + * the format pattern for the resulting date string + * @param dateFrom + * the base date and time from which the random date is calculated + * @param unit + * the temporal unit for the offset (e.g., days, hours, etc.) + * @param timezone + * the timezone used when formatting the resulting date + * @return + * a string representation of the randomly generated date formatted according to `datePattern` + * @throws IllegalArgumentException + * if `positiveDelta` or `negativeDelta` is negative + */ def randomDate( - positiveDelta: Int, - negativeDelta: Int, - datePattern: String, - dateFrom: LocalDateTime, - unit: TemporalUnit, - timezone: ZoneId, - ): String = { + positiveDelta: Int, + negativeDelta: Int, + datePattern: String, + dateFrom: LocalDateTime, + unit: TemporalUnit, + timezone: ZoneId, + ): String = { validateDelta(positiveDelta, negativeDelta) dateFrom .plus(randomValue(-negativeDelta, positiveDelta), unit) @@ -372,29 +375,29 @@ object RandomDataGenerators { } /** Generates a random date based on the specified parameters. - * - * @param offsetDate - * the offset in units to adjust the date; must not be 0 - * @param datePattern - * the format pattern for the resulting date string; defaults to "yyyy-MM-dd" - * @param dateFrom - * the base datetime from which to calculate the random date - * @param unit - * the temporal unit to use for offset (e.g., days, hours, etc.) - * @param timezone - * the timezone to apply when formatting the date - * @return - * a string representation of the randomly generated date formatted according to `datePattern` - * @throws IllegalArgumentException - * if `offsetDate` is 0 - */ + * + * @param offsetDate + * the offset in units to adjust the date; must not be 0 + * @param datePattern + * the format pattern for the resulting date string; defaults to "yyyy-MM-dd" + * @param dateFrom + * the base datetime from which to calculate the random date + * @param unit + * the temporal unit to use for offset (e.g., days, hours, etc.) + * @param timezone + * the timezone to apply when formatting the date + * @return + * a string representation of the randomly generated date formatted according to `datePattern` + * @throws IllegalArgumentException + * if `offsetDate` is 0 + */ def randomDate( - offsetDate: Long, - datePattern: String = "yyyy-MM-dd", - dateFrom: LocalDateTime, - unit: TemporalUnit, - timezone: ZoneId, - ): String = { + offsetDate: Long, + datePattern: String = "yyyy-MM-dd", + dateFrom: LocalDateTime, + unit: TemporalUnit, + timezone: ZoneId, + ): String = { require(offsetDate != 0, s"RandomRangeDateFeeder offset cannot be zero. Current value: offsetDate= $offsetDate") val adjustedDate = if (offsetDate > 0) { @@ -407,14 +410,14 @@ object RandomDataGenerators { } /** Returns the current date and time formatted according to the provided date pattern and timezone. - * - * @param datePattern - * the DateTimeFormatter specifying the pattern to format the date and time - * @param timezone - * the ZoneId representing the timezone to apply when formatting - * @return - * a string representation of the current date and time formatted as per the specified pattern and timezone - */ + * + * @param datePattern + * the DateTimeFormatter specifying the pattern to format the date and time + * @param timezone + * the ZoneId representing the timezone to apply when formatting + * @return + * a string representation of the current date and time formatted as per the specified pattern and timezone + */ def currentDate(datePattern: DateTimeFormatter, timezone: ZoneId): String = { Instant.now.atZone(timezone).format(datePattern) } diff --git a/src/test/scala/org/galaxio/gatling/utils/RandomDataGeneratorsTest.scala b/src/test/scala/org/galaxio/gatling/utils/RandomDataGeneratorsTest.scala index ef796b2..a4d2438 100644 --- a/src/test/scala/org/galaxio/gatling/utils/RandomDataGeneratorsTest.scala +++ b/src/test/scala/org/galaxio/gatling/utils/RandomDataGeneratorsTest.scala @@ -7,17 +7,18 @@ import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks import java.time.temporal.ChronoUnit import java.time.{LocalDateTime, ZoneId} + class RandomDataGeneratorsTest extends AnyFlatSpec with Matchers with ScalaCheckDrivenPropertyChecks { -// //uuid generator + // //uuid generator val uuidPattern = "([a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8})" -// //date generator - val dateFormat = "yyyy-MM-dd'T'HH:mm" - val datePattern = "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}" + // //date generator + val dateFormat = "yyyy-MM-dd'T'HH:mm" + val datePattern = "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}" val dateFrom: LocalDateTime = LocalDateTime.now() - val dateTimezone: ZoneId = ZoneId.systemDefault() - val dateUnit = ChronoUnit.DAYS + val dateTimezone: ZoneId = ZoneId.systemDefault() + val dateUnit = ChronoUnit.DAYS it should "generate a string of the specified length" in { forAll(Gen.alphaNumStr.filter(_.nonEmpty), Gen.choose(1, 50)) { case (alphabet, len) => @@ -30,6 +31,11 @@ class RandomDataGeneratorsTest extends AnyFlatSpec with Matchers with ScalaCheck RandomDataGenerators.randomUUID should fullyMatch regex uuidPattern } + it should "generate correct value that is not greater than the input" in { + RandomDataGenerators.randomValue(1L, 1) should be <= 1L + } + + it should "generate correct random date pattern" in { forAll(Gen.choose(1, 100), Gen.choose(1, 100)) { (positiveOffset: Int, negativeOffset: Int) => RandomDataGenerators