Skip to content

Commit

Permalink
Fix DateTimeType tests (openhab#17764)
Browse files Browse the repository at this point in the history
* Refactor test to verify UTC timestamp independently of time-zone

Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur authored and weymann committed Nov 26, 2024
1 parent 4dde024 commit 429b883
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Base64;
import java.util.Date;
import java.util.stream.Stream;
Expand Down Expand Up @@ -119,25 +118,23 @@ void givenInstantAndDatetimeTargetReturnDatetype() {
var instant = Instant.now();
var converted = instance.convertValue(instant, DateTimeType.class);
assertThat(converted, instanceOf(DateTimeType.class));
assertThat(((DateTimeType) converted).getZonedDateTime(),
is(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()).withFixedOffsetZone()));
assertThat(((DateTimeType) converted).getInstant(), is(instant));
}

@Test
void givenDateAndDatetimeTargetReturnDatetype() {
var date = new Date();
var converted = instance.convertValue(date, DateTimeType.class);
assertThat(converted, instanceOf(DateTimeType.class));
assertThat(((DateTimeType) converted).getZonedDateTime(),
is(ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).withFixedOffsetZone()));
assertThat(((DateTimeType) converted).getInstant(), is(date.toInstant()));
}

@ParameterizedTest
@ValueSource(strings = { "2019-10-12T07:20:50.52Z", "2019-10-12" })
@ValueSource(strings = { "2019-10-12T07:20:50.52", "2019-10-12" })
void givenValidStringDateAndDatetimeTargetReturnDatetype(String date) {
var converted = instance.convertValue(date, DateTimeType.class);
assertThat(converted, instanceOf(DateTimeType.class));
var convertedDateTime = ((DateTimeType) converted).getZonedDateTime();
var convertedDateTime = ((DateTimeType) converted).getInstant().atZone(ZoneId.systemDefault());
assertThat(convertedDateTime.getYear(), is(2019));
assertThat(convertedDateTime.getMonthValue(), is(10));
assertThat(convertedDateTime.getDayOfMonth(), is(12));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openhab.binding.fineoffsetweatherstation.internal.domain.DebugDetails;
import org.openhab.binding.fineoffsetweatherstation.internal.domain.Protocol;
import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.MeasuredValue;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.util.HexUtils;

/**
Expand Down Expand Up @@ -101,7 +102,10 @@ void testLiveDataWH34AndWh45() {
List<MeasuredValue> data = new FineOffsetDataParser(Protocol.DEFAULT).getMeasuredValues(bytes,
new ConversionContext(ZoneOffset.UTC), debugDetails);
Assertions.assertThat(data)
.extracting(MeasuredValue::getChannelId, measuredValue -> measuredValue.getState().toString())
.extracting(MeasuredValue::getChannelId,
measuredValue -> measuredValue.getState() instanceof DateTimeType dateTimeState
? dateTimeState.getInstant().toString()
: measuredValue.getState().toString())
.containsExactly(new Tuple("temperature-indoor", "20.2 °C"), new Tuple("humidity-indoor", "62 %"),
new Tuple("pressure-absolute", "996.4 hPa"), new Tuple("pressure-relative", "996.4 hPa"),
new Tuple("temperature-outdoor", "12.2 °C"), new Tuple("humidity-outdoor", "76 %"),
Expand All @@ -110,8 +114,7 @@ void testLiveDataWH34AndWh45() {
new Tuple("irradiation-uv", "0 mW/m²"), new Tuple("uv-index", "0"),
new Tuple("temperature-channel-1", "13.4 °C"), new Tuple("humidity-channel-1", "85 %"),
new Tuple("water-leak-channel-1", "OFF"), new Tuple("water-leak-channel-3", "OFF"),
new Tuple("lightning-counter", "6"),
new Tuple("lightning-time", "2023-11-07T15:42:41.000+0000"),
new Tuple("lightning-counter", "6"), new Tuple("lightning-time", "2023-11-07T15:42:41Z"),
new Tuple("lightning-distance", "27 km"), new Tuple("wind-max-day", "3.8 m/s"),
new Tuple("temperature-external-channel-1", "13.6 °C"),
new Tuple("sensor-co2-temperature", "20.6 °C"), new Tuple("sensor-co2-humidity", "63 %"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ void testToDPT9ValueFromQuantityType() {
@Test
void testToDPT10ValueFromQuantityType() {
// DateTimeType, not QuantityType
assertEquals("Wed, 17:30:00", ValueEncoder.encode(new DateTimeType("2019-06-12T17:30:00Z"), "10.001"));
assertEquals("Wed, 17:30:00", ValueEncoder.encode(new DateTimeType("2019-06-12T17:30:00"), "10.001"));
}

@Test
void testToDPT11ValueFromQuantityType() {
// DateTimeType, not QuantityType
assertEquals("2019-06-12", ValueEncoder.encode(new DateTimeType("2019-06-12T17:30:00Z"), "11.001"));
assertEquals("2019-06-12", ValueEncoder.encode(new DateTimeType("2019-06-12T17:30:00"), "11.001"));
}

@Test
Expand Down Expand Up @@ -337,7 +337,7 @@ void testToDPT14ValueFromQuantityType() {
@Test
void testToDPT19ValueFromQuantityType() {
// DateTimeType, not QuantityType
assertEquals("2019-06-12 17:30:00", ValueEncoder.encode(new DateTimeType("2019-06-12T17:30:00Z"), "19.001"));
assertEquals("2019-06-12 17:30:00", ValueEncoder.encode(new DateTimeType("2019-06-12T17:30:00"), "19.001"));
// special: clock fault
assertNull(ValueDecoder.decode("19.001", new byte[] { (byte) (2019 - 1900), 1, 15, 17, 30, 0, (byte) 0x80, 0 },
DateTimeType.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public void testVehicleStateDeserializationByConverter() {
VehicleStateContainer vehicleStateContainer = JsonStringDeserializer.getVehicleState(vehicleStateJson);

assertNotNull(vehicleStateContainer);
assertEquals("2024-06-01T00:00",
assertEquals("2024-06-01T00:00:00Z",
((DateTimeType) VehicleStatusUtils
.getNextServiceDate(vehicleStateContainer.getState().getRequiredServices())).getZonedDateTime()
.toLocalDateTime().toString(),
.getNextServiceDate(vehicleStateContainer.getState().getRequiredServices())).getInstant()
.toString(),
"Service Date");

assertEquals("2022-12-21T15:41:23Z", vehicleStateContainer.getState().getLastUpdatedAt(), "Last update time");
Expand Down

0 comments on commit 429b883

Please sign in to comment.