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.ConfigStatus;
22 import org.openhab.binding.luftdateninfo.internal.handler.BaseSensorHandler.LifecycleStatus;
23 import org.openhab.binding.luftdateninfo.internal.handler.BaseSensorHandler.UpdateStatus;
24 import org.openhab.binding.luftdateninfo.internal.mock.PMHandlerExtension;
25 import org.openhab.binding.luftdateninfo.internal.mock.ThingMock;
26 import org.openhab.binding.luftdateninfo.internal.util.FileReader;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.SmartHomeUnits;
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.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, SmartHomeUnits.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM25Cache(),
116 assertEquals(QuantityType.valueOf(5.2, SmartHomeUnits.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM100Cache(),
124 public void testInvalidUpdate() {
125 ThingMock t = new ThingMock();
127 HashMap<String, Object> properties = new HashMap<String, Object>();
128 // String sensorid taken from thing-types.xml
129 properties.put("sensorid", 12345);
130 t.setConfiguration(properties);
132 PMHandlerExtension pmHandler = new PMHandlerExtension(t);
133 String pmJson = FileReader.readFileInString("src/test/resources/noise-result.json");
134 if (pmJson != null) {
135 UpdateStatus result = pmHandler.updateChannels(pmJson);
136 assertEquals(UpdateStatus.VALUE_ERROR, result, "Valid update");
137 assertEquals(QuantityType.valueOf(-1, SmartHomeUnits.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM25Cache(),
139 assertEquals(QuantityType.valueOf(-1, SmartHomeUnits.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM100Cache(),
147 public void testEmptyUpdate() {
148 ThingMock t = new ThingMock();
150 HashMap<String, Object> properties = new HashMap<String, Object>();
151 // String sensorid taken from thing-types.xml
152 properties.put("sensorid", 12345);
153 t.setConfiguration(properties);
155 PMHandlerExtension pmHandler = new PMHandlerExtension(t);
156 UpdateStatus result = pmHandler.updateChannels("[]");
157 assertEquals(UpdateStatus.VALUE_EMPTY, result, "Valid update");
161 public void testNullUpdate() {
162 ThingMock t = new ThingMock();
164 HashMap<String, Object> properties = new HashMap<String, Object>();
165 // String sensorid taken from thing-types.xml
166 properties.put("sensorid", 12345);
167 t.setConfiguration(properties);
169 PMHandlerExtension pmHandler = new PMHandlerExtension(t);
170 UpdateStatus result = pmHandler.updateChannels(null);
171 assertEquals(UpdateStatus.CONNECTION_ERROR, result, "Valid update");