From eda2c82712a465877cc298fd1de96e76d1dab4f7 Mon Sep 17 00:00:00 2001 From: Joseph Kotanchik Date: Tue, 27 Aug 2024 14:45:43 -0400 Subject: [PATCH] MAT-7570: Limit the range dates can be shifted to [0,9999] Years before 0 and after 9999 get funky (see ISO 8601) and are not, at this time, helpful to users. --- pom.xml | 2 +- .../datacriteria/basetypes/DataElement.java | 13 ++++++-- .../basetypes/DataElementTest.java | 31 +++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 src/test/java/gov/cms/madie/models/cqm/datacriteria/basetypes/DataElementTest.java diff --git a/pom.xml b/pom.xml index 494bd73..e83ba29 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ gov.cms.madie madie-java-models - 0.6.58-SNAPSHOT + 0.6.59-SNAPSHOT madie-java-models Java based models for MADiE microservices diff --git a/src/main/java/gov/cms/madie/models/cqm/datacriteria/basetypes/DataElement.java b/src/main/java/gov/cms/madie/models/cqm/datacriteria/basetypes/DataElement.java index dd1508d..fd0f17e 100644 --- a/src/main/java/gov/cms/madie/models/cqm/datacriteria/basetypes/DataElement.java +++ b/src/main/java/gov/cms/madie/models/cqm/datacriteria/basetypes/DataElement.java @@ -117,10 +117,10 @@ public Interval shiftIntervalByYear(Interval interval, int year) { return null; } if (interval.getLow() != null) { - interval.setLow(interval.getLow().plusYears(year)); + interval.setLow(shiftDateByYear(interval.getLow(), year)); } if (interval.getHigh() != null) { - interval.setHigh(interval.getHigh().plusYears(year)); + interval.setHigh(shiftDateByYear(interval.getHigh(), year)); } return interval; } @@ -129,6 +129,13 @@ public ZonedDateTime shiftDateByYear(ZonedDateTime dateTime, int year) { if (dateTime == null) { return null; } - return dateTime.plusYears(year); + ZonedDateTime shiftedDateTime = dateTime.plusYears(year); + if (shiftedDateTime.getYear() > 9999) { + return shiftedDateTime.withYear(9999); + } + if (shiftedDateTime.getYear() < 0) { + return shiftedDateTime.withYear(0); + } + return shiftedDateTime; } } diff --git a/src/test/java/gov/cms/madie/models/cqm/datacriteria/basetypes/DataElementTest.java b/src/test/java/gov/cms/madie/models/cqm/datacriteria/basetypes/DataElementTest.java new file mode 100644 index 0000000..5e45316 --- /dev/null +++ b/src/test/java/gov/cms/madie/models/cqm/datacriteria/basetypes/DataElementTest.java @@ -0,0 +1,31 @@ +package gov.cms.madie.models.cqm.datacriteria.basetypes; + +import gov.cms.madie.models.cqm.datacriteria.EncounterPerformed; +import org.junit.jupiter.api.Test; + +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import static org.assertj.core.api.Assertions.assertThat; + +class DataElementTest { + + @Test + void shiftDateByYearAfterYear9999() { + EncounterPerformed encounterPerformed = new EncounterPerformed(); + ZonedDateTime dateTime = ZonedDateTime.ofInstant(Instant.now(), ZoneId.of("UTC")); + encounterPerformed.setAuthorDatetime(dateTime); + ZonedDateTime shiftedDateTime = encounterPerformed.shiftDateByYear(dateTime, 100000); + assertThat(shiftedDateTime).isEqualTo(dateTime.withYear(9999)); + } + + @Test + void shiftDateByYearBeforeYear0() { + EncounterPerformed encounterPerformed = new EncounterPerformed(); + ZonedDateTime dateTime = ZonedDateTime.ofInstant(Instant.now(), ZoneId.of("UTC")); + encounterPerformed.setAuthorDatetime(dateTime); + ZonedDateTime shiftedDateTime = encounterPerformed.shiftDateByYear(dateTime, -100000); + assertThat(shiftedDateTime).isEqualTo(dateTime.withYear(0)); + } +}