2 * Copyright (c) 2010-2023 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.mielecloud.internal.webservice.api.json;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.io.IOException;
18 import java.util.Arrays;
19 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
24 import com.google.gson.Gson;
27 * @author Björn Lange - Initial contribution
30 public class ActionsTest {
32 public void testNullProcessActionInJsonIsConvertedToEmptyList() throws IOException {
34 String json = "{ \"processAction\": null, \"light\": [1], \"startTime\": [ [0, 0],[23,59] ] }";
37 Actions actions = new Gson().fromJson(json, Actions.class);
40 assertNotNull(actions.getProcessAction());
41 assertTrue(actions.getProcessAction().isEmpty());
45 public void testNullLightInJsonIsConvertedToEmptyList() throws IOException {
47 String json = "{ \"processAction\": [1], \"light\": null, \"startTime\": [ [0, 0],[23,59] ] }";
50 Actions actions = new Gson().fromJson(json, Actions.class);
53 assertNotNull(actions.getLight());
54 assertTrue(actions.getLight().isEmpty());
58 public void testNullStartTimeInJsonIsReturnedAsNull() throws IOException {
60 String json = "{ \"processAction\": [1], \"light\": [1], \"startTime\": null }";
63 Actions actions = new Gson().fromJson(json, Actions.class);
66 assertFalse(actions.getStartTime().isPresent());
70 public void testIdListIsEmptyWhenProgramIdFieldIsMissing() {
72 String json = "{ \"processAction\": [1] }";
75 Actions actions = new Gson().fromJson(json, Actions.class);
78 assertTrue(actions.getProgramId().isEmpty());
82 public void testIdListIsEmptyWhenProgramIdFieldIsNull() {
84 String json = "{ \"programId\": null }";
87 Actions actions = new Gson().fromJson(json, Actions.class);
90 assertTrue(actions.getProgramId().isEmpty());
94 public void testIdListContainsEntriesWhenProgramIdFieldIsPresent() {
96 String json = "{ \"programId\": [1,2,3,4] }";
99 Actions actions = new Gson().fromJson(json, Actions.class);
102 assertEquals(Arrays.asList(1, 2, 3, 4), actions.getProgramId());
106 public void processActionContainsSingleEntryWhenThereIsOneProcessAction() {
108 String json = "{ \"processAction\": [1] }";
111 Actions actions = new Gson().fromJson(json, Actions.class);
114 assertEquals(List.of(ProcessAction.START), actions.getProcessAction());
118 public void processActionContainsTwoEntriesWhenThereAreTwoProcessActions() {
120 String json = "{ \"processAction\": [1,2] }";
123 Actions actions = new Gson().fromJson(json, Actions.class);
126 assertEquals(List.of(ProcessAction.START, ProcessAction.STOP), actions.getProcessAction());