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