Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/NINOA-57'
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDeadlyPianist authored and TomDudley92 committed Dec 9, 2019
2 parents 0f40ff9 + db96107 commit 2444ba9
Show file tree
Hide file tree
Showing 49 changed files with 1,080 additions and 39 deletions.
3 changes: 3 additions & 0 deletions app/config/AppConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ trait AppConfig {

def desToken(): String

def desContext(): String

def featureSwitch: Option[Configuration]
}

Expand All @@ -41,4 +43,5 @@ class AppConfigImpl @Inject()(configuration: ServicesConfig, config: Configurati
override lazy val desBaseUrl: String = configuration.baseUrl("des")
override lazy val desEnvironment: String = configuration.getString(s"$desServicePrefix.env")
override lazy val desToken: String = configuration.getString(s"$desServicePrefix.token")
override lazy val desContext: String = configuration.getString(s"$desServicePrefix.context")
}
45 changes: 45 additions & 0 deletions app/v1/connectors/DesConnector.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2019 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 v1.connectors

import config.AppConfig
import javax.inject.{Inject, Singleton}
import play.api.libs.json.Json
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.bootstrap.http.HttpClient
import v1.connectors.httpParsers.HttpResponseTypes.HttpPostResponse
import v1.connectors.httpParsers.RegisterNinoResponseHttpParser.RegisterNinoResponseReads
import v1.models.request.NinoApplication
import v1.models.response.DesResponseModel

import scala.concurrent.{ExecutionContext, Future}

@Singleton
class DesConnector @Inject()(
http: HttpClient,
appConfig: AppConfig
) {

def sendRegisterRequest(request: NinoApplication)
(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[HttpPostResponse[DesResponseModel]] = {

val url = s"${appConfig.desBaseUrl()}/${appConfig.desContext()}"

http.POST(url, Json.toJson(request))(implicitly, RegisterNinoResponseReads, hc, ec)
}

}
23 changes: 23 additions & 0 deletions app/v1/connectors/httpParsers/HttpResponseTypes.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019 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 v1.connectors.httpParsers

import v1.models.errors.Error

object HttpResponseTypes {
type HttpPostResponse[T] = Either[Error, T]
}
48 changes: 48 additions & 0 deletions app/v1/connectors/httpParsers/RegisterNinoResponseHttpParser.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2019 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 v1.connectors.httpParsers

import play.api.Logger
import play.api.http.Status
import uk.gov.hmrc.http.{HttpReads, HttpResponse}
import v1.connectors.httpParsers.HttpResponseTypes.HttpPostResponse
import v1.models.errors.{Error, InvalidJsonResponseError}
import v1.models.response.DesResponseModel

object RegisterNinoResponseHttpParser {

implicit object RegisterNinoResponseReads extends HttpReads[HttpPostResponse[DesResponseModel]] {
override def read(method: String, url: String, response: HttpResponse): HttpPostResponse[DesResponseModel] = {
response.status match {
case Status.OK =>
Logger.debug("[RegisterNinoResponseHttpParser][read] Status OK")
response.json.validate[DesResponseModel].fold(
invalid => {
Logger.debug(s"[RegisterNinoResponseHttpParser][read] Invalid json - $invalid")
Logger.warn(s"[RegisterNinoResponseHttpParser][read] Invalid json")
Left(InvalidJsonResponseError)
},
Right(_)
)
case status =>
Logger.warn(s"[RegisterNinoResponseHttpParser][read] Unexpected $status response returned")
Left(Error(s"$status", "Downstream error returned from DES when submitting a NINO register request"))
}
}
}

}
25 changes: 21 additions & 4 deletions app/v1/controllers/RegisterNinoController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,31 @@ import javax.inject.{Inject, Singleton}
import play.api.libs.json.Json
import play.api.mvc.{Action, AnyContent, ControllerComponents}
import uk.gov.hmrc.play.bootstrap.controller.BackendController
import v1.models.errors.Error
import v1.models.request.NinoApplication
import v1.services.DesService
import v1.utils.JsonBodyUtil

import scala.concurrent.Future
import scala.concurrent.{ExecutionContext, Future}

@Singleton()
class RegisterNinoController @Inject()(cc: ControllerComponents) extends BackendController(cc) {
@Singleton
class RegisterNinoController @Inject()(
cc: ControllerComponents,
desService: DesService
)(implicit ec: ExecutionContext) extends BackendController(cc) with JsonBodyUtil {

def register(): Action[AnyContent] = Action.async { implicit request =>
Future.successful(Ok(Json.obj("message" -> "A response")))
Future(parsedJsonBody[NinoApplication]).flatMap {
case Left(errors) => Future.successful(BadRequest(Json.toJson(errors)))
case Right(ninoModel) => desService.registerNino(ninoModel).map{
case Right(responseModel) => Ok(Json.toJson(responseModel))
case Left(error) => BadRequest(Json.toJson(error))
}
}.recover {
case t: Throwable => BadRequest(Json.toJson(Error(
s"$BAD_REQUEST", t.getMessage
)))
}
}

}
3 changes: 2 additions & 1 deletion app/v1/models/errors/Error.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ object InvalidBodyTypeError extends Error("INVALID_BODY_TYPE", "Expecting text/j
object InvalidAcceptHeaderError extends Error("ACCEPT_HEADER_INVALID", "The accept header is missing or invalid")
object UnsupportedVersionError extends Error("NOT_FOUND", "The requested resource could not be found")


object JsonValidationError extends Error("JSON_VALIDATION_ERROR", "The provided JSON was unable to be validated as the selected model.")
object InvalidJsonResponseError extends Error("INVALID_JSON", "The Json returned from DES is invalid")

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.libs.json.{Format, Json}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.Logger
import play.api.libs.json.{JsString, Reads, Writes, __}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.Logger
import play.api.libs.functional.syntax._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.Logger
import play.api.libs.json.{JsString, Reads, Writes, __}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.Logger
import play.api.libs.json.{JsString, Reads, Writes, __}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.Logger
import play.api.libs.json._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.Logger
import play.api.libs.json._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.libs.json.{Json, OFormat}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.Logger
import play.api.libs.functional.syntax._
Expand Down Expand Up @@ -81,4 +81,4 @@ object Marriage {

implicit val writes: Writes[Marriage] = Json.writes[Marriage]

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.Logger
import play.api.libs.functional.syntax._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.libs.functional.syntax._
import play.api.libs.json._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.Logger
import play.api.libs.functional.syntax._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.Logger
import play.api.libs.json.{JsString, Reads, Writes, __}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package v1.models
package v1.models.request

import play.api.libs.json.{Json, OFormat}

Expand Down
25 changes: 25 additions & 0 deletions app/v1/models/response/DesResponseModel.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2019 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 v1.models.response

import play.api.libs.json.{Format, Json}

case class DesResponseModel(message: String)

object DesResponseModel {
implicit val formats: Format[DesResponseModel] = Json.format[DesResponseModel]
}
38 changes: 38 additions & 0 deletions app/v1/services/DesService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 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 v1.services

import javax.inject.{Inject, Singleton}
import uk.gov.hmrc.http.HeaderCarrier
import v1.connectors.DesConnector
import v1.connectors.httpParsers.HttpResponseTypes.HttpPostResponse
import v1.models.request.NinoApplication
import v1.models.response.DesResponseModel

import scala.concurrent.{ExecutionContext, Future}

@Singleton
class DesService @Inject()(
desConnector: DesConnector
) {

def registerNino(ninoApplication: NinoApplication)
(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[HttpPostResponse[DesResponseModel]] = {
desConnector.sendRegisterRequest(ninoApplication)
}

}
Loading

0 comments on commit 2444ba9

Please # to comment.