]> git.basschouten.com Git - openhab-addons.git/blob
05a3ae157a67637a7408aa5854a374965fb048ee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.binding.boschshc.internal.services.dto;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.junit.jupiter.api.Assertions.assertNotEquals;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.junit.jupiter.api.Test;
20
21 import com.google.gson.Gson;
22 import com.google.gson.JsonObject;
23
24 /**
25  * Test class
26  *
27  * @author Christian Oeing - Initial contribution
28  */
29 class TestState extends BoschSHCServiceState {
30     public TestState() {
31         super("testState");
32     }
33 }
34
35 /**
36  * Test class
37  *
38  * @author Christian Oeing - Initial contribution
39  */
40 class TestState2 extends BoschSHCServiceState {
41     public TestState2() {
42         super("testState2");
43     }
44 }
45
46 /**
47  * Unit tests for BoschSHCServiceStateTest
48  *
49  * @author Christian Oeing - Initial contribution
50  */
51 @NonNullByDefault
52 public class BoschSHCServiceStateTest {
53     private final Gson gson = new Gson();
54
55     @Test
56     public void fromJson_nullStateForDifferentType() {
57         var state = BoschSHCServiceState.fromJson(gson.fromJson("{\"@type\":\"differentState\"}", JsonObject.class),
58                 TestState.class);
59         assertEquals(null, state);
60     }
61
62     @Test
63     public void fromJson_stateObjectForValidJson() {
64         var state = BoschSHCServiceState.fromJson(gson.fromJson("{\"@type\":\"testState\"}", JsonObject.class),
65                 TestState.class);
66         assertNotEquals(null, state);
67     }
68
69     /**
70      * This checks for a bug we had where the expected type stayed the same for different state classes
71      */
72     @Test
73     public void fromJson_stateObjectForValidJsonAfterOtherState() {
74         BoschSHCServiceState.fromJson(gson.fromJson("{\"@type\":\"testState\"}", JsonObject.class), TestState.class);
75         var state2 = BoschSHCServiceState.fromJson(gson.fromJson("{\"@type\":\"testState2\"}", JsonObject.class),
76                 TestState2.class);
77         assertNotEquals(null, state2);
78     }
79 }