]> git.basschouten.com Git - openhab-addons.git/blob
5d2be09909a78d530a508f347e49b23cf6e52212
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.luftdateninfo.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.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;
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.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, SmartHomeUnits.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM25Cache(),
115                     "PM25");
116             assertEquals(QuantityType.valueOf(5.2, SmartHomeUnits.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM100Cache(),
117                     "PM100");
118         } else {
119             assertTrue(false);
120         }
121     }
122
123     @Test
124     public void testInvalidUpdate() {
125         ThingMock t = new ThingMock();
126
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);
131
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(),
138                     "Values undefined");
139             assertEquals(QuantityType.valueOf(-1, SmartHomeUnits.MICROGRAM_PER_CUBICMETRE), pmHandler.getPM100Cache(),
140                     "Values undefined");
141         } else {
142             assertTrue(false);
143         }
144     }
145
146     @Test
147     public void testEmptyUpdate() {
148         ThingMock t = new ThingMock();
149
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);
154
155         PMHandlerExtension pmHandler = new PMHandlerExtension(t);
156         UpdateStatus result = pmHandler.updateChannels("[]");
157         assertEquals(UpdateStatus.VALUE_EMPTY, result, "Valid update");
158     }
159
160     @Test
161     public void testNullUpdate() {
162         ThingMock t = new ThingMock();
163
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);
168
169         PMHandlerExtension pmHandler = new PMHandlerExtension(t);
170         UpdateStatus result = pmHandler.updateChannels(null);
171         assertEquals(UpdateStatus.CONNECTION_ERROR, result, "Valid update");
172     }
173 }