2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.mapdb;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
18 import java.math.BigDecimal;
19 import java.util.List;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.params.ParameterizedTest;
25 import org.junit.jupiter.params.provider.MethodSource;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.HSBType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.types.StringType;
32 import org.openhab.core.library.unit.ImperialUnits;
33 import org.openhab.core.library.unit.SIUnits;
34 import org.openhab.core.library.unit.SmartHomeUnits;
35 import org.openhab.core.types.State;
36 import org.openhab.persistence.mapdb.internal.StateTypeAdapter;
38 import com.google.gson.Gson;
39 import com.google.gson.GsonBuilder;
43 * @author Martin Kühl - Initial contribution
46 public class StateTypeAdapterTest {
47 private Gson mapper = new GsonBuilder().registerTypeHierarchyAdapter(State.class, new StateTypeAdapter()).create();
49 private static final List<DecimalType> DECIMAL_TYPE_VALUES = List.of(DecimalType.ZERO, new DecimalType(1.123),
50 new DecimalType(10000000));
52 private static final List<HSBType> HSB_TYPE_VALUES = List.of(HSBType.BLACK, HSBType.GREEN, HSBType.WHITE,
53 HSBType.fromRGB(1, 2, 3), HSBType.fromRGB(11, 22, 33), HSBType.fromRGB(0, 0, 255));
55 private static final List<OnOffType> ON_OFF_TYPE_VALUES = List.of(OnOffType.ON, OnOffType.OFF);
57 private static final List<PercentType> PERCENT_TYPE_VALUES = List.of(PercentType.ZERO, PercentType.HUNDRED,
58 PercentType.valueOf("0.0000001"), PercentType.valueOf("12"), PercentType.valueOf("99.999"));
60 private static final List<QuantityType<?>> QUANTITY_TYPE_VALUES = List.of(QuantityType.valueOf("0 W"),
61 QuantityType.valueOf("1 kW"), QuantityType.valueOf(20, SmartHomeUnits.AMPERE),
62 new QuantityType<>(new BigDecimal("21.23"), SIUnits.CELSIUS),
63 new QuantityType<>(new BigDecimal("75"), ImperialUnits.MILES_PER_HOUR),
64 QuantityType.valueOf(1000, SmartHomeUnits.KELVIN),
65 QuantityType.valueOf(100, SmartHomeUnits.METRE_PER_SQUARE_SECOND));
67 private static final List<StringType> STRING_TYPE_VALUES = List.of(StringType.valueOf("test"),
68 StringType.valueOf("a b c 1 2 3"), StringType.valueOf(""), StringType.valueOf("@@@### @@@"));
70 private static final List<State> VALUES = Stream.of(DECIMAL_TYPE_VALUES, HSB_TYPE_VALUES, ON_OFF_TYPE_VALUES,
71 PERCENT_TYPE_VALUES, QUANTITY_TYPE_VALUES, STRING_TYPE_VALUES).flatMap(list -> list.stream())
72 .collect(Collectors.toList());
76 public void readWriteRoundtripShouldRecreateTheWrittenState(State state) {
77 String json = mapper.toJson(state);
78 State actual = mapper.fromJson(json, State.class);
79 assertThat(actual, is(equalTo(state)));
82 public static Stream<State> readWriteRoundtripShouldRecreateTheWrittenState() {
83 return VALUES.stream();