From 9c1df1507157c5fcbfa35842c67c8887b137dedc Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Mon, 13 Feb 2023 16:05:58 +0100 Subject: [PATCH] null values in date range conditions --- .../dhp/broker/oa/util/SubscriptionUtils.java | 18 +++++++++++++++--- .../broker/oa/util/SubscriptionUtilsTest.java | 12 ++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/dhp-workflows/dhp-broker-events/src/main/java/eu/dnetlib/dhp/broker/oa/util/SubscriptionUtils.java b/dhp-workflows/dhp-broker-events/src/main/java/eu/dnetlib/dhp/broker/oa/util/SubscriptionUtils.java index cf3562193..4792a7719 100644 --- a/dhp-workflows/dhp-broker-events/src/main/java/eu/dnetlib/dhp/broker/oa/util/SubscriptionUtils.java +++ b/dhp-workflows/dhp-broker-events/src/main/java/eu/dnetlib/dhp/broker/oa/util/SubscriptionUtils.java @@ -37,12 +37,24 @@ public class SubscriptionUtils { } public static boolean verifyDateRange(final long date, final String min, final String max) { + + long from = 0; + long to = Long.MAX_VALUE; + try { - return date >= DateUtils.parseDate(min, "yyyy-MM-dd").getTime() - && date < DateUtils.parseDate(max, "yyyy-MM-dd").getTime() + ONE_DAY; + from = min != null ? DateUtils.parseDate(min, "yyyy-MM-dd").getTime() : 0; } catch (final ParseException e) { - return false; + from = 0; } + + try { + to = max != null ? DateUtils.parseDate(max, "yyyy-MM-dd").getTime() + ONE_DAY : Long.MAX_VALUE; + } catch (final ParseException e) { + to = Long.MAX_VALUE; + } + + return date >= from && date < to; + } public static boolean verifyExact(final String s1, final String s2) { diff --git a/dhp-workflows/dhp-broker-events/src/test/java/eu/dnetlib/dhp/broker/oa/util/SubscriptionUtilsTest.java b/dhp-workflows/dhp-broker-events/src/test/java/eu/dnetlib/dhp/broker/oa/util/SubscriptionUtilsTest.java index d93390e4a..63b49d362 100644 --- a/dhp-workflows/dhp-broker-events/src/test/java/eu/dnetlib/dhp/broker/oa/util/SubscriptionUtilsTest.java +++ b/dhp-workflows/dhp-broker-events/src/test/java/eu/dnetlib/dhp/broker/oa/util/SubscriptionUtilsTest.java @@ -41,6 +41,18 @@ public class SubscriptionUtilsTest { assertTrue(SubscriptionUtils.verifyDateRange(date, "2010-01-01", "2011-01-01")); assertFalse(SubscriptionUtils.verifyDateRange(date, "2020-01-01", "2021-01-01")); + + assertTrue(SubscriptionUtils.verifyDateRange(date, "2010-01-01", "NULL")); + assertTrue(SubscriptionUtils.verifyDateRange(date, "2010-01-01", null)); + assertTrue(SubscriptionUtils.verifyDateRange(date, "NULL", "2011-01-01")); + assertTrue(SubscriptionUtils.verifyDateRange(date, null, "2011-01-01")); + assertTrue(SubscriptionUtils.verifyDateRange(date, "NULL", "NULL")); + assertTrue(SubscriptionUtils.verifyDateRange(date, null, null)); + + assertFalse(SubscriptionUtils.verifyDateRange(date, "2020-01-01", null)); + assertFalse(SubscriptionUtils.verifyDateRange(date, "2020-01-01", "NULL")); + assertFalse(SubscriptionUtils.verifyDateRange(date, null, "2005-01-01")); + assertFalse(SubscriptionUtils.verifyDateRange(date, "NULL", "2005-01-01")); } @Test