Skip to content

Commit

Permalink
API-6858 - Wrap the call to read WSDL in a class, so it can be mocked
Browse files Browse the repository at this point in the history
  • Loading branch information
anjumabbas5 committed Feb 29, 2024
1 parent 54ea273 commit fd0b5c0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class OutboundService @Inject() (
wsSecurityService: WsSecurityService,
outboundMessageRepository: OutboundMessageRepository,
notificationCallbackConnector: NotificationCallbackConnector,
wsdlParser: WsdlParser,
appConfig: AppConfig,
cache: AsyncCacheApi
)(implicit val ec: ExecutionContext,
Expand Down Expand Up @@ -188,7 +189,7 @@ class OutboundService @Inject() (

private def buildSoapRequest(message: MessageRequest): Future[SoapRequest] = {
cache.getOrElseUpdate[Definition](message.wsdlUrl, appConfig.cacheDuration) {
parseWsdl(message.wsdlUrl)
wsdlParser.parseWsdl(message.wsdlUrl)
} map { wsdlDefinition: Definition =>
val portType = wsdlDefinition.getAllPortTypes.asScala.values.head.asInstanceOf[PortType]
val operation: Operation = portType.getOperations.asScala.map(_.asInstanceOf[Operation])
Expand All @@ -208,12 +209,6 @@ class OutboundService @Inject() (
}
}

private def parseWsdl(wsdlUrl: String): Future[Definition] = {
val reader: WSDLReader = WSDLUtil.newWSDLReaderWithPopulatedExtensionRegistry
reader.setFeature("javax.wsdl.importDocuments", true)
Future.successful(reader.readWSDL(wsdlUrl))
}

private def addHeaders(message: MessageRequest, operation: Operation, envelope: SOAPEnvelope): Unit = {
val wsaNs: OMNamespace = envelope.declareNamespace(WSA_NAMESPACE, "wsa")
addSoapAction(operation, wsaNs, envelope)
Expand Down
37 changes: 37 additions & 0 deletions app/uk/gov/hmrc/apiplatformoutboundsoap/services/WsdlParser.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.apiplatformoutboundsoap.services

import org.apache.axis2.wsdl.WSDLUtil

import javax.wsdl.Definition
import javax.wsdl.xml.WSDLReader
import scala.concurrent.Future

// Turned off coverage for this as it was just one line and no need to test WSDLUtil code as it is not
// ours. However if this ever expands in future, tests will be needed and scoverage ignore removed.
// $COVERAGE-OFF$
class WsdlParser {

def parseWsdl(wsdlUrl: String): Future[Definition] = {
val reader: WSDLReader = WSDLUtil.newWSDLReaderWithPopulatedExtensionRegistry
reader.setFeature("javax.wsdl.importDocuments", true)
Future.successful(reader.readWSDL(wsdlUrl))
}

}
// $COVERAGE-ON$
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import java.util.UUID.randomUUID
import javax.wsdl.WSDLException
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.Future.successful
import scala.concurrent.Future.{failed, successful}
import scala.concurrent.duration.Duration

import com.mongodb.client.result.InsertOneResult
import org.apache.axiom.soap.SOAPEnvelope
import org.apache.pekko.stream.Materializer
Expand All @@ -38,12 +37,10 @@ import org.xmlunit.builder.DiffBuilder
import org.xmlunit.builder.DiffBuilder.compare
import org.xmlunit.diff.DefaultNodeMatcher
import org.xmlunit.diff.ElementSelectors.byName

import play.api.cache.AsyncCacheApi
import play.api.http.Status.OK
import play.api.test.Helpers._
import uk.gov.hmrc.http.{HeaderCarrier, NotFoundException}

import uk.gov.hmrc.apiplatformoutboundsoap.config.AppConfig
import uk.gov.hmrc.apiplatformoutboundsoap.connectors.{NotificationCallbackConnector, OutboundConnector}
import uk.gov.hmrc.apiplatformoutboundsoap.models._
Expand All @@ -60,6 +57,7 @@ class OutboundServiceSpec extends AnyWordSpec with Matchers with GuiceOneAppPerS
val outboundMessageRepositoryMock: OutboundMessageRepository = mock[OutboundMessageRepository]
val wsSecurityServiceMock: WsSecurityService = mock[WsSecurityService]
val notificationCallbackConnectorMock: NotificationCallbackConnector = mock[NotificationCallbackConnector]
val wsdlParser: WsdlParser = mock[WsdlParser]
val appConfigMock: AppConfig = mock[AppConfig]
val cacheSpy: AsyncCacheApi = spy[AsyncCacheApi](cache)
val httpStatus: Int = 200
Expand All @@ -76,7 +74,7 @@ class OutboundServiceSpec extends AnyWordSpec with Matchers with GuiceOneAppPerS
when(appConfigMock.proxyRequiredForThisEnvironment).thenReturn(false)

val underTest: OutboundService =
new OutboundService(outboundConnectorMock, wsSecurityServiceMock, outboundMessageRepositoryMock, notificationCallbackConnectorMock, appConfigMock, cacheSpy) {
new OutboundService(outboundConnectorMock, wsSecurityServiceMock, outboundMessageRepositoryMock, notificationCallbackConnectorMock, wsdlParser, appConfigMock, cacheSpy) {
override def now: Instant = expectedInstantNow

override def randomUUID: UUID = expectedGlobalId
Expand Down Expand Up @@ -418,12 +416,13 @@ class OutboundServiceSpec extends AnyWordSpec with Matchers with GuiceOneAppPerS
"fail when the given WSDL does not exist" in new Setup {
when(wsSecurityServiceMock.addUsernameToken(*)).thenReturn(expectedSoapEnvelope())
when(outboundConnectorMock.postMessage(*, *)).thenReturn(successful(expectedStatus))
when(wsdlParser.parseWsdl(*)).thenReturn(failed(new WSDLException(WSDLException.INVALID_WSDL, "This file was not found: http://localhost:3001/")))

val exception: WSDLException = intercept[WSDLException] {
await(underTest.sendMessage(messageRequestFullAddressing.copy(wsdlUrl = "https://github.com/hmrc/api-platform-outbound-soap/missing")))
await(underTest.sendMessage(messageRequestFullAddressing.copy(wsdlUrl = "http://localhost:3001/")))
}

exception.getMessage should include("This file was not found: https://github.com/hmrc/api-platform-outbound-soap/missing")
exception.getMessage should include("This file was not found: http://localhost:3001/")
}

"fail when the addressing.to field is empty" in new Setup {
Expand Down

0 comments on commit fd0b5c0

Please sign in to comment.