]> git.basschouten.com Git - openhab-addons.git/blob
2a979e86c6baa1b30e7cfc1d21290f7f07e0b5e9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.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;
31
32 /**
33  * The {@link PMHandlerTest} Test Particualte Matter Handler - Config and updates
34  *
35  * @author Bernd Weymann - Initial contribution
36  */
37 @NonNullByDefault
38 public class PMHandlerTest {
39     private Logger logger = LoggerFactory.getLogger(PMHandlerTest.class);
40
41     @Test
42     public void testValidConfigStatus() {
43         ThingMock t = new ThingMock();
44
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);
49
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) {
55             try {
56                 logger.info("LC running not reached - wait");
57                 Thread.sleep(500);
58                 retryCount++;
59             } catch (InterruptedException e) {
60                 // nothing to do
61             }
62         }
63         /*
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
66          */
67         assertEquals(ConfigStatus.EXTERNAL_SENSOR_OK, pmHandler.getConfigStatus(), "Handler Configuration status");
68     }
69
70     @Test
71     public void testInvalidConfigStatus() {
72         ThingMock t = new ThingMock();
73
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);
78
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) {
84             try {
85                 logger.info("LC running not reached - wait");
86                 Thread.sleep(500);
87                 retryCount++;
88             } catch (InterruptedException e) {
89                 // nothing to do
90             }
91         }
92         /*
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
95          */
96         assertEquals(ConfigStatus.SENSOR_ID_NEGATIVE, pmHandler.getConfigStatus(), "Handler Configuration status");
97     }
98
99     @Test
100     public void testValidUpdate() {
101         ThingMock t = new ThingMock();
102
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);
107
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");
116         } else {
117             assertTrue(false);
118         }
119     }
120
121     @Test
122     public void testInvalidUpdate() {
123         ThingMock t = new ThingMock();
124
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);
129
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(),
136                     "Values undefined");
137             assertEquals(QuantityType.valueOf(-1, Units.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM100Cache(),
138                     "Values undefined");
139         } else {
140             assertTrue(false);
141         }
142     }
143
144     @Test
145     public void testEmptyUpdate() {
146         ThingMock t = new ThingMock();
147
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);
152
153         PMHandlerExtension pmHandler = new PMHandlerExtension(t);
154         UpdateStatus result = pmHandler.updateChannels("[]");
155         assertEquals(UpdateStatus.VALUE_EMPTY, result, "Valid update");
156     }
157
158     @Test
159     public void testNullUpdate() {
160         ThingMock t = new ThingMock();
161
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);
166
167         PMHandlerExtension pmHandler = new PMHandlerExtension(t);
168         UpdateStatus result = pmHandler.updateChannels(null);
169         assertEquals(UpdateStatus.CONNECTION_ERROR, result, "Valid update");
170     }
171
172     @Test
173     public void testInternalPMSensor() {
174         ThingMock t = new ThingMock();
175
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);
180
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(),
189                     "PM100");
190         } else {
191             assertTrue(false);
192         }
193     }
194 }