Skip to content

Commit

Permalink
Merge pull request #39 from hmrc/APIS-2888
Browse files Browse the repository at this point in the history
APIS-2888
  • Loading branch information
edjannoo authored Nov 23, 2017
2 parents 5ca1f37 + 0380202 commit 847c2b6
Show file tree
Hide file tree
Showing 26 changed files with 481 additions and 97 deletions.
29 changes: 29 additions & 0 deletions app/Module.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2017 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.
*/

import com.google.inject.AbstractModule
import com.typesafe.config.{Config, ConfigFactory}
import play.api.{Configuration, Environment}
import uk.gov.hmrc.apisubscriptionfields.config.MicroserviceAuditConnector
import uk.gov.hmrc.play.audit.http.connector.AuditConnector

class Module(environment: Environment, configuration: Configuration) extends AbstractModule {

override def configure = {
bind(classOf[Config]).toInstance(ConfigFactory.load())
bind(classOf[AuditConnector]).toInstance(MicroserviceAuditConnector)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

package uk.gov.hmrc.apisubscriptionfields.config

import uk.gov.hmrc.play.audit.http.connector.AuditConnector
import uk.gov.hmrc.play.microservice.config.LoadAuditingConfig
import javax.inject.Inject

object MicroserviceAuditConnector extends AuditConnector {
override lazy val auditingConfig = LoadAuditingConfig(s"auditing")
import com.typesafe.config.Config
import uk.gov.hmrc.play.config.ServicesConfig

class AppContext @Inject()(val config: Config) extends ServicesConfig {
lazy val publishApiDefinition = runModeConfiguration.getBoolean("publishApiDefinition").getOrElse(false)
lazy val apiContext = runModeConfiguration.getString("api.context").getOrElse("api-subscription-fields")
lazy val access = runModeConfiguration.getConfig("api.access")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,46 @@ package uk.gov.hmrc.apisubscriptionfields.config
import com.typesafe.config.Config
import net.ceedubs.ficus.Ficus._
import play.api._
import uk.gov.hmrc.api.config.{ServiceLocatorConfig, ServiceLocatorRegistration}
import uk.gov.hmrc.api.connector.ServiceLocatorConnector
import uk.gov.hmrc.http._
import uk.gov.hmrc.http.hooks.HttpHooks
import uk.gov.hmrc.play.audit.http.HttpAuditing
import uk.gov.hmrc.play.audit.http.connector.AuditConnector
import uk.gov.hmrc.play.config.{AppName, ControllerConfig, RunMode}
import uk.gov.hmrc.play.http.ws.{WSDelete, WSGet, WSPost, WSPut}
import uk.gov.hmrc.play.microservice.bootstrap.DefaultMicroserviceGlobal
import uk.gov.hmrc.play.microservice.config.LoadAuditingConfig
import uk.gov.hmrc.play.microservice.filters.{AuditFilter, LoggingFilter, MicroserviceFilterSupport}

object MicroserviceGlobal extends DefaultMicroserviceGlobal with RunMode with ServiceLocatorRegistration with ServiceLocatorConfig {
lazy val injector = Play.current.injector
lazy val appContext = injector.instanceOf[AppContext]

override lazy val auditConnector = MicroserviceAuditConnector

override def microserviceMetricsConfig(implicit app: Application): Option[Configuration] = app.configuration.getConfig("microservice.metrics")

override lazy val loggingFilter = MicroserviceLoggingFilter
override lazy val microserviceAuditFilter = MicroserviceAuditFilter
override val authFilter = None

override val hc = HeaderCarrier()
override val slConnector = ServiceLocatorConnector(WSHttp)
override lazy val registrationEnabled = appContext.publishApiDefinition
}

object MicroserviceAuditConnector extends AuditConnector with RunMode {
override lazy val auditingConfig = LoadAuditingConfig(s"$env.auditing")
}

trait Hooks extends HttpHooks with HttpAuditing {
override val hooks = Seq(AuditingHook)
override lazy val auditConnector: AuditConnector = MicroserviceAuditConnector
}

trait WSHttp extends HttpGet with WSGet with HttpPut with WSPut with HttpPost with WSPost with HttpDelete with WSDelete with Hooks with AppName
object WSHttp extends WSHttp

object ControllerConfiguration extends ControllerConfig {
lazy val controllerConfigs = Play.current.configuration.underlying.as[Config]("controllers")
Expand All @@ -36,13 +72,3 @@ object MicroserviceAuditFilter extends AuditFilter with AppName with Microservic
object MicroserviceLoggingFilter extends LoggingFilter with MicroserviceFilterSupport {
override def controllerNeedsLogging(controllerName: String) = ControllerConfiguration.paramsForController(controllerName).needsLogging
}

object MicroserviceGlobal extends DefaultMicroserviceGlobal with RunMode {
override lazy val auditConnector = MicroserviceAuditConnector

override def microserviceMetricsConfig(implicit app: Application): Option[Configuration] = app.configuration.getConfig(s"microservice.metrics")

override lazy val loggingFilter = MicroserviceLoggingFilter
override lazy val microserviceAuditFilter = MicroserviceAuditFilter
override val authFilter = None
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2017 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.apisubscriptionfields.controller

import javax.inject.Inject

import controllers.AssetsBuilder
import play.api.http.HttpErrorHandler
import play.api.mvc.Action
import uk.gov.hmrc.apisubscriptionfields.config.AppContext
import uk.gov.hmrc.apisubscriptionfields.model.APIAccess
import uk.gov.hmrc.play.microservice.controller.BaseController
import uk.gov.hmrc.apisubscriptionfields.views.txt

class DocumentationController @Inject()(httpErrorHandler: HttpErrorHandler, appContext: AppContext) extends AssetsBuilder(httpErrorHandler) with BaseController {

def definition = Action {
if(appContext.publishApiDefinition) {
Ok(txt.definition(appContext.apiContext, APIAccess.build(appContext.access))).withHeaders(CONTENT_TYPE -> JSON)
} else NotFound
}

def raml(version: String, file: String) = Action {
if(appContext.publishApiDefinition) {
Ok(txt.application(appContext.apiContext))
} else NotFound
}
}
65 changes: 65 additions & 0 deletions app/uk/gov/hmrc/apisubscriptionfields/model/APIDefinition.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2017 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.apisubscriptionfields.model

import play.api.Configuration
import play.api.libs.json._

case class APIAccess(`type`: APIAccessType.Value, whitelistedApplicationIds: Option[Seq[String]])

object APIAccess {
def build(config: Option[Configuration]): APIAccess = APIAccess(
`type` = APIAccessType.PRIVATE,
whitelistedApplicationIds = config.flatMap(_.getStringSeq(s"whitelistedApplicationIds")).orElse(Some(Seq.empty)))
}

object APIAccessType extends Enumeration {
type APIAccessType = Value
val PRIVATE, PUBLIC = Value
}

object EnumJson {

def enumReads[E <: Enumeration](enum: E): Reads[E#Value] = new Reads[E#Value] {
def reads(json: JsValue): JsResult[E#Value] = json match {
case JsString(s) => {
try {
JsSuccess(enum.withName(s))
} catch {
case _: NoSuchElementException =>
throw new InvalidEnumException(enum.getClass.getSimpleName, s)
}
}
case _ => JsError("String value expected")
}
}

implicit def enumWrites[E <: Enumeration]: Writes[E#Value] = new Writes[E#Value] {
def writes(v: E#Value): JsValue = JsString(v.toString)
}

implicit def enumFormat[E <: Enumeration](enum: E): Format[E#Value] = {
Format(enumReads(enum), enumWrites)
}
}

class InvalidEnumException(className: String, input:String) extends RuntimeException(s"Enumeration expected of type: '$className', but it does not contain '$input'")

object APIDefinition {
implicit val apiAccessTypeJsonformat = EnumJson.enumFormat(APIAccessType)
implicit val apiAccessJsonformat = Json.format[APIAccess]
}
58 changes: 58 additions & 0 deletions app/uk/gov/hmrc/apisubscriptionfields/views/application.scala.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@(apiContext: String)#%RAML 1.0
---

title: API Subscription Fields
version: 1.0
protocols: [ HTTPS ]
baseUri: https://api.service.hmrc.gov.uk/

mediaType: [ application/json ]

uses:
sec: https://developer.service.hmrc.gov.uk/api-documentation/assets/common/modules/securitySchemes.raml
headers: https://developer.service.hmrc.gov.uk/api-documentation/assets/common/modules/headers.raml
annotations: https://developer.service.hmrc.gov.uk/api-documentation/assets/common/modules/annotations.raml
types: https://developer.service.hmrc.gov.uk/api-documentation/assets/common/modules/types.raml

/@apiContext:
/definition:
/context:
/{apiContext}:
uriParameters:
apiContext:
type: string
/version:
/{apiVersion}:
uriParameters:
apiVersion:
type: string
get:
is: [headers.acceptHeader]
securedBy: [ sec.x-application ]
/field:
/application:
/{clientId}:
uriParameters:
clientId:
type: string
/context:
/{apiContext}:
uriParameters:
apiContext:
type: string
/version:
/{apiVersion}:
uriParameters:
apiVersion:
type: string
get:
is: [headers.acceptHeader]
securedBy: [ sec.x-application ]
put:
is: [headers.acceptHeader]
securedBy: [ sec.x-application ]
delete:
is: [headers.acceptHeader]
securedBy: [ sec.x-application ]


19 changes: 19 additions & 0 deletions app/uk/gov/hmrc/apisubscriptionfields/views/definition.scala.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@import uk.gov.hmrc.apisubscriptionfields.model.APIAccess
@import uk.gov.hmrc.apisubscriptionfields.model.APIDefinition._
@import play.api.libs.json.Json
@(apiContext: String, access: APIAccess)
{
"scopes":[],
"api": {
"name": "API Subscription Fields",
"description": "Internal API for use by the developer hub",
"context": "@apiContext",
"versions": [
{
"version": "1.0",
"status": "PUBLISHED",
"access": @Json.toJson(access)
}
]
}
}
31 changes: 24 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import uk.gov.hmrc.versioning.SbtGitVersioning
import scala.language.postfixOps

val compile = Seq(
"uk.gov.hmrc" %% "play-reactivemongo" % "6.1.0",
ws,
"uk.gov.hmrc" %% "microservice-bootstrap" % "6.13.0"
"uk.gov.hmrc" %% "microservice-bootstrap" % "6.13.0",
"uk.gov.hmrc" %% "play-reactivemongo" % "6.1.0",
"uk.gov.hmrc" %% "play-hmrc-api" % "2.0.0"
)

def test(scope: String = "test,acceptance") = Seq(
Expand All @@ -39,7 +40,8 @@ def test(scope: String = "test,acceptance") = Seq(
"org.scalatestplus.play" %% "scalatestplus-play" % "2.0.1" % scope,
"org.pegdown" % "pegdown" % "1.6.0" % scope,
"com.github.tomakehurst" % "wiremock" % "2.11.0" % scope,
"com.typesafe.play" %% "play-test" % play.core.PlayVersion.current % scope
"com.typesafe.play" %% "play-test" % play.core.PlayVersion.current % scope,
"org.mockito" % "mockito-core" % "1.9.5" % "test,it"
)

val appName = "api-subscription-fields"
Expand All @@ -52,9 +54,10 @@ lazy val plugins: Seq[Plugins] = Seq.empty
lazy val playSettings: Seq[Setting[_]] = Seq.empty

lazy val AcceptanceTest = config("acceptance") extend Test

val testConfig = Seq(AcceptanceTest, Test)

lazy val IntegrationTest = config("it") extend Test

lazy val microservice = Project(appName, file("."))
.enablePlugins(Seq(PlayScala, SbtAutoBuildPlugin, SbtGitVersioning, SbtDistributablesPlugin) ++ plugins: _*)
.configs(testConfig: _*)
Expand All @@ -70,19 +73,33 @@ lazy val microservice = Project(appName, file("."))
evictionWarningOptions in update := EvictionWarningOptions.default.withWarnScalaVersionEviction(false)
)
.settings(scoverageSettings)
.configs(IntegrationTest)
.settings(inConfig(IntegrationTest)(Defaults.itSettings): _*)
.settings(
Keys.fork in IntegrationTest := false,
unmanagedSourceDirectories in IntegrationTest <<= (baseDirectory in IntegrationTest) (base => Seq(base / "test" / "it")),
addTestReportOption(IntegrationTest, "integration-reports"),
testGrouping in IntegrationTest := oneForkedJvmPerTest((definedTests in IntegrationTest).value),
parallelExecution in IntegrationTest := false)

lazy val unitTestSettings =
inConfig(Test)(Defaults.testTasks) ++
Seq(
testOptions in Test := Seq(Tests.Filter(onPackageName("unit"))),
unmanagedSourceDirectories in Test := Seq((baseDirectory in Test).value / "test"),
unmanagedSourceDirectories in Test := Seq(
baseDirectory.value / "test" / "unit",
baseDirectory.value / "test" / "util"
),
fork in Test := false,
addTestReportOption(Test, "test-reports")
)

lazy val acceptanceTestSettings =
inConfig(AcceptanceTest)(Defaults.testTasks) ++
Seq(
testOptions in AcceptanceTest := Seq(Tests.Filter(onPackageName("acceptance"))),
unmanagedSourceDirectories in AcceptanceTest := Seq(
baseDirectory.value / "test" / "acceptance",
baseDirectory.value / "test" / "util"
),
fork in AcceptanceTest := false,
parallelExecution in AcceptanceTest := false,
addTestReportOption(AcceptanceTest, "acceptance-reports")
Expand Down
Loading

0 comments on commit 847c2b6

Please sign in to comment.