2 * Copyright (c) 2010-2024 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.sensorcommunity.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.sensorcommunity.internal.handler.BaseSensorHandler.ConfigStatus;
22 import org.openhab.binding.sensorcommunity.internal.handler.BaseSensorHandler.LifecycleStatus;
23 import org.openhab.binding.sensorcommunity.internal.handler.BaseSensorHandler.UpdateStatus;
24 import org.openhab.binding.sensorcommunity.internal.mock.PMHandlerExtension;
25 import org.openhab.binding.sensorcommunity.internal.mock.ThingMock;
26 import org.openhab.binding.sensorcommunity.internal.util.FileReader;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.Units;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The {@link PMHandlerTest} Test Particualte Matter Handler - Config and updates
35 * @author Bernd Weymann - Initial contribution
38 public class PMHandlerTest {
39 private Logger logger = LoggerFactory.getLogger(PMHandlerTest.class);
42 public void testValidConfigStatus() {
43 ThingMock t = new ThingMock();
45 HashMap<String, Object> properties = new HashMap<String, Object>();
46 // String sensorid taken from thing-types.xml
47 properties.put("sensorid", 12345);
48 t.setConfiguration(properties);
50 PMHandlerExtension pmHandler = new PMHandlerExtension(t);
51 pmHandler.initialize();
52 logger.info("LC status: {}", pmHandler.getLifecycleStatus());
53 int retryCount = 0; // Test shall fail after max 10 seconds
54 while (pmHandler.getLifecycleStatus() != LifecycleStatus.RUNNING && retryCount < 20) {
56 logger.info("LC running not reached - wait");
59 } catch (InterruptedException e) {
64 * Test if config status is 0 = CONFIG_OK for valid configuration. Take real int for comparison instead of
65 * BaseHandler constants - in case of change test needs to be adapted
67 assertEquals(ConfigStatus.EXTERNAL_SENSOR_OK, pmHandler.getConfigStatus(), "Handler Configuration status");
71 public void testInvalidConfigStatus() {
72 ThingMock t = new ThingMock();
74 HashMap<String, Object> properties = new HashMap<String, Object>();
75 // String sensorid taken from thing-types.xml
76 properties.put("sensorid", -1);
77 t.setConfiguration(properties);
79 PMHandlerExtension pmHandler = new PMHandlerExtension(t);
80 pmHandler.initialize();
81 logger.info("LC status: {}", pmHandler.getLifecycleStatus());
82 int retryCount = 0; // Test shall fail after max 10 seconds
83 while (pmHandler.getLifecycleStatus() != LifecycleStatus.RUNNING && retryCount < 20) {
85 logger.info("LC running not reached - wait");
88 } catch (InterruptedException e) {
93 * Test if config status is 3 = CONFIG_SENSOR_NUMBER for invalid configuration with non-number sensorid. Take
94 * real int for comparison instead of BaseHandler constants - in case of change test needs to be adapted
96 assertEquals(ConfigStatus.SENSOR_ID_NEGATIVE, pmHandler.getConfigStatus(), "Handler Configuration status");
100 public void testValidUpdate() {
101 ThingMock t = new ThingMock();
103 HashMap<String, Object> properties = new HashMap<String, Object>();
104 // String sensorid taken from thing-types.xml
105 properties.put("sensorid", 12345);
106 t.setConfiguration(properties);
108 PMHandlerExtension pmHandler = new PMHandlerExtension(t);
109 pmHandler.initialize();
110 String pmJson = FileReader.readFileInString("src/test/resources/pm-result.json");
111 if (pmJson != null) {
112 UpdateStatus result = pmHandler.updateChannels(pmJson);
113 assertEquals(UpdateStatus.OK, result, "Valid update");
114 assertEquals(QuantityType.valueOf(2.9, Units.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM25Cache(), "PM25");
115 assertEquals(QuantityType.valueOf(5.2, Units.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM100Cache(), "PM100");
122 public void testInvalidUpdate() {
123 ThingMock t = new ThingMock();
125 HashMap<String, Object> properties = new HashMap<String, Object>();
126 // String sensorid taken from thing-types.xml
127 properties.put("sensorid", 12345);
128 t.setConfiguration(properties);
130 PMHandlerExtension pmHandler = new PMHandlerExtension(t);
131 String pmJson = FileReader.readFileInString("src/test/resources/noise-result.json");
132 if (pmJson != null) {
133 UpdateStatus result = pmHandler.updateChannels(pmJson);
134 assertEquals(UpdateStatus.VALUE_ERROR, result, "Valid update");
135 assertEquals(QuantityType.valueOf(-1, Units.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM25Cache(),
137 assertEquals(QuantityType.valueOf(-1, Units.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM100Cache(),
145 public void testEmptyUpdate() {
146 ThingMock t = new ThingMock();
148 HashMap<String, Object> properties = new HashMap<String, Object>();
149 // String sensorid taken from thing-types.xml
150 properties.put("sensorid", 12345);
151 t.setConfiguration(properties);
153 PMHandlerExtension pmHandler = new PMHandlerExtension(t);
154 UpdateStatus result = pmHandler.updateChannels("[]");
155 assertEquals(UpdateStatus.VALUE_EMPTY, result, "Valid update");
159 public void testNullUpdate() {
160 ThingMock t = new ThingMock();
162 HashMap<String, Object> properties = new HashMap<String, Object>();
163 // String sensorid taken from thing-types.xml
164 properties.put("ipAdress", "192.168.178.1");
165 t.setConfiguration(properties);
167 PMHandlerExtension pmHandler = new PMHandlerExtension(t);
168 UpdateStatus result = pmHandler.updateChannels(null);
169 assertEquals(UpdateStatus.CONNECTION_ERROR, result, "Valid update");
173 public void testInternalPMSensor() {
174 ThingMock t = new ThingMock();
176 HashMap<String, Object> properties = new HashMap<String, Object>();
177 // String sensorid taken from thing-types.xml
178 properties.put("sensorid", 12345);
179 t.setConfiguration(properties);
181 PMHandlerExtension pmHandler = new PMHandlerExtension(t);
182 pmHandler.initialize();
183 String pmJson = FileReader.readFileInString("src/test/resources/internal-data.json");
184 if (pmJson != null) {
185 UpdateStatus result = pmHandler.updateChannels("[" + pmJson + "]");
186 assertEquals(UpdateStatus.OK, result, "Valid update");
187 assertEquals(QuantityType.valueOf(4.3, Units.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM25Cache(), "PM25");
188 assertEquals(QuantityType.valueOf(10.5, Units.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM100Cache(),