Skip to content

Commit

Permalink
Merge pull request #65 from hmrc/change-des-config
Browse files Browse the repository at this point in the history
Changed the DES config loading for reg and subscribe connectors
  • Loading branch information
suyangyu authored Mar 13, 2017
2 parents 422af77 + fbe7f52 commit e6cbb23
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 44 deletions.
19 changes: 13 additions & 6 deletions app/config/MicroserviceAppConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,29 @@ import uk.gov.hmrc.play.config.ServicesConfig

trait AppConfig {
val authURL: String
val ggURL: String
val ggAdminURL: String
val desURL: String
val authenticatorURL: String
val desURL: String
val desEnvironment: String
val desToken: String
val ggURL: String
}

@Singleton
class MicroserviceAppConfig @Inject()(configuration: Configuration) extends AppConfig with ServicesConfig {
class MicroserviceAppConfig @Inject()(val configuration: Configuration) extends AppConfig with ServicesConfig {

private def loadConfig(key: String) = configuration.getString(key).getOrElse(throw new Exception(s"Missing configuration key: $key"))

override lazy val authURL = baseUrl("auth")
override lazy val authenticatorURL = baseUrl("authenticator")
override lazy val desURL = baseUrl("des")
override lazy val ggURL = baseUrl("government-gateway")
override lazy val ggAdminURL = baseUrl("gg-admin")
override lazy val desEnvironment = loadConfig("microservice.services.des.environment")
override lazy val desToken = loadConfig("microservice.services.des.authorization-token")

lazy val desBase = configuration.getString("feature-switching.useNewDesRoute").fold(false)(x=>x.toBoolean) match {
case true =>"microservice.services.new-des"
case _ =>"microservice.services.des"
}
override lazy val desURL = loadConfig(s"$desBase.url")
override lazy val desEnvironment = loadConfig(s"$desBase.environment")
override lazy val desToken = loadConfig(s"$desBase.authorization-token")
}
27 changes: 10 additions & 17 deletions app/connectors/RegistrationConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package connectors
import javax.inject.Inject

import audit.{Logging, LoggingConfig}
import config.AppConfig
import connectors.utils.ConnectorUtils
import models.registration._
import play.api.Configuration
import play.api.http.Status._
import play.api.libs.json.{JsValue, Writes}
import uk.gov.hmrc.play.config.ServicesConfig
Expand All @@ -31,36 +31,29 @@ import uk.gov.hmrc.play.http.logging.Authorization
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class RegistrationConnector @Inject()(config: Configuration,
logging: Logging,
httpPost: HttpPost,
httpGet: HttpGet
class RegistrationConnector @Inject()( appConfig: AppConfig,
logging: Logging,
httpPost: HttpPost,
httpGet: HttpGet
) extends ServicesConfig with RawResponseReads {

import Logging._

lazy val desBase = config.getString("feature-switching.useNewDesRoute").fold(false)(x=>x.toBoolean) match {
case true =>"microservice.services.new-des"
case _ =>"microservice.services.des"
}

lazy val urlHeaderEnvironment: String = config.getString(s"$desBase.environment").fold("")(x => x)
lazy val urlHeaderAuthorization: String = s"Bearer ${config.getString(s"$desBase.authorization-token").fold("")(x => x)}"
lazy val registrationServiceUrl: String = config.getString(s"$desBase.url").fold("")(x => x)
lazy val urlHeaderAuthorization: String = s"Bearer ${appConfig.desToken}"

// DES API numbering [MTD API numbering]
// API4 [API 9]
val newRegistrationUrl: String => String = (nino: String) => s"$registrationServiceUrl/registration/individual/nino/$nino"
val newRegistrationUrl: String => String = (nino: String) => s"${appConfig.desURL}/registration/individual/nino/$nino"

// API 1(b) [API 1 (b)]
val getRegistrationUrl: String => String = (nino: String) => s"$registrationServiceUrl/registration/details?nino=$nino"
val getRegistrationUrl: String => String = (nino: String) => s"${appConfig.desURL}/registration/details?nino=$nino"

def createHeaderCarrierPost: HeaderCarrier =
HeaderCarrier(extraHeaders = Seq("Environment" -> urlHeaderEnvironment, "Content-Type" -> "application/json"),
HeaderCarrier(extraHeaders = Seq("Environment" -> appConfig.desEnvironment, "Content-Type" -> "application/json"),
authorization = Some(Authorization(urlHeaderAuthorization)))

def createHeaderCarrierGet: HeaderCarrier =
HeaderCarrier(extraHeaders = Seq("Environment" -> urlHeaderEnvironment),
HeaderCarrier(extraHeaders = Seq("Environment" -> appConfig.desEnvironment),
authorization = Some(Authorization(urlHeaderAuthorization)))

def register(nino: String, registration: RegistrationRequestModel)(implicit hc: HeaderCarrier): Future[NewRegistrationUtil.Response] = {
Expand Down
19 changes: 7 additions & 12 deletions app/connectors/SubscriptionConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,21 @@ import scala.concurrent.{ExecutionContext, Future}

class SubscriptionConnector @Inject()
(
config: Configuration,
httpPost: HttpPost,
applicationConfig: AppConfig,
httpPost: HttpPost,
logging: Logging
) extends ServicesConfig with RawResponseReads {

lazy val desServiceUrl = applicationConfig.desURL
lazy val urlHeaderEnvironment = applicationConfig.desEnvironment
lazy val urlHeaderAuthorization = applicationConfig.desToken

val businessSubscribeUrl: String => String = nino => s"$desServiceUrl/income-tax-self-assessment/nino/$nino/business"
val propertySubscribeUrl: String => String = nino => s"$desServiceUrl/income-tax-self-assessment/nino/$nino/properties"
val businessSubscribeUrl: String => String = nino => s"${applicationConfig.desURL}/income-tax-self-assessment/nino/$nino/business"
val propertySubscribeUrl: String => String = nino => s"${applicationConfig.desURL}/income-tax-self-assessment/nino/$nino/properties"

def createHeaderCarrierPost(hc: HeaderCarrier): HeaderCarrier =
hc.copy(authorization = Some(Authorization(s"Bearer $urlHeaderAuthorization")))
.withExtraHeaders("Environment" -> urlHeaderEnvironment, "Content-Type" -> "application/json")
hc.copy(authorization = Some(Authorization(s"Bearer ${applicationConfig.desToken}")))
.withExtraHeaders("Environment" -> applicationConfig.desEnvironment, "Content-Type" -> "application/json")

def createHeaderCarrierPostEmpty(headerCarrier: HeaderCarrier): HeaderCarrier =
headerCarrier.copy(authorization = Some(Authorization(s"Bearer $urlHeaderAuthorization")))
.withExtraHeaders("Environment" -> urlHeaderEnvironment)
headerCarrier.copy(authorization = Some(Authorization(s"Bearer ${applicationConfig.desToken}")))
.withExtraHeaders("Environment" -> applicationConfig.desEnvironment)

def businessSubscribe(nino: String, businessSubscriptionPayload: BusinessSubscriptionRequestModel)
(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[BusinessConnectorUtil.Response] = {
Expand Down
5 changes: 2 additions & 3 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ feature-switching {
# this flag should always be set to false here and set to true in the required testing environments
# N.B. this flag MUST never be set to true in prod/live as splunk events may contain sensitive data
debugToWarn = false
useNewDesRoute = true
useNewDesRoute = false
}

# the value below is valid for local environment only
Expand Down Expand Up @@ -214,8 +214,7 @@ microservice {
}

des {
host = localhost
port = 9562
url = "http://localhost:9562"
environment = "dev"
authorization-token = "dev"
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/connectors/RegistrationConnectorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class RegistrationConnectorSpec extends MockRegistrationConnector {

implicit val hc = HeaderCarrier()

val env = config.getString("microservice.services.des.environment").get
val authToken = config.getString("microservice.services.des.authorization-token").get
val env = appConfig.desEnvironment
val authToken = appConfig.desToken

"RegistrationConnector.register" should {
"Put in the correct headers" in {
Expand Down
5 changes: 3 additions & 2 deletions test/unit/connectors/mocks/MockRegistrationConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package unit.connectors.mocks

import audit.Logging
import config.AppConfig
import connectors.RegistrationConnector
import models.registration.RegistrationRequestModel
import org.scalatestplus.play.OneAppPerSuite
Expand All @@ -29,15 +30,15 @@ import utils.TestConstants.{GetRegistrationResponse, NewRegistrationResponse, _}

trait MockRegistrationConnector extends MockHttp with OneAppPerSuite {

lazy val config: Configuration = app.injector.instanceOf[Configuration]
lazy val appConfig: AppConfig = app.injector.instanceOf[AppConfig]
lazy val logging: Logging = app.injector.instanceOf[Logging]
lazy val httpPost: HttpPost = mockHttpPost
lazy val httpGet: HttpGet = mockHttpGet

def mockRegister(payload: RegistrationRequestModel) = (setupMockRegister(testNino, payload) _).tupled
val mockGetRegistration = (setupMockGetRegistration(testNino) _).tupled

object TestRegistrationConnector extends RegistrationConnector(config, logging, httpPost, httpGet)
object TestRegistrationConnector extends RegistrationConnector(appConfig, logging, httpPost, httpGet)

val regSuccess = (OK, NewRegistrationResponse.successResponse(testSafeId))
val getRegSuccess = (OK, GetRegistrationResponse.successResponse(testSafeId))
Expand Down
3 changes: 1 addition & 2 deletions test/unit/connectors/mocks/MockSubscriptionConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import utils.TestConstants.{BusinessSubscriptionResponse, PropertySubscriptionRe

trait MockSubscriptionConnector extends MockHttp with OneAppPerSuite {

lazy val config: Configuration = app.injector.instanceOf[Configuration]
lazy val appConfig: AppConfig = app.injector.instanceOf[AppConfig]
lazy val httpPost: HttpPost = mockHttpPost
lazy val logging: Logging = app.injector.instanceOf[Logging]
Expand All @@ -41,7 +40,7 @@ trait MockSubscriptionConnector extends MockHttp with OneAppPerSuite {
val propertySubscribeSuccess = (OK, PropertySubscriptionResponse.successResponse(testSafeId, testMtditId, testSourceId))
val businessSubscribeSuccess = (OK, BusinessSubscriptionResponse.successResponse(testSafeId, testMtditId, testSourceId))

object TestSubscriptionConnector extends SubscriptionConnector(config, httpPost, appConfig, logging)
object TestSubscriptionConnector extends SubscriptionConnector(appConfig, httpPost, logging)

def setupMockBusinessSubscribe(nino: String, payload: BusinessSubscriptionRequestModel)(status: Int, response: JsValue): Unit =
setupMockHttpPost(url = TestSubscriptionConnector.businessSubscribeUrl(nino), payload)(status, response)
Expand Down

0 comments on commit e6cbb23

Please sign in to comment.