]> git.basschouten.com Git - openhab-addons.git/blob
eb9f52f0d762df0cd80da7a53ad17b93f7649610
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 import static org.openhab.core.library.unit.MetricPrefix.HECTO;
17
18 import java.util.HashMap;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.luftdateninfo.internal.handler.BaseSensorHandler.UpdateStatus;
23 import org.openhab.binding.luftdateninfo.internal.mock.ConditionHandlerExtension;
24 import org.openhab.binding.luftdateninfo.internal.mock.ThingMock;
25 import org.openhab.binding.luftdateninfo.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;
29
30 /**
31  * The {@link ConditionHandlerTest} Test Condition Handler updates
32  *
33  * @author Bernd Weymann - Initial contribution
34  */
35 @NonNullByDefault
36 public class ConditionHandlerTest {
37
38     @Test
39     public void testValidNoPressureUpdate() {
40         ThingMock t = new ThingMock();
41
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);
46
47         ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
48         String pmJson = FileReader.readFileInString("src/test/resources/condition-result-no-pressure.json");
49         if (pmJson != null) {
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");
56         } else {
57             assertTrue(false);
58         }
59     }
60
61     @Test
62     public void testValidWithPressureUpdate() {
63         ThingMock t = new ThingMock();
64
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);
69
70         ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
71         String pmJson = FileReader.readFileInString("src/test/resources/condition-result-plus-pressure.json");
72         if (pmJson != null) {
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(),
79                     "Pressure Sea");
80         } else {
81             assertTrue(false);
82         }
83     }
84
85     @Test
86     public void testInvalidUpdate() {
87         ThingMock t = new ThingMock();
88
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);
93
94         ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
95         String pmJson = FileReader.readFileInString("src/test/resources/noise-result.json");
96         if (pmJson != null) {
97             UpdateStatus result = condHandler.updateChannels(pmJson);
98             assertEquals(UpdateStatus.VALUE_ERROR, result, "Valid update");
99         } else {
100             assertTrue(false);
101         }
102     }
103
104     @Test
105     public void testEmptyUpdate() {
106         ThingMock t = new ThingMock();
107
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);
112
113         ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
114         UpdateStatus result = condHandler.updateChannels("[]");
115         assertEquals(UpdateStatus.VALUE_EMPTY, result, "Valid update");
116     }
117
118     @Test
119     public void testNullUpdate() {
120         ThingMock t = new ThingMock();
121
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);
126
127         ConditionHandlerExtension condHandler = new ConditionHandlerExtension(t);
128         UpdateStatus result = condHandler.updateChannels(null);
129         assertEquals(UpdateStatus.CONNECTION_ERROR, result, "Valid update");
130     }
131
132     @Test
133     public void testInternalUpdate() {
134         ThingMock t = new ThingMock();
135
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);
140
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");
150         } else {
151             assertTrue(false);
152         }
153     }
154 }