2 * Copyright (c) 2010-2023 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.Units;
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, Units.DECIBEL), noiseHandler.getNoiseEQCache(), "Noise EQ");
51 assertEquals(QuantityType.valueOf(47.2, Units.DECIBEL), noiseHandler.getNoiseMinCache(), "Noise Min");
52 assertEquals(QuantityType.valueOf(57.0, Units.DECIBEL), noiseHandler.getNoiseMaxCache(), "Noise Max");
59 public void testInvalidUpdate() {
60 ThingMock t = new ThingMock();
62 HashMap<String, Object> properties = new HashMap<String, Object>();
63 // String sensorid taken from thing-types.xml
64 properties.put("sensorid", 12345);
65 t.setConfiguration(properties);
67 NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
68 String pmJson = FileReader.readFileInString("src/test/resources/condition-result-no-pressure.json");
70 UpdateStatus result = noiseHandler.updateChannels(pmJson);
71 assertEquals(UpdateStatus.VALUE_ERROR, result, "Valid update");
72 assertEquals(QuantityType.valueOf(-1, Units.DECIBEL), noiseHandler.getNoiseEQCache(), "Values undefined");
73 assertEquals(QuantityType.valueOf(-1, Units.DECIBEL), noiseHandler.getNoiseMinCache(), "Values undefined");
74 assertEquals(QuantityType.valueOf(-1, Units.DECIBEL), noiseHandler.getNoiseMaxCache(), "Values undefined");
81 public void testEmptyUpdate() {
82 ThingMock t = new ThingMock();
84 HashMap<String, Object> properties = new HashMap<String, Object>();
85 // String sensorid taken from thing-types.xml
86 properties.put("sensorid", 12345);
87 t.setConfiguration(properties);
89 NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
90 UpdateStatus result = noiseHandler.updateChannels("[]");
91 assertEquals(UpdateStatus.VALUE_EMPTY, result, "Valid update");
95 public void testNullUpdate() {
96 ThingMock t = new ThingMock();
98 HashMap<String, Object> properties = new HashMap<String, Object>();
99 // String sensorid taken from thing-types.xml
100 properties.put("sensorid", 12345);
101 t.setConfiguration(properties);
103 NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
104 UpdateStatus result = noiseHandler.updateChannels(null);
105 assertEquals(UpdateStatus.CONNECTION_ERROR, result, "Valid update");