|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 4 | import com.ubs.tariffapp.models.Country; |
| 5 | +import com.ubs.tariffapp.models.DutyType; |
| 6 | +import com.ubs.tariffapp.models.DutyTypeId; |
| 7 | +import com.ubs.tariffapp.models.Product; |
| 8 | +import com.ubs.tariffapp.models.TariffSchedule; |
5 | 9 | import com.ubs.tariffapp.models.dto.TariffRequest; |
6 | 10 | import com.ubs.tariffapp.models.dto.TariffResponse; |
| 11 | +import com.ubs.tariffapp.models.duty.Duty; |
7 | 12 | import com.ubs.tariffapp.models.request.TariffSearchRequest; |
8 | 13 | import com.ubs.tariffapp.repositories.CountryRepository; |
9 | 14 | import com.ubs.tariffapp.repositories.TariffScheduleRepository; |
@@ -152,6 +157,186 @@ void testSearchTariffs_Unauthorized() throws Exception { |
152 | 157 |
|
153 | 158 | verify(dutyService, never()).searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt()); |
154 | 159 | } |
| 160 | + |
| 161 | + @Test |
| 162 | + @WithMockUser(authorities = "Admins") |
| 163 | + @DisplayName("Should handle tariff with null duty") |
| 164 | + void testSearchTariffs_WithNullDuty() throws Exception { |
| 165 | + // Arrange |
| 166 | + TariffSearchRequest searchRequest = new TariffSearchRequest("USA", "CHN", "010121", 2024); |
| 167 | + TariffSchedule tariffSchedule = createTariffScheduleWithDuty(null); |
| 168 | + |
| 169 | + when(dutyService.searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt())) |
| 170 | + .thenReturn(Collections.singletonList(tariffSchedule)); |
| 171 | + |
| 172 | + // Act & Assert |
| 173 | + mockMvc.perform(post("/api/admin/tariffs/search") |
| 174 | + .with(csrf()) |
| 175 | + .contentType(MediaType.APPLICATION_JSON) |
| 176 | + .content(objectMapper.writeValueAsString(searchRequest))) |
| 177 | + .andExpect(status().isOk()) |
| 178 | + .andExpect(jsonPath("$.status").value("success")) |
| 179 | + .andExpect(jsonPath("$.tariffs[0].dutyCategory").doesNotExist()); |
| 180 | + |
| 181 | + verify(dutyService, times(1)).searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt()); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + @WithMockUser(authorities = "Admins") |
| 186 | + @DisplayName("Should handle AdValoremDuty and extract rate") |
| 187 | + void testSearchTariffs_WithAdValoremDuty() throws Exception { |
| 188 | + // Arrange |
| 189 | + TariffSearchRequest searchRequest = new TariffSearchRequest("USA", "CHN", "010121", 2024); |
| 190 | + com.ubs.tariffapp.models.duty.AdValoremDuty adValoremDuty = |
| 191 | + new com.ubs.tariffapp.models.duty.AdValoremDuty(); |
| 192 | + adValoremDuty.setDutyNature("AD_VALOREM"); |
| 193 | + adValoremDuty.setRatePercent(java.math.BigDecimal.valueOf(5.0)); |
| 194 | + |
| 195 | + TariffSchedule tariffSchedule = createTariffScheduleWithDuty(adValoremDuty); |
| 196 | + |
| 197 | + when(dutyService.searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt())) |
| 198 | + .thenReturn(Collections.singletonList(tariffSchedule)); |
| 199 | + |
| 200 | + // Act & Assert |
| 201 | + mockMvc.perform(post("/api/admin/tariffs/search") |
| 202 | + .with(csrf()) |
| 203 | + .contentType(MediaType.APPLICATION_JSON) |
| 204 | + .content(objectMapper.writeValueAsString(searchRequest))) |
| 205 | + .andExpect(status().isOk()) |
| 206 | + .andExpect(jsonPath("$.status").value("success")) |
| 207 | + .andExpect(jsonPath("$.tariffs[0].dutyCategory").value("AD_VALOREM")) |
| 208 | + .andExpect(jsonPath("$.tariffs[0].adValoremRate").value(5.0)); |
| 209 | + |
| 210 | + verify(dutyService, times(1)).searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt()); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + @WithMockUser(authorities = "Admins") |
| 215 | + @DisplayName("Should handle SpecificDuty and extract amount and unit") |
| 216 | + void testSearchTariffs_WithSpecificDuty() throws Exception { |
| 217 | + // Arrange |
| 218 | + TariffSearchRequest searchRequest = new TariffSearchRequest("USA", "CHN", "010121", 2024); |
| 219 | + com.ubs.tariffapp.models.duty.SpecificDuty specificDuty = |
| 220 | + new com.ubs.tariffapp.models.duty.SpecificDuty(); |
| 221 | + specificDuty.setDutyNature("SPECIFIC"); |
| 222 | + specificDuty.setAmount(java.math.BigDecimal.valueOf(10.0)); |
| 223 | + specificDuty.setUnit("kg"); |
| 224 | + |
| 225 | + TariffSchedule tariffSchedule = createTariffScheduleWithDuty(specificDuty); |
| 226 | + |
| 227 | + when(dutyService.searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt())) |
| 228 | + .thenReturn(Collections.singletonList(tariffSchedule)); |
| 229 | + |
| 230 | + // Act & Assert |
| 231 | + mockMvc.perform(post("/api/admin/tariffs/search") |
| 232 | + .with(csrf()) |
| 233 | + .contentType(MediaType.APPLICATION_JSON) |
| 234 | + .content(objectMapper.writeValueAsString(searchRequest))) |
| 235 | + .andExpect(status().isOk()) |
| 236 | + .andExpect(jsonPath("$.status").value("success")) |
| 237 | + .andExpect(jsonPath("$.tariffs[0].dutyCategory").value("SPECIFIC")) |
| 238 | + .andExpect(jsonPath("$.tariffs[0].specificRate").value(10.0)) |
| 239 | + .andExpect(jsonPath("$.tariffs[0].specificRateUnit").value("kg")); |
| 240 | + |
| 241 | + verify(dutyService, times(1)).searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt()); |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + @WithMockUser(authorities = "Admins") |
| 246 | + @DisplayName("Should handle CombinedDuty with null ratePercent") |
| 247 | + void testSearchTariffs_WithCombinedDuty_NullRatePercent() throws Exception { |
| 248 | + // Arrange |
| 249 | + TariffSearchRequest searchRequest = new TariffSearchRequest("USA", "CHN", "010121", 2024); |
| 250 | + com.ubs.tariffapp.models.duty.CombinedDuty combinedDuty = |
| 251 | + new com.ubs.tariffapp.models.duty.CombinedDuty(); |
| 252 | + combinedDuty.setDutyNature("COMBINED"); |
| 253 | + combinedDuty.setRatePercent(null); // Null rate |
| 254 | + combinedDuty.setAmount(java.math.BigDecimal.valueOf(15.0)); |
| 255 | + combinedDuty.setUnit("kg"); |
| 256 | + |
| 257 | + TariffSchedule tariffSchedule = createTariffScheduleWithDuty(combinedDuty); |
| 258 | + |
| 259 | + when(dutyService.searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt())) |
| 260 | + .thenReturn(Collections.singletonList(tariffSchedule)); |
| 261 | + |
| 262 | + // Act & Assert |
| 263 | + mockMvc.perform(post("/api/admin/tariffs/search") |
| 264 | + .with(csrf()) |
| 265 | + .contentType(MediaType.APPLICATION_JSON) |
| 266 | + .content(objectMapper.writeValueAsString(searchRequest))) |
| 267 | + .andExpect(status().isOk()) |
| 268 | + .andExpect(jsonPath("$.status").value("success")) |
| 269 | + .andExpect(jsonPath("$.tariffs[0].dutyCategory").value("COMBINED")) |
| 270 | + .andExpect(jsonPath("$.tariffs[0].adValoremRate").doesNotExist()) |
| 271 | + .andExpect(jsonPath("$.tariffs[0].specificRate").value(15.0)) |
| 272 | + .andExpect(jsonPath("$.tariffs[0].specificRateUnit").value("kg")); |
| 273 | + |
| 274 | + verify(dutyService, times(1)).searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt()); |
| 275 | + } |
| 276 | + |
| 277 | + @Test |
| 278 | + @WithMockUser(authorities = "Admins") |
| 279 | + @DisplayName("Should handle CombinedDuty with null amount") |
| 280 | + void testSearchTariffs_WithCombinedDuty_NullAmount() throws Exception { |
| 281 | + // Arrange |
| 282 | + TariffSearchRequest searchRequest = new TariffSearchRequest("USA", "CHN", "010121", 2024); |
| 283 | + com.ubs.tariffapp.models.duty.CombinedDuty combinedDuty = |
| 284 | + new com.ubs.tariffapp.models.duty.CombinedDuty(); |
| 285 | + combinedDuty.setDutyNature("COMBINED"); |
| 286 | + combinedDuty.setRatePercent(java.math.BigDecimal.valueOf(8.5)); |
| 287 | + combinedDuty.setAmount(null); // Null amount |
| 288 | + combinedDuty.setUnit("kg"); |
| 289 | + |
| 290 | + TariffSchedule tariffSchedule = createTariffScheduleWithDuty(combinedDuty); |
| 291 | + |
| 292 | + when(dutyService.searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt())) |
| 293 | + .thenReturn(Collections.singletonList(tariffSchedule)); |
| 294 | + |
| 295 | + // Act & Assert |
| 296 | + mockMvc.perform(post("/api/admin/tariffs/search") |
| 297 | + .with(csrf()) |
| 298 | + .contentType(MediaType.APPLICATION_JSON) |
| 299 | + .content(objectMapper.writeValueAsString(searchRequest))) |
| 300 | + .andExpect(status().isOk()) |
| 301 | + .andExpect(jsonPath("$.status").value("success")) |
| 302 | + .andExpect(jsonPath("$.tariffs[0].dutyCategory").value("COMBINED")) |
| 303 | + .andExpect(jsonPath("$.tariffs[0].adValoremRate").value(8.5)) |
| 304 | + .andExpect(jsonPath("$.tariffs[0].specificRate").doesNotExist()) |
| 305 | + .andExpect(jsonPath("$.tariffs[0].specificRateUnit").value("kg")); |
| 306 | + |
| 307 | + verify(dutyService, times(1)).searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt()); |
| 308 | + } |
| 309 | + |
| 310 | + @Test |
| 311 | + @WithMockUser(authorities = "Admins") |
| 312 | + @DisplayName("Should handle OtherDuty and extract raw text") |
| 313 | + void testSearchTariffs_WithOtherDuty() throws Exception { |
| 314 | + // Arrange |
| 315 | + TariffSearchRequest searchRequest = new TariffSearchRequest("USA", "CHN", "010121", 2024); |
| 316 | + com.ubs.tariffapp.models.duty.OtherDuty otherDuty = |
| 317 | + new com.ubs.tariffapp.models.duty.OtherDuty(); |
| 318 | + otherDuty.setDutyNature("OTHER"); |
| 319 | + otherDuty.setRawText("See note 9822.04.01"); |
| 320 | + otherDuty.setIsComputable(false); |
| 321 | + |
| 322 | + TariffSchedule tariffSchedule = createTariffScheduleWithDuty(otherDuty); |
| 323 | + |
| 324 | + when(dutyService.searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt())) |
| 325 | + .thenReturn(Collections.singletonList(tariffSchedule)); |
| 326 | + |
| 327 | + // Act & Assert |
| 328 | + mockMvc.perform(post("/api/admin/tariffs/search") |
| 329 | + .with(csrf()) |
| 330 | + .contentType(MediaType.APPLICATION_JSON) |
| 331 | + .content(objectMapper.writeValueAsString(searchRequest))) |
| 332 | + .andExpect(status().isOk()) |
| 333 | + .andExpect(jsonPath("$.status").value("success")) |
| 334 | + .andExpect(jsonPath("$.tariffs[0].dutyCategory").value("OTHER")) |
| 335 | + .andExpect(jsonPath("$.tariffs[0].rawText").value("See note 9822.04.01")) |
| 336 | + .andExpect(jsonPath("$.tariffs[0].isComputable").value(false)); |
| 337 | + |
| 338 | + verify(dutyService, times(1)).searchAvailableTariffs(anyString(), anyString(), anyString(), anyInt()); |
| 339 | + } |
155 | 340 | } |
156 | 341 |
|
157 | 342 | @Nested |
@@ -523,4 +708,49 @@ private Country createCountry(String code, String name) { |
523 | 708 | country.setCountryName(name); |
524 | 709 | return country; |
525 | 710 | } |
| 711 | + |
| 712 | + /** |
| 713 | + * Helper method to create a TariffSchedule with the specified Duty |
| 714 | + * Used for testing branch coverage in searchTariffs endpoint |
| 715 | + */ |
| 716 | + private TariffSchedule createTariffScheduleWithDuty(Duty duty) { |
| 717 | + TariffSchedule tariffSchedule = new TariffSchedule(); |
| 718 | + tariffSchedule.setTariffId(1); |
| 719 | + tariffSchedule.setTariffYear(2024); |
| 720 | + |
| 721 | + // Create reporter country |
| 722 | + Country reporter = new Country(); |
| 723 | + reporter.setCountryId("USA"); |
| 724 | + reporter.setCountryName("United States"); |
| 725 | + tariffSchedule.setReporter(reporter); |
| 726 | + |
| 727 | + // Create partner country |
| 728 | + Country partner = new Country(); |
| 729 | + partner.setCountryId("CHN"); |
| 730 | + partner.setCountryName("China"); |
| 731 | + tariffSchedule.setPartner(partner); |
| 732 | + |
| 733 | + // Create product |
| 734 | + Product product = new Product(); |
| 735 | + product.setTlCode("010121"); |
| 736 | + product.setDescription("Live horses"); |
| 737 | + tariffSchedule.setProduct(product); |
| 738 | + |
| 739 | + // Create duty type |
| 740 | + DutyType dutyType = new DutyType(); |
| 741 | + DutyTypeId dutyTypeId = new DutyTypeId(); |
| 742 | + dutyTypeId.setDutyType("0"); |
| 743 | + dutyTypeId.setDutyCode("0"); |
| 744 | + dutyType.setId(dutyTypeId); |
| 745 | + dutyType.setDutyTypeDescription("Standard (MFN)"); |
| 746 | + tariffSchedule.setDutyType(dutyType); |
| 747 | + |
| 748 | + tariffSchedule.setTlsSuffix(""); |
| 749 | + tariffSchedule.setNote("Test tariff"); |
| 750 | + |
| 751 | + // Set the duty (can be null or any Duty subtype) |
| 752 | + tariffSchedule.setDuty(duty); |
| 753 | + |
| 754 | + return tariffSchedule; |
| 755 | + } |
526 | 756 | } |
0 commit comments