Skip to content

Commit

Permalink
Merge pull request #3 from hmrc/MIPR-1067
Browse files Browse the repository at this point in the history
MIPR-1067: Added initial tests
  • Loading branch information
nadinJ authored Nov 14, 2024
2 parents 82dbc52 + 0d9db3c commit 0bab77c
Show file tree
Hide file tree
Showing 29 changed files with 1,168 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ class AppConfig @Inject()(config: Configuration, servicesConfig: ServicesConfig)
lazy val serviceSignOut:String = servicesConfig.getString("service-signout.url")
lazy val ITSAPenaltiesAppealsHomeUrl = "/penalties-appeals/income-tax"
val alphaBannerUrl: String = servicesConfig.getString("alpha-banner-url")
def getFeatureSwitchValue(feature: String): Boolean = config.get[Boolean](feature)
def selfUrl: String = servicesConfig.baseUrl("income-tax-penalties-appeals-frontend")

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ class LanguageSwitchController @Inject()(
"cymraeg" -> Lang("cy")
)

override def fallbackURL: String = routes.HelloWorldController.helloWorld.url
override def fallbackURL: String = routes.ServiceController.helloWorld.url
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import javax.inject.{Inject, Singleton}
import scala.concurrent.Future

@Singleton
class HelloWorldController @Inject()(
class ServiceController @Inject()(
mcc: MessagesControllerComponents,
helloWorldPage: HelloWorldPage)(appConfig: AppConfig)
extends FrontendController(mcc) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2023 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.incometaxpenaltiesappealsfrontend.featureswitch.api.controllers

import play.api.libs.json.Json
import play.api.mvc.{Action, AnyContent, InjectedController}
import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.featureswitch.api.services.FeatureSwitchService
import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.config.AppConfig
import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.featureswitch.core.config.FeatureSwitching
import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.featureswitch.core.models.FeatureSwitchSetting

import javax.inject.{Inject, Singleton}

@Singleton
class FeatureSwitchApiController @Inject()(featureSwitchService: FeatureSwitchService,
val config: AppConfig) extends InjectedController with FeatureSwitching {

def getFeatureSwitches: Action[AnyContent] = Action {
Ok(Json.toJson(featureSwitchService.getFeatureSwitches()))
}

def updateFeatureSwitches(): Action[Seq[FeatureSwitchSetting]] = Action(parse.json[Seq[FeatureSwitchSetting]]) {
req =>
val updatedFeatureSwitches = featureSwitchService.updateFeatureSwitches(req.body)
Ok(Json.toJson(updatedFeatureSwitches))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2023 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.incometaxpenaltiesappealsfrontend.featureswitch.api.services

import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.config.AppConfig
import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.featureswitch.core.config.{FeatureSwitchRegistry, FeatureSwitching}
import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.featureswitch.core.models.FeatureSwitchSetting

import javax.inject.{Inject, Singleton}

@Singleton
class FeatureSwitchService @Inject()(featureSwitchRegistry: FeatureSwitchRegistry,
val config: AppConfig) extends FeatureSwitching {

def getFeatureSwitches(): Seq[FeatureSwitchSetting] =
featureSwitchRegistry.switches.map(
switch =>
FeatureSwitchSetting(
switch.configName,
switch.displayName,
isEnabled(switch)
)
)

def updateFeatureSwitches(updatedFeatureSwitches: Seq[FeatureSwitchSetting]): Seq[FeatureSwitchSetting] = {
updatedFeatureSwitches.foreach(
featureSwitchSetting =>
featureSwitchRegistry.get(featureSwitchSetting.configName).foreach {
featureSwitch =>
if (featureSwitchSetting.isEnabled) enable(featureSwitch) else disable(featureSwitch)
}
)

getFeatureSwitches()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 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.incometaxpenaltiesappealsfrontend.featureswitch.core.config

import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.featureswitch.core.models.FeatureSwitch

trait FeatureSwitchRegistry {

def switches: Seq[FeatureSwitch]

def apply(name: String): FeatureSwitch =
get(name) match {
case Some(switch) => switch
case None => throw new IllegalArgumentException("Invalid feature switch: " + name)
}

def get(name: String): Option[FeatureSwitch] = switches find (_.configName == name)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2023 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.incometaxpenaltiesappealsfrontend.featureswitch.core.config

import play.api.Logging
import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.config.AppConfig
import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.featureswitch.core.models.FeatureSwitch

trait FeatureSwitching extends Logging {

val config: AppConfig

val FEATURE_SWITCH_ON = "true"
val FEATURE_SWITCH_OFF = "false"

def isEnabled(featureSwitch: FeatureSwitch): Boolean =
sys.props get featureSwitch.configName match {
case Some(value) => value == FEATURE_SWITCH_ON
case None => config.getFeatureSwitchValue(featureSwitch.configName)
}

def enable(featureSwitch: FeatureSwitch): Unit = {
logger.warn(s"[enable] $featureSwitch")
sys.props += featureSwitch.configName -> FEATURE_SWITCH_ON
}

def disable(featureSwitch: FeatureSwitch): Unit = {
logger.warn(s"[disable] $featureSwitch")
sys.props += featureSwitch.configName -> FEATURE_SWITCH_OFF
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2023 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.incometaxpenaltiesappealsfrontend.featureswitch.core.config

import play.api.inject.{Binding, Module}
import play.api.{Configuration, Environment}
import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.featureswitch.core.models.FeatureSwitch

import javax.inject.Singleton

@Singleton
class FeatureSwitchingModule extends Module with FeatureSwitchRegistry {

val switches: Seq[FeatureSwitch] = Seq(
UseStubForBackend
)

override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
Seq(
bind[FeatureSwitchRegistry].to(this).eagerly()
)
}
}

case object UseStubForBackend extends FeatureSwitch {
override val configName: String = "features.useStubForBackend"
override val displayName: String = "Use stub instead of Penalties backend service"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2023 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.incometaxpenaltiesappealsfrontend.featureswitch.core.models

trait FeatureSwitch {
val configName: String
val displayName: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2023 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.incometaxpenaltiesappealsfrontend.featureswitch.core.models

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


case class FeatureSwitchSetting(configName: String,
displayName: String,
isEnabled: Boolean)

object FeatureSwitchSetting {

implicit val format: OFormat[FeatureSwitchSetting] = Json.format[FeatureSwitchSetting]

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2023 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.incometaxpenaltiesappealsfrontend.featureswitch.frontend.config

import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.config.AppConfig
import uk.gov.hmrc.incometaxpenaltiesappealsfrontend.featureswitch.frontend.models.FeatureSwitchProvider

import javax.inject.{Inject, Singleton}

@Singleton
class FeatureSwitchProviderConfig @Inject()(appConfig: AppConfig) {

lazy val selfBaseUrl: String = appConfig.selfUrl

lazy val selfFeatureSwitchUrl = s"$selfBaseUrl/penalties-appeals/income-tax/test-only/api/feature-switches"

lazy val selfFeatureSwitchProvider: FeatureSwitchProvider = FeatureSwitchProvider(
id = "income-tax-penalties-appeals-frontend",
appName = "Income Tax Penalties Appeals Frontend",
url = selfFeatureSwitchUrl
)

lazy val featureSwitchProviders: Seq[FeatureSwitchProvider] =
Seq(selfFeatureSwitchProvider)

}
Loading

0 comments on commit 0bab77c

Please sign in to comment.