]> git.basschouten.com Git - openhab-addons.git/blob
97eb43a5612b750c25500e05ba448d75e7cf6a57
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.luftdateninfo.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.HashMap;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.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.Units;
28
29 /**
30  * The {@link ConditionHandlerTest} Test Condition Handler updates
31  *
32  * @author Bernd Weymann - Initial contribution
33  */
34 @NonNullByDefault
35 public class ConditionHandlerTest {
36
37     @Test
38     public void testValidNoPressureUpdate() {
39         ThingMock t = new ThingMock();
40
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);
45
46         ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
47         String pmJson = FileReader.readFileInString("src/test/resources/condition-result-no-pressure.json");
48         if (pmJson != null) {
49             UpdateStatus result = condHandler.updateChannels(pmJson);
50             assertEquals(UpdateStatus.OK, result, "Valid update");
51             assertEquals(QuantityType.valueOf(22.7, SIUnits.CELSIUS), condHandler.getTemperature(), "Temperature");
52             assertEquals(QuantityType.valueOf(61.0, Units.PERCENT), condHandler.getHumidity(), "Humidity");
53             assertEquals(QuantityType.valueOf(-1, SIUnits.PASCAL), condHandler.getPressure(), "Pressure");
54             assertEquals(QuantityType.valueOf(-1, SIUnits.PASCAL), condHandler.getPressureSea(), "Pressure Sea");
55         } else {
56             assertTrue(false);
57         }
58     }
59
60     @Test
61     public void testValidWithPressureUpdate() {
62         ThingMock t = new ThingMock();
63
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);
68
69         ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
70         String pmJson = FileReader.readFileInString("src/test/resources/condition-result-plus-pressure.json");
71         if (pmJson != null) {
72             UpdateStatus result = condHandler.updateChannels(pmJson);
73             assertEquals(UpdateStatus.OK, result, "Valid update");
74             assertEquals(QuantityType.valueOf(21.5, SIUnits.CELSIUS), condHandler.getTemperature(), "Temperature");
75             assertEquals(QuantityType.valueOf(58.5, Units.PERCENT), condHandler.getHumidity(), "Humidity");
76             assertEquals(QuantityType.valueOf(100200.0, SIUnits.PASCAL), condHandler.getPressure(), "Pressure");
77             assertEquals(QuantityType.valueOf(101968.7, SIUnits.PASCAL), condHandler.getPressureSea(), "Pressure Sea");
78         } else {
79             assertTrue(false);
80         }
81     }
82
83     @Test
84     public void testInvalidUpdate() {
85         ThingMock t = new ThingMock();
86
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);
91
92         ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
93         String pmJson = FileReader.readFileInString("src/test/resources/noise-result.json");
94         if (pmJson != null) {
95             UpdateStatus result = condHandler.updateChannels(pmJson);
96             assertEquals(UpdateStatus.VALUE_ERROR, result, "Valid update");
97         } else {
98             assertTrue(false);
99         }
100     }
101
102     @Test
103     public void testEmptyUpdate() {
104         ThingMock t = new ThingMock();
105
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);
110
111         ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
112         UpdateStatus result = condHandler.updateChannels("[]");
113         assertEquals(UpdateStatus.VALUE_EMPTY, result, "Valid update");
114     }
115
116     @Test
117     public void testNullUpdate() {
118         ThingMock t = new ThingMock();
119
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);
124
125         ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
126         UpdateStatus result = condHandler.updateChannels(null);
127         assertEquals(UpdateStatus.CONNECTION_ERROR, result, "Valid update");
128     }
129 }