Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/NINOA-64'
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDeadlyPianist authored and TomDudley92 committed Dec 16, 2019
2 parents 2444ba9 + 5dd3050 commit fc8c69c
Show file tree
Hide file tree
Showing 23 changed files with 585 additions and 61 deletions.
14 changes: 13 additions & 1 deletion app/config/AppConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package config
import javax.inject.{Inject, Singleton}
import play.api.Configuration
import uk.gov.hmrc.play.bootstrap.config.ServicesConfig
import v1.config.featureSwitch.Features
import config.ConfigKeys._

trait AppConfig {

Expand All @@ -30,18 +32,28 @@ trait AppConfig {

def desContext(): String

def desStubUrl: String

def desStubContext: String

def featureSwitch: Option[Configuration]

def features: Features
}

@Singleton
class AppConfigImpl @Inject()(configuration: ServicesConfig, config: Configuration)
class AppConfigImpl @Inject()(implicit val configuration: ServicesConfig, config: Configuration)
extends AppConfig {

private val desServicePrefix = "microservice.services.des"

def featureSwitch: Option[Configuration] = config.getOptional[Configuration]("feature-switch")

override lazy val desBaseUrl: String = configuration.baseUrl("des")
override lazy val desEnvironment: String = configuration.getString(s"$desServicePrefix.env")
override lazy val desToken: String = configuration.getString(s"$desServicePrefix.token")
override lazy val desContext: String = configuration.getString(s"$desServicePrefix.context")
override lazy val desStubUrl: String = configuration.baseUrl("desStub")
override lazy val desStubContext: String = configuration.getString(desStubContextKey)
override lazy val features: Features = new Features
}
25 changes: 25 additions & 0 deletions app/config/ConfigKeys.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2019 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 config

object ConfigKeys {
val featureSwitchKey = "feature-switch"
val servicesKey = "microservice.services"

val useDesStubKey = s"$featureSwitchKey.useDesStub"
val desStubContextKey = s"$servicesKey.desStub.context"
}
26 changes: 26 additions & 0 deletions app/v1/config/featureSwitch/BaseFeature.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2019 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 v1.config.featureSwitch

import play.api.Configuration

trait BaseFeature {

def getConfig(key: String)(implicit config: Configuration): String =
sys.props.get(key).fold(config.getString(key).getOrElse(throw new RuntimeException(s"Missing config for key: $key")))(x => x)

}
27 changes: 27 additions & 0 deletions app/v1/config/featureSwitch/Feature.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2019 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 v1.config.featureSwitch

import play.api.Configuration

class Feature(val key: String)(implicit config: Configuration) extends BaseFeature {

def apply(value: Boolean): Unit = sys.props += key -> value.toString

def apply(): Boolean = getConfig(key).toBoolean

}
27 changes: 27 additions & 0 deletions app/v1/config/featureSwitch/FeatureSwitchModel.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2019 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 v1.config.featureSwitch

import play.api.libs.json.{Format, Json}

case class FeatureSwitchModel(
useDesStub: Boolean
)

object FeatureSwitchModel {
implicit val formats: Format[FeatureSwitchModel] = Json.format[FeatureSwitchModel]
}
28 changes: 28 additions & 0 deletions app/v1/config/featureSwitch/Features.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2019 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 v1.config.featureSwitch

import javax.inject.{Inject, Singleton}
import play.api.Configuration
import config.ConfigKeys.useDesStubKey

@Singleton
class Features @Inject()(implicit config: Configuration) extends BaseFeature {

lazy val useDesStub = new Feature(useDesStubKey)

}
6 changes: 5 additions & 1 deletion app/v1/connectors/DesConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ class DesConnector @Inject()(
def sendRegisterRequest(request: NinoApplication)
(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[HttpPostResponse[DesResponseModel]] = {

val url = s"${appConfig.desBaseUrl()}/${appConfig.desContext()}"
val url = if(appConfig.features.useDesStub()) {
s"${appConfig.desStubUrl}/${appConfig.desStubContext}"
} else {
s"${appConfig.desBaseUrl()}/${appConfig.desContext()}"
}

http.POST(url, Json.toJson(request))(implicitly, RegisterNinoResponseReads, hc, ec)
}
Expand Down
54 changes: 54 additions & 0 deletions app/v1/testOnly/controllers/FeatureSwitchController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019 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 v1.testOnly.controllers

import config.AppConfig
import javax.inject.Inject
import play.api.Logger
import play.api.libs.json.Json
import play.api.mvc.{Action, AnyContent, ControllerComponents, Result}
import uk.gov.hmrc.play.bootstrap.controller.BackendController
import v1.config.featureSwitch.FeatureSwitchModel

class FeatureSwitchController @Inject()(
appConfig: AppConfig,
cc: ControllerComponents
) extends BackendController(cc) {

lazy val update: Action[AnyContent] = Action { implicit request =>
request.body.asJson match {
case Some(jsonBody) => jsonBody.asOpt[FeatureSwitchModel] match {
case Some(model) =>
appConfig.features.useDesStub(model.useDesStub)
result
case None =>
Logger.warn("[FeatureSwitchController][update] Unable to parse json as FeatureSwitchModel")
result
}
case None =>
Logger.warn("[FeatureSwitchController][update] Unable to validate body as JSON")
result
}
}

def result: Result = {
Ok(Json.toJson(FeatureSwitchModel(
appConfig.features.useDesStub()
)))
}

}
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ lazy val microservice = Project(appName, file("."))
.settings(resolvers += Resolver.jcenterRepo)
.settings(CodeCoverageSettings.settings: _*)
.settings(scalaVersion := "2.11.12")
.settings(resourceDirectory in IntegrationTest := (baseDirectory apply { baseDir: File => baseDir / "it/resources" }).value)
10 changes: 9 additions & 1 deletion conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,21 @@ microservice {
port = 9791
token = some-token
env = local
context = desEndpoint
context = desContext
}

desStub {
host = localhost
port = 9751
context = register
}

}
}

feature-switch {
useDesStub = true

version-1 {
enabled = true
}
Expand Down
3 changes: 2 additions & 1 deletion conf/testOnly.routes
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
# Failing to follow this rule may result in test routes deployed in production.

# Add all the application routes to the prod.routes file
-> / prod.Routes
-> / prod.Routes
POST /test-only/featureSwitch @v1.testOnly.controllers.FeatureSwitchController.update
1 change: 1 addition & 0 deletions it/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application.router = testOnly.Routes
1 change: 1 addition & 0 deletions it/support/IntegrationBaseSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ trait IntegrationBaseSpec extends WordSpecLike
def servicesConfig: Map[String, String] = Map(
"microservice.services.des.host" -> mockHost,
"microservice.services.des.port" -> mockPort,
"microservice.services.desStub.port" -> mockPort,
"microservice.services.auth.host" -> mockHost,
"microservice.services.auth.port" -> mockPort,
"auditing.consumer.baseUri.port" -> mockPort
Expand Down
Loading

0 comments on commit fc8c69c

Please # to comment.