]> git.basschouten.com Git - openhab-addons.git/blob
88de17def39ccc69a70e31346d90eb9c84293b95
[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.mielecloud.internal.webservice.api.json;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.io.IOException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21
22 import com.google.gson.Gson;
23
24 /**
25  * @author Björn Lange - Initial contribution
26  */
27 @NonNullByDefault
28 public class StateTest {
29     @Test
30     public void testNullRemainingTimeInJsonCausesRemainingTimeListToBeNull() throws IOException {
31         // given:
32         String json = "{ \"remainingTime\": null, \"startTime\": [0, 0], \"targetTemperature\": [{}], \"temperature\": [{}], \"elapsedTime\": [0, 0] }";
33
34         // when:
35         State state = new Gson().fromJson(json, State.class);
36
37         // then:
38         assertFalse(state.getRemainingTime().isPresent());
39     }
40
41     @Test
42     public void testNullStartTimeInJsonCausesStartTimeListToBeNull() throws IOException {
43         // given:
44         String json = "{ \"remainingTime\": [0, 0], \"startTime\": null, \"targetTemperature\": [{}], \"temperature\": [{}], \"elapsedTime\": [0, 0] }";
45
46         // when:
47         State state = new Gson().fromJson(json, State.class);
48
49         // then:
50         assertFalse(state.getStartTime().isPresent());
51     }
52
53     @Test
54     public void testNullElapsedTimeInJsonCausesElapsedTimeListToBeNull() throws IOException {
55         // given:
56         String json = "{ \"remainingTime\": [0, 0], \"startTime\": [0, 0], \"targetTemperature\": [{}], \"temperature\": [{}], \"elapsedTime\": null }";
57
58         // when:
59         State state = new Gson().fromJson(json, State.class);
60
61         // then:
62         assertFalse(state.getElapsedTime().isPresent());
63     }
64
65     @Test
66     public void testNullTargetTemperatureInJsonIsConvertedToEmptyList() throws IOException {
67         // given:
68         String json = "{ \"remainingTime\": [0, 0], \"startTime\": [0, 0], \"targetTemperature\": null, \"temperature\": [{}], \"elapsedTime\": [0, 0] }";
69
70         // when:
71         State state = new Gson().fromJson(json, State.class);
72
73         // then:
74         assertNotNull(state.getTargetTemperature());
75         assertTrue(state.getTargetTemperature().isEmpty());
76     }
77
78     @Test
79     public void testNullTemperatureInJsonIsConvertedToEmptyList() throws IOException {
80         // given:
81         String json = "{ \"remainingTime\": [0, 0], \"startTime\": [0, 0], \"targetTemperature\": [{}], \"temperature\": null, \"elapsedTime\": [0, 0] }";
82
83         // when:
84         State state = new Gson().fromJson(json, State.class);
85
86         // then:
87         assertNotNull(state.getTemperature());
88         assertTrue(state.getTemperature().isEmpty());
89     }
90 }