]> git.basschouten.com Git - openhab-addons.git/blob
225b1aad68ebc455c620f617231e3bc80c0ab3f2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 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;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28
29 /**
30  *
31  * @author Martin Kühl - Initial contribution
32  */
33 public class StateTypeAdapterTest {
34     Gson mapper = new GsonBuilder().registerTypeHierarchyAdapter(State.class, new StateTypeAdapter()).create();
35
36     @Test
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"))));
42     }
43
44     private State roundtrip(State state) {
45         return mapper.fromJson(mapper.toJson(state), State.class);
46     }
47 }