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 org.junit.jupiter.api.Test;
19 import org.openhab.core.library.types.HSBType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.PercentType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.types.State;
24 import org.openhab.persistence.mapdb.internal.StateTypeAdapter;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
31 * @author Martin Kühl - Initial contribution
33 public class StateTypeAdapterTest {
34 Gson mapper = new GsonBuilder().registerTypeHierarchyAdapter(State.class, new StateTypeAdapter()).create();
37 public void readWriteRoundtripShouldRecreateTheWrittenState() {
38 assertThat(roundtrip(OnOffType.ON), is(equalTo(OnOffType.ON)));
39 assertThat(roundtrip(PercentType.HUNDRED), is(equalTo(PercentType.HUNDRED)));
40 assertThat(roundtrip(HSBType.GREEN), is(equalTo(HSBType.GREEN)));
41 assertThat(roundtrip(StringType.valueOf("test")), is(equalTo(StringType.valueOf("test"))));
44 private State roundtrip(State state) {
45 return mapper.fromJson(mapper.toJson(state), State.class);