mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-26 15:21:41 +01:00
Fix DateTimeType
tests (#17764)
* Refactor test to verify UTC timestamp independently of time-zone Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
parent
ed1dc963d0
commit
35aa4bcdc0
@ -21,7 +21,6 @@ import java.nio.charset.Charset;
|
|||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
@ -119,8 +118,7 @@ class Value2StateConverterTest {
|
|||||||
var instant = Instant.now();
|
var instant = Instant.now();
|
||||||
var converted = instance.convertValue(instant, DateTimeType.class);
|
var converted = instance.convertValue(instant, DateTimeType.class);
|
||||||
assertThat(converted, instanceOf(DateTimeType.class));
|
assertThat(converted, instanceOf(DateTimeType.class));
|
||||||
assertThat(((DateTimeType) converted).getZonedDateTime(),
|
assertThat(((DateTimeType) converted).getInstant(), is(instant));
|
||||||
is(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()).withFixedOffsetZone()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -128,16 +126,15 @@ class Value2StateConverterTest {
|
|||||||
var date = new Date();
|
var date = new Date();
|
||||||
var converted = instance.convertValue(date, DateTimeType.class);
|
var converted = instance.convertValue(date, DateTimeType.class);
|
||||||
assertThat(converted, instanceOf(DateTimeType.class));
|
assertThat(converted, instanceOf(DateTimeType.class));
|
||||||
assertThat(((DateTimeType) converted).getZonedDateTime(),
|
assertThat(((DateTimeType) converted).getInstant(), is(date.toInstant()));
|
||||||
is(ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).withFixedOffsetZone()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@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) {
|
void givenValidStringDateAndDatetimeTargetReturnDatetype(String date) {
|
||||||
var converted = instance.convertValue(date, DateTimeType.class);
|
var converted = instance.convertValue(date, DateTimeType.class);
|
||||||
assertThat(converted, instanceOf(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.getYear(), is(2019));
|
||||||
assertThat(convertedDateTime.getMonthValue(), is(10));
|
assertThat(convertedDateTime.getMonthValue(), is(10));
|
||||||
assertThat(convertedDateTime.getDayOfMonth(), is(12));
|
assertThat(convertedDateTime.getDayOfMonth(), is(12));
|
||||||
|
@ -24,6 +24,7 @@ import org.openhab.binding.fineoffsetweatherstation.internal.domain.ConversionCo
|
|||||||
import org.openhab.binding.fineoffsetweatherstation.internal.domain.DebugDetails;
|
import org.openhab.binding.fineoffsetweatherstation.internal.domain.DebugDetails;
|
||||||
import org.openhab.binding.fineoffsetweatherstation.internal.domain.Protocol;
|
import org.openhab.binding.fineoffsetweatherstation.internal.domain.Protocol;
|
||||||
import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.MeasuredValue;
|
import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.MeasuredValue;
|
||||||
|
import org.openhab.core.library.types.DateTimeType;
|
||||||
import org.openhab.core.util.HexUtils;
|
import org.openhab.core.util.HexUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -101,7 +102,10 @@ class FineOffsetDataParserTest {
|
|||||||
List<MeasuredValue> data = new FineOffsetDataParser(Protocol.DEFAULT).getMeasuredValues(bytes,
|
List<MeasuredValue> data = new FineOffsetDataParser(Protocol.DEFAULT).getMeasuredValues(bytes,
|
||||||
new ConversionContext(ZoneOffset.UTC), debugDetails);
|
new ConversionContext(ZoneOffset.UTC), debugDetails);
|
||||||
Assertions.assertThat(data)
|
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 %"),
|
.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("pressure-absolute", "996.4 hPa"), new Tuple("pressure-relative", "996.4 hPa"),
|
||||||
new Tuple("temperature-outdoor", "12.2 °C"), new Tuple("humidity-outdoor", "76 %"),
|
new Tuple("temperature-outdoor", "12.2 °C"), new Tuple("humidity-outdoor", "76 %"),
|
||||||
@ -110,8 +114,7 @@ class FineOffsetDataParserTest {
|
|||||||
new Tuple("irradiation-uv", "0 mW/m²"), new Tuple("uv-index", "0"),
|
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("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("water-leak-channel-1", "OFF"), new Tuple("water-leak-channel-3", "OFF"),
|
||||||
new Tuple("lightning-counter", "6"),
|
new Tuple("lightning-counter", "6"), new Tuple("lightning-time", "2023-11-07T15:42:41Z"),
|
||||||
new Tuple("lightning-time", "2023-11-07T15:42:41.000+0000"),
|
|
||||||
new Tuple("lightning-distance", "27 km"), new Tuple("wind-max-day", "3.8 m/s"),
|
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("temperature-external-channel-1", "13.6 °C"),
|
||||||
new Tuple("sensor-co2-temperature", "20.6 °C"), new Tuple("sensor-co2-humidity", "63 %"),
|
new Tuple("sensor-co2-temperature", "20.6 °C"), new Tuple("sensor-co2-humidity", "63 %"),
|
||||||
|
@ -201,13 +201,13 @@ class DPTTest {
|
|||||||
@Test
|
@Test
|
||||||
void testToDPT10ValueFromQuantityType() {
|
void testToDPT10ValueFromQuantityType() {
|
||||||
// DateTimeType, not QuantityType
|
// 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
|
@Test
|
||||||
void testToDPT11ValueFromQuantityType() {
|
void testToDPT11ValueFromQuantityType() {
|
||||||
// DateTimeType, not QuantityType
|
// 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
|
@Test
|
||||||
@ -337,7 +337,7 @@ class DPTTest {
|
|||||||
@Test
|
@Test
|
||||||
void testToDPT19ValueFromQuantityType() {
|
void testToDPT19ValueFromQuantityType() {
|
||||||
// DateTimeType, not QuantityType
|
// 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
|
// special: clock fault
|
||||||
assertNull(ValueDecoder.decode("19.001", new byte[] { (byte) (2019 - 1900), 1, 15, 17, 30, 0, (byte) 0x80, 0 },
|
assertNull(ValueDecoder.decode("19.001", new byte[] { (byte) (2019 - 1900), 1, 15, 17, 30, 0, (byte) 0x80, 0 },
|
||||||
DateTimeType.class));
|
DateTimeType.class));
|
||||||
|
@ -48,10 +48,10 @@ public class VehicleStateContainerTest {
|
|||||||
VehicleStateContainer vehicleStateContainer = JsonStringDeserializer.getVehicleState(vehicleStateJson);
|
VehicleStateContainer vehicleStateContainer = JsonStringDeserializer.getVehicleState(vehicleStateJson);
|
||||||
|
|
||||||
assertNotNull(vehicleStateContainer);
|
assertNotNull(vehicleStateContainer);
|
||||||
assertEquals("2024-06-01T00:00",
|
assertEquals("2024-06-01T00:00:00Z",
|
||||||
((DateTimeType) VehicleStatusUtils
|
((DateTimeType) VehicleStatusUtils
|
||||||
.getNextServiceDate(vehicleStateContainer.getState().getRequiredServices())).getZonedDateTime()
|
.getNextServiceDate(vehicleStateContainer.getState().getRequiredServices())).getInstant()
|
||||||
.toLocalDateTime().toString(),
|
.toString(),
|
||||||
"Service Date");
|
"Service Date");
|
||||||
|
|
||||||
assertEquals("2022-12-21T15:41:23Z", vehicleStateContainer.getState().getLastUpdatedAt(), "Last update time");
|
assertEquals("2022-12-21T15:41:23Z", vehicleStateContainer.getState().getLastUpdatedAt(), "Last update time");
|
||||||
|
Loading…
Reference in New Issue
Block a user