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..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] @@ -73,6 +74,7 @@ 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"))) } } diff --git a/common/test/model/dotcomrendering/ElementEnhancerTest.scala b/common/test/model/dotcomrendering/ElementEnhancerTest.scala new file mode 100644 index 00000000000..c9985590b10 --- /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 + } +}