|
| 1 | +/* |
| 2 | + * Copyright 2026 HM Revenue & Customs |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package utils |
| 18 | + |
| 19 | +import base.SpecBase |
| 20 | +import models.Land |
| 21 | + |
| 22 | +class SortServiceSpec extends SpecBase { |
| 23 | + |
| 24 | + val service = new SortService() |
| 25 | + |
| 26 | + private def createLand(landId: Option[String], lastUpdatedDate: Option[String]): Land = { |
| 27 | + Land( |
| 28 | + landID = landId, |
| 29 | + returnID = Some("RET123456789"), |
| 30 | + propertyType = Some("01"), |
| 31 | + interestCreatedTransferred = Some("FG"), |
| 32 | + houseNumber = Some("123"), |
| 33 | + address1 = Some("Baker Street"), |
| 34 | + address2 = Some("Marylebone"), |
| 35 | + address3 = Some("London"), |
| 36 | + address4 = None, |
| 37 | + postcode = Some("NW1 6XE"), |
| 38 | + landArea = Some("250.5"), |
| 39 | + areaUnit = Some("SQMETRE"), |
| 40 | + localAuthorityNumber = Some("5900"), |
| 41 | + mineralRights = Some("NO"), |
| 42 | + NLPGUPRN = Some("10012345678"), |
| 43 | + willSendPlanByPost = Some("NO"), |
| 44 | + titleNumber = Some("TGL12456"), |
| 45 | + landResourceRef = Some("LDN-REF-001"), |
| 46 | + nextLandID = Some("LND002"), |
| 47 | + DARPostcode = Some("NW1 6XE"), |
| 48 | + lastUpdateDate = lastUpdatedDate |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + private def mainLandID: String = "LND001" |
| 53 | + |
| 54 | + "SortService" - { |
| 55 | + ".sortByLastUpdatedDate" - { |
| 56 | + "when the object is Land" - { |
| 57 | + "must sort a list with one item" in { |
| 58 | + |
| 59 | + val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = Some("2025-11-09 19:53:34")) |
| 60 | + val seqOfLands = Seq(land1) |
| 61 | + |
| 62 | + val sortedLandList = Seq(land1) |
| 63 | + |
| 64 | + service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, Some(mainLandID))(_.lastUpdateDate, _.landID) mustBe sortedLandList |
| 65 | + } |
| 66 | + |
| 67 | + "must return the list as is when there is one item and lastUpdateDate is missing" in { |
| 68 | + |
| 69 | + val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = None) |
| 70 | + val seqOfLands = Seq(land1) |
| 71 | + |
| 72 | + val sortedLandList = Seq(land1) |
| 73 | + |
| 74 | + service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, Some(mainLandID))(_.lastUpdateDate, _.landID) mustBe sortedLandList |
| 75 | + } |
| 76 | + |
| 77 | + "must return the list as is when there is one item and mainLandID is missing" in { |
| 78 | + |
| 79 | + val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = Some("2025-11-09 19:53:34")) |
| 80 | + val seqOfLands = Seq(land1) |
| 81 | + |
| 82 | + val sortedLandList = Seq(land1) |
| 83 | + |
| 84 | + service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, mainObjectId = None)(_.lastUpdateDate, _.landID) mustBe sortedLandList |
| 85 | + } |
| 86 | + |
| 87 | + "must return the list as is when there is one item and both lastUpdateDate and mainLandID are missing" in { |
| 88 | + |
| 89 | + val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = None) |
| 90 | + val seqOfLands = Seq(land1) |
| 91 | + |
| 92 | + val sortedLandList = Seq(land1) |
| 93 | + |
| 94 | + service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, mainObjectId = None)(_.lastUpdateDate, _.landID) mustBe sortedLandList |
| 95 | + } |
| 96 | + |
| 97 | + "must sort a list with multiple items" in { |
| 98 | + |
| 99 | + val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = Some("2022-11-09 19:53:34")) |
| 100 | + val land2 = createLand(landId = Some("LND002"), lastUpdatedDate = Some("2023-11-09 19:53:34")) |
| 101 | + val land3 = createLand(landId = Some("LND003"), lastUpdatedDate = Some("2024-12-09 19:53:34")) |
| 102 | + val land4 = createLand(landId = Some("LND004"), lastUpdatedDate = Some("2025-11-09 19:53:34")) |
| 103 | + val land5 = createLand(landId = Some("LND005"), lastUpdatedDate = Some("2025-11-09 19:54:34")) |
| 104 | + |
| 105 | + val seqOfLands = Seq(land4, land2, land1, land5, land3) |
| 106 | + |
| 107 | + val sortedLandList = Seq(land1, land2, land3, land4, land5) |
| 108 | + |
| 109 | + service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, Some(mainLandID))(_.lastUpdateDate, _.landID) mustBe sortedLandList |
| 110 | + } |
| 111 | + |
| 112 | + "must sort a list with multiple items when some lastUpdateDate are missing" in { |
| 113 | + |
| 114 | + val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = Some("2025-11-09 19:53:34")) |
| 115 | + val land2 = createLand(landId = Some("LND002"), lastUpdatedDate = None) |
| 116 | + val land3 = createLand(landId = Some("LND003"), lastUpdatedDate = None) |
| 117 | + val land4 = createLand(landId = Some("LND004"), lastUpdatedDate = Some("2024-11-09 19:53:34")) |
| 118 | + val land5 = createLand(landId = Some("LND005"), lastUpdatedDate = Some("2024-11-09 19:56:34")) |
| 119 | + |
| 120 | + val seqOfLands = Seq(land4, land2, land1, land5, land3) |
| 121 | + |
| 122 | + val sortedLandList = Seq(land1, land2, land3, land4, land5) |
| 123 | + |
| 124 | + service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, Some(mainLandID))(_.lastUpdateDate, _.landID) mustBe sortedLandList |
| 125 | + } |
| 126 | + |
| 127 | + "must sort a list with multiple items when mainLandID is missing" in { |
| 128 | + |
| 129 | + val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = Some("2022-11-09 19:53:34")) |
| 130 | + val land2 = createLand(landId = Some("LND002"), lastUpdatedDate = Some("2023-11-09 19:53:34")) |
| 131 | + val land3 = createLand(landId = Some("LND003"), lastUpdatedDate = Some("2024-12-09 19:53:34")) |
| 132 | + val land4 = createLand(landId = Some("LND004"), lastUpdatedDate = Some("2025-11-09 19:53:34")) |
| 133 | + val land5 = createLand(landId = Some("LND005"), lastUpdatedDate = Some("2025-11-09 19:54:34")) |
| 134 | + |
| 135 | + val seqOfLands = Seq(land4, land2, land1, land5, land3) |
| 136 | + |
| 137 | + val sortedLandList = Seq(land1, land2, land3, land4, land5) |
| 138 | + |
| 139 | + service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, mainObjectId = None)(_.lastUpdateDate, _.landID) mustBe sortedLandList |
| 140 | + } |
| 141 | + |
| 142 | + "must return the list with multiple items as is when all lastUpdateDate and mainLandID are missing" in { |
| 143 | + |
| 144 | + val land1 = createLand(landId = Some("LND001"), lastUpdatedDate = None) |
| 145 | + val land2 = createLand(landId = Some("LND002"), lastUpdatedDate = None) |
| 146 | + val land3 = createLand(landId = Some("LND003"), lastUpdatedDate = None) |
| 147 | + val land4 = createLand(landId = Some("LND004"), lastUpdatedDate = None) |
| 148 | + val land5 = createLand(landId = Some("LND005"), lastUpdatedDate = None) |
| 149 | + |
| 150 | + val seqOfLands = Seq(land4, land2, land1, land5, land3) |
| 151 | + |
| 152 | + val sortedLandList = Seq(land4, land2, land1, land5, land3) |
| 153 | + |
| 154 | + service.sortByMainObjectLastUpdateDate[Land](list = seqOfLands, mainObjectId = None)(_.lastUpdateDate, _.landID) mustBe sortedLandList |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments