diff --git a/app/uk/gov/hmrc/apiplatformoutboundsoap/controllers/ConfirmationController.scala b/app/uk/gov/hmrc/apiplatformoutboundsoap/controllers/ConfirmationController.scala index d4274cc..51b041f 100644 --- a/app/uk/gov/hmrc/apiplatformoutboundsoap/controllers/ConfirmationController.scala +++ b/app/uk/gov/hmrc/apiplatformoutboundsoap/controllers/ConfirmationController.scala @@ -40,11 +40,15 @@ class ConfirmationController @Inject()(cc: ControllerComponents, val xml: NodeSeq = request.body def callService(deliveryStatus: DeliveryStatus, id: String): Future[Result] = { - confirmationService.processConfirmation(xml, id, deliveryStatus) map { - case NoContentUpdateResult => NoContent - case _ => - logger.warn(s"No message found with global ID [$id]. Request is ${xml}") - NotFound + if(id.trim.nonEmpty) { + confirmationService.processConfirmation(xml, id.trim, deliveryStatus) map { + case NoContentUpdateResult => NoContent + case _ => + logger.warn(s"No message found with global ID [$id]. Request is ${xml}") + NotFound + } + } else { + Future.successful(BadRequest) } } diff --git a/test/uk/gov/hmrc/apiplatformoutboundsoap/controllers/ConfirmationControllerSpec.scala b/test/uk/gov/hmrc/apiplatformoutboundsoap/controllers/ConfirmationControllerSpec.scala index 3c8302d..e2c4d5f 100644 --- a/test/uk/gov/hmrc/apiplatformoutboundsoap/controllers/ConfirmationControllerSpec.scala +++ b/test/uk/gov/hmrc/apiplatformoutboundsoap/controllers/ConfirmationControllerSpec.scala @@ -44,7 +44,7 @@ class ConfirmationControllerSpec extends AnyWordSpec with Matchers with GuiceOne "message" should { val fakeRequest = FakeRequest("POST", "/acknowledgement") - val codMessage: String = + def createCodMessage(relatesTo: String) = { """ | | @@ -52,7 +52,7 @@ class ConfirmationControllerSpec extends AnyWordSpec with Matchers with GuiceOne | | [FROM] | - | 1234abcd + | [RELATES_TO] | [COD_MESSAGE_ID] | [TO] | @@ -62,6 +62,8 @@ class ConfirmationControllerSpec extends AnyWordSpec with Matchers with GuiceOne | | |""".stripMargin.replaceAll("\n", "") + .replaceAll("\\[RELATES_TO]", relatesTo) + } val codMessageWithNoRelatesTo = """ @@ -123,7 +125,7 @@ class ConfirmationControllerSpec extends AnyWordSpec with Matchers with GuiceOne val confirmationXmlRequestCaptor = ArgumentCaptor.forClass(classOf[NodeSeq]) val msgIdCaptor = ArgumentCaptor.forClass(classOf[String]) val confirmationTypeCaptor = ArgumentCaptor.forClass(classOf[DeliveryStatus]) - val requestBodyXml: Elem = XML.loadString(codMessage) + val requestBodyXml: Elem = XML.loadString(createCodMessage("1234abcd")) when(confirmationServiceMock.processConfirmation(confirmationXmlRequestCaptor.capture, msgIdCaptor.capture(), confirmationTypeCaptor.capture)(*)) .thenReturn(Future.successful(NoContentUpdateResult)) val result: Future[Result] = underTest.message()(fakeRequest.withBody(requestBodyXml) @@ -144,6 +146,16 @@ class ConfirmationControllerSpec extends AnyWordSpec with Matchers with GuiceOne verifyZeroInteractions(confirmationServiceMock) } + "return 400 when exists an XML request with blank, whitespaces, newline and tab space in RelatesTo element" in new Setup { + Seq("", " ", "\n", "\t") foreach { relatesTo => + val requestBodyXml: Elem = XML.loadString(createCodMessage(relatesTo)) + val result: Future[Result] = underTest.message()(fakeRequest.withBody(requestBodyXml) + .withHeaders("ContentType" -> "text/xml", "x-soap-action" -> "CCN2.Service.Platform.AcknowledgementService/CoD")) + status(result) shouldBe BAD_REQUEST + verifyZeroInteractions(confirmationServiceMock) + } + } + "call the confirmation service with a CoE request" in new Setup { val confirmationXmlRequestCaptor = ArgumentCaptor.forClass(classOf[NodeSeq]) val msgIdCaptor = ArgumentCaptor.forClass(classOf[String]) @@ -170,14 +182,14 @@ class ConfirmationControllerSpec extends AnyWordSpec with Matchers with GuiceOne } "handle receiving a request with an invalid SOAP action" in new Setup { - val result: Future[Result] = underTest.message()(fakeRequest.withBody(XML.loadString(codMessage)) + val result: Future[Result] = underTest.message()(fakeRequest.withBody(XML.loadString(createCodMessage("1234abcd"))) .withHeaders("ContentType" -> "text/xml", "x-soap-action" -> "foobar")) status(result) shouldBe BAD_REQUEST verifyZeroInteractions(confirmationServiceMock) } "handle receiving a request with no x-soap-action header" in new Setup { - val result: Future[Result] = underTest.message()(fakeRequest.withBody(XML.loadString(codMessage)) + val result: Future[Result] = underTest.message()(fakeRequest.withBody(XML.loadString(createCodMessage("1234abcd"))) .withHeaders("ContentType" -> "text/xml")) status(result) shouldBe BAD_REQUEST verifyZeroInteractions(confirmationServiceMock)