2 * Copyright (c) 2010-2020 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.luftdateninfo.internal;
15 import static org.junit.Assert.*;
17 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.Test;
21 import org.openhab.binding.luftdateninfo.internal.handler.BaseSensorHandler.UpdateStatus;
22 import org.openhab.binding.luftdateninfo.internal.mock.ConditionHandlerExtension;
23 import org.openhab.binding.luftdateninfo.internal.mock.ThingMock;
24 import org.openhab.binding.luftdateninfo.internal.util.FileReader;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.unit.SIUnits;
27 import org.openhab.core.library.unit.SmartHomeUnits;
30 * The {@link ConditionHandlerTest} Test Condition Handler updates
32 * @author Bernd Weymann - Initial contribution
35 public class ConditionHandlerTest {
38 public void testValidNoPressureUpdate() {
39 ThingMock t = new ThingMock();
41 HashMap<String, Object> properties = new HashMap<String, Object>();
42 // String sensorid taken from thing-types.xml
43 properties.put("sensorid", 12345);
44 t.setConfiguration(properties);
46 ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
47 String pmJson = FileReader.readFileInString("src/test/resources/condition-result-no-pressure.json");
49 UpdateStatus result = condHandler.updateChannels(pmJson);
50 assertEquals("Valid update", UpdateStatus.OK, result);
51 assertEquals("Temperature", QuantityType.valueOf(22.7, SIUnits.CELSIUS), condHandler.getTemperature());
52 assertEquals("Humidity", QuantityType.valueOf(61.0, SmartHomeUnits.PERCENT), condHandler.getHumidity());
53 assertEquals("Pressure", QuantityType.valueOf(-1, SIUnits.PASCAL), condHandler.getPressure());
54 assertEquals("Pressure Sea", QuantityType.valueOf(-1, SIUnits.PASCAL), condHandler.getPressureSea());
61 public void testValidWithPressureUpdate() {
62 ThingMock t = new ThingMock();
64 HashMap<String, Object> properties = new HashMap<String, Object>();
65 // String sensorid taken from thing-types.xml
66 properties.put("sensorid", 12345);
67 t.setConfiguration(properties);
69 ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
70 String pmJson = FileReader.readFileInString("src/test/resources/condition-result-plus-pressure.json");
72 UpdateStatus result = condHandler.updateChannels(pmJson);
73 assertEquals("Valid update", UpdateStatus.OK, result);
74 assertEquals("Temperature", QuantityType.valueOf(21.5, SIUnits.CELSIUS), condHandler.getTemperature());
75 assertEquals("Humidity", QuantityType.valueOf(58.5, SmartHomeUnits.PERCENT), condHandler.getHumidity());
76 assertEquals("Pressure", QuantityType.valueOf(100200.0, SIUnits.PASCAL), condHandler.getPressure());
77 assertEquals("Pressure Sea", QuantityType.valueOf(101968.7, SIUnits.PASCAL), condHandler.getPressureSea());
84 public void testInvalidUpdate() {
85 ThingMock t = new ThingMock();
87 HashMap<String, Object> properties = new HashMap<String, Object>();
88 // String sensorid taken from thing-types.xml
89 properties.put("sensorid", 12345);
90 t.setConfiguration(properties);
92 ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
93 String pmJson = FileReader.readFileInString("src/test/resources/noise-result.json");
95 UpdateStatus result = condHandler.updateChannels(pmJson);
96 assertEquals("Valid update", UpdateStatus.VALUE_ERROR, result);
103 public void testEmptyUpdate() {
104 ThingMock t = new ThingMock();
106 HashMap<String, Object> properties = new HashMap<String, Object>();
107 // String sensorid taken from thing-types.xml
108 properties.put("sensorid", 12345);
109 t.setConfiguration(properties);
111 ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
112 UpdateStatus result = condHandler.updateChannels("[]");
113 assertEquals("Valid update", UpdateStatus.VALUE_EMPTY, result);
117 public void testNullUpdate() {
118 ThingMock t = new ThingMock();
120 HashMap<String, Object> properties = new HashMap<String, Object>();
121 // String sensorid taken from thing-types.xml
122 properties.put("sensorid", 12345);
123 t.setConfiguration(properties);
125 ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
126 UpdateStatus result = condHandler.updateChannels(null);
127 assertEquals("Valid update", UpdateStatus.CONNECTION_ERROR, result);