diff --git a/app/uk/gov/hmrc/apiplatformoutboundsoap/services/OutboundService.scala b/app/uk/gov/hmrc/apiplatformoutboundsoap/services/OutboundService.scala index 9ca2be5..177c753 100644 --- a/app/uk/gov/hmrc/apiplatformoutboundsoap/services/OutboundService.scala +++ b/app/uk/gov/hmrc/apiplatformoutboundsoap/services/OutboundService.scala @@ -140,7 +140,7 @@ class OutboundService @Inject()(outboundConnector: OutboundConnector, } private def addHeaders(message: MessageRequest, operation: Operation, envelope: SOAPEnvelope): Unit = { - val wsaNs: OMNamespace = getOMFactory.createOMNamespace(WSA_NAMESPACE, "wsa") + val wsaNs: OMNamespace = envelope.declareNamespace(WSA_NAMESPACE, "wsa") addSoapAction(operation, wsaNs, envelope) addMessageHeader(message, operation, envelope) addOptionalAddressingHeaders(message, wsaNs, envelope) @@ -175,10 +175,10 @@ class OutboundService @Inject()(outboundConnector: OutboundConnector, } private def addOptionalAddressingHeaders(message: MessageRequest, wsaNs: OMNamespace, envelope: SOAPEnvelope): Unit = { - message.addressing.from.foreach(addToSoapHeader(_, WSA_FROM, wsaNs, envelope)) + message.addressing.from.foreach(addWithAddressElementChildToSoapHeader(_, WSA_FROM, wsaNs, envelope)) addToSoapHeader(message.addressing.to, WSA_TO, wsaNs, envelope) - addToSoapHeader(message.addressing.replyTo, WSA_REPLY_TO, wsaNs, envelope) - message.addressing.faultTo.foreach(addToSoapHeader(_, WSA_FAULT_TO, wsaNs, envelope)) + addWithAddressElementChildToSoapHeader(message.addressing.replyTo, WSA_REPLY_TO, wsaNs, envelope) + message.addressing.faultTo.foreach(addWithAddressElementChildToSoapHeader(_, WSA_FAULT_TO, wsaNs, envelope)) addToSoapHeader(message.addressing.messageId, WSA_MESSAGE_ID, wsaNs, envelope) message.addressing.relatesTo.foreach(addToSoapHeader(_, WSA_RELATES_TO, wsaNs, envelope)) } @@ -189,6 +189,14 @@ class OutboundService @Inject()(outboundConnector: OutboundConnector, envelope.getHeader.addChild(addressingElement) } + private def addWithAddressElementChildToSoapHeader(property: String, elementName: String, namespace: OMNamespace, envelope: SOAPEnvelope): Unit = { + val addressingElement: OMElement = getOMFactory.createOMElement(elementName, namespace) + val addressingElementInner: OMElement = getOMFactory.createOMElement("Address", namespace) + getOMFactory.createOMText(addressingElementInner, property) + addressingElement.addChild(addressingElementInner) + envelope.getHeader.addChild(addressingElement) + } + private def addBody(message: MessageRequest, operation: Operation, envelope: SOAPEnvelope): Unit = { val inputMessageName = operation.getInput.getMessage.getParts.asScala.values.head.asInstanceOf[Part].getElementName val payload = getOMFactory.createOMElement(inputMessageName) diff --git a/test/uk/gov/hmrc/apiplatformoutboundsoap/services/OutboundServiceSpec.scala b/test/uk/gov/hmrc/apiplatformoutboundsoap/services/OutboundServiceSpec.scala index 8187b75..7091707 100644 --- a/test/uk/gov/hmrc/apiplatformoutboundsoap/services/OutboundServiceSpec.scala +++ b/test/uk/gov/hmrc/apiplatformoutboundsoap/services/OutboundServiceSpec.scala @@ -115,21 +115,21 @@ class OutboundServiceSpec extends AnyWordSpec with Matchers with GuiceOneAppPerS val expectedStatus: Int = OK val allAddressingHeaders = - """HMRC - |CCN2 - |ReplyTo - |FaultTo - |123 - |RelatesTo""".stripMargin.replaceAll("\n", "") + """HMRC + |CCN2 + |ReplyTo + |FaultTo + |123 + |RelatesTo""".stripMargin.replaceAll("\n", "") val mandatoryAddressingHeaders = - """CCN2 - |ReplyTo - |123""".stripMargin.replaceAll("\n", "") + """CCN2 + |ReplyTo + |123""".stripMargin.replaceAll("\n", "") def expectedSoapEnvelope(extraHeaders: String = ""): String = s""" - | + | | |CCN2.Service.Customs.EU.ICS.RiskAnalysisOrchestrationBAS/IE4N03notifyERiskAnalysisHit | @@ -291,8 +291,6 @@ class OutboundServiceSpec extends AnyWordSpec with Matchers with GuiceOneAppPerS when(outboundMessageRepositoryMock.persist(*)).thenReturn(Future(InsertOneResult.acknowledged(BsonNumber(1)))) await(underTest.sendMessage(messageRequestMinimalAddressing)) - getXmlDiff(messageCaptor.getValue.toString, expectedSoapEnvelope(mandatoryAddressingHeaders)).build().getDifferences.forEach(d => println(d)) - getXmlDiff(messageCaptor.getValue.toString, expectedSoapEnvelope(mandatoryAddressingHeaders)).build().hasDifferences shouldBe false } @@ -304,7 +302,6 @@ class OutboundServiceSpec extends AnyWordSpec with Matchers with GuiceOneAppPerS when(outboundMessageRepositoryMock.persist(*)).thenReturn(Future(InsertOneResult.acknowledged(BsonNumber(1)))) await(underTest.sendMessage(messageRequestMinimalAddressing)) - getXmlDiff(messageCaptor.getValue.toString, expectedSoapEnvelope(mandatoryAddressingHeaders)).build().getDifferences.forEach(d => println(d)) getXmlDiff(messageCaptor.getValue.toString, expectedSoapEnvelope(mandatoryAddressingHeaders)).build().hasDifferences shouldBe false }