Skip to content

Commit

Permalink
Remove duplicated method and add test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Nov 11, 2024
1 parent 7df4b06 commit 9ec455c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("+")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;

Expand Down Expand Up @@ -400,4 +403,25 @@ private LocalDateTime createLocalDateTimeFromInput(Map<String, Integer> 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<Arguments> 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") //
);
}
}

0 comments on commit 9ec455c

Please sign in to comment.