]> git.basschouten.com Git - openhab-addons.git/blob
18d8124a32ab648875674b3edb4a5332d1ea0539
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 import java.util.Arrays;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22
23 import com.google.gson.Gson;
24
25 /**
26  * @author Björn Lange - Initial contribution
27  */
28 @NonNullByDefault
29 public class ActionsTest {
30     @Test
31     public void testNullProcessActionInJsonIsConvertedToEmptyList() throws IOException {
32         // given:
33         String json = "{ \"processAction\": null, \"light\": [1], \"startTime\": [ [0, 0],[23,59] ] }";
34
35         // when:
36         Actions actions = new Gson().fromJson(json, Actions.class);
37
38         // then:
39         assertNotNull(actions.getProcessAction());
40         assertTrue(actions.getProcessAction().isEmpty());
41     }
42
43     @Test
44     public void testNullLightInJsonIsConvertedToEmptyList() throws IOException {
45         // given:
46         String json = "{ \"processAction\": [1], \"light\": null, \"startTime\": [ [0, 0],[23,59] ] }";
47
48         // when:
49         Actions actions = new Gson().fromJson(json, Actions.class);
50
51         // then:
52         assertNotNull(actions.getLight());
53         assertTrue(actions.getLight().isEmpty());
54     }
55
56     @Test
57     public void testNullStartTimeInJsonIsReturnedAsNull() throws IOException {
58         // given:
59         String json = "{ \"processAction\": [1], \"light\": [1], \"startTime\": null }";
60
61         // when:
62         Actions actions = new Gson().fromJson(json, Actions.class);
63
64         // then:
65         assertFalse(actions.getStartTime().isPresent());
66     }
67
68     @Test
69     public void testIdListIsEmptyWhenProgramIdFieldIsMissing() {
70         // given:
71         String json = "{ \"processAction\": [1] }";
72
73         // when:
74         Actions actions = new Gson().fromJson(json, Actions.class);
75
76         // then:
77         assertTrue(actions.getProgramId().isEmpty());
78     }
79
80     @Test
81     public void testIdListIsEmptyWhenProgramIdFieldIsNull() {
82         // given:
83         String json = "{ \"programId\": null }";
84
85         // when:
86         Actions actions = new Gson().fromJson(json, Actions.class);
87
88         // then:
89         assertTrue(actions.getProgramId().isEmpty());
90     }
91
92     @Test
93     public void testIdListContainsEntriesWhenProgramIdFieldIsPresent() {
94         // given:
95         String json = "{ \"programId\": [1,2,3,4] }";
96
97         // when:
98         Actions actions = new Gson().fromJson(json, Actions.class);
99
100         // then:
101         assertEquals(Arrays.asList(1, 2, 3, 4), actions.getProgramId());
102     }
103 }