Skip to content

Commit c39c133

Browse files
feature/Cannot create consent for max amount - 180 Days
1 parent c1cf4d9 commit c39c133

File tree

2 files changed

+108
-21
lines changed

2 files changed

+108
-21
lines changed

obp-api/src/main/scala/code/api/berlin/group/v1_3/BgSpecValidation.scala

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ object BgSpecValidation {
3636

3737
if (date.isBefore(today)) {
3838
Left(s"$InvalidDateFormat The `validUntil` date ($dateStr) cannot be in the past!")
39-
} else if (date.isEqual(MaxValidDays) || date.isAfter(MaxValidDays)) {
39+
} else if (date.isAfter(MaxValidDays)) {
4040
Left(s"$InvalidDateFormat The `validUntil` date ($dateStr) exceeds the maximum allowed period of 180 days (until $MaxValidDays).")
4141
} else {
42-
Right(date) // Valid date
42+
Right(date) // Valid date (inclusive of 180 days)
4343
}
4444
} catch {
4545
case _: DateTimeParseException =>
@@ -55,23 +55,4 @@ object BgSpecValidation {
5555
}
5656
}
5757

58-
// Example usage
59-
def main(args: Array[String]): Unit = {
60-
val testDates = Seq(
61-
"2025-05-10", // More than 180 days ahead
62-
"9999-12-31", // Exceeds max allowed
63-
"2015-01-01", // In the past
64-
"invalid-date", // Invalid format
65-
LocalDate.now().plusDays(90).toString, // Valid (within 180 days)
66-
LocalDate.now().plusDays(180).toString, // Valid (exactly 180 days)
67-
LocalDate.now().plusDays(181).toString // More than 180 days
68-
)
69-
70-
testDates.foreach { date =>
71-
validateValidUntil(date) match {
72-
case Right(validDate) => println(s"Valid date: $validDate")
73-
case Left(error) => println(s"Error: $error")
74-
}
75-
}
76-
}
7758
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)