]> git.basschouten.com Git - openhab-addons.git/blob
2c3fc0f86e8049fd6a3e4d29fdb7668d6b030ea9
[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 import org.openhab.binding.boschshc.internal.services.userstate.dto.UserStateServiceState;
21
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonPrimitive;
24
25 /**
26  * Test class
27  *
28  * @author Christian Oeing - Initial contribution
29  */
30 class TestState extends BoschSHCServiceState {
31     public TestState() {
32         super("testState");
33     }
34 }
35
36 /**
37  * Test class
38  *
39  * @author Christian Oeing - Initial contribution
40  */
41 class TestState2 extends BoschSHCServiceState {
42     public TestState2() {
43         super("testState2");
44     }
45 }
46
47 /**
48  * Unit tests for BoschSHCServiceStateTest
49  *
50  * @author Christian Oeing - Initial contribution
51  */
52 @NonNullByDefault
53 class BoschSHCServiceStateTest {
54
55     @Test
56     void fromJsonNullStateForDifferentType() {
57         var state = BoschSHCServiceState.fromJson(
58                 GsonUtils.DEFAULT_GSON_INSTANCE.fromJson("{\"@type\":\"differentState\"}", JsonObject.class),
59                 TestState.class);
60         assertEquals(null, state);
61     }
62
63     @Test
64     void fromJsonStateObjectForValidJson() {
65         var state = BoschSHCServiceState.fromJson(
66                 GsonUtils.DEFAULT_GSON_INSTANCE.fromJson("{\"@type\":\"testState\"}", JsonObject.class),
67                 TestState.class);
68         assertNotEquals(null, state);
69     }
70
71     /**
72      * This checks for a bug we had where the expected type stayed the same for different state classes
73      */
74     @Test
75     void fromJsonStateObjectForValidJsonAfterOtherState() {
76         BoschSHCServiceState.fromJson(
77                 GsonUtils.DEFAULT_GSON_INSTANCE.fromJson("{\"@type\":\"testState\"}", JsonObject.class),
78                 TestState.class);
79         var state2 = BoschSHCServiceState.fromJson(
80                 GsonUtils.DEFAULT_GSON_INSTANCE.fromJson("{\"@type\":\"testState2\"}", JsonObject.class),
81                 TestState2.class);
82         assertNotEquals(null, state2);
83     }
84
85     @Test
86     void fromJsonReturnsUserStateServiceStateForValidJson() {
87         var state = BoschSHCServiceState.fromJson(new JsonPrimitive("false"), UserStateServiceState.class);
88         assertNotEquals(null, state);
89         assertTrue(state.getClass().isAssignableFrom(UserStateServiceState.class));
90         assertFalse(state.isState());
91     }
92 }