ItemStateConverterImpl implements ItemStateConverter { @Override public State convertToAcceptedState(@Nullable State state, @Nullable Item item) { if (state == null) { logger.error("A conversion of null was requested:", new IllegalArgumentException("State must not be null.")); return UnDefType.NULL; } if (item != null && !isAccepted(item, state)) { for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) { State convertedState = state.as(acceptedType); if (convertedState != null) { logger.debug("Converting {} '{}' to {} '{}' for item '{}'", state.getClass().getSimpleName(), state, convertedState.getClass().getSimpleName(), convertedState, item.getName()); return convertedState; } } } if (item instanceof NumberItem && state instanceof QuantityType) { NumberItem numberItem = (NumberItem) item; if (numberItem.getDimension() != null) { QuantityType<?> quantityState = (QuantityType<?>) state; Unit<?> itemUnit = parseItemUnit(numberItem); if (itemUnit != null) { if (!itemUnit.equals(quantityState.getUnit())) { return convertOrUndef(quantityState, itemUnit); } return quantityState; } Class<? extends Quantity<?>> dimension = numberItem.getDimension(); @SuppressWarnings({ "unchecked", "rawtypes" }) Unit<? extends Quantity<?>> conversionUnit = unitProvider .getUnit((Class<? extends Quantity>) dimension); if (conversionUnit != null && UnitUtils.isDifferentMeasurementSystem(conversionUnit, quantityState.getUnit())) { return convertOrUndef(quantityState, conversionUnit); } return state; } else { State convertedState = state.as(DecimalType.class); if (convertedState != null) { return convertedState; } } } return state; } @Activate  ItemStateConverterImpl(final @Reference UnitProvider unitProvider); @Override State convertToAcceptedState(@Nullable State state, @Nullable Item item);  }
@Test public void testNullState() { State undef = itemStateConverter.convertToAcceptedState(null, null); assertThat(undef, is(UnDefType.NULL)); }
@Test public void testNoConversion() { Item item = new NumberItem("number"); State originalState = new DecimalType(12.34); State state = itemStateConverter.convertToAcceptedState(originalState, item); assertTrue(originalState == state); }
@Test public void testStateConversion() { Item item = new NumberItem("number"); State originalState = new PercentType("42"); State convertedState = itemStateConverter.convertToAcceptedState(originalState, item); assertThat(convertedState, is(new DecimalType("0.42"))); }
@Test public void numberItemWithoutDimensionShouldConvertToDecimalType() { Item item = new NumberItem("number"); State originalState = new QuantityType<>("12.34 °C"); State convertedState = itemStateConverter.convertToAcceptedState(originalState, item); assertThat(convertedState, is(new DecimalType("12.34"))); }
@Test public void numberItemWitDimensionShouldConvertToItemStateDescriptionUnit() { NumberItem item = mock(NumberItem.class); StateDescription stateDescription = mock(StateDescription.class); when(item.getStateDescription()).thenReturn(stateDescription); doReturn(Temperature.class).when(item).getDimension(); when(stateDescription.getPattern()).thenReturn("%.1f K"); State originalState = new QuantityType<>("12.34 °C"); State convertedState = itemStateConverter.convertToAcceptedState(originalState, item); assertThat(convertedState, is(new QuantityType<>("285.49 K"))); }
@Test public void numberItemWitDimensionShouldConvertToLocaleBasedUnit() { NumberItem item = mock(NumberItem.class); doReturn(Temperature.class).when(item).getDimension(); State originalState = new QuantityType<>("12.34 °C"); State convertedState = itemStateConverter.convertToAcceptedState(originalState, item); assertThat(convertedState, is(new QuantityType<>("54.212 °F"))); }
@Test public void numberItemShouldNotConvertUnitsWhereMeasurmentSystemEquals() { NumberItem item = mock(NumberItem.class); doReturn(Length.class).when(item).getDimension(); QuantityType<Length> originalState = new QuantityType<>("100 cm"); @SuppressWarnings("unchecked") QuantityType<Length> convertedState = (QuantityType<Length>) itemStateConverter .convertToAcceptedState(originalState, item); assertThat(convertedState.getUnit(), is(originalState.getUnit())); }