Skip to content

Commit

Permalink
[MIBM-145][PH] add PaymentCalculationPageSpec (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHodgson authored Nov 2, 2020
1 parent 61f8849 commit 0ee39c3
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}

0 comments on commit 0ee39c3

Please sign in to comment.