From 9ec455c3c430ab480e54200e7a7ab49bd529ef30 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Mon, 11 Nov 2024 22:29:54 +0100 Subject: [PATCH] Remove duplicated method and add test coverage Signed-off-by: Jacob Laursen --- .../rest/core/item/EnrichedItemDTOMapper.java | 26 +------------------ .../core/library/types/DateTimeType.java | 6 ++++- .../core/library/types/DateTimeTypeTest.java | 24 +++++++++++++++++ 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTOMapper.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTOMapper.java index ae1adee3155..1cc0e40190a 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTOMapper.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTOMapper.java @@ -12,9 +12,7 @@ */ package org.openhab.core.io.rest.core.item; -import java.time.Instant; import java.time.ZoneId; -import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashSet; @@ -56,10 +54,6 @@ public class EnrichedItemDTOMapper { private static final Pattern EXTRACT_TRANSFORM_FUNCTION_PATTERN = Pattern.compile("(.*?)\\((.*)\\):(.*)"); - private static final String DATE_FORMAT_PATTERN_WITH_TZ_RFC = "yyyy-MM-dd'T'HH:mm[:ss[.SSSSSSSSS]]Z"; - private static final DateTimeFormatter FORMATTER_TZ_RFC = DateTimeFormatter - .ofPattern(DATE_FORMAT_PATTERN_WITH_TZ_RFC); - private static final Logger LOGGER = LoggerFactory.getLogger(EnrichedItemDTOMapper.class); /** @@ -100,7 +94,7 @@ private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, boolean drillDown if (dateTime == null) { state = item.getState().toFullString(); } else { - state = formatDateTime(dateTime.getInstant(), zoneId); + state = dateTime.toFullString(zoneId); } } else { state = item.getState().toFullString(); @@ -155,24 +149,6 @@ private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, boolean drillDown return enrichedItemDTO; } - private static String formatDateTime(Instant instant, ZoneId zoneId) { - String formatted = instant.atZone(zoneId).format(FORMATTER_TZ_RFC); - if (formatted.contains(".")) { - String sign = ""; - if (formatted.contains("+")) { - sign = "+"; - } else if (formatted.contains("-")) { - sign = "-"; - } - if (!sign.isEmpty()) { - // the formatted string contains 9 fraction-of-second digits - // truncate at most 2 trailing groups of 000s - return formatted.replace("000" + sign, sign).replace("000" + sign, sign); - } - } - return formatted; - } - private static @Nullable StateDescription considerTransformation(@Nullable StateDescription stateDescription) { if (stateDescription != null) { String pattern = stateDescription.getPattern(); diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java index 391117c3655..0261122f3f7 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java @@ -225,7 +225,11 @@ public String toString() { @Override public String toFullString() { - String formatted = getZonedDateTime().format(FORMATTER_TZ_RFC); + return toFullString(ZoneId.systemDefault()); + } + + public String toFullString(ZoneId zoneId) { + String formatted = instant.atZone(zoneId).format(FORMATTER_TZ_RFC); if (formatted.contains(".")) { String sign = ""; if (formatted.contains("+")) { diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java index 2da6fac85ee..9af63daea94 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java @@ -13,6 +13,7 @@ package org.openhab.core.library.types; import static java.util.Map.entry; +import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.*; @@ -30,11 +31,13 @@ import java.util.Objects; import java.util.TimeZone; import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.ValueSource; @@ -400,4 +403,25 @@ private LocalDateTime createLocalDateTimeFromInput(Map inputTim return LocalDateTime.of(year, month + 1, dayOfMonth, hourOfDay, minute, second, durationInNano); } + + @ParameterizedTest + @MethodSource("provideTestCasesForToFullStringWithZone") + void toFullStringWithZone(String instant, ZoneId zoneId, String expected) { + DateTimeType dt = new DateTimeType(Instant.parse(instant)); + String actual = dt.toFullString(zoneId); + assertThat(actual, is(equalTo(expected))); + } + + private static Stream provideTestCasesForToFullStringWithZone() { + return Stream.of( // + Arguments.of("2024-11-11T20:39:00Z", ZoneId.of("UTC"), "2024-11-11T20:39:00.000+0000"), // + Arguments.of("2024-11-11T20:39:00.000000000Z", ZoneId.of("UTC"), "2024-11-11T20:39:00.000+0000"), // + Arguments.of("2024-11-11T20:39:00.000000001Z", ZoneId.of("UTC"), "2024-11-11T20:39:00.000000001+0000"), // + Arguments.of("2024-11-11T20:39:00.123000000Z", ZoneId.of("UTC"), "2024-11-11T20:39:00.123+0000"), // + Arguments.of("2024-11-11T20:39:00.123456000Z", ZoneId.of("UTC"), "2024-11-11T20:39:00.123456+0000"), // + Arguments.of("2024-11-11T20:39:00.123456789Z", ZoneId.of("UTC"), "2024-11-11T20:39:00.123456789+0000"), // + Arguments.of("2024-11-11T20:39:00.123Z", ZoneId.of("Europe/Paris"), "2024-11-11T21:39:00.123+0100"), // + Arguments.of("2024-11-11T04:59:59.999Z", ZoneId.of("America/New_York"), "2024-11-10T23:59:59.999-0500") // + ); + } }