|
| 1 | +package code.api.berlin.group.v1_3 |
| 2 | + |
| 3 | +import code.api.berlin.group.v1_3.BgSpecValidation._ |
| 4 | +import code.api.v4_0_0.V400ServerSetup |
| 5 | +import org.scalatest.Tag |
| 6 | + |
| 7 | +import java.time.LocalDate |
| 8 | +import java.util.Date |
| 9 | + |
| 10 | +class BgSpecValidationTest extends V400ServerSetup { |
| 11 | + |
| 12 | + // Test tags |
| 13 | + object File extends Tag("BgSpecValidation.scala") |
| 14 | + object Function1 extends Tag("validateValidUntil") |
| 15 | + object Function2 extends Tag("getErrorMessage") |
| 16 | + object Function3 extends Tag("getDate") |
| 17 | + object Function4 extends Tag("formatToISODate") |
| 18 | + |
| 19 | + feature(s"Test function: $Function1 at file $File") { |
| 20 | + |
| 21 | + scenario("Reject past date", Function1) { |
| 22 | + When("The client provides a date in the past") |
| 23 | + val yesterday = LocalDate.now().minusDays(1).toString |
| 24 | + |
| 25 | + Then("It should be rejected") |
| 26 | + val error = getErrorMessage(yesterday) |
| 27 | + error should include("cannot be in the past") |
| 28 | + } |
| 29 | + |
| 30 | + scenario("Accept today's date", Function1) { |
| 31 | + When("The client provides today's date") |
| 32 | + val today = LocalDate.now().toString |
| 33 | + |
| 34 | + Then("It should be accepted") |
| 35 | + val error = getErrorMessage(today) |
| 36 | + error shouldBe "" |
| 37 | + } |
| 38 | + |
| 39 | + scenario("Accept exactly 180 days in the future", Function1) { |
| 40 | + When("The client provides the maximum allowed date (180 days)") |
| 41 | + val maxDay = MaxValidDays.toString |
| 42 | + |
| 43 | + Then("It should be accepted") |
| 44 | + val error = getErrorMessage(maxDay) |
| 45 | + error shouldBe "" |
| 46 | + } |
| 47 | + |
| 48 | + scenario("Reject date beyond 180 days", Function1) { |
| 49 | + When("The client provides a date 181 days in the future") |
| 50 | + val tooFar = MaxValidDays.plusDays(1).toString |
| 51 | + |
| 52 | + Then("It should be rejected") |
| 53 | + val error = getErrorMessage(tooFar) |
| 54 | + error should include("exceeds the maximum allowed period") |
| 55 | + } |
| 56 | + |
| 57 | + scenario("Reject invalid date format", Function1) { |
| 58 | + When("The client provides a date in wrong format") |
| 59 | + val invalid = "2025/12/31" |
| 60 | + |
| 61 | + Then("It should be rejected") |
| 62 | + val error = getErrorMessage(invalid) |
| 63 | + error should include("invalid") |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + feature(s"Test function: $Function2 and $Function3 at file $File") { |
| 68 | + |
| 69 | + scenario("getDate returns valid Date for correct input", Function3) { |
| 70 | + When("We provide a valid ISO date") |
| 71 | + val today = LocalDate.now().toString |
| 72 | + val result = getDate(today) |
| 73 | + |
| 74 | + Then("It should return a non-null Date") |
| 75 | + result shouldBe a[Date] |
| 76 | + } |
| 77 | + |
| 78 | + scenario("getDate returns null for invalid input", Function3) { |
| 79 | + When("We provide an invalid date format") |
| 80 | + val result = getDate("2025/12/31") |
| 81 | + |
| 82 | + Then("It should return null") |
| 83 | + result shouldBe null |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + feature(s"Test function: $Function4 at file $File") { |
| 88 | + |
| 89 | + scenario("formatToISODate formats a valid Date", Function4) { |
| 90 | + When("We pass a valid Date object") |
| 91 | + val today = new Date() |
| 92 | + val formatted = formatToISODate(today) |
| 93 | + |
| 94 | + Then("It should return an ISO date string") |
| 95 | + formatted should fullyMatch regex """\d{4}-\d{2}-\d{2}""" |
| 96 | + } |
| 97 | + |
| 98 | + scenario("formatToISODate handles null gracefully", Function4) { |
| 99 | + When("We pass null") |
| 100 | + val formatted = formatToISODate(null) |
| 101 | + |
| 102 | + Then("It should return empty string") |
| 103 | + formatted shouldBe "" |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments