]> git.basschouten.com Git - openhab-addons.git/blob
4436b886534d0e7e672bddae3ae7a8c25f6b31bf
[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.Assert.*;
16
17 import java.util.HashMap;
18
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.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("Valid update", UpdateStatus.OK, result);
50             assertEquals("Noise EQ", QuantityType.valueOf(51.0, SmartHomeUnits.DECIBEL),
51                     noiseHandler.getNoiseEQCache());
52             assertEquals("Noise Min", QuantityType.valueOf(47.2, SmartHomeUnits.DECIBEL),
53                     noiseHandler.getNoiseMinCache());
54             assertEquals("Noise Max", QuantityType.valueOf(57.0, SmartHomeUnits.DECIBEL),
55                     noiseHandler.getNoiseMaxCache());
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("Valid update", UpdateStatus.VALUE_ERROR, result);
75             assertEquals("Values undefined", QuantityType.valueOf(-1, SmartHomeUnits.DECIBEL),
76                     noiseHandler.getNoiseEQCache());
77             assertEquals("Values undefined", QuantityType.valueOf(-1, SmartHomeUnits.DECIBEL),
78                     noiseHandler.getNoiseMinCache());
79             assertEquals("Values undefined", QuantityType.valueOf(-1, SmartHomeUnits.DECIBEL),
80                     noiseHandler.getNoiseMaxCache());
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("Valid update", UpdateStatus.VALUE_EMPTY, result);
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("Valid update", UpdateStatus.CONNECTION_ERROR, result);
112     }
113 }