]> git.basschouten.com Git - openhab-addons.git/blob
71e2fcf5c64b83b45b3865c4cdb363b8caab9564
[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.foobot.internal.handler;
14
15 import static org.junit.Assert.*;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19
20 import org.apache.commons.io.IOUtils;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.openhab.binding.foobot.internal.FoobotApiConnector;
24 import org.openhab.binding.foobot.internal.FoobotApiException;
25 import org.openhab.binding.foobot.internal.json.FoobotJsonData;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.SIUnits;
29 import org.openhab.core.thing.Thing;
30
31 /**
32  * Unit test for {@link FoobotDeviceHandler}.
33  *
34  * @author Hilbrand Bouwkamp - Initial contribution
35  */
36 public class FoobotDeviceHandlerTest {
37
38     private @Mock Thing thing;
39     private final FoobotApiConnector connector = new FoobotApiConnector() {
40         @Override
41         protected String request(String url, String apiKey) throws FoobotApiException {
42             try (InputStream stream = getClass().getResourceAsStream("../sensors.json")) {
43                 return IOUtils.toString(stream);
44             } catch (IOException e) {
45                 throw new AssertionError(e.getMessage());
46             }
47         };
48     };
49     private final FoobotDeviceHandler handler = new FoobotDeviceHandler(thing, connector);
50
51     @Test
52     public void testSensorDataToState() throws IOException, FoobotApiException {
53         final FoobotJsonData sensorData = connector.getSensorData("1234");
54
55         assertNotNull("No sensor data read", sensorData);
56         assertEquals(handler.sensorDataToState("temperature", sensorData), new QuantityType(12.345, SIUnits.CELSIUS));
57         assertEquals(handler.sensorDataToState("gpi", sensorData), new DecimalType(5.6789012));
58     }
59 }