]> git.basschouten.com Git - openhab-addons.git/blob
d06878667192f501c8c664edc837ead5aa078311
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.airgradient.internal.handler;
14
15 import static org.eclipse.jdt.annotation.Checks.requireNonNull;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.verify;
18 import static org.openhab.binding.airgradient.internal.AirGradientBindingConstants.*;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.Mockito;
25 import org.openhab.binding.airgradient.internal.model.Measure;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.ThingHandlerCallback;
31 import org.openhab.core.types.UnDefType;
32
33 /**
34  * @author Jørgen Austvik - Initial contribution
35  */
36 @SuppressWarnings({ "null" })
37 @NonNullByDefault
38 public class AirGradientLocationHandlerTest {
39
40     private static final Measure TEST_MEASURE = new Measure() {
41         {
42             locationId = "12345";
43             locationName = "Location name";
44             pm01 = 2d;
45             pm02 = 3d;
46             pm10 = 4d;
47             pm003Count = 636d;
48             atmp = 19.63;
49             rhum = null;
50             rco2 = 455d;
51             tvoc = 51.644928;
52             wifi = -59d;
53             timestamp = "2024-01-07T11:28:56.000Z";
54             serialno = "ecda3b1a2a50";
55             firmwareVersion = "12345";
56             tvocIndex = 1d;
57             noxIndex = 2d;
58         }
59     };
60
61     @Nullable
62     private AirGradientLocationHandler sut;
63
64     @Nullable
65     private ThingHandlerCallback callbackMock;
66
67     @Nullable
68     private Thing thing;
69
70     @BeforeEach
71     public void setUp() {
72         callbackMock = Mockito.mock(ThingHandlerCallback.class);
73         Mockito.when(callbackMock.isChannelLinked(any(ChannelUID.class))).thenReturn(true);
74         thing = Mockito.mock(Thing.class);
75
76         sut = new AirGradientLocationHandler(requireNonNull(thing));
77         sut.setCallback(callbackMock);
78
79         Mockito.when(thing.getUID()).thenReturn(new ThingUID(THING_TYPE_LOCATION, "1234"));
80     }
81
82     @Test
83     public void testSetMeasure() {
84         sut.setCallback(callbackMock);
85         sut.setMeasurment(TEST_MEASURE);
86
87         verify(callbackMock).stateUpdated(new ChannelUID(sut.getThing().getUID(), CHANNEL_WIFI),
88                 new QuantityType<>("-59 dBm"));
89         verify(callbackMock).stateUpdated(new ChannelUID(sut.getThing().getUID(), CHANNEL_PM_01),
90                 new QuantityType<>("2 µg/m³"));
91         verify(callbackMock).stateUpdated(new ChannelUID(sut.getThing().getUID(), CHANNEL_PM_02),
92                 new QuantityType<>("3 µg/m³"));
93         verify(callbackMock).stateUpdated(new ChannelUID(sut.getThing().getUID(), CHANNEL_PM_10),
94                 new QuantityType<>("4 µg/m³"));
95         verify(callbackMock).stateUpdated(new ChannelUID(sut.getThing().getUID(), CHANNEL_PM_003_COUNT),
96                 new QuantityType<>("636"));
97         verify(callbackMock).stateUpdated(new ChannelUID(sut.getThing().getUID(), CHANNEL_ATMP),
98                 new QuantityType<>("19.63 °C"));
99         verify(callbackMock).stateUpdated(new ChannelUID(sut.getThing().getUID(), CHANNEL_RHUM), UnDefType.NULL);
100         verify(callbackMock).stateUpdated(new ChannelUID(sut.getThing().getUID(), CHANNEL_RCO2),
101                 new QuantityType<>("455 ppm"));
102         verify(callbackMock).stateUpdated(new ChannelUID(sut.getThing().getUID(), CHANNEL_TVOC),
103                 new QuantityType<>("51 ppb"));
104     }
105 }