From af3c615cfb927c656f4a3461d99091a500eaa79a Mon Sep 17 00:00:00 2001 From: JakeReid2020 <75027964+JakeReid2020@users.noreply.github.com> Date: Mon, 22 Sep 2025 09:29:45 +0100 Subject: [PATCH] [NGR-1137] Fixing tests --- ...SpacesOrGaragesIncludedInRentController.scala | 10 ++++++---- ...arkingSpacesOrGaragesIncludedInRentForm.scala | 16 ++++++++-------- ...esOrGaragesIncludedInRentControllerSpec.scala | 2 +- ...ngSpacesOrGaragesIncludedInRentFormSpec.scala | 2 +- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/app/uk/gov/hmrc/ngrraldfrontend/controllers/HowManyParkingSpacesOrGaragesIncludedInRentController.scala b/app/uk/gov/hmrc/ngrraldfrontend/controllers/HowManyParkingSpacesOrGaragesIncludedInRentController.scala index a3fa14c3..4345074a 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/controllers/HowManyParkingSpacesOrGaragesIncludedInRentController.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/controllers/HowManyParkingSpacesOrGaragesIncludedInRentController.scala @@ -74,7 +74,9 @@ class HowManyParkingSpacesOrGaragesIncludedInRentController @Inject()(howManyPar form.bindFromRequest().fold( formWithErrors => { val formWithCorrectedErrors = formWithErrors.errors.head match { - case value if value.key.isEmpty && value.messages.contains("howManyParkingSpacesOrGaragesIncludedInRent.error.required") => + case value if value.key.isEmpty && + value.messages.contains("howManyParkingSpacesOrGaragesIncludedInRent.allFields.error.required") || + value.messages.contains("howManyParkingSpacesOrGaragesIncludedInRent.error.required") => val uncoveredSpaces = value.copy(key = "uncoveredSpaces") val coveredSpaces = value.copy(key = "coveredSpaces") val garages = value.copy(key = "garages") @@ -94,9 +96,9 @@ class HowManyParkingSpacesOrGaragesIncludedInRentController @Inject()(howManyPar rentAmount => raldRepo.insertHowManyParkingSpacesOrGaragesIncludedInRent( credId = CredId(request.credId.getOrElse("")), - uncoveredSpaces = rentAmount.uncoveredSpaces, - coveredSpaces = rentAmount.coveredSpaces, - garages = rentAmount.garages + uncoveredSpaces = if(rentAmount.uncoveredSpaces == -1) 0 else rentAmount.uncoveredSpaces, + coveredSpaces = if(rentAmount.coveredSpaces == -1) 0 else rentAmount.coveredSpaces, + garages = if(rentAmount.garages == -1) 0 else rentAmount.garages ) Future.successful(Redirect(routes.CheckRentFreePeriodController.show.url)) ) diff --git a/app/uk/gov/hmrc/ngrraldfrontend/models/forms/HowManyParkingSpacesOrGaragesIncludedInRentForm.scala b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/HowManyParkingSpacesOrGaragesIncludedInRentForm.scala index 2bd6103f..b889a1aa 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/models/forms/HowManyParkingSpacesOrGaragesIncludedInRentForm.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/HowManyParkingSpacesOrGaragesIncludedInRentForm.scala @@ -53,11 +53,11 @@ object HowManyParkingSpacesOrGaragesIncludedInRentForm extends CommonFormValidat Constraint[A] = Constraint((input: A) => { val formData = input.asInstanceOf[HowManyParkingSpacesOrGaragesIncludedInRentForm] - val totalSpaces = Seq(formData.uncoveredSpaces, formData.coveredSpaces, formData.garages).sum - if (totalSpaces > 0) - Valid - else - Invalid(fieldRequired) + (formData.uncoveredSpaces, formData.coveredSpaces, formData.garages) match { + case (-1,-1,-1) => Invalid(allFieldsRequiredError) + case (0, 0, 0) => Invalid(fieldRequired) + case (_,_,_)=> Valid + } }) val form: Form[HowManyParkingSpacesOrGaragesIncludedInRentForm] = { @@ -67,19 +67,19 @@ object HowManyParkingSpacesOrGaragesIncludedInRentForm extends CommonFormValidat text() .transform[String](_.strip().replaceAll(",", ""), identity) .verifying(regexp(wholePositiveNumberRegexp.pattern(), uncoveredSpacesWholeNumError)) - ).transform[Int](_.map(_.toInt).getOrElse(0), value => Some(value.toString)) + ).transform[Int](_.map(_.toInt).getOrElse(-1), value => Some(value.toString)) .verifying(maximumValue[Int](maxValue, uncoveredSpacesTooHighError)), "coveredSpaces" -> optional( text() .transform[String](_.strip().replaceAll(",", ""), identity) .verifying(regexp(wholePositiveNumberRegexp.pattern(), coveredSpacesWholeNumError)) - ).transform[Int](_.map(_.toInt).getOrElse(0), value => Some(value.toString)) + ).transform[Int](_.map(_.toInt).getOrElse(-1), value => Some(value.toString)) .verifying(maximumValue[Int](maxValue, coveredSpacesTooHighError)), "garages" -> optional( text() .transform[String](_.strip().replaceAll(",", ""), identity) .verifying(regexp(wholePositiveNumberRegexp.pattern(), garagesWholeNumError)) - ).transform[Int](_.map(_.toInt).getOrElse(0), value => Some(value.toString)) + ).transform[Int](_.map(_.toInt).getOrElse(-1), value => Some(value.toString)) .verifying(maximumValue[Int](maxValue, garagesTooHighError)), ) (HowManyParkingSpacesOrGaragesIncludedInRentForm.apply)(HowManyParkingSpacesOrGaragesIncludedInRentForm.unapply) diff --git a/test/uk/gov/hmrc/ngrraldfrontend/controllers/HowManyParkingSpacesOrGaragesIncludedInRentControllerSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/controllers/HowManyParkingSpacesOrGaragesIncludedInRentControllerSpec.scala index 69c794de..c7d4725f 100644 --- a/test/uk/gov/hmrc/ngrraldfrontend/controllers/HowManyParkingSpacesOrGaragesIncludedInRentControllerSpec.scala +++ b/test/uk/gov/hmrc/ngrraldfrontend/controllers/HowManyParkingSpacesOrGaragesIncludedInRentControllerSpec.scala @@ -81,7 +81,7 @@ class HowManyParkingSpacesOrGaragesIncludedInRentControllerSpec extends Controll mockRequestWithoutProperty() val fakePostRequest = FakeRequest(routes.HowManyParkingSpacesOrGaragesIncludedInRentController.submit) .withFormUrlEncodedBody( - "uncoveredSpaces" -> "", + "uncoveredSpaces" -> "0", "coveredSpaces" -> "0", "garages" -> "0" ).withHeaders(HeaderNames.authorisation -> "Bearer 1") diff --git a/test/uk/gov/hmrc/ngrraldfrontend/models/forms/HowManyParkingSpacesOrGaragesIncludedInRentFormSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/models/forms/HowManyParkingSpacesOrGaragesIncludedInRentFormSpec.scala index 5c9f3744..2d1be97e 100644 --- a/test/uk/gov/hmrc/ngrraldfrontend/models/forms/HowManyParkingSpacesOrGaragesIncludedInRentFormSpec.scala +++ b/test/uk/gov/hmrc/ngrraldfrontend/models/forms/HowManyParkingSpacesOrGaragesIncludedInRentFormSpec.scala @@ -106,7 +106,7 @@ class HowManyParkingSpacesOrGaragesIncludedInRentFormSpec extends AnyWordSpec wi val boundForm = HowManyParkingSpacesOrGaragesIncludedInRentForm.form.bind(data) boundForm.hasErrors shouldBe true - boundForm.errors shouldBe List(FormError("", "howManyParkingSpacesOrGaragesIncludedInRent.error.required")) + boundForm.errors shouldBe List(FormError("", "howManyParkingSpacesOrGaragesIncludedInRent.allFields.error.required")) } "fail to bind when input fields are all 0" in {