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.foobot.internal.handler;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.nio.charset.StandardCharsets;
20 import java.util.Objects;
22 import org.junit.jupiter.api.Test;
23 import org.mockito.Mock;
24 import org.openhab.binding.foobot.internal.FoobotApiConnector;
25 import org.openhab.binding.foobot.internal.FoobotApiException;
26 import org.openhab.binding.foobot.internal.json.FoobotJsonData;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.SIUnits;
30 import org.openhab.core.thing.Thing;
33 * Unit test for {@link FoobotDeviceHandler}.
35 * @author Hilbrand Bouwkamp - Initial contribution
37 public class FoobotDeviceHandlerTest {
39 private @Mock Thing thing;
40 private final FoobotApiConnector connector = new FoobotApiConnector() {
42 protected String request(String url, String apiKey) throws FoobotApiException {
43 try (InputStream stream = getClass().getResourceAsStream("../sensors.json")) {
44 return new String(stream.readAllBytes(), StandardCharsets.UTF_8);
45 } catch (IOException e) {
46 throw new AssertionError(e.getMessage());
50 private final FoobotDeviceHandler handler = new FoobotDeviceHandler(thing, connector);
53 public void testSensorDataToState() throws IOException, FoobotApiException {
54 final FoobotJsonData sensorData = connector.getSensorData("1234");
56 assertNotNull(sensorData, "No sensor data read");
57 Objects.requireNonNull(sensorData);
58 assertEquals(handler.sensorDataToState("temperature", sensorData), new QuantityType(12.345, SIUnits.CELSIUS));
59 assertEquals(handler.sensorDataToState("gpi", sensorData), new DecimalType(5.6789012));