]> git.basschouten.com Git - openhab-addons.git/blob
7350092adfa1e8f70981ff9e77292f2b7986ed95
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.persistence.mapdb;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.math.BigDecimal;
19 import java.util.List;
20 import java.util.Objects;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.params.ParameterizedTest;
26 import org.junit.jupiter.params.provider.MethodSource;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.HSBType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.PercentType;
31 import org.openhab.core.library.types.QuantityType;
32 import org.openhab.core.library.types.StringType;
33 import org.openhab.core.library.unit.ImperialUnits;
34 import org.openhab.core.library.unit.SIUnits;
35 import org.openhab.core.library.unit.Units;
36 import org.openhab.core.types.State;
37 import org.openhab.persistence.mapdb.internal.StateTypeAdapter;
38
39 import com.google.gson.Gson;
40 import com.google.gson.GsonBuilder;
41
42 /**
43  *
44  * @author Martin Kühl - Initial contribution
45  */
46 @NonNullByDefault
47 public class StateTypeAdapterTest {
48     private Gson mapper = new GsonBuilder().registerTypeHierarchyAdapter(State.class, new StateTypeAdapter()).create();
49
50     private static final List<DecimalType> DECIMAL_TYPE_VALUES = List.of(DecimalType.ZERO, new DecimalType(1.123),
51             new DecimalType(10000000));
52
53     private static final List<HSBType> HSB_TYPE_VALUES = List.of(HSBType.BLACK, HSBType.GREEN, HSBType.WHITE,
54             HSBType.fromRGB(1, 2, 3), HSBType.fromRGB(11, 22, 33), HSBType.fromRGB(0, 0, 255));
55
56     private static final List<OnOffType> ON_OFF_TYPE_VALUES = List.of(OnOffType.ON, OnOffType.OFF);
57
58     private static final List<PercentType> PERCENT_TYPE_VALUES = List.of(PercentType.ZERO, PercentType.HUNDRED,
59             PercentType.valueOf("0.0000001"), PercentType.valueOf("12"), PercentType.valueOf("99.999"));
60
61     private static final List<QuantityType<?>> QUANTITY_TYPE_VALUES = List.of(QuantityType.valueOf("0 W"),
62             QuantityType.valueOf("1 kW"), QuantityType.valueOf(20, Units.AMPERE),
63             new QuantityType<>(new BigDecimal("21.23"), SIUnits.CELSIUS),
64             new QuantityType<>(new BigDecimal("75"), ImperialUnits.MILES_PER_HOUR),
65             QuantityType.valueOf(1000, Units.KELVIN), QuantityType.valueOf(100, Units.METRE_PER_SQUARE_SECOND));
66
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("@@@###   @@@"));
69
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());
73
74     @ParameterizedTest
75     @MethodSource
76     public void readWriteRoundtripShouldRecreateTheWrittenState(State state) {
77         String json = mapper.toJson(state);
78         State actual = Objects.requireNonNull(mapper.fromJson(json, State.class));
79         assertThat(actual, is(equalTo(state)));
80     }
81
82     public static Stream<State> readWriteRoundtripShouldRecreateTheWrittenState() {
83         return VALUES.stream();
84     }
85 }