From 2003d53dad88e66efe045a131a16d94acda8b510 Mon Sep 17 00:00:00 2001 From: oliverabrahams Date: Tue, 1 Oct 2024 15:57:29 +0100 Subject: [PATCH 1/4] update the audio article image to include the type and the element ID --- .../app/model/dotcomrendering/DotcomRenderingDataModel.scala | 2 +- common/app/model/dotcomrendering/ElementsEnhancer.scala | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/common/app/model/dotcomrendering/DotcomRenderingDataModel.scala b/common/app/model/dotcomrendering/DotcomRenderingDataModel.scala index 3698db4bb64..b8771ad55e7 100644 --- a/common/app/model/dotcomrendering/DotcomRenderingDataModel.scala +++ b/common/app/model/dotcomrendering/DotcomRenderingDataModel.scala @@ -89,7 +89,7 @@ case class DotcomRenderingDataModel( commercialProperties: Map[String, EditionCommercialProperties], pageType: PageType, starRating: Option[Int], - audioArticleImage: Option[ImageBlockElement], + audioArticleImage: Option[PageElement], trailText: String, nav: Nav, showBottomSocialButtons: Boolean, diff --git a/common/app/model/dotcomrendering/ElementsEnhancer.scala b/common/app/model/dotcomrendering/ElementsEnhancer.scala index 8e74f7fe4a0..ca3dfb00e1c 100644 --- a/common/app/model/dotcomrendering/ElementsEnhancer.scala +++ b/common/app/model/dotcomrendering/ElementsEnhancer.scala @@ -73,6 +73,8 @@ object ElementsEnhancer { Json.obj("mainMediaElements" -> enhanceElements(obj.value("mainMediaElements"))) ++ Json.obj("keyEvents" -> enhanceObjectsWithElements(obj.value("keyEvents"))) ++ Json.obj("pinnedPost" -> enhanceObjectWithElements(obj.value("pinnedPost"))) ++ - Json.obj("promotedNewsletter" -> obj.value("promotedNewsletter")) + Json.obj("promotedNewsletter" -> obj.value("promotedNewsletter")) ++ + Json.obj("audioArticleImage" -> enhanceElement(obj.value("audioArticleImage"))) + } } From 26fe1e273efa57492d1c1a490ea5faae116b00c4 Mon Sep 17 00:00:00 2001 From: oliverabrahams Date: Tue, 1 Oct 2024 17:24:16 +0100 Subject: [PATCH 2/4] make sure the element is a JS object before adding elementId --- .../dotcomrendering/ElementsEnhancer.scala | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/common/app/model/dotcomrendering/ElementsEnhancer.scala b/common/app/model/dotcomrendering/ElementsEnhancer.scala index ca3dfb00e1c..a754b6b466a 100644 --- a/common/app/model/dotcomrendering/ElementsEnhancer.scala +++ b/common/app/model/dotcomrendering/ElementsEnhancer.scala @@ -10,18 +10,19 @@ import play.api.libs.json.{Json, _} object ElementsEnhancer { def enhanceElement(element: JsValue): JsValue = { - // Add an elementId to the element - val elementWithId = element.as[JsObject] ++ Json.obj("elementId" -> java.util.UUID.randomUUID.toString) - // Extract element type - val elementType = elementWithId.value("_type").as[String] + // Check if the element is a JsObject before proceeding + element match { + case obj: JsObject => + val elementWithId = obj ++ Json.obj("elementId" -> java.util.UUID.randomUUID.toString) + val elementType = elementWithId.value("_type").as[String] - // If element has further nesting, continue to enhance. otherwise, return the enhanced element - elementType match { - case "model.dotcomrendering.pageElements.ListBlockElement" => enhanceListBlockElement(elementWithId) - case "model.dotcomrendering.pageElements.TimelineBlockElement" => enhanceTimelineBlockElement(elementWithId) - case _ => elementWithId; + elementType match { + case "model.dotcomrendering.pageElements.ListBlockElement" => enhanceListBlockElement(elementWithId) + case "model.dotcomrendering.pageElements.TimelineBlockElement" => enhanceTimelineBlockElement(elementWithId) + case _ => elementWithId; + } + case _ => element } - } def enhanceListBlockElement(elementWithId: JsObject): JsObject = { val listItems = elementWithId.value("items").as[JsArray] @@ -75,6 +76,5 @@ object ElementsEnhancer { Json.obj("pinnedPost" -> enhanceObjectWithElements(obj.value("pinnedPost"))) ++ Json.obj("promotedNewsletter" -> obj.value("promotedNewsletter")) ++ Json.obj("audioArticleImage" -> enhanceElement(obj.value("audioArticleImage"))) - } } From e93271c9944e4b7e943b3dd36b83ba3964d0be1c Mon Sep 17 00:00:00 2001 From: oliverabrahams Date: Wed, 2 Oct 2024 16:32:46 +0100 Subject: [PATCH 3/4] added some tests to the elementEnhancer --- .../dotcomrendering/ElementEnhancerTest.scala | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 common/test/model/dotcomrendering/ElementEnhancerTest.scala diff --git a/common/test/model/dotcomrendering/ElementEnhancerTest.scala b/common/test/model/dotcomrendering/ElementEnhancerTest.scala new file mode 100644 index 00000000000..d664f8049a2 --- /dev/null +++ b/common/test/model/dotcomrendering/ElementEnhancerTest.scala @@ -0,0 +1,68 @@ +package model.dotcomrendering + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import play.api.libs.json.{JsArray, JsNull, JsObject, JsValue, Json} +import model.dotcomrendering.ElementsEnhancer + +class ElementEnhancerTest extends AnyFlatSpec with Matchers { + + "enhanceElement" should "add elementId and enhance ListBlockElement" in { + val mockElement: JsObject = Json.obj( + "_type" -> "model.dotcomrendering.pageElements.ListBlockElement", + "items" -> Json.arr(Json.obj( + "elements" -> Json.arr(Json.obj("_type" -> "model.dotcomrendering.pageElements.TextElement"))) + ) + ) + + val result = ElementsEnhancer.enhanceElement(mockElement) + + (result \ "elementId").asOpt[String] should not be empty + val items = (result \ "items").as[JsArray] + items.value.size shouldEqual 1 + val elements = (items(0) \ "elements").as[JsArray] + elements.value.size shouldEqual 1 + (elements(0) \ "elementId").as[String] should not be empty + } + + "enhanceElement" should "add elementId and enhance TimelineBlockElement" in { + val mockElement: JsObject = Json.obj( + "_type" -> "model.dotcomrendering.pageElements.TimelineBlockElement", + "sections" -> Json.arr( + Json.obj( + "events" -> Json.arr( + Json.obj("body" -> Json.arr(Json.obj("_type" -> "model.dotcomrendering.pageElements.TextElement"))) + ) + ) + ) + ) + + val result = ElementsEnhancer.enhanceElement(mockElement) + + (result \ "elementId").asOpt[String] should not be empty + + val sections = (result \ "sections").as[JsArray] + val events = (sections(0) \ "events").as[JsArray] + val body = (events(0) \ "body").as[JsArray] + val bodyElementIds = (body(0) \ "elementId").as[String] + bodyElementIds should not be empty + } + + "enhanceElement" should "return the original element with elementId for unknown types" in { + val mockElement: JsObject = Json.obj( + "_type" -> "model.dotcomrendering.pageElements.TextElement", + "someField" -> "someValue" + ) + + val result = ElementsEnhancer.enhanceElement(mockElement) + + (result \ "elementId").asOpt[String] should not be empty + (result \ "someField").asOpt[String] shouldEqual Some("someValue") + } + + "enhanceElement" should "return JsNull if JsNull is passed" in { + val result = ElementsEnhancer.enhanceElement(JsNull) + + result shouldEqual JsNull + } +} From e69880f13297ed9651db33fcbeefc6b6110748ea Mon Sep 17 00:00:00 2001 From: oliverabrahams Date: Wed, 2 Oct 2024 17:09:38 +0100 Subject: [PATCH 4/4] formatting --- .../dotcomrendering/ElementEnhancerTest.scala | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/common/test/model/dotcomrendering/ElementEnhancerTest.scala b/common/test/model/dotcomrendering/ElementEnhancerTest.scala index d664f8049a2..c9985590b10 100644 --- a/common/test/model/dotcomrendering/ElementEnhancerTest.scala +++ b/common/test/model/dotcomrendering/ElementEnhancerTest.scala @@ -10,9 +10,9 @@ class ElementEnhancerTest extends AnyFlatSpec with Matchers { "enhanceElement" should "add elementId and enhance ListBlockElement" in { val mockElement: JsObject = Json.obj( "_type" -> "model.dotcomrendering.pageElements.ListBlockElement", - "items" -> Json.arr(Json.obj( - "elements" -> Json.arr(Json.obj("_type" -> "model.dotcomrendering.pageElements.TextElement"))) - ) + "items" -> Json.arr( + Json.obj("elements" -> Json.arr(Json.obj("_type" -> "model.dotcomrendering.pageElements.TextElement"))), + ), ) val result = ElementsEnhancer.enhanceElement(mockElement) @@ -31,10 +31,10 @@ class ElementEnhancerTest extends AnyFlatSpec with Matchers { "sections" -> Json.arr( Json.obj( "events" -> Json.arr( - Json.obj("body" -> Json.arr(Json.obj("_type" -> "model.dotcomrendering.pageElements.TextElement"))) - ) - ) - ) + Json.obj("body" -> Json.arr(Json.obj("_type" -> "model.dotcomrendering.pageElements.TextElement"))), + ), + ), + ), ) val result = ElementsEnhancer.enhanceElement(mockElement) @@ -51,7 +51,7 @@ class ElementEnhancerTest extends AnyFlatSpec with Matchers { "enhanceElement" should "return the original element with elementId for unknown types" in { val mockElement: JsObject = Json.obj( "_type" -> "model.dotcomrendering.pageElements.TextElement", - "someField" -> "someValue" + "someField" -> "someValue", ) val result = ElementsEnhancer.enhanceElement(mockElement)