Skip to content

Commit

Permalink
MMCA 4770 : Upgrade scala & library dependancies to latest version (#128
Browse files Browse the repository at this point in the history
)

* MMCA-4770 : initial commit

* MMCA-4770 : added unused warning, fixed ErrorHandler, Layout

* MMCA-4770 : initial fixes for test cases

* MMCA-4770 : test cases fixes - httpclient and mock changes

* MMCA-4770 : fixed url formation

* MMCA-4770 : fixed mock of http call in connectorSpec

* MMCA-4770 : fixed scalastyle issues

* MMCA-4770 :  compilation fixes in html view component

* MMCA-4770 : revoked changes of ErrorHandler to use LegacyFrontendErrorHandler

* MMCA-4770: PR comment fixes 01
  • Loading branch information
HariHmrc authored Jul 16, 2024
1 parent 3768d55 commit dcb2c56
Show file tree
Hide file tree
Showing 36 changed files with 396 additions and 174 deletions.
7 changes: 5 additions & 2 deletions app/config/ErrorHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ import javax.inject.{Inject, Singleton}
import play.api.i18n.MessagesApi
import play.api.mvc.Request
import play.twirl.api.Html
import uk.gov.hmrc.play.bootstrap.frontend.http.FrontendErrorHandler
import uk.gov.hmrc.play.bootstrap.frontend.http.LegacyFrontendErrorHandler
import views.html.{ErrorTemplate, not_found}

import scala.concurrent.{ExecutionContext, Future}

@Singleton
class ErrorHandler @Inject()(errorTemplate: ErrorTemplate,
notFound: not_found,
val messagesApi: MessagesApi)(implicit appConfig: AppConfig) extends FrontendErrorHandler {
val messagesApi: MessagesApi)
(implicit appConfig: AppConfig) extends LegacyFrontendErrorHandler {

override def standardErrorTemplate(pageTitle: String,
heading: String,
Expand Down
21 changes: 13 additions & 8 deletions app/connectors/CustomsDataStoreConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ import play.api.Logger
import play.api.http.Status.NOT_FOUND
import services.MetricsReporterService
import uk.gov.hmrc.auth.core.retrieve.Email
import uk.gov.hmrc.http.HttpReads.Implicits._
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, UpstreamErrorResponse}
import uk.gov.hmrc.http.HttpReads.Implicits.*
import uk.gov.hmrc.http.client.HttpClientV2
import uk.gov.hmrc.http.{HeaderCarrier, StringContextOps, UpstreamErrorResponse}

import java.net.URL
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}

@Singleton
class CustomsDataStoreConnector @Inject()(http: HttpClient,
class CustomsDataStoreConnector @Inject()(http: HttpClientV2,
metricsReporter: MetricsReporterService)
(implicit appConfig: AppConfig, ec: ExecutionContext) {

Expand All @@ -41,11 +43,14 @@ class CustomsDataStoreConnector @Inject()(http: HttpClient,
val resourceNameForMetrics = "customs-data-store.get.email"

metricsReporter.withResponseTimeLogging(resourceNameForMetrics) {
http.GET[EmailResponse](dataStoreEndpoint).map {
case EmailResponse(Some(address), _, None) => Right(Email(address))
case EmailResponse(Some(email), _, Some(_)) => Left(UndeliverableEmail(email))
case _ => Left(UnverifiedEmail)
}.recover {

http.get(url"$dataStoreEndpoint")
.execute[EmailResponse]
.map {
case EmailResponse(Some(address), _, None) => Right(Email(address))
case EmailResponse(Some(email), _, Some(_)) => Left(UndeliverableEmail(email))
case _ => Left(UnverifiedEmail)
}.recover {
case UpstreamErrorResponse(_, NOT_FOUND, _, _) =>
Left(UnverifiedEmail)
case exception =>
Expand Down
52 changes: 31 additions & 21 deletions app/connectors/CustomsFinancialsApiConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@
package connectors

import config.AppConfig
import models.CashDailyStatement._
import models._
import models.CashDailyStatement.*
import models.*
import models.email.{EmailUnverifiedResponse, EmailVerifiedResponse}
import models.request.{CashDailyStatementRequest, IdentifierRequest}
import org.slf4j.LoggerFactory
import play.api.http.Status.{NOT_FOUND, REQUEST_ENTITY_TOO_LARGE}
import play.api.mvc.AnyContent
import repositories.CacheRepository
import services.MetricsReporterService
import uk.gov.hmrc.http.HttpReads.Implicits._
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, UpstreamErrorResponse}
import uk.gov.hmrc.http.HttpReads.Implicits.*
import uk.gov.hmrc.http.client.HttpClientV2
import uk.gov.hmrc.http.{HeaderCarrier, StringContextOps, UpstreamErrorResponse}

import java.net.URL
import java.time.LocalDate
import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}
import play.api.libs.ws.JsonBodyWritables.writeableOf_JsValue

class CustomsFinancialsApiConnector @Inject()(httpClient: HttpClient,
class CustomsFinancialsApiConnector @Inject()(httpClient: HttpClientV2,
appConfig: AppConfig,
metricsReporter: MetricsReporterService,
cacheRepository: CacheRepository)
Expand All @@ -53,8 +56,10 @@ class CustomsFinancialsApiConnector @Inject()(httpClient: HttpClient,
)

metricsReporter.withResponseTimeLogging("customs-financials-api.get.accounts") {
httpClient.POST[AccountsAndBalancesRequestContainer, AccountsAndBalancesResponseContainer](
accountsUrl, accountsAndBalancesRequest).map(_.toCashAccounts)
httpClient.post(url"$accountsUrl")
.withBody[AccountsAndBalancesRequestContainer](accountsAndBalancesRequest)
.execute[AccountsAndBalancesResponseContainer]
.map(_.toCashAccounts)
}.map(_.find(_.owner == request.eori))
}

Expand All @@ -63,8 +68,10 @@ class CustomsFinancialsApiConnector @Inject()(httpClient: HttpClient,
to: LocalDate)
(implicit hc: HeaderCarrier): Future[Either[ErrorResponse, CashTransactions]] = {
val cashDailyStatementRequest = CashDailyStatementRequest(can, from, to)
httpClient.POST[CashDailyStatementRequest, CashTransactions](
retrieveCashTransactionsUrl, cashDailyStatementRequest).map(Right(_))

httpClient.post(url"$retrieveCashTransactionsUrl")
.withBody[CashDailyStatementRequest](cashDailyStatementRequest)
.execute[CashTransactions].map(Right(_))
}.recover {
case UpstreamErrorResponse(_, REQUEST_ENTITY_TOO_LARGE, _, _) =>
logger.error(s"Entity too large to download")
Expand All @@ -89,16 +96,17 @@ class CustomsFinancialsApiConnector @Inject()(httpClient: HttpClient,
case Some(value) => Future.successful(Right(value))

case None =>
httpClient.POST[CashDailyStatementRequest, CashTransactions](
retrieveCashTransactionsUrl, cashDailyStatementRequest).flatMap { response =>

cacheRepository.set(can, response).map { successfulWrite =>
if (!successfulWrite) {
logger.error("Failed to store data in the session cache defaulting to the api response")
httpClient.post(url"$retrieveCashTransactionsUrl")
.withBody[CashDailyStatementRequest](cashDailyStatementRequest)
.execute[CashTransactions]
.flatMap { response =>
cacheRepository.set(can, response).map { successfulWrite =>
if (!successfulWrite) {
logger.error("Failed to store data in the session cache defaulting to the api response")
}
Right(response)
}
Right(response)
}
}
}.recover {
case UpstreamErrorResponse(_, REQUEST_ENTITY_TOO_LARGE, _, _) =>
logger.error(s"Entity too large to download")
Expand All @@ -120,8 +128,10 @@ class CustomsFinancialsApiConnector @Inject()(httpClient: HttpClient,
(implicit hc: HeaderCarrier): Future[Either[ErrorResponse, CashTransactions]] = {
val cashDailyStatementRequest = CashDailyStatementRequest(can, from, to)

httpClient.POST[CashDailyStatementRequest, CashTransactions](
retrieveCashTransactionsDetailUrl, cashDailyStatementRequest).map(Right(_))
httpClient.post(url"$retrieveCashTransactionsDetailUrl")
.withBody[CashDailyStatementRequest](cashDailyStatementRequest)
.execute[CashTransactions]
.map(Right(_))
}.recover {
case UpstreamErrorResponse(_, REQUEST_ENTITY_TOO_LARGE, _, _) =>
logger.error(s"Entity too large to download")
Expand All @@ -139,7 +149,7 @@ class CustomsFinancialsApiConnector @Inject()(httpClient: HttpClient,
def verifiedEmail(implicit hc: HeaderCarrier): Future[EmailVerifiedResponse] = {
val emailDisplayApiUrl = s"$baseUrl/subscriptions/email-display"

httpClient.GET[EmailVerifiedResponse](emailDisplayApiUrl).recover {
httpClient.get(url"$emailDisplayApiUrl").execute[EmailVerifiedResponse].recover {
case _ =>
logger.error(s"Error occurred while calling API $emailDisplayApiUrl")
EmailVerifiedResponse(None)
Expand All @@ -149,7 +159,7 @@ class CustomsFinancialsApiConnector @Inject()(httpClient: HttpClient,
def retrieveUnverifiedEmail(implicit hc: HeaderCarrier): Future[EmailUnverifiedResponse] = {
val unverifiedEmailDisplayApiUrl = s"$baseUrl/subscriptions/unverified-email-display"

httpClient.GET[EmailUnverifiedResponse](unverifiedEmailDisplayApiUrl).recover {
httpClient.get(url"$unverifiedEmailDisplayApiUrl").execute[EmailUnverifiedResponse].recover {
case _ =>
logger.error(s"Error occurred while calling API $unverifiedEmailDisplayApiUrl")
EmailUnverifiedResponse(None)
Expand Down
2 changes: 1 addition & 1 deletion app/forms/CashTransactionsRequestPageFormProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class CashTransactionsRequestPageFormProvider @Inject() (implicit clock: Clock)
invalidLength = "cf.form.error.year.length"
)
)
)(CashTransactionDates.apply)(CashTransactionDates.unapply)
)(CashTransactionDates.apply)(ctd => Some(Tuple.fromProductTyped(ctd)))
)
}
}
6 changes: 6 additions & 0 deletions app/models/AccountsAndBalances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package models

import play.api.libs.json._
import play.api.{Logger, LoggerLike}
import play.api.libs.ws.BodyWritable

import java.time.Instant
import java.time.format.DateTimeFormatter
Expand Down Expand Up @@ -145,4 +146,9 @@ object AccountsAndBalancesRequestContainer {

implicit val accountsAndBalancesRequestContainerFormat: OFormat[AccountsAndBalancesRequestContainer] =
Json.format[AccountsAndBalancesRequestContainer]

implicit def jsonBodyWritable[T](implicit
writes: Writes[T],
jsValueBodyWritable: BodyWritable[JsValue]
): BodyWritable[T] = jsValueBodyWritable.map(writes.writes)
}
13 changes: 10 additions & 3 deletions app/models/request/CashDailyStatementRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ package models.request

import java.time.LocalDate
import models.domain.CAN
import play.api.libs.json.{Json, OWrites}
import play.api.libs.json.{JsValue, Json, OWrites, Writes, Format}
import play.api.libs.ws.BodyWritable

case class CashDailyStatementRequest(can: CAN, from: LocalDate, to: LocalDate)

object CashDailyStatementRequest {
implicit val CashDailyStatementRequestWrites: OWrites[CashDailyStatementRequest] =
Json.writes[CashDailyStatementRequest]

implicit val format: Format[CashDailyStatementRequest] = Json.format[CashDailyStatementRequest]

implicit val writes: Writes[CashDailyStatementRequest] = Json.writes[CashDailyStatementRequest]

implicit def jsonBodyWritable[T](implicit writes: Writes[T],
jsValueBodyWritable: BodyWritable[JsValue]
): BodyWritable[T] = jsValueBodyWritable.map(writes.writes)
}
5 changes: 4 additions & 1 deletion app/repositories/CacheRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import java.util.concurrent.TimeUnit
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}

import org.mongodb.scala.SingleObservableFuture
import org.mongodb.scala.ToSingleObservablePublisher

@Singleton
class DefaultCacheRepository @Inject()(mongo: MongoComponent,
config: Configuration,
Expand All @@ -45,7 +48,7 @@ class DefaultCacheRepository @Inject()(mongo: MongoComponent,
IndexModel(
ascending("lastUpdated"),
IndexOptions().name("cash-account-cache-last-updated-index")
.expireAfter(config.get[Int]("mongodb.timeToLiveInSeconds"), TimeUnit.SECONDS)
.expireAfter(config.get[Long]("mongodb.timeToLiveInSeconds"), TimeUnit.SECONDS)
)
)) with CacheRepository {

Expand Down
5 changes: 4 additions & 1 deletion app/repositories/RequestedTransactionsCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import java.util.concurrent.TimeUnit
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}

import org.mongodb.scala.SingleObservableFuture
import org.mongodb.scala.ToSingleObservablePublisher

@Singleton
class DefaultRequestedTransactionsCache @Inject()(mongoComponent: MongoComponent,
config: Configuration)(implicit executionContext: ExecutionContext)
Expand All @@ -40,7 +43,7 @@ class DefaultRequestedTransactionsCache @Inject()(mongoComponent: MongoComponent
ascending("lastUpdated"),
IndexOptions()
.name("requested-transactions-cache-last-updated-index")
.expireAfter(config.get[Int]("mongodb.timeToLiveInSeconds"),
.expireAfter(config.get[Long]("mongodb.timeToLiveInSeconds"),
TimeUnit.SECONDS)
))) with RequestedTransactionsCache {

Expand Down
4 changes: 2 additions & 2 deletions app/services/AuditingService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class AuditingService @Inject()(appConfig: AppConfig, auditConnector: AuditConne

val log: LoggerLike = Logger(this.getClass)

private val referrer: HeaderCarrier =>
String = _.headers(Seq(HeaderNames.REFERER)).headOption.map(_._2).getOrElse("-")
private def referrer(hc: HeaderCarrier): String =
hc.headers(Seq(HeaderNames.REFERER)).headOption.map(_._2).getOrElse("-")

def audit(auditModel: AuditModel)(implicit hc: HeaderCarrier): Future[AuditResult] = {
val dataEvent = toExtendedDataEvent(appConfig.appName, auditModel, referrer(hc))
Expand Down
2 changes: 1 addition & 1 deletion app/viewmodels/CashTransactionsViewModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ case class CashTransactionsViewModel(cashTransactions: CashTransactions, page: O
}

override val allItems: Seq[PaginatedTransactions] =
pendingTransactionsGroupedByDate ++ cashTransactions.cashDailyStatements.sorted.map(PaginatedDailyStatement)
pendingTransactionsGroupedByDate ++ cashTransactions.cashDailyStatements.sorted.map(PaginatedDailyStatement.apply)

override val itemsPerPage: Int = appConfig.numberOfDaysToShow
override val requestedPage: Int = page.getOrElse(1)
Expand Down
40 changes: 26 additions & 14 deletions app/views/Layout.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
@import uk.gov.hmrc.govukfrontend.views.viewmodels.content.HtmlContent
@import uk.gov.hmrc.govukfrontend.views.viewmodels.phasebanner.PhaseBanner
@import uk.gov.hmrc.hmrcfrontend.views.html.components.HmrcTimeoutDialog
@import uk.gov.hmrc.hmrcfrontend.views.html.helpers.{HmrcLayout, HmrcReportTechnicalIssueHelper}
@import uk.gov.hmrc.hmrcfrontend.views.html.helpers.{HmrcStandardPage, HmrcReportTechnicalIssueHelper}
@import uk.gov.hmrc.hmrcfrontend.views.viewmodels.timeoutdialog.TimeoutDialog
@import views.html.helper.CSPNonce
@import views.html.components.fullWidthMainContent
@import uk.gov.hmrc.hmrcfrontend.views.viewmodels.hmrcstandardpage._
@import uk.gov.hmrc.hmrcfrontend.views.viewmodels.userresearchbanner.UserResearchBanner
@import uk.gov.hmrc.govukfrontend.views.viewmodels.backlink.BackLink
@import utils.Utils.emptyString

@this(appConfig: AppConfig,
hmrcLayout: HmrcLayout,
hmrcStandardPage: HmrcStandardPage,
govukBackLink: GovukBackLink,
hmrcReportTechnicalIssueHelper: HmrcReportTechnicalIssueHelper,
hmrcTimeoutDialog: HmrcTimeoutDialog,
Expand Down Expand Up @@ -79,15 +83,23 @@
}
}

@hmrcLayout(
userResearchBannerUrl = Some(appConfig.helpMakeGovUkBetterUrl),
pageTitle = fullPageTitle,
serviceUrl = Some(appConfig.customsFinancialsFrontendHomepage),
signOutUrl = Some(routes.LogoutController.logout.url),
backLinkUrl = backLink,
additionalHeadBlock = Some(additionalHead),
mainContentLayout = Some(if(fullWidth) fullWidthMainContent(_) else twoThirdsMainContent(_)),
phaseBanner = Some(PhaseBanner(tag = Some(Tag(content = Text("BETA"))), content = HtmlContent(phaseBannerContent))),
isWelshTranslationAvailable = welshToggle,
accessibilityStatementUrl = Some("/accessibility-statement/customs-financials")
)(mainContent)
@hmrcStandardPage(

HmrcStandardPageParams(
pageTitle = fullPageTitle,
serviceURLs = ServiceURLs(
serviceUrl = Some(appConfig.customsFinancialsFrontendHomepage),
signOutUrl = Some(routes.LogoutController.logout.url),
accessibilityStatementUrl = Some("/accessibility-statement/customs-financials")
),
backLink = Some(BackLink(href = backLink.getOrElse(emptyString))),
templateOverrides = TemplateOverrides(
additionalHeadBlock = Some(additionalHead),
mainContentLayout = Some(if(fullWidth) fullWidthMainContent(_) else twoThirdsMainContent(_))
),
banners = Banners(
phaseBanner = Some(PhaseBanner(tag = Some(Tag(content = Text("BETA"))), content = HtmlContent(phaseBannerContent))),
userResearchBanner = Some(UserResearchBanner(url = appConfig.helpMakeGovUkBetterUrl))
),
isWelshTranslationAvailable = welshToggle
))(mainContent)
2 changes: 1 addition & 1 deletion app/views/cash_transactions_no_result.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@(summary: ResultsPageSummary)(implicit request: Request[_], messages: Messages, appConfig: AppConfig)

@main_template(pageTitle = Some(messages("cf.cash-account.detail.title")),
backLink = Some(controllers.routes.RequestTransactionsController.onPageLoad.url),
backLink = Some(controllers.routes.RequestTransactionsController.onPageLoad().url),
fullWidth = false,
help = true
) {
Expand Down
4 changes: 2 additions & 2 deletions app/views/cash_transactions_request_page.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
help = true
) {

@formHelper(action = controllers.routes.RequestTransactionsController.onSubmit(), 'autoComplete -> "off") {
@errorSummary(form.errors,errorFieldName = None, isErrorKeyUpdateEnabled = true, Some(updateFormErrorKeyForStartAndEndDate))
@formHelper(action = controllers.routes.RequestTransactionsController.onSubmit(), Symbol("autoComplete") -> "off") {
@errorSummary(form.errors,errorFieldName = None, isErrorKeyUpdateEnabled = true, Some(updateFormErrorKeyForStartAndEndDate()))
@h1("cf.cash-account.transactions.request.heading")
@p("cf.cash-account.transactions.request.p")

Expand Down
2 changes: 1 addition & 1 deletion app/views/cash_transactions_result_page.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)

@main_template(pageTitle = Some(messages("cf.cash-account.transactions.result.transaction")),
backLink = Some(controllers.routes.RequestTransactionsController.onPageLoad.url),
backLink = Some(controllers.routes.RequestTransactionsController.onPageLoad().url),
fullWidth = false,
help = true
) {
Expand Down
2 changes: 1 addition & 1 deletion app/views/cash_transactions_too_many_results.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@

@inset("cf.cash-account.transactions.result.between", additionalContent = Some(summary.rows(true).value.content.asHtml))

@link("cf.cash-account.transactions.result.search.again", controllers.routes.RequestTransactionsController.onPageLoad.url)
@link("cf.cash-account.transactions.result.search.again", controllers.routes.RequestTransactionsController.onPageLoad().url)
}
2 changes: 1 addition & 1 deletion app/views/components/inset.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
)(implicit messages: Messages)

@govukInsetText(InsetText(
content = HtmlContent(Html(messages(msg)) + additionalContent.map(v => s" $v").getOrElse("")),
content = HtmlContent(Html(messages(msg)).toString + additionalContent.map(v => s" $v").getOrElse("")),
id = id
))
Loading

0 comments on commit dcb2c56

Please sign in to comment.