]> git.basschouten.com Git - openhab-addons.git/blob
d5a38051ffc7447542438f58368e720ee70d2dfa
[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.List;
18 import java.util.Objects;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.sensorcommunity.internal.dto.SensorDataValue;
23 import org.openhab.binding.sensorcommunity.internal.handler.HTTPHandler;
24 import org.openhab.binding.sensorcommunity.internal.util.FileReader;
25 import org.openhab.binding.sensorcommunity.internal.utils.Constants;
26
27 /**
28  * The {@link HTTPHandlerValueTest} test values decoding of HTTPHandler
29  *
30  * @author Bernd Weymann - Initial contribution
31  */
32 @NonNullByDefault
33 public class HTTPHandlerValueTest {
34     private HTTPHandler http = new HTTPHandler();
35
36     /**
37      * test if really the latest values are returned
38      * resource1 is json with ordering according to time while resource2 the entries flipped
39      */
40     @Test
41     public void testValueDecoding() {
42         String resource1 = FileReader.readFileInString("src/test/resources/condition-result-no-pressure.json");
43         assertNotNull(resource1);
44         Objects.requireNonNull(resource1);
45         List<SensorDataValue> l = http.getLatestValues(resource1);
46         assertNotNull(l);
47         Objects.requireNonNull(l);
48         l.forEach(sd -> {
49             testSensorValue(sd);
50         });
51
52         String resource2 = FileReader
53                 .readFileInString("src/test/resources/condition-result-no-pressure-flipped-values.json");
54         assertNotNull(resource2);
55         Objects.requireNonNull(resource2);
56         l = http.getLatestValues(resource2);
57         assertNotNull(l);
58         Objects.requireNonNull(l);
59         l.forEach(sd -> {
60             testSensorValue(sd);
61         });
62     }
63
64     private void testSensorValue(SensorDataValue s) {
65         if (s.getValueType().equals(Constants.TEMPERATURE)) {
66             assertEquals("22.70", s.getValue(), "Temperature resource 1");
67         } else if (s.getValueType().equals(Constants.HUMIDITY)) {
68             assertEquals("61.00", s.getValue(), "Humidity resource 1");
69         } else {
70             assertTrue(false);
71         }
72         // System.out.println(s.getValue_type() + ":" + s.getValue());
73     }
74 }