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