diff --git a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/generators/Generators.scala b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/generators/Generators.scala index 4ec25af5e..22d89809a 100644 --- a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/generators/Generators.scala +++ b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/generators/Generators.scala @@ -23,8 +23,6 @@ import org.scalacheck.Arbitrary.arbitrary import org.scalacheck.Gen import org.scalacheck.Gen.{alphaStr, choose} -import scala.math.BigDecimal.RoundingMode.HALF_UP - trait Generators { def nonBooleans: Gen[String] = diff --git a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/BasePageSpec.scala b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/BasePageSpec.scala index 2578288f5..453c6eb71 100644 --- a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/BasePageSpec.scala +++ b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/BasePageSpec.scala @@ -53,6 +53,7 @@ trait BasePageSpec[P <: BasePage] extends BaseSpecWithApplication with WireMockS lazy val removeGoodsPage: RadioButtonPage[YesNo] = wire[RadioButtonPage[YesNo]] lazy val goodsRemovedPage: GoodsRemovedPage = wire[GoodsRemovedPage] lazy val agentDetailsPage: AgentDetailsPage = wire[AgentDetailsPage] + lazy val paymentCalculationPage: PaymentCalculationPage = wire[PaymentCalculationPage] lazy val eoriNumberPage: EoriNumberPage = wire[EoriNumberPage] lazy val journeyDetailsPage: JourneyDetailsPage = wire[JourneyDetailsPage] lazy val vehicleRegistrationNumberPage: VehicleRegistrationNumberPage = wire[VehicleRegistrationNumberPage] @@ -101,6 +102,25 @@ trait BasePageSpec[P <: BasePage] extends BaseSpecWithApplication with WireMockS } } + def aPageThatRequiresACompletedGoodsEntry(path: String): Unit = { + s"redirect to ${InvalidRequestPage.path}" when { + "there are no started goods entries" in { + givenADeclarationJourney(startedImportJourney) + open(path) mustBe InvalidRequestPage.path + } + + "there is no complete goods entries" in { + givenADeclarationJourney(importJourneyWithStartedGoodsEntry) + open(path) mustBe InvalidRequestPage.path + } + + "there are incomplete goods entries" in { + givenADeclarationJourney(importJourneyWithOneCompleteAndOneStartedGoodsEntry) + open(path) mustBe InvalidRequestPage.path + } + } + } + def givenAGoodsEntryIsComplete(): Unit = givenADeclarationJourney(importJourneyWithOneCompleteGoodsEntry) def givenTwoGoodsEntriesAreComplete(): Unit = givenADeclarationJourney(importJourneyWithTwoCompleteGoodsEntries) diff --git a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/CheckYourAnswersPageSpec.scala b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/CheckYourAnswersPageSpec.scala index a26d47b8a..f71fb1ee4 100644 --- a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/CheckYourAnswersPageSpec.scala +++ b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/CheckYourAnswersPageSpec.scala @@ -16,42 +16,19 @@ package uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs -import uk.gov.hmrc.http.HeaderCarrier import uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs.pages.CheckYourAnswersPage._ -import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.{DeclarationJourney, PaymentCalculations} import uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs.pages.{CheckYourAnswersPage, InvalidRequestPage} -import uk.gov.hmrc.merchandiseinbaggagefrontend.service.CalculationService -import uk.gov.hmrc.merchandiseinbaggagefrontend.stubs.CurrencyConversionStub.givenCurrencyIsFound import uk.gov.hmrc.merchandiseinbaggagefrontend.stubs.PayApiStub._ -import scala.concurrent.Future - -class CheckYourAnswersPageSpec extends BasePageSpec[CheckYourAnswersPage] { +class CheckYourAnswersPageSpec extends BasePageSpec[CheckYourAnswersPage] with TaxCalculation{ override lazy val page: CheckYourAnswersPage = checkYourAnswersPage - private lazy val calculationService = injector.instanceOf[CalculationService] - - override def beforeEach(): Unit = { - super.beforeEach() - webDriver.manage().deleteAllCookies() - } - - def createDeclarationAndCalculateTaxDue(declarationJourney: DeclarationJourney): Future[PaymentCalculations] = { - implicit val hc: HeaderCarrier = HeaderCarrier() - - givenCurrencyIsFound("EUR", wireMockServer) - - givenADeclarationJourney(declarationJourney) - - calculationService.paymentCalculation(declarationJourney.declarationIfRequiredAndComplete.get.declarationGoods) - } - "the page" should { behave like aPageWhichRequiresADeclarationJourney(path) "render correctly" when { "the declaration is complete" in { - val taxDue = createDeclarationAndCalculateTaxDue(completedDeclarationJourney).futureValue + val taxDue = givenADeclarationWithTaxDue(completedDeclarationJourney).futureValue val declaration = completedDeclarationJourney.declarationIfRequiredAndComplete.get open(path) @@ -62,7 +39,7 @@ class CheckYourAnswersPageSpec extends BasePageSpec[CheckYourAnswersPage] { "the declaration is complete but sparse" in { val declaration = sparseCompleteDeclarationJourney.declarationIfRequiredAndComplete.get - val taxDue = createDeclarationAndCalculateTaxDue(sparseCompleteDeclarationJourney).futureValue + val taxDue = givenADeclarationWithTaxDue(sparseCompleteDeclarationJourney).futureValue open(path) @@ -80,7 +57,7 @@ class CheckYourAnswersPageSpec extends BasePageSpec[CheckYourAnswersPage] { } "allow the user to make a payment" in { - createDeclarationAndCalculateTaxDue(completedDeclarationJourney).futureValue + givenADeclarationWithTaxDue(completedDeclarationJourney).futureValue givenTaxArePaid(wireMockServer) open(path) diff --git a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/PaymentCalculationPageSpec.scala b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/PaymentCalculationPageSpec.scala new file mode 100644 index 000000000..a26228c57 --- /dev/null +++ b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/PaymentCalculationPageSpec.scala @@ -0,0 +1,66 @@ +/* + * Copyright 2020 HM Revenue & Customs + * + * 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 uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs + +import uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs.pages.PaymentCalculationPage._ +import uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs.pages.{CustomsAgentPage, PaymentCalculationPage} + +class PaymentCalculationPageSpec extends BasePageSpec[PaymentCalculationPage] with TaxCalculation { + override def page: PaymentCalculationPage = paymentCalculationPage + + private def setUpTaxCalculationAndOpenPage() = { + val taxCalculation = givenADeclarationWithTaxDue(importJourneyWithTwoCompleteGoodsEntries).futureValue + + open(path) + + taxCalculation + } + + "the Payment Calculation Page" should { + behave like aPageWhichRequiresADeclarationJourney(path) + behave like aPageThatRequiresACompletedGoodsEntry(path) + + "render" in { + val taxCalculation = setUpTaxCalculationAndOpenPage() + val paymentCalculations = taxCalculation.paymentCalculations + + page.mustRenderBasicContent(path, title(taxCalculation.totalTaxDue)) + page.summaryHeaders mustBe Seq("Type of goods", "Customs Duty", "VAT", "Total") + + Range(0, 1).foreach { index => + val calculationResult = paymentCalculations(index).calculationResult + val goods = paymentCalculations(index).goods + + page.summaryRow(index) mustBe + Seq( + goods.categoryQuantityOfGoods.category, + calculationResult.duty.formattedInPounds, + s"${calculationResult.vat.formattedInPounds} at ${goods.goodsVatRate.value}%", + calculationResult.taxDue.formattedInPounds) + } + + page.summaryRow(2) mustBe Seq("Payment due", taxCalculation.totalTaxDue.formattedInPounds) + } + + "redirect to the customs agent page" when { + "the user clicks the CTA" in { + setUpTaxCalculationAndOpenPage() + page.clickOnCTA() mustBe CustomsAgentPage.path + } + } + } +} diff --git a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/ReviewGoodsPageSpec.scala b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/ReviewGoodsPageSpec.scala index 6c1166fa5..09e4663c0 100644 --- a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/ReviewGoodsPageSpec.scala +++ b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/ReviewGoodsPageSpec.scala @@ -18,13 +18,14 @@ package uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.YesNo.{No, Yes} import uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs.pages.ReviewGoodsPage._ -import uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs.pages.{InvalidRequestPage, PaymentCalculationPage, RemoveGoodsPage, ReviewGoodsPage, SearchGoodsPage} +import uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs.pages.{PaymentCalculationPage, RemoveGoodsPage, ReviewGoodsPage, SearchGoodsPage} class ReviewGoodsPageSpec extends BasePageSpec[ReviewGoodsPage] { override lazy val page: ReviewGoodsPage = reviewGoodsPage "the review goods page" should { behave like aPageWhichRequiresADeclarationJourney(path) + behave like aPageThatRequiresACompletedGoodsEntry(path) "render correctly" when { "a single goods entry is complete" in { @@ -88,23 +89,6 @@ class ReviewGoodsPageSpec extends BasePageSpec[ReviewGoodsPage] { } } - s"redirect to ${InvalidRequestPage.path}" when { - "there are no started goods entries" in { - givenADeclarationJourney(startedImportJourney) - open(path) mustBe InvalidRequestPage.path - } - - "there is no complete goods entries" in { - givenADeclarationJourney(importJourneyWithStartedGoodsEntry) - open(path) mustBe InvalidRequestPage.path - } - - "there are incomplete goods entries" in { - givenADeclarationJourney(importJourneyWithOneCompleteAndOneStartedGoodsEntry) - open(path) mustBe InvalidRequestPage.path - } - } - s"redirect to /search-goods/:idx" when { "the user elects to make a second goods entry" in { givenADeclarationJourney(importJourneyWithOneCompleteGoodsEntry) diff --git a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/TaxCalculation.scala b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/TaxCalculation.scala new file mode 100644 index 000000000..6ca1b006a --- /dev/null +++ b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/TaxCalculation.scala @@ -0,0 +1,53 @@ +/* + * Copyright 2020 HM Revenue & Customs + * + * 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 uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs + +import uk.gov.hmrc.http.HeaderCarrier +import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.{DeclarationJourney, PaymentCalculations} +import uk.gov.hmrc.merchandiseinbaggagefrontend.model.currencyconversion.Currency +import uk.gov.hmrc.merchandiseinbaggagefrontend.service.CalculationService +import uk.gov.hmrc.merchandiseinbaggagefrontend.stubs.CurrencyConversionStub.givenCurrencyIsFound + +import scala.concurrent.Future + +trait TaxCalculation { + this: BasePageSpec[_] => + + private lazy val calculationService = injector.instanceOf[CalculationService] + + def givenADeclarationWithTaxDue(declarationJourney: DeclarationJourney): Future[PaymentCalculations] = { + implicit val hc: HeaderCarrier = HeaderCarrier() + + def givenCurrenciesAreFound(): Unit = + declarationJourney + .goodsEntries + .declarationGoodsIfComplete + .get + .goods + .map(_.purchaseDetails.currency) + .toSet + .foreach { ccy: Currency => + givenCurrencyIsFound(ccy.currencyCode, wireMockServer) + () + } + + givenADeclarationJourney(declarationJourney) + givenCurrenciesAreFound() + + calculationService.paymentCalculation(declarationJourney.goodsEntries.declarationGoodsIfComplete.get) + } +} diff --git a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/pages/PageWithCTA.scala b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/pages/PageWithCTA.scala index 005450456..f77cd0ea1 100644 --- a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/pages/PageWithCTA.scala +++ b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/pages/PageWithCTA.scala @@ -19,7 +19,7 @@ package uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs.pages import org.openqa.selenium.WebDriver import org.scalatestplus.selenium.WebBrowser -abstract class PageWithCTA(implicit webDriver: WebDriver) extends BasePage() { +abstract class PageWithCTA(implicit webDriver: WebDriver) extends BasePage { import WebBrowser._ diff --git a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/pages/PaymentCalculationPage.scala b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/pages/PaymentCalculationPage.scala index 786de5b56..4476e2d84 100644 --- a/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/pages/PaymentCalculationPage.scala +++ b/test/uk/gov/hmrc/merchandiseinbaggagefrontend/pagespecs/pages/PaymentCalculationPage.scala @@ -16,6 +16,37 @@ package uk.gov.hmrc.merchandiseinbaggagefrontend.pagespecs.pages +import org.openqa.selenium.{By, WebDriver} +import org.scalatestplus.selenium.WebBrowser +import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.AmountInPence + +import scala.collection.JavaConverters._ + +class PaymentCalculationPage(implicit webDriver: WebDriver) extends BasePage { + + import WebBrowser._ + + def clickOnCTA(): String = { + val button = find(ClassNameQuery("govuk-button")).get + click on button + + readPath() + } + + def summaryHeaders: Seq[String] = { + val tableHead = find(ClassNameQuery("govuk-table__head")).head.underlying + tableHead.findElements(By.className("govuk-table__header")).asScala.map(_.getText) + } + + def summaryRow(index: Int): Seq[String] = { + val tableBody = find(ClassNameQuery("govuk-table__body")).head.underlying + val tableRows = tableBody.findElements(By.className("govuk-table__row")).asScala + tableRows(index).findElements(By.className("govuk-table__cell")).asScala.map(_.getText) + } +} + object PaymentCalculationPage { val path: String = "/merchandise-in-baggage/payment-calculation" + + def title(amountInPence: AmountInPence) = s"Payment due on these goods ${amountInPence.formattedInPounds}" } \ No newline at end of file