Skip to content

Commit

Permalink
API-4199 (#62)
Browse files Browse the repository at this point in the history
* API-4199: Refactor WIP

* API-4199: moved Acceptance Tests into root

* API-4199: acceptance tests now passing

* API-4199: acceptance tests now passing

* API-4199: acceptance tests now passing

Co-authored-by: Chris Rowe <[email protected]>
Co-authored-by: Matt Clark <[email protected]>
  • Loading branch information
3 people authored Mar 6, 2020
1 parent b6e3ef3 commit 0bea821
Show file tree
Hide file tree
Showing 32 changed files with 201 additions and 263 deletions.
5 changes: 3 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down Expand Up @@ -178,15 +179,15 @@
APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2018 HM Revenue & Customs
Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ package uk.gov.hmrc.apisubscriptionfields.acceptance
import java.util.UUID

import org.scalatest._
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import org.scalatestplus.play.guice.GuiceOneServerPerSuite
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.Json
import play.api.mvc.{AnyContentAsEmpty, AnyContentAsJson}
import play.api.mvc._
import play.api.mvc.request.RequestTarget
import play.api.test.FakeRequest
import play.modules.reactivemongo.MongoDbConnection
import play.api.test.Helpers._
import play.modules.reactivemongo.ReactiveMongoComponent
import uk.gov.hmrc.apisubscriptionfields.RequestHeaders
import uk.gov.hmrc.apisubscriptionfields.model.JsonFormatters._
import uk.gov.hmrc.apisubscriptionfields.model._
import uk.gov.hmrc.apisubscriptionfields.util.{ExternalServicesConfig, RequestHeaders}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
Expand All @@ -38,7 +40,7 @@ trait AcceptanceTestSpec extends FeatureSpec
with GivenWhenThen
with BeforeAndAfterAll
with Matchers
with GuiceOneAppPerSuite {
with GuiceOneServerPerSuite {

protected val ValidRequest = FakeRequest()
.withHeaders(RequestHeaders.ACCEPT_HMRC_JSON_HEADER)
Expand All @@ -65,13 +67,13 @@ trait AcceptanceTestSpec extends FeatureSpec
validSubscriptionPutRequest(SubscriptionFieldsRequest(fields))

protected def validSubscriptionPutRequest(contents: SubscriptionFieldsRequest): FakeRequest[AnyContentAsJson] =
fakeRequestWithHeaders.withJsonBody(Json.toJson(contents))
fakeRequestWithHeaders.withMethod(PUT).withJsonBody(Json.toJson(contents))

protected def validDefinitionPutRequest(fieldDefinitions: Seq[FieldDefinition]): FakeRequest[AnyContentAsJson] =
validDefinitionPutRequest(FieldsDefinitionRequest(fieldDefinitions))

protected def validDefinitionPutRequest(contents: FieldsDefinitionRequest): FakeRequest[AnyContentAsJson] =
fakeRequestWithHeaders.withJsonBody(Json.toJson(contents))
fakeRequestWithHeaders.withMethod(PUT).withJsonBody(Json.toJson(contents))

protected def fakeRequestWithHeaders: FakeRequest[AnyContentAsEmpty.type] = {
FakeRequest().withHeaders(RequestHeaders.ACCEPT_HMRC_JSON_HEADER, RequestHeaders.CONTENT_TYPE_HEADER)
Expand All @@ -98,6 +100,11 @@ trait AcceptanceTestSpec extends FeatureSpec
}

private def dropDatabase(): Unit = {
await(new MongoDbConnection() {}.db().drop())
await( app.injector.instanceOf[ReactiveMongoComponent].mongoConnector.db().drop())
}

def createRequest(method: String, path: String) =
ValidRequest
.withMethod(method)
.withTarget( RequestTarget(uriString ="", path= path, queryString = Map.empty))
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,31 @@ package uk.gov.hmrc.apisubscriptionfields.acceptance

import org.scalatest.OptionValues
import play.api.Logger
import play.api.libs.json.Json
import play.api.mvc._
import play.api.mvc.request.RequestTarget
import play.api.test.FakeRequest
import play.api.test.Helpers._
import uk.gov.hmrc.apisubscriptionfields.model._
import uk.gov.hmrc.apisubscriptionfields.util.{FieldsDefinitionTestData, SubscriptionFieldsTestData}
import uk.gov.hmrc.apisubscriptionfields.{FieldsDefinitionTestData, SubscriptionFieldsTestData}

import scala.concurrent.Future

class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
with OptionValues
with JsonFormatters
with SubscriptionFieldsTestData
with FieldsDefinitionTestData {

import uk.gov.hmrc.apisubscriptionfields.model.JsonFormatters._

feature("Subscription-Fields") {

Logger.logger.info(s"App.mode = ${app.mode.toString}")

scenario("the API is called to store some values for a new subscription field") {
Given("a request with valid payload")
val request = validSubscriptionPutRequest(SampleFields1)
.copyFakeRequest(method = PUT, uri = subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))
val request = createSubscriptionFieldsRequest()

When("a PUT request with data is sent to the API")
When("a PUT request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)

Then(s"a response with a 201 status is received")
Expand All @@ -58,11 +59,21 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
sfr.get shouldBe SubscriptionFieldsResponse(fakeRawClientId, "acontext", "1.0.2", fieldsId, SampleFields1)
}

def createSubscriptionFieldsRequest(): FakeRequest[AnyContentAsJson] = {
createRequest(PUT, subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))
.withJsonBody(Json.toJson(SubscriptionFieldsRequest(SampleFields1)))
}

def createSubscriptionFields()= {
route(app, createSubscriptionFieldsRequest())
}

scenario("the API is called to GET some existing subscription fields") {

Given("a request with a known subscription field")
val request = ValidRequest
.copyFakeRequest(method = GET, uri = subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))
createSubscriptionFields()

val request = createRequest(GET, subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))

When("a GET request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand All @@ -84,8 +95,9 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
scenario("the API is called to GET all existing subscription fields") {

Given("a request with a known subscription field")
val request = ValidRequest
.copyFakeRequest(method = GET, uri = allSubscriptionFieldsEndpoint)
createSubscriptionFields()

val request = createRequest(GET, allSubscriptionFieldsEndpoint)

When("a GET request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand All @@ -107,8 +119,10 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
scenario("the API is called to GET with a known fieldsId") {

Given("a request with a known fieldsId")
val requestId = ValidRequest
.copyFakeRequest(method = GET, uri = subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))

createSubscriptionFields()

val requestId = createRequest(GET, subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))

When("an id GET request with data is sent to the API")
val result: Option[Future[Result]] = route(app, requestId)
Expand All @@ -124,7 +138,9 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec

Given("a request with a known fieldsId")
val fieldsId = sfr.get.fieldsId
val requestFieldsId = ValidRequest.copyFakeRequest(method = GET, uri = fieldsIdEndpoint(fieldsId.value))

val requestFieldsId = createRequest(GET, fieldsIdEndpoint(fieldsId.value))


When("a fieldsId GET request with data is sent to the API")
val resultFieldsId: Option[Future[Result]] = route(app, requestFieldsId)
Expand All @@ -143,7 +159,9 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
scenario("the API is called to GET existing subscription fields by application clientId") {

Given("a request with a known client identifier")
val request = ValidRequest.copyFakeRequest(method = GET, uri = byClientIdEndpoint(fakeRawClientId))
createSubscriptionFields()

val request = createRequest(GET, byClientIdEndpoint(fakeRawClientId))

When("a GET request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand All @@ -165,8 +183,9 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
scenario("the API is called to update existing subscription fields") {

Given("a request with valid payload")
createSubscriptionFields()
val request = validSubscriptionPutRequest(SampleFields2)
.copyFakeRequest(method = PUT, uri = subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))
.withTarget(RequestTarget(uriString = "", path = subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion), queryString = Map.empty))

When("a PUT request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand All @@ -188,8 +207,9 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
scenario("the API is called to DELETE existing subscription fields") {

Given("a request with existing subscription fields")
val request = ValidRequest
.copyFakeRequest(method = DELETE, uri = subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))
createSubscriptionFields()

val request = createRequest(DELETE, subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))

When("a GET request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import play.api.Logger
import play.api.libs.json.Json
import play.api.mvc._
import play.api.test.Helpers._
import uk.gov.hmrc.apisubscriptionfields.SubscriptionFieldsTestData
import uk.gov.hmrc.apisubscriptionfields.model.ErrorCode.{INVALID_REQUEST_PAYLOAD, NOT_FOUND_CODE}
import uk.gov.hmrc.apisubscriptionfields.model.JsErrorResponse
import uk.gov.hmrc.apisubscriptionfields.util.SubscriptionFieldsTestData

import scala.concurrent.Future

Expand All @@ -37,9 +37,7 @@ class ApiSubscriptionFieldsUnhappySpec extends AcceptanceTestSpec
scenario("the API is called to GET non-existing subscription fields") {

Given("the API is called to GET non-existing subscription fields")
val request = ValidRequest
.copyFakeRequest(method = GET, uri = subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))

val request = createRequest(GET, subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))
When("a GET request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)

Expand All @@ -56,7 +54,7 @@ class ApiSubscriptionFieldsUnhappySpec extends AcceptanceTestSpec
scenario("the API is called to GET with an unknown fieldsId") {

Given("the API is called to GET with an unknown fieldsId")
val request = ValidRequest.copyFakeRequest(method = GET, uri = fieldsIdEndpoint(FakeRawFieldsId))
val request = createRequest(GET, fieldsIdEndpoint(FakeRawFieldsId))

When("a GET request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand All @@ -74,7 +72,7 @@ class ApiSubscriptionFieldsUnhappySpec extends AcceptanceTestSpec
scenario("the API is called to GET an unknown application clientId") {

Given("the API is called to GET an unknown application clientId")
val request = ValidRequest.copyFakeRequest(method = GET, uri = byClientIdEndpoint(fakeRawClientId))
val request = createRequest(GET, byClientIdEndpoint(fakeRawClientId))

When("a GET request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand All @@ -92,8 +90,7 @@ class ApiSubscriptionFieldsUnhappySpec extends AcceptanceTestSpec
scenario("the API is called to DELETE an unknown subscription field") {

Given("a request with an unknown subscription field")
val request = ValidRequest
.copyFakeRequest(method = DELETE, uri = subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))
val request = createRequest(DELETE, subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))

When("a GET request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand All @@ -111,8 +108,8 @@ class ApiSubscriptionFieldsUnhappySpec extends AcceptanceTestSpec
scenario("the API is called to PUT subscription fields with an invalid JSON payload") {

Given("the API is called to PUT subscription fields with an invalid JSON payload")
val request = ValidRequest
.copyFakeRequest(method = PUT, uri = subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion), body = Json.parse("{}"))
val request = createRequest(PUT, subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))
.withJsonBody(Json.parse("{}"))

When("a PUT request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand All @@ -130,8 +127,8 @@ class ApiSubscriptionFieldsUnhappySpec extends AcceptanceTestSpec
scenario("the API is called to PUT subscription fields with an invalid non JSON payload") {

Given("the API is called to PUT subscription fields with an invalid non JSON payload")
val request = ValidRequest
.copyFakeRequest(method = PUT, uri = subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion), body = InvalidNonJsonPayload)
val request = createRequest(PUT, subscriptionFieldsEndpoint(fakeRawClientId, fakeRawContext, fakeRawVersion))
.withBody(InvalidNonJsonPayload)

When("a PUT request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package uk.gov.hmrc.apisubscriptionfields.util
package uk.gov.hmrc.apisubscriptionfields.acceptance

object ExternalServicesConfig {
val Port: Int = 11111
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,51 @@ package uk.gov.hmrc.apisubscriptionfields.acceptance

import org.scalatest.OptionValues
import play.api.mvc._
import play.api.mvc.request.RequestTarget
import play.api.test.Helpers._
import uk.gov.hmrc.apisubscriptionfields.model._
import uk.gov.hmrc.apisubscriptionfields.util.{FieldsDefinitionTestData, SubscriptionFieldsTestData}
import uk.gov.hmrc.apisubscriptionfields.{FieldsDefinitionTestData, SubscriptionFieldsTestData}

import scala.concurrent.Future

class SubscriptionFieldDefinitionsHappySpec extends AcceptanceTestSpec
with OptionValues
with SubscriptionFieldsTestData
with FieldsDefinitionTestData {

import uk.gov.hmrc.apisubscriptionfields.model.JsonFormatters._
with FieldsDefinitionTestData
with JsonFormatters {

feature("Fields-Definition") {

scenario("the API is called to store some new fields definitions") {

Given("a request with valid payload")
val request = validDefinitionPutRequest(FakeFieldsDefinitions)
.copyFakeRequest(method = PUT, uri = definitionEndpoint(fakeRawContext, fakeRawVersion))
Given("Definitiions are created ")
val putRequest = validDefinitionPutRequest(FakeFieldsDefinitions)
.withTarget( RequestTarget(uriString="", path=definitionEndpoint(fakeRawContext, fakeRawVersion), queryString = Map.empty))

When("a PUT request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
val putResult: Option[Future[Result]] = route(app, putRequest)

Then(s"a response with a 201 status is received")
result shouldBe 'defined
val resultFuture = result.value
putResult shouldBe 'defined
val putResultFuture = putResult.value

status(resultFuture) shouldBe CREATED
status(putResultFuture) shouldBe CREATED

And("the response body should be a valid response")
val sfr = contentAsJson(resultFuture).validate[FieldsDefinitionResponse]
val sfr = contentAsJson(putResultFuture).validate[FieldsDefinitionResponse]

sfr.isSuccess shouldBe true
sfr.get shouldBe FieldsDefinitionResponse(fakeRawContext, fakeRawVersion, FakeFieldsDefinitions)
}




scenario("the API is called to GET a known fields definition") {

Given("a request with a known fields definition")
val request = ValidRequest.copyFakeRequest(method = GET, uri = definitionEndpoint(fakeRawContext, fakeRawVersion))
Then("a request with a known fields definition")
val request = ValidRequest
.withMethod(GET)
.withTarget(RequestTarget(uriString = "", path = definitionEndpoint(fakeRawContext, fakeRawVersion), queryString = Map.empty))

When("a GET request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand All @@ -79,7 +83,9 @@ class SubscriptionFieldDefinitionsHappySpec extends AcceptanceTestSpec
scenario("the API is called to GET all fields definitions") {

Given("a request for all fields definition")
val request = ValidRequest.copyFakeRequest(method = GET, uri = allDefinitionsEndpoint)
val request = ValidRequest
.withMethod(GET)
.withTarget(RequestTarget(uriString = allDefinitionsEndpoint, path = allDefinitionsEndpoint, queryString = Map.empty))

When("a GET request is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand All @@ -100,8 +106,8 @@ class SubscriptionFieldDefinitionsHappySpec extends AcceptanceTestSpec
scenario("the API is called to update some existing fields definitions") {

Given("a request with valid payload")
val request = validDefinitionPutRequest(Seq.empty)
.copyFakeRequest(method = PUT, uri = definitionEndpoint(fakeRawContext, fakeRawVersion))
val request = validDefinitionPutRequest(Seq.empty)
.withTarget( RequestTarget(uriString="", path=definitionEndpoint(fakeRawContext, fakeRawVersion), queryString = Map.empty))

When("a PUT request with data is sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand All @@ -122,8 +128,9 @@ class SubscriptionFieldDefinitionsHappySpec extends AcceptanceTestSpec
scenario("the API is called to delete some existing fields definitions") {

Given("a request with valid payload")
val request = validDefinitionPutRequest(Seq.empty)
.copyFakeRequest(method = DELETE, uri = definitionEndpoint(fakeRawContext, fakeRawVersion))
val request = ValidRequest
.withMethod(DELETE)
.withTarget( RequestTarget(uriString="", path=definitionEndpoint(fakeRawContext, fakeRawVersion), queryString = Map.empty))

When("a DELETE request sent to the API")
val result: Option[Future[Result]] = route(app, request)
Expand Down
Loading

0 comments on commit 0bea821

Please sign in to comment.