2 * Copyright (c) 2010-2024 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.sensorcommunity.internal;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.core.library.unit.MetricPrefix.HECTO;
18 import java.util.HashMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.sensorcommunity.internal.handler.BaseSensorHandler.UpdateStatus;
23 import org.openhab.binding.sensorcommunity.internal.mock.ConditionHandlerExtension;
24 import org.openhab.binding.sensorcommunity.internal.mock.ThingMock;
25 import org.openhab.binding.sensorcommunity.internal.util.FileReader;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.unit.SIUnits;
28 import org.openhab.core.library.unit.Units;
31 * The {@link ConditionHandlerTest} Test Condition Handler updates
33 * @author Bernd Weymann - Initial contribution
36 public class ConditionHandlerTest {
39 public void testValidNoPressureUpdate() {
40 ThingMock t = new ThingMock();
42 HashMap<String, Object> properties = new HashMap<String, Object>();
43 // String sensorid taken from thing-types.xml
44 properties.put("sensorid", 12345);
45 t.setConfiguration(properties);
47 ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
48 String pmJson = FileReader.readFileInString("src/test/resources/condition-result-no-pressure.json");
50 UpdateStatus result = condHandler.updateChannels(pmJson);
51 assertEquals(UpdateStatus.OK, result, "Valid update");
52 assertEquals(QuantityType.valueOf(22.7, SIUnits.CELSIUS), condHandler.getTemperature(), "Temperature");
53 assertEquals(QuantityType.valueOf(61., Units.PERCENT), condHandler.getHumidity(), "Humidity");
54 assertEquals(QuantityType.valueOf(-1, HECTO(SIUnits.PASCAL)), condHandler.getPressure(), "Pressure");
55 assertEquals(QuantityType.valueOf(-1, HECTO(SIUnits.PASCAL)), condHandler.getPressureSea(), "Pressure Sea");
62 public void testValidWithPressureUpdate() {
63 ThingMock t = new ThingMock();
65 HashMap<String, Object> properties = new HashMap<String, Object>();
66 // String sensorid taken from thing-types.xml
67 properties.put("sensorid", 12345);
68 t.setConfiguration(properties);
70 ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
71 String pmJson = FileReader.readFileInString("src/test/resources/condition-result-plus-pressure.json");
73 UpdateStatus result = condHandler.updateChannels(pmJson);
74 assertEquals(UpdateStatus.OK, result, "Valid update");
75 assertEquals(QuantityType.valueOf(21.5, SIUnits.CELSIUS), condHandler.getTemperature(), "Temperature");
76 assertEquals(QuantityType.valueOf(58.5, Units.PERCENT), condHandler.getHumidity(), "Humidity");
77 assertEquals(QuantityType.valueOf(1002.0, HECTO(SIUnits.PASCAL)), condHandler.getPressure(), "Pressure");
78 assertEquals(QuantityType.valueOf(1019.7, HECTO(SIUnits.PASCAL)), condHandler.getPressureSea(),
86 public void testInvalidUpdate() {
87 ThingMock t = new ThingMock();
89 HashMap<String, Object> properties = new HashMap<String, Object>();
90 // String sensorid taken from thing-types.xml
91 properties.put("sensorid", 12345);
92 t.setConfiguration(properties);
94 ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
95 String pmJson = FileReader.readFileInString("src/test/resources/noise-result.json");
97 UpdateStatus result = condHandler.updateChannels(pmJson);
98 assertEquals(UpdateStatus.VALUE_ERROR, result, "Valid update");
105 public void testEmptyUpdate() {
106 ThingMock t = new ThingMock();
108 HashMap<String, Object> properties = new HashMap<String, Object>();
109 // String sensorid taken from thing-types.xml
110 properties.put("sensorid", 12345);
111 t.setConfiguration(properties);
113 ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
114 UpdateStatus result = condHandler.updateChannels("[]");
115 assertEquals(UpdateStatus.VALUE_EMPTY, result, "Valid update");
119 public void testNullUpdate() {
120 ThingMock t = new ThingMock();
122 HashMap<String, Object> properties = new HashMap<String, Object>();
123 // String sensorid taken from thing-types.xml
124 properties.put("sensorid", 12345);
125 t.setConfiguration(properties);
127 ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
128 UpdateStatus result = condHandler.updateChannels(null);
129 assertEquals(UpdateStatus.CONNECTION_ERROR, result, "Valid update");
133 public void testInternalUpdate() {
134 ThingMock t = new ThingMock();
136 HashMap<String, Object> properties = new HashMap<String, Object>();
137 // String sensorid taken from thing-types.xml
138 properties.put("ipAddress", "192.168.178.1");
139 t.setConfiguration(properties);
141 ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
142 String pmJson = FileReader.readFileInString("src/test/resources/internal-data.json");
143 if (pmJson != null) {
144 UpdateStatus result = condHandler.updateChannels("[" + pmJson + "]");
145 assertEquals(UpdateStatus.OK, result, "Valid update");
146 assertEquals(QuantityType.valueOf(17.6, SIUnits.CELSIUS), condHandler.getTemperature(), "Temperature");
147 assertEquals(QuantityType.valueOf(57.8, Units.PERCENT), condHandler.getHumidity(), "Humidity");
148 assertEquals(QuantityType.valueOf(986.8, HECTO(SIUnits.PASCAL)), condHandler.getPressure(), "Pressure");
149 assertEquals(QuantityType.valueOf(-1, HECTO(SIUnits.PASCAL)), condHandler.getPressureSea(), "Pressure Sea");