diff --git a/app/Module.scala b/app/Module.scala new file mode 100644 index 0000000..ab2c72a --- /dev/null +++ b/app/Module.scala @@ -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) + } +} diff --git a/app/uk/gov/hmrc/apisubscriptionfields/config/microserviceWiring.scala b/app/uk/gov/hmrc/apisubscriptionfields/config/AppContext.scala similarity index 58% rename from app/uk/gov/hmrc/apisubscriptionfields/config/microserviceWiring.scala rename to app/uk/gov/hmrc/apisubscriptionfields/config/AppContext.scala index c00563d..8fae875 100644 --- a/app/uk/gov/hmrc/apisubscriptionfields/config/microserviceWiring.scala +++ b/app/uk/gov/hmrc/apisubscriptionfields/config/AppContext.scala @@ -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") } diff --git a/app/uk/gov/hmrc/apisubscriptionfields/config/microserviceGlobal.scala b/app/uk/gov/hmrc/apisubscriptionfields/config/microserviceGlobal.scala index 4cbb83f..02e6061 100644 --- a/app/uk/gov/hmrc/apisubscriptionfields/config/microserviceGlobal.scala +++ b/app/uk/gov/hmrc/apisubscriptionfields/config/microserviceGlobal.scala @@ -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") @@ -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 -} diff --git a/app/uk/gov/hmrc/apisubscriptionfields/controller/DocumentationController.scala b/app/uk/gov/hmrc/apisubscriptionfields/controller/DocumentationController.scala new file mode 100644 index 0000000..8b152d5 --- /dev/null +++ b/app/uk/gov/hmrc/apisubscriptionfields/controller/DocumentationController.scala @@ -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 + } +} diff --git a/app/uk/gov/hmrc/apisubscriptionfields/model/APIDefinition.scala b/app/uk/gov/hmrc/apisubscriptionfields/model/APIDefinition.scala new file mode 100644 index 0000000..ff7e23c --- /dev/null +++ b/app/uk/gov/hmrc/apisubscriptionfields/model/APIDefinition.scala @@ -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] +} diff --git a/app/uk/gov/hmrc/apisubscriptionfields/views/application.scala.txt b/app/uk/gov/hmrc/apisubscriptionfields/views/application.scala.txt new file mode 100644 index 0000000..2f53795 --- /dev/null +++ b/app/uk/gov/hmrc/apisubscriptionfields/views/application.scala.txt @@ -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 ] + + diff --git a/app/uk/gov/hmrc/apisubscriptionfields/views/definition.scala.txt b/app/uk/gov/hmrc/apisubscriptionfields/views/definition.scala.txt new file mode 100644 index 0000000..c5031c9 --- /dev/null +++ b/app/uk/gov/hmrc/apisubscriptionfields/views/definition.scala.txt @@ -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) + } + ] + } +} diff --git a/build.sbt b/build.sbt index ae87ab9..1f5c9bc 100644 --- a/build.sbt +++ b/build.sbt @@ -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( @@ -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" @@ -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: _*) @@ -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") diff --git a/conf/application.conf b/conf/application.conf index 663d291..c4b19b4 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -79,14 +79,14 @@ play.http.router=prod.Routes # The below controllers are the default exceptions to this rule. controllers { - # 300 is the default, you may need to change this according to your needs - confidenceLevel = 300 - - com.kenshoo.play.metrics.MetricsController = { - needsAuth = false - needsLogging = false - needsAuditing = false - } + # 300 is the default, you may need to change this according to your needs + confidenceLevel = 300 + + com.kenshoo.play.metrics.MetricsController = { + needsAuth = false + needsLogging = false + needsAuditing = false + } } @@ -97,49 +97,49 @@ controllers { # Metrics plugin settings - graphite reporting is configured on a per env basis metrics { - name = ${appName} - rateUnit = SECONDS - durationUnit = SECONDS - showSamples = true - jvm = true - enabled = true + name = ${appName} + rateUnit = SECONDS + durationUnit = SECONDS + showSamples = true + jvm = true + enabled = true } # Microservice specific config auditing { - enabled=false - traceRequests=false - consumer { - baseUri { - host = localhost - port = 8100 - } + enabled=false + traceRequests=false + consumer { + baseUri { + host = localhost + port = 8100 } + } } mongodb { - uri = "mongodb://localhost:27017/api-subscription-fields" + uri = "mongodb://localhost:27017/api-subscription-fields" } mongo-async-driver { - akka { - loggers = [ "akka.event.slf4j.Slf4jLogger" ] - loglevel = WARNING - } + akka { + loggers = [ "akka.event.slf4j.Slf4jLogger" ] + loglevel = WARNING + } } microservice { - metrics { - graphite { - host = graphite - port = 2003 - prefix = play.${appName}. - enabled = false - } + metrics { + graphite { + host = graphite + port = 2003 + prefix = play.${appName}. + enabled = false } + } } @@ -149,29 +149,30 @@ wiremock-port = 11111 wiremock-port = ${?WIREMOCK_PORT} Dev { + microservice { + metrics { + graphite { + host = graphite + port = 2003 + prefix = play.${appName}. + enabled = false + } + } - microservice { - metrics { - graphite { - host = graphite - port = 2003 - prefix = play.${appName}. - enabled = false - } - } + services { + timeout = 5 seconds + + delay-response = 2 seconds + + protocol = http + + service-locator { + host = localhost + port = 9602 + } } + } } Prod { - - microservice { - metrics { - graphite { - host = graphite - port = 2003 - prefix = play.${appName}. - enabled = true - } - } - } } diff --git a/conf/definition.routes b/conf/definition.routes new file mode 100644 index 0000000..304170d --- /dev/null +++ b/conf/definition.routes @@ -0,0 +1,2 @@ +GET /api/definition @uk.gov.hmrc.apisubscriptionfields.controller.DocumentationController.definition() +GET /api/conf/:version/*file @uk.gov.hmrc.apisubscriptionfields.controller.DocumentationController.raml(version, file) diff --git a/conf/prod.routes b/conf/prod.routes index 4d31107..37a5dac 100644 --- a/conf/prod.routes +++ b/conf/prod.routes @@ -1,5 +1,6 @@ # Add all the application routes to the app.routes file -> / app.Routes +-> / definition.Routes -> / health.Routes GET /admin/metrics @com.kenshoo.play.metrics.MetricsController.metrics diff --git a/project/plugins.sbt b/project/plugins.sbt index f02392b..757150e 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -3,11 +3,7 @@ resolvers += Resolver.url("hmrc-sbt-plugin-releases", url("https://dl.bintray.co resolvers += "Typesafe Releases" at "http://repo.typesafe.com/typesafe/releases/" addSbtPlugin("uk.gov.hmrc" % "sbt-auto-build" % "1.7.0") - addSbtPlugin("uk.gov.hmrc" % "sbt-git-versioning" % "0.9.0") - addSbtPlugin("uk.gov.hmrc" % "sbt-distributables" % "1.0.0") - addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.12") - addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.1") diff --git a/test/acceptance/AcceptanceTestSpec.scala b/test/acceptance/AcceptanceTestSpec.scala index 6069316..98cfa2c 100644 --- a/test/acceptance/AcceptanceTestSpec.scala +++ b/test/acceptance/AcceptanceTestSpec.scala @@ -14,8 +14,6 @@ * limitations under the License. */ -package acceptance - import java.util.UUID import org.scalatest._ diff --git a/test/it/PlatformIntegrationSpec.scala b/test/it/PlatformIntegrationSpec.scala new file mode 100644 index 0000000..9874455 --- /dev/null +++ b/test/it/PlatformIntegrationSpec.scala @@ -0,0 +1,131 @@ +/* + * 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.github.tomakehurst.wiremock.WireMockServer +import com.github.tomakehurst.wiremock.client.WireMock +import com.github.tomakehurst.wiremock.client.WireMock._ +import com.github.tomakehurst.wiremock.core.WireMockConfiguration._ +import com.typesafe.config.ConfigFactory +import org.scalatest.concurrent.ScalaFutures +import org.scalatest.mockito.MockitoSugar +import org.scalatest.{BeforeAndAfterEach, TestData} +import org.scalatestplus.play.guice.GuiceOneAppPerTest +import play.api.http.LazyHttpErrorHandler +import play.api.inject.guice.GuiceApplicationBuilder +import play.api.libs.json.Json +import play.api.test.FakeRequest +import play.api.{Application, Mode} +import uk.gov.hmrc.api.domain.Registration +import uk.gov.hmrc.apisubscriptionfields.config.AppContext +import uk.gov.hmrc.apisubscriptionfields.controller.DocumentationController +import uk.gov.hmrc.play.microservice.filters.MicroserviceFilterSupport +import uk.gov.hmrc.play.test.UnitSpec + +/** + * Testcase to verify the capability of integration with the API platform. + * + * 1, To integrate with API platform the service needs to register itself to the service locator by calling the /registration endpoint and providing + * - application name + * - application url + * + * 2a, To expose API's to Third Party Developers, the service needs to make the API definition available under api/definition GET endpoint + * 2b, The endpoints need to be defined in an application.raml file for all versions For all of the endpoints defined documentation will be provided and be available under api/documentation/[version]/[endpoint name] GET endpoint + * Example: api/documentation/1.0/Fetch-Some-Data + * + * See: https://confluence.tools.tax.service.gov.uk/display/ApiPlatform/API+Platform+Architecture+with+Flows + */ + + +trait PlatformIntegrationSpec extends UnitSpec with MockitoSugar with ScalaFutures with BeforeAndAfterEach with GuiceOneAppPerTest { + + val publishApiDefinition: Boolean + val stubHost = "localhost" + val stubPort = sys.env.getOrElse("WIREMOCK_SERVICE_LOCATOR_PORT", "11111").toInt + val wireMockServer = new WireMockServer(wireMockConfig().port(stubPort)) + + override def newAppForTest(testData: TestData): Application = GuiceApplicationBuilder() + .configure("run.mode" -> "Stub") + .configure(Map( + "appName" -> "application-name", + "appUrl" -> "http://localhost/microservice", + "publishApiDefinition" -> publishApiDefinition, + "api.context" -> "test-api-context", + "Test.microservice.services.service-locator.host" -> stubHost, + "Test.microservice.services.service-locator.port" -> stubPort, + "Test.microservice.services.service-locator.enabled" -> true + )).in(Mode.Test).build() + + override def beforeEach() { + wireMockServer.start() + WireMock.configureFor(stubHost, stubPort) + stubFor(post(urlMatching("/registration")).willReturn(aResponse().withStatus(204))) + } + + override protected def afterEach() = { + wireMockServer.stop() + wireMockServer.resetMappings() + } + + trait Setup extends MicroserviceFilterSupport { + val documentationController = new DocumentationController(LazyHttpErrorHandler, new AppContext(ConfigFactory.load())) {} + val request = FakeRequest() + } +} + +class PublishApiDefinitionEnabledSpec extends PlatformIntegrationSpec { + val publishApiDefinition = true + + "microservice" should { + + "register itelf to service-locator" in new Setup { + def regPayloadStringFor(serviceName: String, serviceUrl: String): String = + Json.toJson(Registration(serviceName, serviceUrl, Some(Map("third-party-api" -> "true")))).toString + + verify(1, postRequestedFor(urlMatching("/registration")). + withHeader("content-type", equalTo("application/json")). + withRequestBody(equalTo(regPayloadStringFor("application-name", "http://localhost/microservice")))) + } + + "return the JSON definition" in new Setup { + val result = await(documentationController.definition()(request)) + status(result) shouldBe 200 + bodyOf(result) should include(""""context": "test-api-context"""") + } + + "return the RAML" in new Setup { + val result = await(documentationController.raml("1.0", "application.raml")(request)) + status(result) shouldBe 200 + bodyOf(result) should include("/test-api-context") + } + } +} + +class PublishApiDefinitionDisabledSpec extends PlatformIntegrationSpec { + val publishApiDefinition = false + + "microservice" should { + + "return a 404 from the definition endpoint" in new Setup { + val result = await(documentationController.definition()(request)) + status(result) shouldBe 404 + } + + "return a 404 from the RAML endpoint" in new Setup { + val result = await(documentationController.raml("1.0", "application.raml")(request)) + status(result) shouldBe 404 + } + } +} diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerDeleteSpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerDeleteSpec.scala index 8683c78..7d6ac07 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerDeleteSpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerDeleteSpec.scala @@ -14,13 +14,12 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.controller +package uk.gov.hmrc.apisubscriptionfields.controller import org.scalamock.scalatest.MockFactory import play.api.libs.json.{JsDefined, JsString} import play.api.test.FakeRequest import play.api.test.Helpers._ -import uk.gov.hmrc.apisubscriptionfields.controller.FieldsDefinitionController import uk.gov.hmrc.apisubscriptionfields.model.JsonFormatters import uk.gov.hmrc.apisubscriptionfields.service.FieldsDefinitionService import uk.gov.hmrc.play.test.UnitSpec diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerGetSpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerGetSpec.scala index cb04cd7..9e57612 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerGetSpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerGetSpec.scala @@ -14,13 +14,12 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.controller +package uk.gov.hmrc.apisubscriptionfields.controller import org.scalamock.scalatest.MockFactory import play.api.libs.json.{JsDefined, JsString, Json} import play.api.test.Helpers._ import play.api.test._ -import uk.gov.hmrc.apisubscriptionfields.controller.FieldsDefinitionController import uk.gov.hmrc.apisubscriptionfields.model.{BulkFieldsDefinitionsResponse, FieldsDefinitionResponse, JsonFormatters} import uk.gov.hmrc.apisubscriptionfields.service.FieldsDefinitionService import uk.gov.hmrc.play.test.UnitSpec diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerPutSpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerPutSpec.scala index f34257c..220f4bd 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerPutSpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/FieldsDefinitionControllerPutSpec.scala @@ -14,14 +14,13 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.controller +package uk.gov.hmrc.apisubscriptionfields.controller import org.scalamock.scalatest.MockFactory import play.api.libs.json.{JsValue, Json} import play.api.mvc._ import play.api.test.Helpers._ import play.api.test._ -import uk.gov.hmrc.apisubscriptionfields.controller.FieldsDefinitionController import uk.gov.hmrc.apisubscriptionfields.model.{FieldsDefinitionRequest, JsonFormatters} import uk.gov.hmrc.apisubscriptionfields.service.FieldsDefinitionService import uk.gov.hmrc.play.test.UnitSpec diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerDeleteSpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerDeleteSpec.scala index ea1e17a..59bd488 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerDeleteSpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerDeleteSpec.scala @@ -14,13 +14,12 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.controller +package uk.gov.hmrc.apisubscriptionfields.controller import org.scalamock.scalatest.MockFactory import play.api.libs.json.{JsDefined, JsString} import play.api.test.FakeRequest import play.api.test.Helpers._ -import uk.gov.hmrc.apisubscriptionfields.controller.SubscriptionFieldsController import uk.gov.hmrc.apisubscriptionfields.model.JsonFormatters import uk.gov.hmrc.apisubscriptionfields.service.SubscriptionFieldsService import uk.gov.hmrc.play.test.UnitSpec diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerGetSpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerGetSpec.scala index dcc365f..150c9b9 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerGetSpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerGetSpec.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.controller +package uk.gov.hmrc.apisubscriptionfields.controller import org.scalamock.scalatest.MockFactory import play.api.libs.json.{JsDefined, JsString, Json} diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerPutSpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerPutSpec.scala index 2f7b35b..9ce9cbd 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerPutSpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/controller/SubscriptionFieldsControllerPutSpec.scala @@ -14,14 +14,13 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.controller +package uk.gov.hmrc.apisubscriptionfields.controller import org.scalamock.scalatest.MockFactory import play.api.libs.json.{JsValue, Json} import play.api.mvc._ import play.api.test.FakeRequest import play.api.test.Helpers._ -import uk.gov.hmrc.apisubscriptionfields.controller.SubscriptionFieldsController import uk.gov.hmrc.apisubscriptionfields.model._ import uk.gov.hmrc.apisubscriptionfields.service.SubscriptionFieldsService import uk.gov.hmrc.play.test.UnitSpec diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/model/JsonFormatterSpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/model/JsonFormatterSpec.scala index 9e5044e..f946e6a 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/model/JsonFormatterSpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/model/JsonFormatterSpec.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.model +package uk.gov.hmrc.apisubscriptionfields.model import org.scalatest.{Matchers, WordSpec} import uk.gov.hmrc.apisubscriptionfields.model._ diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/FieldsDefinitionRepositorySpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/FieldsDefinitionRepositorySpec.scala index 8976746..8a79a62 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/FieldsDefinitionRepositorySpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/FieldsDefinitionRepositorySpec.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.repository +package uk.gov.hmrc.apisubscriptionfields.repository import org.scalamock.scalatest.MockFactory import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach} diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/MongoErrorHandlerSpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/MongoErrorHandlerSpec.scala index fad4d0d..e399b96 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/MongoErrorHandlerSpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/MongoErrorHandlerSpec.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.repository +package uk.gov.hmrc.apisubscriptionfields.repository import reactivemongo.api.commands._ import reactivemongo.bson.BSONInteger diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/SubscriptionFieldsRepositorySpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/SubscriptionFieldsRepositorySpec.scala index 8cca2db..2e54352 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/SubscriptionFieldsRepositorySpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/repository/SubscriptionFieldsRepositorySpec.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.repository +package uk.gov.hmrc.apisubscriptionfields.repository import java.util.UUID diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/service/FieldsDefinitionServiceSpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/service/FieldsDefinitionServiceSpec.scala index 2e7c6c4..4d3d0e2 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/service/FieldsDefinitionServiceSpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/service/FieldsDefinitionServiceSpec.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.service +package uk.gov.hmrc.apisubscriptionfields.service import org.scalamock.scalatest.MockFactory import uk.gov.hmrc.apisubscriptionfields.model._ diff --git a/test/unit/uk/gov/hmrc/apisubscriptionfields/service/SubscriptionFieldsServiceSpec.scala b/test/unit/uk/gov/hmrc/apisubscriptionfields/service/SubscriptionFieldsServiceSpec.scala index 7a53cdc..ab01012 100644 --- a/test/unit/uk/gov/hmrc/apisubscriptionfields/service/SubscriptionFieldsServiceSpec.scala +++ b/test/unit/uk/gov/hmrc/apisubscriptionfields/service/SubscriptionFieldsServiceSpec.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package unit.uk.gov.hmrc.apisubscriptionfields.service +package uk.gov.hmrc.apisubscriptionfields.service import java.util.UUID