]> git.basschouten.com Git - openhab-addons.git/blob
c17f7ce9633f2444e218c9f533be927c623b6147
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.sensorcommunity.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.sensorcommunity.internal.handler.BaseSensorHandler.UpdateStatus;
22 import org.openhab.binding.sensorcommunity.internal.mock.NoiseHandlerExtension;
23 import org.openhab.binding.sensorcommunity.internal.mock.ThingMock;
24 import org.openhab.binding.sensorcommunity.internal.util.FileReader;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.unit.Units;
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, 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");
53         } else {
54             assertTrue(false);
55         }
56     }
57
58     @Test
59     public void testInvalidUpdate() {
60         ThingMock t = new ThingMock();
61
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);
66
67         NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
68         String pmJson = FileReader.readFileInString("src/test/resources/condition-result-no-pressure.json");
69         if (pmJson != null) {
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");
75         } else {
76             assertTrue(false);
77         }
78     }
79
80     @Test
81     public void testEmptyUpdate() {
82         ThingMock t = new ThingMock();
83
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);
88
89         NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
90         UpdateStatus result = noiseHandler.updateChannels("[]");
91         assertEquals(UpdateStatus.VALUE_EMPTY, result, "Valid update");
92     }
93
94     @Test
95     public void testNullUpdate() {
96         ThingMock t = new ThingMock();
97
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);
102
103         NoiseHandlerExtension noiseHandler = new NoiseHandlerExtension(t);
104         UpdateStatus result = noiseHandler.updateChannels(null);
105         assertEquals(UpdateStatus.CONNECTION_ERROR, result, "Valid update");
106     }
107 }