Skip to content

Commit

Permalink
write tests for JSON de/serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
meghana-viswanath committed Sep 22, 2021
1 parent 66bec46 commit 9587e37
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,20 @@ object HECTaxCheckData {
val entityType: EntityType = EntityType.Company
}

implicit val formatIndividual: OFormat[IndividualHECTaxCheckData] = Json.format[IndividualHECTaxCheckData]
implicit val formatCompany: OFormat[CompanyHECTaxCheckData] = Json.format[CompanyHECTaxCheckData]

implicit val format: OFormat[HECTaxCheckData] = new OFormat[HECTaxCheckData] {

override def reads(json: JsValue): JsResult[HECTaxCheckData] =
(json \ "type")
.validate[EntityType]
.flatMap {
case EntityType.Individual => formatIndividual.reads(json)
case EntityType.Company => formatCompany.reads(json)
case EntityType.Individual => Json.reads[IndividualHECTaxCheckData].reads(json)
case EntityType.Company => Json.reads[CompanyHECTaxCheckData].reads(json)
}

override def writes(o: HECTaxCheckData): JsObject = {
val json = o match {
case i: IndividualHECTaxCheckData => formatIndividual.writes(i)
case c: CompanyHECTaxCheckData => formatCompany.writes(c)
case i: IndividualHECTaxCheckData => Json.writes[IndividualHECTaxCheckData].writes(i)
case c: CompanyHECTaxCheckData => Json.writes[CompanyHECTaxCheckData].writes(c)
}
json ++ Json.obj("type" -> o.entityType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,19 @@ object RetrievedApplicantData {
val entityType: EntityType = EntityType.Company
}

implicit val formatIndividual: OFormat[IndividualRetrievedData] = Json.format[IndividualRetrievedData]
implicit val formatCompany: OFormat[CompanyRetrievedData] = Json.format[CompanyRetrievedData]

implicit val format: OFormat[RetrievedApplicantData] = new OFormat[RetrievedApplicantData] {
override def reads(json: JsValue): JsResult[RetrievedApplicantData] =
(json \ "type")
.validate[EntityType]
.flatMap {
case EntityType.Individual => formatIndividual.reads(json)
case EntityType.Company => formatCompany.reads(json)
case EntityType.Individual => Json.reads[IndividualRetrievedData].reads(json)
case EntityType.Company => Json.reads[CompanyRetrievedData].reads(json)
}

override def writes(o: RetrievedApplicantData): JsObject = {
val json = o match {
case i: IndividualRetrievedData => formatIndividual.writes(i)
case c: CompanyRetrievedData => formatCompany.writes(c)
case i: IndividualRetrievedData => Json.writes[IndividualRetrievedData].writes(i)
case c: CompanyRetrievedData => Json.writes[CompanyRetrievedData].writes(c)
}
json ++ Json.obj("type" -> o.entityType)
}
Expand Down
11 changes: 4 additions & 7 deletions app/uk/gov/hmrc/hecapplicantfrontend/models/UserAnswers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,19 @@ object UserAnswers {

val empty: IncompleteUserAnswers = IncompleteUserAnswers(None, None, None, None, None, None)

implicit val formatIncomplete: OFormat[IncompleteUserAnswers] = Json.format[IncompleteUserAnswers]
implicit val formatComplete: OFormat[CompleteUserAnswers] = Json.format[CompleteUserAnswers]

implicit val format: OFormat[UserAnswers] = new OFormat[UserAnswers] {
override def reads(json: JsValue): JsResult[UserAnswers] =
(json \ "type")
.validate[UserAnswersType]
.flatMap {
case UserAnswersType.Incomplete => formatIncomplete.reads(json)
case UserAnswersType.Complete => formatComplete.reads(json)
case UserAnswersType.Incomplete => Json.reads[IncompleteUserAnswers].reads(json)
case UserAnswersType.Complete => Json.reads[CompleteUserAnswers].reads(json)
}

override def writes(o: UserAnswers): JsObject = {
val json = o match {
case i: IncompleteUserAnswers => formatIncomplete.writes(i)
case c: CompleteUserAnswers => formatComplete.writes(c)
case i: IncompleteUserAnswers => Json.writes[IncompleteUserAnswers].writes(i)
case c: CompleteUserAnswers => Json.writes[CompleteUserAnswers].writes(c)
}
json ++ Json.obj("type" -> o.userAnswersType)
}
Expand Down
126 changes: 126 additions & 0 deletions test/uk/gov/hmrc/hecapplicantfrontend/models/HECTaxCheckDataSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2021 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.hecapplicantfrontend.models

import java.time.LocalDate

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import play.api.libs.json.Json
import uk.gov.hmrc.hecapplicantfrontend.models.ApplicantDetails.{CompanyApplicantDetails, IndividualApplicantDetails}
import uk.gov.hmrc.hecapplicantfrontend.models.HECTaxCheckData.{CompanyHECTaxCheckData, IndividualHECTaxCheckData}
import uk.gov.hmrc.hecapplicantfrontend.models.TaxDetails.{CompanyTaxDetails, IndividualTaxDetails}
import uk.gov.hmrc.hecapplicantfrontend.models.ids.{CTUTR, GGCredId, NINO, SAUTR}
import uk.gov.hmrc.hecapplicantfrontend.models.licence.{LicenceDetails, LicenceTimeTrading, LicenceType, LicenceValidityPeriod}

class HECTaxCheckDataSpec extends AnyWordSpec with Matchers {

"HECTaxCheckData" must {

"perform JSON de/serialisation correctly" must {
val individualTaxCheckData: HECTaxCheckData =
IndividualHECTaxCheckData(
IndividualApplicantDetails(
GGCredId("ggCredId"),
Name("first", "last"),
DateOfBirth(LocalDate.now())
),
LicenceDetails(
LicenceType.ScrapMetalMobileCollector,
LicenceTimeTrading.EightYearsOrMore,
LicenceValidityPeriod.UpToThreeYears
),
IndividualTaxDetails(
NINO("nino"),
Some(SAUTR("utr")),
TaxSituation.SA,
Some(IncomeDeclared.Yes)
)
)

val individualJson = Json.parse("""{
| "applicantDetails":{
| "ggCredId":"ggCredId",
| "name":{
| "firstName":"first",
| "lastName":"last"
| },
| "dateOfBirth":"20210922"
| },
| "licenceDetails":{
| "licenceType":"ScrapMetalMobileCollector",
| "licenceTimeTrading":"EightYearsOrMore",
| "licenceValidityPeriod":"UpToThreeYears"
| },
| "taxDetails":{
| "nino":"nino",
| "sautr":"utr",
| "taxSituation":"SA",
| "saIncomeDeclared":"Yes"
| },
| "type":"Individual"
|}""".stripMargin)

val companyTaxCheckData: HECTaxCheckData =
CompanyHECTaxCheckData(
CompanyApplicantDetails(
GGCredId("ggCredId")
),
LicenceDetails(
LicenceType.ScrapMetalMobileCollector,
LicenceTimeTrading.EightYearsOrMore,
LicenceValidityPeriod.UpToThreeYears
),
CompanyTaxDetails(
CTUTR("utr")
)
)

val companyJson = Json.parse("""{
| "applicantDetails":{
| "ggCredId":"ggCredId"
| },
| "licenceDetails":{
| "licenceType":"ScrapMetalMobileCollector",
| "licenceTimeTrading":"EightYearsOrMore",
| "licenceValidityPeriod":"UpToThreeYears"
| },
| "taxDetails":{
| "ctutr":"utr"
| },
| "type":"Company"
|}""".stripMargin)

"serialize Individual data" in {
Json.toJson(individualTaxCheckData) shouldBe individualJson
}

"serialize Company data" in {
Json.toJson(companyTaxCheckData) shouldBe companyJson
}

"deserialize Individual data" in {
Json.fromJson[HECTaxCheckData](individualJson).get shouldBe individualTaxCheckData
}

"deserialize Company data" in {
Json.fromJson[HECTaxCheckData](companyJson).get shouldBe companyTaxCheckData
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2021 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.hecapplicantfrontend.models

import java.time.LocalDate

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import play.api.libs.json.Json
import uk.gov.hmrc.hecapplicantfrontend.models.RetrievedApplicantData.{CompanyRetrievedData, IndividualRetrievedData}
import uk.gov.hmrc.hecapplicantfrontend.models.ids.{CTUTR, GGCredId, NINO, SAUTR}

class RetrievedApplicantDataSpec extends AnyWordSpec with Matchers {

"RetrievedApplicantData" must {

"perform JSON de/serialisation correctly" must {
val individualRetrievedData: RetrievedApplicantData =
IndividualRetrievedData(
GGCredId("ggCredId"),
NINO("nino"),
Some(SAUTR("utr")),
Name("first", "last"),
DateOfBirth(LocalDate.now()),
Some(EmailAddress("email")),
None
)

val individualJson = Json.parse("""{
|"ggCredId":"ggCredId",
|"nino":"nino",
|"sautr":"utr",
|"name":{
| "firstName":"first",
| "lastName":"last"
|},
|"dateOfBirth":"20210922",
|"emailAddress":"email",
|"type":"Individual"
|}""".stripMargin)

val companyRetrievedData: RetrievedApplicantData =
CompanyRetrievedData(
GGCredId("ggCredId"),
Some(CTUTR("utr")),
Some(EmailAddress("email"))
)

val companyJson = Json.parse("""{
|"ggCredId":"ggCredId",
|"ctutr":"utr",
|"emailAddress":"email",
|"type":"Company"
|}""".stripMargin)

"serialize Individual retrieved data" in {
Json.toJson(individualRetrievedData) shouldBe individualJson
}

"serialize Company retrieved data" in {
Json.toJson(companyRetrievedData) shouldBe companyJson
}

"deserialize Individual retrieved data" in {
Json.fromJson[RetrievedApplicantData](individualJson).get shouldBe individualRetrievedData
}

"deserialize Company retrieved data" when {
"all fields are present" in {
Json.fromJson[RetrievedApplicantData](companyJson).get shouldBe companyRetrievedData
}

"only mandatory fields are present" in {
Json
.fromJson[RetrievedApplicantData](Json.parse("""{"ggCredId":"ggCredId", "type":"Company"}"""))
.get shouldBe CompanyRetrievedData(GGCredId("ggCredId"), None, None)
}
}
}
}

}
56 changes: 56 additions & 0 deletions test/uk/gov/hmrc/hecapplicantfrontend/models/UserAnswersSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package uk.gov.hmrc.hecapplicantfrontend.models

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import play.api.libs.json.Json
import uk.gov.hmrc.hecapplicantfrontend.models.UserAnswers.{CompleteUserAnswers, IncompleteUserAnswers}
import uk.gov.hmrc.hecapplicantfrontend.models.licence.{LicenceTimeTrading, LicenceType, LicenceValidityPeriod}

Expand Down Expand Up @@ -120,6 +121,61 @@ class UserAnswersSpec extends AnyWordSpec with Matchers {

}

"perform JSON de/serialisation correctly" must {
val incompleteAnswers: UserAnswers =
IncompleteUserAnswers(
Some(LicenceType.DriverOfTaxisAndPrivateHires),
Some(LicenceTimeTrading.ZeroToTwoYears),
Some(LicenceValidityPeriod.UpToThreeYears),
Some(TaxSituation.PAYE),
None,
Some(EntityType.Company)
)

val incompleteJson = Json.parse("""{
|"licenceType":"DriverOfTaxisAndPrivateHires",
|"licenceTimeTrading":"ZeroToTwoYears",
|"licenceValidityPeriod":"UpToThreeYears",
|"taxSituation":"PAYE",
|"entityType":"Company",
|"type":"Incomplete"
|}""".stripMargin)

val completeAnswers: UserAnswers =
CompleteUserAnswers(
LicenceType.DriverOfTaxisAndPrivateHires,
LicenceTimeTrading.ZeroToTwoYears,
LicenceValidityPeriod.UpToThreeYears,
TaxSituation.PAYE,
None,
Some(EntityType.Company)
)

val completeJson = Json.parse("""{
|"licenceType":"DriverOfTaxisAndPrivateHires",
|"licenceTimeTrading":"ZeroToTwoYears",
|"licenceValidityPeriod":"UpToThreeYears",
|"taxSituation":"PAYE",
|"entityType":"Company",
|"type":"Complete"
|}""".stripMargin)

"serialize Incomplete answers" in {
Json.toJson(incompleteAnswers) shouldBe incompleteJson
}

"serialize Complete answers" in {
Json.toJson(completeAnswers) shouldBe completeJson
}

"deserialize Incomplete answers" in {
Json.fromJson[UserAnswers](incompleteJson).get shouldBe incompleteAnswers
}

"deserialize Complete answers" in {
Json.fromJson[UserAnswers](completeJson).get shouldBe completeAnswers
}
}
}

}

0 comments on commit 9587e37

Please sign in to comment.