]> git.basschouten.com Git - openhab-addons.git/blob
f0f1a29a5fb22db81f6d95a54eb2dbcfac9e995c
[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 import org.openhab.binding.boschshc.internal.serialization.GsonUtils;
20
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
53     @Test
54     public void fromJsonNullStateForDifferentType() {
55         var state = BoschSHCServiceState.fromJson(
56                 GsonUtils.DEFAULT_GSON_INSTANCE.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(
64                 GsonUtils.DEFAULT_GSON_INSTANCE.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 fromJsonStateObjectForValidJsonAfterOtherState() {
74         BoschSHCServiceState.fromJson(
75                 GsonUtils.DEFAULT_GSON_INSTANCE.fromJson("{\"@type\":\"testState\"}", JsonObject.class),
76                 TestState.class);
77         var state2 = BoschSHCServiceState.fromJson(
78                 GsonUtils.DEFAULT_GSON_INSTANCE.fromJson("{\"@type\":\"testState2\"}", JsonObject.class),
79                 TestState2.class);
80         assertNotEquals(null, state2);
81     }
82 }