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.jupiter.api.Assertions.*;
17 import java.util.HashMap;
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;
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(UpdateStatus.OK, result, "Valid update");
50 assertEquals(QuantityType.valueOf(51.0, SmartHomeUnits.DECIBEL), noiseHandler.getNoiseEQCache(),
52 assertEquals(QuantityType.valueOf(47.2, SmartHomeUnits.DECIBEL), noiseHandler.getNoiseMinCache(),
54 assertEquals(QuantityType.valueOf(57.0, SmartHomeUnits.DECIBEL), 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(UpdateStatus.VALUE_ERROR, result, "Valid update");
75 assertEquals(QuantityType.valueOf(-1, SmartHomeUnits.DECIBEL), noiseHandler.getNoiseEQCache(),
77 assertEquals(QuantityType.valueOf(-1, SmartHomeUnits.DECIBEL), noiseHandler.getNoiseMinCache(),
79 assertEquals(QuantityType.valueOf(-1, SmartHomeUnits.DECIBEL), 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(UpdateStatus.VALUE_EMPTY, result, "Valid update");
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(UpdateStatus.CONNECTION_ERROR, result, "Valid update");