Skip to content

Commit

Permalink
Merge pull request #5 from hmrc/APB-716-tidy
Browse files Browse the repository at this point in the history
Apb 716 tidy
  • Loading branch information
ralphcollett authored May 2, 2017
2 parents cc1fbd0 + b06b45b commit 01c005e
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 77 deletions.
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,50 @@

[![Build Status](https://travis-ci.org/hmrc/agent-client-relationships.svg)](https://travis-ci.org/hmrc/agent-client-relationships) [ ![Download](https://api.bintray.com/packages/hmrc/releases/agent-client-relationships/images/download.svg) ](https://bintray.com/hmrc/releases/agent-client-relationships/_latestVersion)

This is a placeholder README.md for a new repository
This is a backend microservice whose domain is Agent Client Relationships.

## Running the tests

sbt test it:test

## Running the app locally

sm --start AGENT_MTD -f
sm --stop AGENT_CLIENT_RELATIONSHIPS
./run-local

## Proposed API

We're still building this service so some/all of the API described here might not be implemented yet!

### Check whether a relationship exists between an Agent and a client for service HMRC-MTD-IT

GET /agent/:arn/service/HMRC-MTD-IT/client/MTDITID/:mtditid

This endpoint checks whether the agent represented by the arn is allocated to the client represented by the mtdItId
within Government Gateway.

Possible responses:

#### OK

If the agent is allocated to the client then a 200 OK response with no JSON body will be returned

#### Not Found

If the agent is not allocated to the client then a 404 Not Found will be returned with a JSON structure as follows:

{
"code": "RELATIONSHIP_NOT_FOUND"
}

The provided code is to help diagnose potential issues in production and will usually be "RELATIONSHIP_NOT_FOUND".


### Future development

It is anticipated that additional methods for other services will be added and will use a similar utl structure.


### License

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@ package uk.gov.hmrc.agentclientrelationships.connectors

import java.net.URL
import javax.inject.{Inject, Named, Singleton}
import javax.xml.XMLConstants
import javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING
import javax.xml.parsers.SAXParserFactory

import org.apache.xerces.impl.Constants
import com.codahale.metrics.MetricRegistry
import com.kenshoo.play.metrics.Metrics
import org.apache.xerces.impl.Constants._
import play.api.http.ContentTypes.XML
import play.api.http.HeaderNames.CONTENT_TYPE
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import uk.gov.hmrc.agent.kenshoo.monitoring.HttpAPIMonitor
import uk.gov.hmrc.play.http.{HeaderCarrier, HttpPost, Upstream5xxResponse}
import uk.gov.hmrc.agentmtdidentifiers.model.{Arn, MtdItId}
import uk.gov.hmrc.domain.AgentCode
import uk.gov.hmrc.play.http.{HeaderCarrier, HttpPost}

import scala.concurrent.Future
import scala.xml.Elem
import com.kenshoo.play.metrics.Metrics
import uk.gov.hmrc.agentmtdidentifiers.model.{Arn, MtdItId}
import uk.gov.hmrc.domain.AgentCode
import scala.xml.XML.withSAXParser

@Singleton
class GovernmentGatewayProxyConnector @Inject()(@Named("government-gateway-proxy-baseUrl") baseUrl: URL, httpPost: HttpPost, metrics: Metrics)
extends HttpAPIMonitor {
override val kenshooRegistry = metrics.defaultRegistry
override val kenshooRegistry: MetricRegistry = metrics.defaultRegistry

private def path(method: String): String = new URL(baseUrl, s"/government-gateway-proxy/api/admin/$method").toString

Expand All @@ -59,9 +61,9 @@ class GovernmentGatewayProxyConnector @Inject()(@Named("government-gateway-proxy
})
}

def getAllocatedAgentCodes(mtditid: MtdItId)(implicit hc: HeaderCarrier): Future[Set[AgentCode]] = {
def getAllocatedAgentCodes(mtdItid: MtdItId)(implicit hc: HeaderCarrier): Future[Set[AgentCode]] = {
monitor("ConsumedAPI-GGW-GsoAdminGetAssignedAgents-POST") {
httpPost.POSTString(path("GsoAdminGetAssignedAgents"), GsoAdminGetAssignedAgentsXmlInput(mtditid), Seq(CONTENT_TYPE -> XML))
httpPost.POSTString(path("GsoAdminGetAssignedAgents"), GsoAdminGetAssignedAgentsXmlInput(mtdItid), Seq(CONTENT_TYPE -> XML))
}.map({ response =>
val xml: Elem = toXmlElement(response.body)
val agentDetails = xml \ "AllocatedAgents" \ "AgentDetails"
Expand All @@ -71,13 +73,11 @@ class GovernmentGatewayProxyConnector @Inject()(@Named("government-gateway-proxy

private def toXmlElement(xmlString: String): Elem = {
val factory = SAXParserFactory.newInstance("org.apache.xerces.jaxp.SAXParserFactoryImpl", this.getClass.getClassLoader)
factory.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE, false)
factory.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE, false)
factory.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.DISALLOW_DOCTYPE_DECL_FEATURE, true)
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true)
val XML = scala.xml.XML.withSAXParser(factory.newSAXParser())

XML.loadString(xmlString)
factory.setFeature(SAX_FEATURE_PREFIX + EXTERNAL_GENERAL_ENTITIES_FEATURE, false)
factory.setFeature(SAX_FEATURE_PREFIX + EXTERNAL_PARAMETER_ENTITIES_FEATURE, false)
factory.setFeature(XERCES_FEATURE_PREFIX + DISALLOW_DOCTYPE_DECL_FEATURE, true)
factory.setFeature(FEATURE_SECURE_PROCESSING, true)
withSAXParser(factory.newSAXParser())loadString xmlString
}

private def GsoAdminGetCredentialsForDirectEnrolmentsXmlInput(arn: Arn): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package uk.gov.hmrc.agentclientrelationships.controllers

import javax.inject.{Inject, Singleton}

import play.api.mvc.Action
import play.api.mvc.{Action, AnyContent}
import uk.gov.hmrc.agentclientrelationships.connectors.{GovernmentGatewayProxyConnector, RelationshipNotFound}
import uk.gov.hmrc.agentclientrelationships.controllers.fluentSyntax._
import uk.gov.hmrc.agentmtdidentifiers.model.{Arn, MtdItId}
Expand All @@ -29,21 +29,18 @@ import scala.concurrent.ExecutionContext.Implicits.global
@Singleton
class Relationships @Inject()(val gg: GovernmentGatewayProxyConnector) extends BaseController {

def check(arn: Arn, mtditid: MtdItId) = Action.async { implicit request =>
def check(arn: Arn, mtdItId: MtdItId): Action[AnyContent] = Action.async { implicit request =>

val result = for {
credentialIdentifier <- gg.getCredIdFor(arn)
agentCode <- gg.getAgentCodeFor(credentialIdentifier)
allocatedAgents <- gg.getAllocatedAgentCodes(mtditid)
allocatedAgents <- gg.getAllocatedAgentCodes(mtdItId)
result <- if (allocatedAgents.contains(agentCode)) returnValue(agentCode)
else raiseError(RelationshipNotFound("RELATIONSHIP_NOT_FOUND"))
} yield result

result map {
case _ => Ok("")
} recover {
result map (_ => Ok ) recover {
case RelationshipNotFound(errorCode) => NotFound(toJson(errorCode))
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package uk.gov.hmrc.agentclientrelationships.controllers
import play.api.libs.json.{JsObject, Json}

import scala.concurrent.Future
import scala.concurrent.Future.{failed, successful}

object fluentSyntax {

def returnValue[T](a: T): Future[T] = Future.successful(a)
def returnValue[T](a: T): Future[T] = successful(a)

def raiseError(exception: Throwable): Future[Nothing] = Future.failed(exception)
def raiseError(exception: Throwable): Future[Nothing] = failed(exception)

def toJson(code: String): JsObject = Json.obj("code" -> code)

Expand Down
15 changes: 5 additions & 10 deletions app/uk/gov/hmrc/agentclientrelationships/microserviceGlobal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import uk.gov.hmrc.play.microservice.bootstrap.DefaultMicroserviceGlobal

class GuiceModule() extends AbstractModule with ServicesConfig {

def configure() = {
def configure(): Unit = {
bind(classOf[HttpGet]).toInstance(WSHttp)
bind(classOf[HttpPost]).toInstance(WSHttp)
bind(classOf[AuditConnector]).toInstance(MicroserviceGlobal.auditConnector)
Expand All @@ -50,31 +50,27 @@ class GuiceModule() extends AbstractModule with ServicesConfig {
override lazy val get = new URL(baseUrl(serviceName))
}

private def bindConfigProperty(propertyName: String) =
bind(classOf[String]).annotatedWith(Names.named(s"$propertyName")).toProvider(new ConfigPropertyProvider(propertyName))

private class ConfigPropertyProvider(propertyName: String) extends Provider[String] {
override lazy val get = getConfString(propertyName, throw new RuntimeException(s"No configuration value found for '$propertyName'"))
}

}


object ControllerConfiguration extends ControllerConfig {
lazy val controllerConfigs = Play.current.configuration.underlying.as[Config]("controllers")
lazy val controllerConfigs: Config = Play.current.configuration.underlying.as[Config]("controllers")
}

object AuthParamsControllerConfiguration extends AuthParamsControllerConfig {
lazy val controllerConfigs = ControllerConfiguration.controllerConfigs
lazy val controllerConfigs: Config = ControllerConfiguration.controllerConfigs
}

object MicroserviceAuditFilter extends AuditFilter with AppName with MicroserviceFilterSupport {
override val auditConnector = MicroserviceAuditConnector
override def controllerNeedsAuditing(controllerName: String) = ControllerConfiguration.paramsForController(controllerName).needsAuditing
override def controllerNeedsAuditing(controllerName: String): Boolean = ControllerConfiguration.paramsForController(controllerName).needsAuditing
}

object MicroserviceLoggingFilter extends LoggingFilter with MicroserviceFilterSupport {
override def controllerNeedsLogging(controllerName: String) = ControllerConfiguration.paramsForController(controllerName).needsLogging
override def controllerNeedsLogging(controllerName: String): Boolean = ControllerConfiguration.paramsForController(controllerName).needsLogging
}

object MicroserviceAuthFilter extends AuthorisationFilter with MicroserviceFilterSupport {
Expand All @@ -93,5 +89,4 @@ object MicroserviceGlobal extends DefaultMicroserviceGlobal with RunMode {
override val microserviceAuditFilter = MicroserviceAuditFilter

override val authFilter = Some(MicroserviceAuthFilter)

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package uk.gov.hmrc.agentclientrelationships

import uk.gov.hmrc.play.audit.http.config.LoadAuditingConfig
import uk.gov.hmrc.play.audit.http.config.{AuditingConfig, LoadAuditingConfig}
import uk.gov.hmrc.play.audit.http.connector.AuditConnector
import uk.gov.hmrc.play.auth.microservice.connectors.AuthConnector
import uk.gov.hmrc.play.config.{AppName, RunMode, ServicesConfig}
Expand All @@ -28,9 +28,9 @@ object WSHttp extends WSGet with WSPut with WSPost with WSDelete with WSPatch wi
}

object MicroserviceAuditConnector extends AuditConnector with RunMode {
override lazy val auditingConfig = LoadAuditingConfig(s"auditing")
override lazy val auditingConfig: AuditingConfig = LoadAuditingConfig(s"auditing")
}

object MicroserviceAuthConnector extends AuthConnector with ServicesConfig {
override val authBaseUrl = baseUrl("auth")
override val authBaseUrl: String = baseUrl("auth")
}
2 changes: 1 addition & 1 deletion conf/app.routes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GET /agent/:arn/service/HMRC-MTD-IT/client/MTDITID/:mtditid @uk.gov.hmrc.agentclientrelationships.controllers.Relationships.check(arn: uk.gov.hmrc.agentmtdidentifiers.model.Arn, mtditid: uk.gov.hmrc.agentmtdidentifiers.model.MtdItId)
GET /agent/:arn/service/HMRC-MTD-IT/client/MTDITID/:mtdItId @uk.gov.hmrc.agentclientrelationships.controllers.Relationships.check(arn: uk.gov.hmrc.agentmtdidentifiers.model.Arn, mtdItId: uk.gov.hmrc.agentmtdidentifiers.model.MtdItId)
1 change: 0 additions & 1 deletion it/uk/gov/hmrc/DELETEME

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import uk.gov.hmrc.agentclientrelationships.WSHttp
import uk.gov.hmrc.agentclientrelationships.connectors.GovernmentGatewayProxyConnector
import uk.gov.hmrc.agentmtdidentifiers.model.{Arn, MtdItId}
import uk.gov.hmrc.agentrelationships.stubs.GovernmentGatewayProxyStubs
import uk.gov.hmrc.agentrelationships.support.WireMockSupport
import uk.gov.hmrc.domain.AgentCode
import uk.gov.hmrc.play.http.HeaderCarrier
import uk.gov.hmrc.play.test.UnitSpec
import uk.gov.hmrc.support.WireMockSupport

class GovernmentGatewayProxyConnectorSpec extends UnitSpec with OneServerPerSuite with WireMockSupport with GovernmentGatewayProxyStubs {

Expand Down Expand Up @@ -71,6 +71,5 @@ class GovernmentGatewayProxyConnectorSpec extends UnitSpec with OneServerPerSuit
givenAgentIsNotAllocatedToClient("foo")
await(connector.getAllocatedAgentCodes(MtdItId("foo"))) should not contain AgentCode("bar")
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import uk.gov.hmrc.agentmtdidentifiers.model.Arn
import uk.gov.hmrc.agentrelationships.stubs.GovernmentGatewayProxyStubs
import uk.gov.hmrc.agentsubscription.support.Resource
import uk.gov.hmrc.agentrelationships.support.{Resource, WireMockSupport}
import uk.gov.hmrc.play.test.UnitSpec
import uk.gov.hmrc.support.WireMockSupport

class RelationshipISpec extends UnitSpec with OneServerPerSuite with WireMockSupport with GovernmentGatewayProxyStubs {

Expand Down
Loading

0 comments on commit 01c005e

Please sign in to comment.