Skip to content

Commit

Permalink
Added tests for messages from properties file (#4205)
Browse files Browse the repository at this point in the history
Added properties for UTF_8 failure details and action.
Also added tests to check messages in properties files are correct
  • Loading branch information
ian-hoyle authored Oct 9, 2024
1 parent 5864a3c commit 55cc2dc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 77 deletions.
19 changes: 11 additions & 8 deletions app/controllers/DraftMetadataChecksResultsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import controllers.util.ExcelUtils
import controllers.util.MetadataProperty.filePath
import graphql.codegen.GetConsignmentStatus.getConsignmentStatus.GetConsignment
import org.pac4j.play.scala.SecurityComponents
import play.api.i18n.{I18nSupport, Messages}
import play.api.i18n.{I18nSupport, Lang, MessagesApi}
import play.api.mvc.{Action, AnyContent, Request}
import services.FileError.SCHEMA_VALIDATION
import services.Statuses._
Expand All @@ -26,7 +26,8 @@ class DraftMetadataChecksResultsController @Inject() (
val consignmentService: ConsignmentService,
val applicationConfig: ApplicationConfig,
val consignmentStatusService: ConsignmentStatusService,
val draftMetadataService: DraftMetadataService
val draftMetadataService: DraftMetadataService,
val messages: MessagesApi
)(implicit val ec: ExecutionContext)
extends TokenSecurity
with I18nSupport {
Expand Down Expand Up @@ -73,18 +74,20 @@ class DraftMetadataChecksResultsController @Inject() (
}
}

private def actionMessage(fileError: FileError.FileError)(implicit messages: Messages): String = {
implicit val defaultLang: Lang = Lang.defaultLang

private def actionMessage(fileError: FileError.FileError): String = {
val key = s"draftMetadata.validation.action.$fileError"
if (Messages.isDefinedAt(key))
Messages(key)
if (messages.isDefinedAt(key))
messages(key)
else
s"Require action message for $key"
}

private def detailsMessage(fileError: FileError.FileError)(implicit messages: Messages): String = {
private def detailsMessage(fileError: FileError.FileError): String = {
val key = s"draftMetadata.validation.details.$fileError"
if (Messages.isDefinedAt(key))
Messages(key)
if (messages.isDefinedAt(key))
messages(key)
else
s"Require details message for $key"
}
Expand Down
2 changes: 2 additions & 0 deletions conf/messages
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ notification.savedProgress.metadataInfo=Your records and any metadata you added
additionalMetadata.descriptive.sensitive=If the description of a record contains sensitive information, you must enter the full uncensored version on the Descriptive metadata page before entering an alternative description on the Closure metadata page.
draftMetadata.validation.details.SCHEMA_VALIDATION=We found validation errors in the uploaded metadata.
draftMetadata.validation.action.SCHEMA_VALIDATION=Download the report below for details on individual validation errors.
draftMetadata.validation.details.UTF_8=The metadata file you uploaded was not a CSV.
draftMetadata.validation.action.UTF_8=Ensure that you save your Excel file as file type ''CSV UTF-8 (comma separated)''.
117 changes: 48 additions & 69 deletions test/controllers/DraftMetadataChecksResultsControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.scalatest.matchers.should.Matchers._
import play.api.Configuration
import play.api.Play.materializer
import play.api.http.HttpVerbs.GET
import play.api.i18n.DefaultMessagesApi
import play.api.test.CSRFTokenHelper._
import play.api.test.FakeRequest
import play.api.test.Helpers.{contentAsBytes, contentAsString, defaultAwaitTimeout, status => playStatus, _}
Expand All @@ -22,9 +23,11 @@ import uk.gov.nationalarchives.tdr.validation.Metadata

import java.io.ByteArrayInputStream
import java.time.{LocalDateTime, ZoneId, ZonedDateTime}
import java.util.UUID
import java.util.{Properties, UUID}
import scala.concurrent.{ExecutionContext, Future}
import scala.io.Source
import scala.jdk.CollectionConverters.IterableHasAsScala
import scala.util.Using

class DraftMetadataChecksResultsControllerSpec extends FrontEndTestHelper {
implicit val ec: ExecutionContext = ExecutionContext.global
Expand Down Expand Up @@ -117,10 +120,15 @@ class DraftMetadataChecksResultsControllerSpec extends FrontEndTestHelper {

"DraftMetadataChecksResultsController should render the error page with error report download button" should {
val draftMetadataStatuses = Table(
("status", "fileError"),
(CompletedWithIssuesValue.value, FileError.SCHEMA_VALIDATION)
("status", "fileError", "detailsMessage", "actionMessage"),
(
CompletedWithIssuesValue.value,
FileError.SCHEMA_VALIDATION,
"We found validation errors in the uploaded metadata.",
"Download the report below for details on individual validation errors."
)
)
forAll(draftMetadataStatuses) { (statusValue, fileError) =>
forAll(draftMetadataStatuses) { (statusValue, fileError, detailsMessage, actionMessage) =>
{
s"render the draftMetadataResults page when the status is $statusValue" in {
val controller = instantiateController(blockDraftMetadataUpload = false, fileError = fileError)
Expand All @@ -145,36 +153,8 @@ class DraftMetadataChecksResultsControllerSpec extends FrontEndTestHelper {
| Results of your metadata checks
|</h1>""".stripMargin
)
pageAsString must include(
s"""<dl class="govuk-summary-list">
| <div class="govuk-summary-list__row">
| <dt class="govuk-summary-list__key">
| Status
| </dt>
| <dd class="govuk-summary-list__value">
| <strong class="govuk-tag govuk-tag--orange">Issues found</strong>
| </dd>
| </div>
|
| <div class="govuk-summary-list__row">
| <dt class="govuk-summary-list__key">
| Details
| </dt>
| <dd class="govuk-summary-list__value">
| Require details message for draftMetadata.validation.details.$fileError
| </dd>
| </div>
|
| <div class="govuk-summary-list__row">
| <dt class="govuk-summary-list__key">
| Action
| </dt>
| <dd class="govuk-summary-list__value">
| Require action message for draftMetadata.validation.action.$fileError
| </dd>
| </div>
|</dl>""".stripMargin
)
pageAsString must include(detailsMessage)
pageAsString must include(actionMessage)
pageAsString must include("""<p class="govuk-body">The report below contains details about issues found.</p>""")
pageAsString must include(
s"""<a class="govuk-button govuk-button--secondary govuk-!-margin-bottom-8 download-metadata" href="/consignment/$consignmentId/draft-metadata/download-report">
Expand All @@ -194,16 +174,24 @@ class DraftMetadataChecksResultsControllerSpec extends FrontEndTestHelper {

"DraftMetadataChecksResultsController should render the error page with no error download for some errors" should {
val draftMetadataStatuses = Table(
("status", "fileError"),
(FailedValue.value, FileError.UNKNOWN)
("status", "fileError", "detailsMessage", "actionMessage"),
(FailedValue.value, FileError.UNKNOWN, "Require details message for draftMetadata.validation.details.", "Require action message for draftMetadata.validation.action."),
(
CompletedWithIssuesValue.value,
FileError.UTF_8,
"The metadata file you uploaded was not a CSV.",
s"Ensure that you save your Excel file as file type &#x27;CSV UTF-8 (comma separated)&#x27;."
)
)
forAll(draftMetadataStatuses) { (statusValue, fileError) =>
forAll(draftMetadataStatuses) { (statusValue, fileError, detailsMessage, actionMessage) =>
{
s"render the draftMetadataResults page when the status is $statusValue" in {
s"render the draftMetadataResults page when the status is $statusValue and error is $fileError" in {
val controller = instantiateController(blockDraftMetadataUpload = false, fileError = fileError)
when(draftMetaDataService.getErrorTypeFromErrorJson(any[UUID])).thenReturn(Future.successful(fileError))
val additionalMetadataEntryMethodPage = controller
.draftMetadataChecksResultsPage(consignmentId)
.apply(FakeRequest(GET, "/draft-metadata/checks-results").withCSRFToken)

setConsignmentTypeResponse(wiremockServer, "standard")
setConsignmentReferenceResponse(wiremockServer)
val someDateTime = ZonedDateTime.of(LocalDateTime.of(2022, 3, 10, 1, 0), ZoneId.systemDefault())
Expand All @@ -222,36 +210,8 @@ class DraftMetadataChecksResultsControllerSpec extends FrontEndTestHelper {
| Results of your metadata checks
|</h1>""".stripMargin
)
pageAsString must include(
s"""<dl class="govuk-summary-list">
| <div class="govuk-summary-list__row">
| <dt class="govuk-summary-list__key">
| Status
| </dt>
| <dd class="govuk-summary-list__value">
| <strong class="govuk-tag govuk-tag--orange">Issues found</strong>
| </dd>
| </div>
|
| <div class="govuk-summary-list__row">
| <dt class="govuk-summary-list__key">
| Details
| </dt>
| <dd class="govuk-summary-list__value">
| Require details message for draftMetadata.validation.details.$fileError
| </dd>
| </div>
|
| <div class="govuk-summary-list__row">
| <dt class="govuk-summary-list__key">
| Action
| </dt>
| <dd class="govuk-summary-list__value">
| Require action message for draftMetadata.validation.action.$fileError
| </dd>
| </div>
|</dl>""".stripMargin
)
pageAsString must include(detailsMessage)
pageAsString must include(actionMessage)
}
}
}
Expand Down Expand Up @@ -328,6 +288,25 @@ class DraftMetadataChecksResultsControllerSpec extends FrontEndTestHelper {
val consignmentStatusService = new ConsignmentStatusService(graphQLConfiguration)
when(draftMetaDataService.getErrorTypeFromErrorJson(any[UUID])).thenReturn(Future.successful(fileError))

new DraftMetadataChecksResultsController(securityComponents, keycloakConfiguration, consignmentService, applicationConfig, consignmentStatusService, draftMetaDataService)
val properties = new Properties()
Using(Source.fromFile("conf/messages")) { source =>
properties.load(source.bufferedReader())
}
import scala.jdk.CollectionConverters._
val map = properties.asScala.toMap
val testMessages = Map(
"default" -> map
)
val messagesApi = new DefaultMessagesApi(testMessages)

new DraftMetadataChecksResultsController(
securityComponents,
keycloakConfiguration,
consignmentService,
applicationConfig,
consignmentStatusService,
draftMetaDataService,
messagesApi
)
}
}

0 comments on commit 55cc2dc

Please sign in to comment.