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