2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.boschshc.internal.services.dto;
15 import static org.junit.jupiter.api.Assertions.*;
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;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonPrimitive;
28 * @author Christian Oeing - Initial contribution
30 class TestState extends BoschSHCServiceState {
39 * @author Christian Oeing - Initial contribution
41 class TestState2 extends BoschSHCServiceState {
48 * Unit tests for BoschSHCServiceStateTest
50 * @author Christian Oeing - Initial contribution
53 class BoschSHCServiceStateTest {
56 void fromJsonNullStateForDifferentType() {
57 var state = BoschSHCServiceState.fromJson(
58 GsonUtils.DEFAULT_GSON_INSTANCE.fromJson("{\"@type\":\"differentState\"}", JsonObject.class),
60 assertEquals(null, state);
64 void fromJsonStateObjectForValidJson() {
65 var state = BoschSHCServiceState.fromJson(
66 GsonUtils.DEFAULT_GSON_INSTANCE.fromJson("{\"@type\":\"testState\"}", JsonObject.class),
68 assertNotEquals(null, state);
72 * This checks for a bug we had where the expected type stayed the same for different state classes
75 void fromJsonStateObjectForValidJsonAfterOtherState() {
76 BoschSHCServiceState.fromJson(
77 GsonUtils.DEFAULT_GSON_INSTANCE.fromJson("{\"@type\":\"testState\"}", JsonObject.class),
79 var state2 = BoschSHCServiceState.fromJson(
80 GsonUtils.DEFAULT_GSON_INSTANCE.fromJson("{\"@type\":\"testState2\"}", JsonObject.class),
82 assertNotEquals(null, state2);
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());