]> git.basschouten.com Git - openhab-addons.git/blob
86031b10655cebd9deaf44cefc1ae7f5a3418b8a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.NoiseHandlerExtension;
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.SmartHomeUnits;
27
28 /**
29  * The {@link NoiseHandlerTest} Test Noise Handler updates
30  *
31  * @author Bernd Weymann - Initial contribution
32  */
33 @NonNullByDefault
34 public class NoiseHandlerTest {
35
36     @Test
37     public void testValidUpdate() {
38         ThingMock t = new ThingMock();
39
40         HashMap<String, Object> properties = new HashMap<String, Object>();
41         // String sensorid taken from thing-types.xml
42         properties.put("sensorid", 12345);
43         t.setConfiguration(properties);
44
45         NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
46         String pmJson = FileReader.readFileInString("src/test/resources/noise-result.json");
47         if (pmJson != null) {
48             UpdateStatus result = noiseHandler.updateChannels(pmJson);
49             assertEquals(UpdateStatus.OK, result, "Valid update");
50             assertEquals(QuantityType.valueOf(51.0, SmartHomeUnits.DECIBEL), noiseHandler.getNoiseEQCache(),
51                     "Noise EQ");
52             assertEquals(QuantityType.valueOf(47.2, SmartHomeUnits.DECIBEL), noiseHandler.getNoiseMinCache(),
53                     "Noise Min");
54             assertEquals(QuantityType.valueOf(57.0, SmartHomeUnits.DECIBEL), noiseHandler.getNoiseMaxCache(),
55                     "Noise Max");
56         } else {
57             assertTrue(false);
58         }
59     }
60
61     @Test
62     public void testInvalidUpdate() {
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         NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
71         String pmJson = FileReader.readFileInString("src/test/resources/condition-result-no-pressure.json");
72         if (pmJson != null) {
73             UpdateStatus result = noiseHandler.updateChannels(pmJson);
74             assertEquals(UpdateStatus.VALUE_ERROR, result, "Valid update");
75             assertEquals(QuantityType.valueOf(-1, SmartHomeUnits.DECIBEL), noiseHandler.getNoiseEQCache(),
76                     "Values undefined");
77             assertEquals(QuantityType.valueOf(-1, SmartHomeUnits.DECIBEL), noiseHandler.getNoiseMinCache(),
78                     "Values undefined");
79             assertEquals(QuantityType.valueOf(-1, SmartHomeUnits.DECIBEL), noiseHandler.getNoiseMaxCache(),
80                     "Values undefined");
81         } else {
82             assertTrue(false);
83         }
84     }
85
86     @Test
87     public void testEmptyUpdate() {
88         ThingMock t = new ThingMock();
89
90         HashMap<String, Object> properties = new HashMap<String, Object>();
91         // String sensorid taken from thing-types.xml
92         properties.put("sensorid", 12345);
93         t.setConfiguration(properties);
94
95         NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
96         UpdateStatus result = noiseHandler.updateChannels("[]");
97         assertEquals(UpdateStatus.VALUE_EMPTY, result, "Valid update");
98     }
99
100     @Test
101     public void testNullUpdate() {
102         ThingMock t = new ThingMock();
103
104         HashMap<String, Object> properties = new HashMap<String, Object>();
105         // String sensorid taken from thing-types.xml
106         properties.put("sensorid", 12345);
107         t.setConfiguration(properties);
108
109         NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
110         UpdateStatus result = noiseHandler.updateChannels(null);
111         assertEquals(UpdateStatus.CONNECTION_ERROR, result, "Valid update");
112     }
113 }