From 55cc2dcf06222793e1df93b7c0ed43e2fb68505f Mon Sep 17 00:00:00 2001 From: ian-hoyle Date: Wed, 9 Oct 2024 09:48:54 +0100 Subject: [PATCH] Added tests for messages from properties file (#4205) Added properties for UTF_8 failure details and action. Also added tests to check messages in properties files are correct --- ...DraftMetadataChecksResultsController.scala | 19 +-- conf/messages | 2 + ...tMetadataChecksResultsControllerSpec.scala | 117 +++++++----------- 3 files changed, 61 insertions(+), 77 deletions(-) diff --git a/app/controllers/DraftMetadataChecksResultsController.scala b/app/controllers/DraftMetadataChecksResultsController.scala index 4f28fbffc..26f5d7400 100644 --- a/app/controllers/DraftMetadataChecksResultsController.scala +++ b/app/controllers/DraftMetadataChecksResultsController.scala @@ -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._ @@ -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 { @@ -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" } diff --git a/conf/messages b/conf/messages index 9ed9892ce..3818e2e1b 100644 --- a/conf/messages +++ b/conf/messages @@ -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)''. diff --git a/test/controllers/DraftMetadataChecksResultsControllerSpec.scala b/test/controllers/DraftMetadataChecksResultsControllerSpec.scala index 9c5cc94b5..e77d31c7d 100644 --- a/test/controllers/DraftMetadataChecksResultsControllerSpec.scala +++ b/test/controllers/DraftMetadataChecksResultsControllerSpec.scala @@ -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, _} @@ -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 @@ -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) @@ -145,36 +153,8 @@ class DraftMetadataChecksResultsControllerSpec extends FrontEndTestHelper { | Results of your metadata checks |""".stripMargin ) - pageAsString must include( - s"""
- |
- |
- | Status - |
- |
- | Issues found - |
- |
- | - |
- |
- | Details - |
- |
- | Require details message for draftMetadata.validation.details.$fileError - |
- |
- | - |
- |
- | Action - |
- |
- | Require action message for draftMetadata.validation.action.$fileError - |
- |
- |
""".stripMargin - ) + pageAsString must include(detailsMessage) + pageAsString must include(actionMessage) pageAsString must include("""

The report below contains details about issues found.

""") pageAsString must include( s""" @@ -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 'CSV UTF-8 (comma separated)'." + ) ) - 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()) @@ -222,36 +210,8 @@ class DraftMetadataChecksResultsControllerSpec extends FrontEndTestHelper { | Results of your metadata checks |""".stripMargin ) - pageAsString must include( - s"""
- |
- |
- | Status - |
- |
- | Issues found - |
- |
- | - |
- |
- | Details - |
- |
- | Require details message for draftMetadata.validation.details.$fileError - |
- |
- | - |
- |
- | Action - |
- |
- | Require action message for draftMetadata.validation.action.$fileError - |
- |
- |
""".stripMargin - ) + pageAsString must include(detailsMessage) + pageAsString must include(actionMessage) } } } @@ -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 + ) } }