diff --git a/libs/user-sdk-event/src/test/java/it/pagopa/selfcare/user/UserUtilsTest.java b/libs/user-sdk-event/src/test/java/it/pagopa/selfcare/user/UserUtilsTest.java index 343d3f62..f8cfe5f0 100644 --- a/libs/user-sdk-event/src/test/java/it/pagopa/selfcare/user/UserUtilsTest.java +++ b/libs/user-sdk-event/src/test/java/it/pagopa/selfcare/user/UserUtilsTest.java @@ -9,10 +9,7 @@ import java.time.LocalTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; +import java.util.*; import static org.junit.jupiter.api.Assertions.*; @@ -124,4 +121,82 @@ void getHMAC256_withValidInputs_shouldReturnValidHash() { assertNotNull(hash); } + + @Test + void retrieveFdProductIfItChanged_withValidProducts_shouldReturnMostRecentlyUpdatedProduct() { + List products = new ArrayList<>(); + OnboardedProduct product1 = new OnboardedProduct(); + product1.setProductId("1"); + product1.setUpdatedAt(OffsetDateTime.now().minusDays(1)); + product1.setCreatedAt(OffsetDateTime.now().minusDays(2)); + products.add(product1); + + OnboardedProduct product2 = new OnboardedProduct(); + product2.setProductId("2"); + product2.setUpdatedAt(OffsetDateTime.now()); + product2.setCreatedAt(OffsetDateTime.now().minusDays(1)); + products.add(product2); + + List productIdToCheck = List.of("1", "2"); + + OnboardedProduct result = UserUtils.retrieveFdProductIfItChanged(products, productIdToCheck); + + assertNotNull(result); + assertEquals("2", result.getProductId()); + } + + @Test + void retrieveFdProductIfItChanged_withEmptyProductList() { + List products = Collections.emptyList(); + List productIdToCheck = List.of("1", "2"); + + OnboardedProduct result = UserUtils.retrieveFdProductIfItChanged(products, productIdToCheck); + + assertNull(result); + } + + @Test + void retrieveFdProductIfItChanged_withNoFdProductIds() { + List products = new ArrayList<>(); + OnboardedProduct product1 = new OnboardedProduct(); + product1.setProductId("1"); + product1.setUpdatedAt(OffsetDateTime.now().minusDays(1)); + product1.setCreatedAt(OffsetDateTime.now().minusDays(2)); + products.add(product1); + + OnboardedProduct product2 = new OnboardedProduct(); + product2.setProductId("2"); + product2.setUpdatedAt(OffsetDateTime.now()); + product2.setCreatedAt(OffsetDateTime.now().minusDays(1)); + products.add(product2); + + List productIdToCheck = List.of("3", "4"); + + OnboardedProduct result = UserUtils.retrieveFdProductIfItChanged(products, productIdToCheck); + + assertNull(result); + } + + @Test + void retrieveFdProductIfItChanged_withProductsWithSameUpdatedAt() { + List products = new ArrayList<>(); + OnboardedProduct product1 = new OnboardedProduct(); + product1.setProductId("1"); + product1.setUpdatedAt(OffsetDateTime.now()); + product1.setCreatedAt(OffsetDateTime.now().minusDays(2)); + products.add(product1); + + OnboardedProduct product2 = new OnboardedProduct(); + product2.setProductId("2"); + product2.setUpdatedAt(OffsetDateTime.now()); + product2.setCreatedAt(OffsetDateTime.now().minusDays(1)); + products.add(product2); + + List productIdToCheck = List.of("1", "2"); + + OnboardedProduct result = UserUtils.retrieveFdProductIfItChanged(products, productIdToCheck); + + assertNotNull(result); + assertEquals("2", result.getProductId()); + } }