Skip to content

Commit

Permalink
Merge branch 'master' into SAR-454
Browse files Browse the repository at this point in the history
  • Loading branch information
MJCallahanPage authored Mar 17, 2017
2 parents b1315b6 + 353eb01 commit 29b43ce
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 82 deletions.
12 changes: 7 additions & 5 deletions app/config/AppContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package config

import play.api.Play._
import javax.inject.{Inject, Singleton}
import play.api.Application
import uk.gov.hmrc.play.config.ServicesConfig

object AppContext extends ServicesConfig {
lazy val appName = current.configuration.getString("appName").getOrElse(throw new RuntimeException("appName is not configured"))
lazy val appUrl = current.configuration.getString("appUrl").getOrElse(throw new RuntimeException("appUrl is not configured"))
}
@Singleton
class AppContext @Inject()(val application: Application) extends ServicesConfig {
lazy val appName = application.configuration.getString("appName").getOrElse(throw new RuntimeException("appName is not configured"))
lazy val appUrl = application.configuration.getString("appUrl").getOrElse(throw new RuntimeException("appUrl is not configured"))
}
28 changes: 28 additions & 0 deletions app/config/ControllerConfig.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 config

import javax.inject.{Inject, Singleton}
import com.typesafe.config.Config
import play.api.Configuration
import uk.gov.hmrc.play.config.{ControllerConfig => HmrcControllerConfig}
import net.ceedubs.ficus.Ficus._

@Singleton
class ControllerConfig @Inject()(val configuration: Configuration) extends HmrcControllerConfig {
lazy val controllerConfigs: Config = configuration.underlying.as[Config]("controllers")
}
2 changes: 1 addition & 1 deletion app/config/MicroserviceAppConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package config

import com.google.inject.{Inject, Singleton}
import javax.inject.{Inject,Singleton}
import play.api.Configuration
import uk.gov.hmrc.play.config.ServicesConfig

Expand Down
12 changes: 4 additions & 8 deletions app/config/microserviceGlobal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,24 @@

package config

import com.typesafe.config.Config
import models.{ErrorGenericBadRequest, ErrorInternalServerError, ErrorNotFound, ErrorUnauthorized}
import net.ceedubs.ficus.Ficus._
import play.api._
import play.api.http.Status.UNAUTHORIZED
import play.api.libs.json.Json
import play.api.mvc.Results._
import play.api.mvc.{RequestHeader, Result}
import uk.gov.hmrc.play.audit.filters.AuditFilter
import uk.gov.hmrc.play.auth.controllers.AuthParamsControllerConfig
import uk.gov.hmrc.play.auth.microservice.filters.AuthorisationFilter
import uk.gov.hmrc.play.config.{AppName, ControllerConfig, RunMode}
import uk.gov.hmrc.play.config.{AppName, RunMode}
import uk.gov.hmrc.play.filters.MicroserviceFilterSupport
import uk.gov.hmrc.play.http.HeaderCarrier
import uk.gov.hmrc.play.http.logging.filters.LoggingFilter
import uk.gov.hmrc.play.microservice.bootstrap.DefaultMicroserviceGlobal

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

object ControllerConfiguration extends ControllerConfig {
lazy val controllerConfigs = Play.current.configuration.underlying.as[Config]("controllers")
}
object ControllerConfiguration extends ControllerConfig(Play.current.configuration)

object AuthParamsControllerConfiguration extends AuthParamsControllerConfig {
lazy val controllerConfigs = ControllerConfiguration.controllerConfigs
Expand Down Expand Up @@ -75,7 +71,7 @@ object MicroserviceGlobal extends DefaultMicroserviceGlobal with RunMode {
super.onError(request, ex) map (res => {
res.header.status
match {
case 401 => Status(ErrorUnauthorized.httpStatusCode)(Json.toJson(ErrorUnauthorized))
case UNAUTHORIZED => Status(ErrorUnauthorized.httpStatusCode)(Json.toJson(ErrorUnauthorized))
case _ => Status(ErrorInternalServerError.httpStatusCode)(Json.toJson(ErrorInternalServerError))
}
})
Expand Down
14 changes: 6 additions & 8 deletions app/connectors/SubscriptionConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ class SubscriptionConnector @Inject()
headerCarrier.copy(authorization = Some(Authorization(urlHeaderAuthorization)))
.withExtraHeaders("Environment" -> applicationConfig.desEnvironment, "Content-Type" -> "application/json")

def createHeaderCarrierPostEmpty(headerCarrier: HeaderCarrier): HeaderCarrier =
headerCarrier.copy(authorization = Some(Authorization(urlHeaderAuthorization)))
.withExtraHeaders("Environment" -> applicationConfig.desEnvironment)

def businessSubscribe(nino: String, businessSubscriptionPayload: BusinessSubscriptionRequestModel)
(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[BusinessConnectorUtil.Response] = {
import BusinessConnectorUtil._
Expand All @@ -63,7 +59,7 @@ class SubscriptionConnector @Inject()
lazy val auditRequest = logging.auditFor(auditBusinessSubscribeName, requestDetails)(updatedHc)
auditRequest(eventTypeRequest)

logging.debug(s"Request:\n$requestDetails")
logging.debug(s"Request:\n$requestDetails\n\nHeader Carrier:\n$updatedHc")
httpPost.POST[BusinessSubscriptionRequestModel, HttpResponse](businessSubscribeUrl(nino), businessSubscriptionPayload)(
implicitly[Writes[BusinessSubscriptionRequestModel]], HttpReads.readRaw, createHeaderCarrierPost(hc)
).map { response =>
Expand All @@ -85,9 +81,11 @@ class SubscriptionConnector @Inject()
import SubscriptionConnector._
implicit val loggingConfig = SubscriptionConnector.propertySubscribeLoggingConfig
lazy val requestDetails: Map[String, String] = Map("nino" -> nino)
val updatedHc = createHeaderCarrierPostEmpty(hc)
logging.debug(s"Request:\n$requestDetails")
httpPost.POSTEmpty[HttpResponse](propertySubscribeUrl(nino))(HttpReads.readRaw, createHeaderCarrierPostEmpty(hc)).map {


val updatedHc = createHeaderCarrierPost(hc)
logging.debug(s"Request:\n$requestDetails\n\nHeader Carrier:\n$updatedHc")
httpPost.POST[JsValue, HttpResponse](propertySubscribeUrl(nino),"{}": JsValue)(implicitly[Writes[JsValue]], HttpReads.readRaw, updatedHc).map {
response =>

lazy val audit = logging.auditFor(auditPropertySubscribeName, requestDetails + ("response" -> response.body))(updatedHc)
Expand Down
37 changes: 18 additions & 19 deletions project/MicroServiceBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@ private object AppDependencies {
import play.core.PlayVersion
import play.sbt.PlayImport._

private val microserviceBootstrapVersion = "5.7.0"
private val playAuthVersion = "4.2.0"
private val playHealthVersion = "2.0.0"
private val microserviceBootstrapVersion = "5.13.0"
private val playAuthVersion = "4.3.0"
private val playHealthVersion = "2.1.0"
private val logbackJsonLoggerVersion = "3.1.0"
private val playUrlBindersVersion = "2.0.0"
private val playConfigVersion = "3.0.0"
private val domainVersion = "4.0.0"
private val hmrcTestVersion = "2.1.0"
private val scalaTestVersion = "2.2.6"
private val playUrlBindersVersion = "2.1.0"
private val playConfigVersion = "4.3.0"
private val domainVersion = "4.1.0"
private val hmrcTestVersion = "2.3.0"
private val scalaTestVersion = "3.0.1"
private val scalaTestPlusVersion = "2.0.0"
private val pegdownVersion = "1.6.0"
private val mockitoVersion = "2.7.17"

private val wireMockVersion = "2.5.0"
private val scalaJVersion = "1.1.5"
private val cucumberVersion = "1.2.4"
private val scalaJVersion = "2.3.0"
private val cucumberVersion = "1.2.5"

private val mongoLockVersion = "4.0.0"
private val reactiveMongoVersion = "5.1.0"
private val mongoLockVersion = "4.1.0"
private val reactiveMongoVersion = "5.2.0"


val compile = Seq(
Expand Down Expand Up @@ -56,12 +57,11 @@ private object AppDependencies {
"org.scalatest" %% "scalatest" % scalaTestVersion % scope,
"org.pegdown" % "pegdown" % pegdownVersion % scope,
"com.typesafe.play" %% "play-test" % PlayVersion.current % scope,
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % scope,
"org.scalatestplus.play" %% "scalatestplus-play" % scalaTestPlusVersion % scope,
"org.scalaj" %% "scalaj-http" % scalaJVersion % scope,
"com.github.tomakehurst" % "wiremock" % wireMockVersion % scope,
"info.cukes" %% "cucumber-scala" % cucumberVersion % scope,
"info.cukes" % "cucumber-junit" % cucumberVersion % scope,
"org.mockito" % "mockito-core" % "1.9.5" % scope,
"org.mockito" % "mockito-core" % mockitoVersion % scope,
"com.github.fge" % "json-schema-validator" % "2.2.6" % scope
)
}.test
Expand All @@ -77,12 +77,11 @@ private object AppDependencies {
"org.scalatest" %% "scalatest" % scalaTestVersion % scope,
"org.pegdown" % "pegdown" % pegdownVersion % scope,
"com.typesafe.play" %% "play-test" % PlayVersion.current % scope,
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % scope,
"org.scalatestplus.play" %% "scalatestplus-play" % scalaTestPlusVersion % scope,
"org.scalaj" %% "scalaj-http" % scalaJVersion % scope,
"com.github.tomakehurst" % "wiremock" % wireMockVersion % scope,
"info.cukes" %% "cucumber-scala" % cucumberVersion % scope,
"info.cukes" % "cucumber-junit" % cucumberVersion % scope,
"org.mockito" % "mockito-core" % "1.9.5" % scope,
"org.mockito" % "mockito-core" % mockitoVersion % scope,
"com.github.fge" % "json-schema-validator" % "2.2.6" % scope
)
}.test
Expand Down
6 changes: 3 additions & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ 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.4.0")
addSbtPlugin("uk.gov.hmrc" % "sbt-auto-build" % "1.6.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.8")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.12")

resolvers += Resolver.url("scoverage-bintray", url("https://dl.bintray.com/sksamuel/sbt-plugins/"))(Resolver.ivyStylePatterns)

addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.3.5")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.0")

addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.8.0")

Expand Down
6 changes: 3 additions & 3 deletions test/it/services/its/ITTrait.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ package it.services.its
import audit.Logging
import config.AppConfig
import org.scalatest.BeforeAndAfterEach
import org.scalatest.mock.MockitoSugar
import org.scalatestplus.play.OneServerPerSuite
import org.scalatest.mockito.MockitoSugar
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.Configuration
import uk.gov.hmrc.play.test.UnitSpec


trait ITTrait extends UnitSpec
with MockitoSugar
with BeforeAndAfterEach
with OneServerPerSuite {
with GuiceOneAppPerSuite {

lazy val config = app.injector.instanceOf[Configuration]
lazy val appConfig = app.injector.instanceOf[AppConfig]
Expand Down
8 changes: 4 additions & 4 deletions test/unit/connectors/AuthConnectorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import java.util.UUID

import connectors.AuthConnector
import models.auth.{Authority, UserIds}
import org.mockito.Matchers
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.{reset, when}
import org.scalatest.BeforeAndAfter
import play.api.http.Status._
Expand Down Expand Up @@ -78,10 +78,10 @@ Authority
val userIDs = UserIds("foo", "bar")
val expected = Authority(uri, ggid, userDetailsLink, userIDs)

when(mockHttp.GET[HttpResponse](Matchers.eq("localhost/auth/authority"))(Matchers.any(), Matchers.any())).
when(mockHttp.GET[HttpResponse](ArgumentMatchers.eq("localhost/auth/authority"))(ArgumentMatchers.any(), ArgumentMatchers.any())).
thenReturn(Future.successful(HttpResponse(OK, Some(authResponseJson(uri, userDetailsLink, ggid, idsLink)))))

when(mockHttp.GET[HttpResponse](Matchers.eq(s"localhost$idsLink"))(Matchers.any(), Matchers.any())).
when(mockHttp.GET[HttpResponse](ArgumentMatchers.eq(s"localhost$idsLink"))(ArgumentMatchers.any(), ArgumentMatchers.any())).
thenReturn(Future.successful(HttpResponse(OK, Some(idsResponseJson(userIDs.internalId, userIDs.externalId)))))


Expand All @@ -94,7 +94,7 @@ Authority

"return None when an authority isn't found" in {

when(mockHttp.GET[HttpResponse](Matchers.eq("localhost/auth/authority"))(Matchers.any(), Matchers.any())).
when(mockHttp.GET[HttpResponse](ArgumentMatchers.eq("localhost/auth/authority"))(ArgumentMatchers.any(), ArgumentMatchers.any())).
thenReturn(Future.successful(HttpResponse(NOT_FOUND, None)))

implicit val hc = new HeaderCarrier(sessionId = Some(SessionId(s"session-${UUID.randomUUID}")))
Expand Down
7 changes: 0 additions & 7 deletions test/unit/connectors/SubscriptionConnectorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ class SubscriptionConnectorSpec extends MockSubscriptionConnector {
rHc.headers.contains("Environment" -> appConfig.desEnvironment) shouldBe true
}

"The connector should have the correct POST EMPTY request headers for DES" in {
val rHc = TestSubscriptionConnector.createHeaderCarrierPostEmpty(hc)
rHc.headers.contains("Authorization" -> s"Bearer ${appConfig.desToken}") shouldBe true
rHc.headers.contains("Content-Type" -> "application/json") shouldBe false
rHc.headers.contains("Environment" -> appConfig.desEnvironment) shouldBe true
}

"SubscriptionConnector.businessSubscribe" should {

def result = await(TestSubscriptionConnector.businessSubscribe(testNino, businessSubscriptionRequestPayload))
Expand Down
10 changes: 5 additions & 5 deletions test/unit/connectors/mocks/MockAuthConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ package unit.connectors.mocks

import connectors.AuthConnector
import models.auth.{Authority, UserIds}
import org.mockito.Matchers
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.{reset, when}
import org.scalatest.BeforeAndAfterEach
import org.scalatest.mock.MockitoSugar
import org.scalatestplus.play.OneAppPerSuite
import org.scalatest.mockito.MockitoSugar
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import uk.gov.hmrc.play.http.HeaderCarrier
import uk.gov.hmrc.play.test.UnitSpec

import scala.concurrent.Future


trait MockAuthConnector extends UnitSpec with MockitoSugar with BeforeAndAfterEach with OneAppPerSuite {
trait MockAuthConnector extends UnitSpec with MockitoSugar with BeforeAndAfterEach with GuiceOneAppPerSuite {

lazy val mockAuthConnector: AuthConnector = mock[AuthConnector]

Expand All @@ -39,7 +39,7 @@ trait MockAuthConnector extends UnitSpec with MockitoSugar with BeforeAndAfterEa
}

def setupMockCurrentAuthority(authority: Option[Authority]): Unit = {
when(mockAuthConnector.getCurrentAuthority()(Matchers.any[HeaderCarrier]())).thenReturn(Future.successful(authority))
when(mockAuthConnector.getCurrentAuthority()(ArgumentMatchers.any[HeaderCarrier]())).thenReturn(Future.successful(authority))
}

val validAuthority = Authority(
Expand Down
26 changes: 13 additions & 13 deletions test/unit/connectors/mocks/MockHttp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package unit.connectors.mocks

import org.mockito.Matchers
import org.mockito.ArgumentMatchers
import org.mockito.Mockito._
import org.scalatest.BeforeAndAfterEach
import org.scalatest.mock.MockitoSugar
import org.scalatest.mockito.MockitoSugar
import play.api.libs.json.JsValue
import uk.gov.hmrc.play.http.{HttpGet, HttpPost, HttpResponse}
import uk.gov.hmrc.play.test.UnitSpec
Expand All @@ -39,26 +39,26 @@ trait MockHttp extends UnitSpec with MockitoSugar with BeforeAndAfterEach {
}

def setupMockHttpPost[I](url: Option[String] = None, body: Option[I] = None)(status: Int, response: JsValue): Unit = {
lazy val urlMatcher = url.fold(Matchers.any[String]())(x => Matchers.eq(x))
lazy val bodyMatcher = body.fold(Matchers.any[I]())(x => Matchers.eq(x))
when(mockHttpPost.POST[I, HttpResponse](urlMatcher, bodyMatcher, Matchers.any()
)(Matchers.any(), Matchers.any(), Matchers.any())).thenReturn(Future.successful(HttpResponse(status, Some(response))))
lazy val urlMatcher = url.fold(ArgumentMatchers.any[String]())(x => ArgumentMatchers.eq(x))
lazy val bodyMatcher = body.fold(ArgumentMatchers.any[I]())(x => ArgumentMatchers.eq(x))
when(mockHttpPost.POST[I, HttpResponse](urlMatcher, bodyMatcher, ArgumentMatchers.any()
)(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(Future.successful(HttpResponse(status, Some(response))))
}

def setupMockHttpPostEmpty(url: Option[String] = None)(status: Int, response: Option[JsValue]): Unit = {
lazy val urlMatcher = url.fold(Matchers.any[String]())(x => Matchers.eq(x))
when(mockHttpPost.POSTEmpty[HttpResponse](urlMatcher)(Matchers.any(), Matchers.any())).thenReturn(Future.successful(HttpResponse(status, response)))
lazy val urlMatcher = url.fold(ArgumentMatchers.any[String]())(x => ArgumentMatchers.eq(x))
when(mockHttpPost.POSTEmpty[HttpResponse](urlMatcher)(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(Future.successful(HttpResponse(status, response)))
}

def setupMockHttpGet(url: Option[String] = None)(status: Int, response: JsValue): Unit = {
lazy val urlMatcher = url.fold(Matchers.any[String]())(x => Matchers.eq(x))
lazy val urlMatcher = url.fold(ArgumentMatchers.any[String]())(x => ArgumentMatchers.eq(x))

when(mockHttpGet.GET[HttpResponse](urlMatcher)(Matchers.any(), Matchers.any())).thenReturn(Future.successful(HttpResponse(status, Some(response))))
when(mockHttpGet.GET[HttpResponse](urlMatcher)(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(Future.successful(HttpResponse(status, Some(response))))
}

def setupMockHttpGetWithParams(url: Option[String], params: Option[Seq[(String, String)]])(status: Int, response: JsValue): Unit = {
lazy val urlMatcher = url.fold(Matchers.any[String]())(x => Matchers.eq(x))
lazy val paramsMatcher = params.fold(Matchers.any[Seq[(String, String)]]())(x => Matchers.eq(x))
when(mockHttpGet.GET[HttpResponse](urlMatcher, paramsMatcher)(Matchers.any(), Matchers.any())).thenReturn(Future.successful(HttpResponse(status, Some(response))))
lazy val urlMatcher = url.fold(ArgumentMatchers.any[String]())(x => ArgumentMatchers.eq(x))
lazy val paramsMatcher = params.fold(ArgumentMatchers.any[Seq[(String, String)]]())(x => ArgumentMatchers.eq(x))
when(mockHttpGet.GET[HttpResponse](urlMatcher, paramsMatcher)(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(Future.successful(HttpResponse(status, Some(response))))
}
}
5 changes: 2 additions & 3 deletions test/unit/connectors/mocks/MockSubscriptionConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import config.AppConfig
import connectors.SubscriptionConnector
import models.subscription.business.BusinessSubscriptionRequestModel
import org.scalatestplus.play.OneAppPerSuite
import play.api.Configuration
import play.api.http.Status._
import play.api.libs.json.JsValue
import uk.gov.hmrc.play.http.HttpPost
import utils.Implicits._
import utils.JsonUtils._
import utils.TestConstants.{BusinessSubscriptionResponse, PropertySubscriptionResponse, _}

trait MockSubscriptionConnector extends MockHttp with OneAppPerSuite {
Expand All @@ -46,6 +45,6 @@ trait MockSubscriptionConnector extends MockHttp with OneAppPerSuite {
setupMockHttpPost(url = TestSubscriptionConnector.businessSubscribeUrl(nino), payload)(status, response)

def setupMockPropertySubscribe(nino: String)(status: Int, response: JsValue): Unit =
setupMockHttpPostEmpty(url = TestSubscriptionConnector.propertySubscribeUrl(nino))(status, response)
setupMockHttpPost(url = TestSubscriptionConnector.propertySubscribeUrl(nino), OptionUtl("{}": JsValue))(status, response)

}
Loading

0 comments on commit 29b43ce

Please sign in to comment.