Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DateTimeType tests #17764

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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