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.Collections;
20 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
25 import com.google.gson.Gson;
28 * @author Björn Lange - Initial contribution
31 public class ActionsTest {
33 public void testNullProcessActionInJsonIsConvertedToEmptyList() throws IOException {
35 String json = "{ \"processAction\": null, \"light\": [1], \"startTime\": [ [0, 0],[23,59] ] }";
38 Actions actions = new Gson().fromJson(json, Actions.class);
41 assertNotNull(actions.getProcessAction());
42 assertTrue(actions.getProcessAction().isEmpty());
46 public void testNullLightInJsonIsConvertedToEmptyList() throws IOException {
48 String json = "{ \"processAction\": [1], \"light\": null, \"startTime\": [ [0, 0],[23,59] ] }";
51 Actions actions = new Gson().fromJson(json, Actions.class);
54 assertNotNull(actions.getLight());
55 assertTrue(actions.getLight().isEmpty());
59 public void testNullStartTimeInJsonIsReturnedAsNull() throws IOException {
61 String json = "{ \"processAction\": [1], \"light\": [1], \"startTime\": null }";
64 Actions actions = new Gson().fromJson(json, Actions.class);
67 assertFalse(actions.getStartTime().isPresent());
71 public void testIdListIsEmptyWhenProgramIdFieldIsMissing() {
73 String json = "{ \"processAction\": [1] }";
76 Actions actions = new Gson().fromJson(json, Actions.class);
79 assertTrue(actions.getProgramId().isEmpty());
83 public void testIdListIsEmptyWhenProgramIdFieldIsNull() {
85 String json = "{ \"programId\": null }";
88 Actions actions = new Gson().fromJson(json, Actions.class);
91 assertTrue(actions.getProgramId().isEmpty());
95 public void testIdListContainsEntriesWhenProgramIdFieldIsPresent() {
97 String json = "{ \"programId\": [1,2,3,4] }";
100 Actions actions = new Gson().fromJson(json, Actions.class);
103 assertEquals(Arrays.asList(1, 2, 3, 4), actions.getProgramId());
107 public void processActionContainsSingleEntryWhenThereIsOneProcessAction() {
109 String json = "{ \"processAction\": [1] }";
112 Actions actions = new Gson().fromJson(json, Actions.class);
115 assertEquals(Collections.singletonList(ProcessAction.START), actions.getProcessAction());
119 public void processActionContainsTwoEntriesWhenThereAreTwoProcessActions() {
121 String json = "{ \"processAction\": [1,2] }";
124 Actions actions = new Gson().fromJson(json, Actions.class);
127 assertEquals(List.of(ProcessAction.START, ProcessAction.STOP), actions.getProcessAction());