Skip to content

Commit

Permalink
MTDSA-1578 Added unit tests for obligations (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
westwater authored and MarkAKelly committed May 1, 2018
1 parent 4e2173e commit 682faa6
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/uk/gov/hmrc/vatapi/connectors/BaseConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ trait BaseConnector {

private def withDesHeaders: HeaderCarrier => HeaderCarrier = { hc =>
val newHc: HeaderCarrier = hc
.copy(authorization = Some(Authorization(s"Bearer ${AppContext.desToken}")))
.copy(authorization = Some(Authorization(s"Bearer ${appContext.desToken}")))
.withExtraHeaders(
"Environment" -> AppContext.desEnv,
"Environment" -> appContext.desEnv,
"Accept" -> "application/json",
"Originator-Id" -> "DA_SDI"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ trait ObligationsConnector extends BaseConnector {
logger.debug(s"[ObligationsConnector][get] Retrieve obligations for VRN $vrn with the given query parameters.")

val queryString = s"from=${queryParams.from}&to=${queryParams.to}&status=${queryParams.status}"

httpGet[ObligationsResponse](baseUrl + s"/enterprise/obligation-data/vrn/$vrn/VATC?$queryString", ObligationsResponse)
}
}
2 changes: 1 addition & 1 deletion project/MicroService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ trait MicroService {
Seq(
ScoverageKeys.coverageExcludedPackages := "<empty>;.*(Reverse|BuildInfo|Routes).*",
ScoverageKeys.coverageMinimum := 80,
ScoverageKeys.coverageFailOnMinimum := false,
ScoverageKeys.coverageFailOnMinimum := true,
ScoverageKeys.coverageHighlighting := true
)
}
Expand Down
24 changes: 21 additions & 3 deletions test/uk/gov/hmrc/vatapi/connectors/ObligationsConnectorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package uk.gov.hmrc.vatapi.connectors

import org.scalatestplus.play.OneAppPerSuite
import play.api.libs.json.JsValue
import play.mvc.Http.{HeaderNames, MimeTypes}
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.http.{HeaderCarrier, HttpResponse}
import uk.gov.hmrc.vatapi.UnitSpec
Expand All @@ -40,8 +41,12 @@ class ObligationsConnectorSpec extends UnitSpec with OneAppPerSuite
override val appContext = mockAppContext
}
MockAppContext.desUrl returns desBaseUrl
MockAppContext.desToken returns desToken
MockAppContext.desEnv returns desEnvironment
}

lazy val desToken = "test-token"
lazy val desEnvironment = "test-env"
lazy val desBaseUrl = "des-base-url"

val vrn: Vrn = generateVrn
Expand All @@ -50,13 +55,26 @@ class ObligationsConnectorSpec extends UnitSpec with OneAppPerSuite
val desUrl = s"$desBaseUrl/enterprise/obligation-data/vrn/$vrn/VATC?$queryString"
val desObligationsJson: JsValue = Jsons.Obligations.desResponse(vrn)

val desHttpResponse = HttpResponse(200, Some(desObligationsJson))

implicit val hc = HeaderCarrier()

"get" should {
"returns an ObligationsResponse" when {
"DES returns a 200 response" in new Setup {
val desHttpResponse = HttpResponse(200, Some(desObligationsJson))
"have the DES headers in the request" in new Setup {
MockHttp.GET[HttpResponse](desUrl)
.returns(Future.successful(desHttpResponse))

await(testObligationsConnector.get(vrn, queryParams))

val headers = MockHttp.fetchHeaderCarrier.headers.toMap
headers("Accept") shouldBe MimeTypes.JSON
headers("Environment") shouldBe desEnvironment
headers("Authorization") shouldBe s"Bearer $desToken"
headers("Originator-Id") shouldBe "DA_SDI"
}

"return an ObligationsResponse" when {
"DES returns a 200 response" in new Setup {
MockHttp.GET[HttpResponse](desUrl)
.returns(Future.successful(desHttpResponse))

Expand Down
10 changes: 7 additions & 3 deletions test/uk/gov/hmrc/vatapi/mocks/MockHttp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package uk.gov.hmrc.vatapi.mocks

import org.mockito.Matchers
import org.mockito.{ArgumentCaptor, Matchers}
import org.mockito.stubbing.OngoingStubbing
import org.scalatest.Suite
import play.api.libs.json.Writes
Expand All @@ -35,10 +35,14 @@ trait MockHttp extends Mock { _: Suite =>
reset(mockHttp)
}

object MockHttp{
object MockHttp {
private val headerCarrierCaptor = ArgumentCaptor.forClass(classOf[HeaderCarrier])

def GET[T](url: String): OngoingStubbing[Future[T]] = {
when(mockHttp.GET[T](eqTo(url))(any(), any(), any()))
when(mockHttp.GET[T](eqTo(url))(any(), headerCarrierCaptor.capture(), any()))
}

def fetchHeaderCarrier: HeaderCarrier = headerCarrierCaptor.getValue
}

def setupMockHttpGet(url: String)(response: HttpResponse): OngoingStubbing[Future[HttpResponse]] =
Expand Down
2 changes: 2 additions & 0 deletions test/uk/gov/hmrc/vatapi/mocks/config/MockAppContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ trait MockAppContext extends Mock { _: Suite =>

object MockAppContext {
def desUrl = when(mockAppContext.desUrl)
def desToken = when(mockAppContext.desToken)
def desEnv = when(mockAppContext.desEnv)
}

override protected def beforeEach(): Unit = {
Expand Down
1 change: 1 addition & 0 deletions test/uk/gov/hmrc/vatapi/services/NRSServiceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

package uk.gov.hmrc.vatapi.services

import org.joda.time.DateTime
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.mockito.MockitoSugar
Expand Down

0 comments on commit 682faa6

Please sign in to comment.