Skip to content

Commit

Permalink
Merge pull request #76 from hmrc/APIS-4758
Browse files Browse the repository at this point in the history
APIS-4758 - Add PPNS Topic field type
  • Loading branch information
adampridmore authored Jun 9, 2020
2 parents 0577e25 + 46cef41 commit 664571c
Show file tree
Hide file tree
Showing 42 changed files with 819 additions and 350 deletions.
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.acceptance
package uk.gov.hmrc.apisubscriptionfields

import java.util.UUID

Expand All @@ -28,23 +28,21 @@ import play.api.mvc.request.RequestTarget
import play.api.test.FakeRequest
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 Types.Fields

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import cats.data.NonEmptyList
import uk.gov.hmrc.apisubscriptionfields.FieldDefinitionTestData
import uk.gov.hmrc.apisubscriptionfields.controller.Helper

trait AcceptanceTestSpec extends FeatureSpec
with GivenWhenThen
with BeforeAndAfterAll
with Matchers
with GuiceOneServerPerSuite
with FieldDefinitionTestData {
with FieldDefinitionTestData
with Helper {

protected val ValidRequest = FakeRequest()
.withHeaders(RequestHeaders.ACCEPT_HMRC_JSON_HEADER)
Expand All @@ -67,17 +65,11 @@ trait AcceptanceTestSpec extends FeatureSpec
protected val SampleFields1 = Map(fieldN(1) -> "http://www.example.com/some-endpoint", fieldN(2) -> "value2")
protected val SampleFields2 = Map(fieldN(1) -> "https://www.example2.com/updated", fieldN(3) -> "value3")

protected def validSubscriptionPutRequest(fields: Fields): FakeRequest[AnyContentAsJson] =
validSubscriptionPutRequest(SubscriptionFieldsRequest(fields))

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

protected def validDefinitionPutRequest(fieldDefinitions: NonEmptyList[FieldDefinition]): FakeRequest[AnyContentAsJson] =
validDefinitionPutRequest(FieldDefinitionsRequest(fieldDefinitions))

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

protected def fakeRequestWithHeaders: FakeRequest[AnyContentAsEmpty.type] = {
FakeRequest().withHeaders(RequestHeaders.ACCEPT_HMRC_JSON_HEADER, RequestHeaders.CONTENT_TYPE_HEADER)
Expand All @@ -87,6 +79,7 @@ trait AcceptanceTestSpec extends FeatureSpec
s"/field/application/$clientId/context/$apiContext/version/$apiVersion"

override def fakeApplication(): Application = new GuiceApplicationBuilder().configure(Map(
"metrics.jvm" -> false,
"run.mode" -> "Stub",
"Test.microservice.services.api-subscription-fields.host" -> ExternalServicesConfig.Host,
"Test.microservice.services.api-subscription-fields.port" -> ExternalServicesConfig.Port
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.acceptance
package uk.gov.hmrc.apisubscriptionfields

import org.scalatest.OptionValues
import org.scalatest.BeforeAndAfterAll
Expand All @@ -25,7 +25,6 @@ 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.{FieldDefinitionTestData, SubscriptionFieldsTestData}
import scala.concurrent.Future
import scala.concurrent.Await
import scala.concurrent.duration._
Expand All @@ -38,7 +37,7 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
with BeforeAndAfterAll {

override def beforeAll() {
val putRequest = validDefinitionPutRequest(FieldDefinitionsRequest(NelOfFieldDefinitions))
val putRequest = validDefinitionPutRequest(NelOfFieldDefinitions)
.withTarget( RequestTarget(uriString="", path=definitionEndpoint(fakeRawContext, fakeRawVersion), queryString = Map.empty))

Await.result(route(app, putRequest).get, 10.seconds)
Expand Down Expand Up @@ -70,16 +69,16 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
status(resultFuture) shouldBe CREATED

And("the response body should be a valid response")
val sfr = contentAsJson(resultFuture).validate[SubscriptionFieldsResponse]
val sfr = contentAsJson(resultFuture).validate[SubscriptionFields]
val fieldsId = sfr.get.fieldsId

sfr.isSuccess shouldBe true
sfr.get shouldBe SubscriptionFieldsResponse(fakeRawClientId, "acontext", "1.0.2", fieldsId, SampleFields1)
sfr.get shouldBe SubscriptionFields(FakeClientId, ApiContext("acontext"), ApiVersion("1.0.2"), fieldsId, SampleFields1)
}

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

def createSubscriptionFields()= {
Expand All @@ -103,11 +102,11 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
status(resultFuture) shouldBe OK

And("the response body should be a valid response")
val sfr = contentAsJson(resultFuture).validate[SubscriptionFieldsResponse]
val sfr = contentAsJson(resultFuture).validate[SubscriptionFields]
val fieldsId = sfr.get.fieldsId

sfr.isSuccess shouldBe true
sfr.get shouldBe SubscriptionFieldsResponse(fakeRawClientId, "acontext", "1.0.2", fieldsId, SampleFields1)
sfr.get shouldBe SubscriptionFields(FakeClientId, ApiContext("acontext"), ApiVersion("1.0.2"), fieldsId, SampleFields1)
}

scenario("the API is called to GET all existing subscription fields") {
Expand All @@ -131,7 +130,7 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
val fieldsId = sfr.get.subscriptions.head.fieldsId

sfr.isSuccess shouldBe true
sfr.get shouldBe BulkSubscriptionFieldsResponse(Seq(SubscriptionFieldsResponse(fakeRawClientId, "acontext", "1.0.2", fieldsId, SampleFields1)))
sfr.get shouldBe BulkSubscriptionFieldsResponse(Seq(SubscriptionFields(FakeClientId, ApiContext("acontext"), ApiVersion("1.0.2"), fieldsId, SampleFields1)))
}

scenario("the API is called to GET with a known fieldsId") {
Expand All @@ -152,7 +151,7 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
status(resultFuture) shouldBe OK

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

Given("a request with a known fieldsId")
val fieldsId = sfr.get.fieldsId
Expand All @@ -168,10 +167,10 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
val resultFieldsIdFuture = result.value

status(resultFieldsIdFuture) shouldBe OK
val sfrFieldsId = contentAsJson(resultFuture).validate[SubscriptionFieldsResponse]
val sfrFieldsId = contentAsJson(resultFuture).validate[SubscriptionFields]

sfrFieldsId.isSuccess shouldBe true
sfrFieldsId.get shouldBe SubscriptionFieldsResponse(fakeRawClientId, "acontext", "1.0.2", fieldsId, SampleFields1)
sfrFieldsId.get shouldBe SubscriptionFields(FakeClientId, ApiContext("acontext"), ApiVersion("1.0.2"), fieldsId, SampleFields1)
}

scenario("the API is called to GET existing subscription fields by application clientId") {
Expand All @@ -196,7 +195,7 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
val fieldsId = sfr.get.subscriptions.head.fieldsId

sfr.isSuccess shouldBe true
sfr.get shouldBe BulkSubscriptionFieldsResponse(Seq(SubscriptionFieldsResponse(fakeRawClientId, "acontext", "1.0.2", fieldsId, SampleFields1)))
sfr.get shouldBe BulkSubscriptionFieldsResponse(Seq(SubscriptionFields(FakeClientId, ApiContext("acontext"), ApiVersion("1.0.2"), fieldsId, SampleFields1)))
}

scenario("the API is called to update existing subscription fields") {
Expand All @@ -216,11 +215,11 @@ class ApiSubscriptionFieldsHappySpec extends AcceptanceTestSpec
status(resultFuture) shouldBe OK

And("the response body should be a valid response")
val sfr = contentAsJson(resultFuture).validate[SubscriptionFieldsResponse]
val sfr = contentAsJson(resultFuture).validate[SubscriptionFields]
val fieldsId = sfr.get.fieldsId

sfr.isSuccess shouldBe true
sfr.get shouldBe SubscriptionFieldsResponse(fakeRawClientId, "acontext", "1.0.2", fieldsId, SampleFields2)
sfr.get shouldBe SubscriptionFields(FakeClientId, ApiContext("acontext"), ApiVersion("1.0.2"), fieldsId, SampleFields2)
}

scenario("the API is called to DELETE existing subscription fields") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
* limitations under the License.
*/

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

import org.scalatest.OptionValues
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

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.acceptance
package uk.gov.hmrc.apisubscriptionfields

object ExternalServicesConfig {
val Port: Int = 11111
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
* limitations under the License.
*/

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

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.{FieldDefinitionTestData, SubscriptionFieldsTestData}

import scala.concurrent.Future

class SubscriptionFieldDefinitionsHappySpec extends AcceptanceTestSpec
Expand All @@ -37,7 +35,7 @@ class SubscriptionFieldDefinitionsHappySpec extends AcceptanceTestSpec

scenario("the API is called to store some new fields definitions") {
Given("Definitiions are created ")
val putRequest = validDefinitionPutRequest(FieldDefinitionsRequest(NelOfFieldDefinitions))
val putRequest = validDefinitionPutRequest(NelOfFieldDefinitions)
.withTarget( RequestTarget(uriString="", path=definitionEndpoint(fakeRawContext, fakeRawVersion), queryString = Map.empty))

When("a PUT request with data is sent to the API")
Expand All @@ -50,10 +48,10 @@ class SubscriptionFieldDefinitionsHappySpec extends AcceptanceTestSpec
status(putResultFuture) shouldBe CREATED

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

sfr.isSuccess shouldBe true
sfr.get shouldBe ApiFieldDefinitionsResponse(fakeRawContext, fakeRawVersion, NelOfFieldDefinitions)
sfr.get shouldBe ApiFieldDefinitions(FakeContext, FakeVersion, NelOfFieldDefinitions)
}


Expand All @@ -76,7 +74,7 @@ class SubscriptionFieldDefinitionsHappySpec extends AcceptanceTestSpec
status(resultFuture) shouldBe OK

And("the response body should be a valid response")
val fdr = contentAsJson(resultFuture).validate[ApiFieldDefinitionsResponse]
val fdr = contentAsJson(resultFuture).validate[ApiFieldDefinitions]

fdr.isSuccess shouldBe true
fdr.get shouldBe FakeApiFieldDefinitionsResponse
Expand Down Expand Up @@ -108,7 +106,7 @@ class SubscriptionFieldDefinitionsHappySpec extends AcceptanceTestSpec
scenario("the API is called to update some existing fields definitions") {

Given("a request with valid payload")
val request = validDefinitionPutRequest(FieldDefinitionsRequest(NelOfFieldDefinitions))
val request = validDefinitionPutRequest(NelOfFieldDefinitions)
.withTarget( RequestTarget(uriString="", path=definitionEndpoint(fakeRawContext, fakeRawVersion), queryString = Map.empty))

When("a PUT request with data is sent to the API")
Expand All @@ -120,10 +118,10 @@ class SubscriptionFieldDefinitionsHappySpec extends AcceptanceTestSpec
status(resultFuture) shouldBe OK

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

sfr.isSuccess shouldBe true
sfr.get shouldBe ApiFieldDefinitionsResponse(fakeRawContext, fakeRawVersion, NelOfFieldDefinitions)
sfr.get shouldBe ApiFieldDefinitions(FakeContext, FakeVersion, NelOfFieldDefinitions)
}

scenario("the API is called to delete some existing fields definitions") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
* limitations under the License.
*/

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

import org.scalatest.OptionValues
import play.api.libs.json.Json
import play.api.mvc._
import play.api.mvc.request.RequestTarget
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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package uk.gov.hmrc.apisubscriptionfields.controller

import uk.gov.hmrc.apisubscriptionfields.model.{Types, FieldDefinition}
import cats.data.{NonEmptyList => NEL}

trait Helper {
def makeSubscriptionFieldsRequest(fields: Types.Fields): SubscriptionFieldsRequest = SubscriptionFieldsRequest(fields)
def makeFieldDefinitionsRequest(definitions: NEL[FieldDefinition]): FieldDefinitionsRequest = FieldDefinitionsRequest(definitions)
}

object Helper extends Helper
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2020 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.config

import com.google.inject.{ImplementedBy, Inject, Singleton}
import play.api.Configuration

@ImplementedBy(classOf[ApplicationConfigImpl])
trait ApplicationConfig {
def pushPullNotificationServiceURL: String
}

@Singleton
class ApplicationConfigImpl @Inject()(config: Configuration) extends ApplicationConfig {
private val HOCON = config.underlying

// Moving away from complex layers configurations
val pushPullNotificationServiceURL = HOCON.getString("microservice.services.push-pull-notification.uri")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2020 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.connector

import play.api.libs.json._
import uk.gov.hmrc.apisubscriptionfields.model.{ClientId, TopicId}

trait JsonFormatters {
implicit val clientIdJF = Json.valueFormat[ClientId]
implicit val topicIdJF = Json.valueFormat[TopicId]

implicit val createTopicRequestJF = Json.format[CreateTopicRequest]
implicit val createTopicResponseJF = Json.format[CreateTopicResponse]
implicit val subscribersRequestJF = Json.format[SubscribersRequest]
implicit val updateSubscribersRequestJF = Json.format[UpdateSubscribersRequest]
implicit val updateSubscribersResponseJF = Json.format[UpdateSubscribersResponse]
}

object JsonFormatters extends JsonFormatters
Loading

0 comments on commit 664571c

Please sign in to comment.