2 * Copyright (c) 2010-2020 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.luftdateninfo.internal;
15 import static org.junit.Assert.*;
17 import java.util.HashMap;
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;
29 * The {@link NoiseHandlerTest} Test Noise Handler updates
31 * @author Bernd Weymann - Initial contribution
34 public class NoiseHandlerTest {
37 public void testValidUpdate() {
38 ThingMock t = new ThingMock();
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);
45 NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
46 String pmJson = FileReader.readFileInString("src/test/resources/noise-result.json");
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());
62 public void testInvalidUpdate() {
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 NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
71 String pmJson = FileReader.readFileInString("src/test/resources/condition-result-no-pressure.json");
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());
87 public void testEmptyUpdate() {
88 ThingMock t = new ThingMock();
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);
95 NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
96 UpdateStatus result = noiseHandler.updateChannels("[]");
97 assertEquals("Valid update", UpdateStatus.VALUE_EMPTY, result);
101 public void testNullUpdate() {
102 ThingMock t = new ThingMock();
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);
109 NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
110 UpdateStatus result = noiseHandler.updateChannels(null);
111 assertEquals("Valid update", UpdateStatus.CONNECTION_ERROR, result);