]> git.basschouten.com Git - openhab-addons.git/blob
db0b20fa1e61aeed0c14b6ecec46e0d95a7228c5
[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.mqtt.ruuvigateway.internal.parser;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.nio.ByteBuffer;
18 import java.nio.charset.StandardCharsets;
19 import java.time.Instant;
20 import java.util.Optional;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.mqtt.ruuvigateway.internal.parser.GatewayPayloadParser.GatewayPayload;
25
26 import com.google.gson.JsonSyntaxException;
27
28 /**
29  * Tests for {@link GatewayPayloadParser}
30  *
31  * @author Sami Salonen - Initial Contribution
32  */
33 @NonNullByDefault
34 public class GatewayPayloadParserTests {
35
36     private byte[] bytes(String str) {
37         ByteBuffer buffer = StandardCharsets.UTF_8.encode(str);
38         buffer.rewind();
39         byte[] bytes = new byte[buffer.remaining()];
40         buffer.get(bytes);
41         return bytes;
42     }
43
44     /**
45      * Test with valid data.
46      *
47      * See 'valid case' test vector from
48      * https://docs.ruuvi.com/communication/bluetooth-advertisements/data-format-5-rawv2
49      */
50     @Test
51     public void testValid() {
52         GatewayPayload parsed = GatewayPayloadParser.parse(bytes(//
53                 "{\"gw_mac\": \"DE:AD:BE:EF:00:00\","//
54                         + "  \"rssi\": -83,"//
55                         + "  \"aoa\": [],"//
56                         + "  \"gwts\": \"1659365438\","//
57                         + "  \"ts\": \"1659365439\","//
58                         + "  \"data\": \"0201061BFF99040512FC5394C37C0004FFFC040CAC364200CDCBB8334C884F\","//
59                         + "  \"coords\": \"\"" + "}"));
60         assertNotNull(parsed);
61         assertEquals(-83, parsed.rssi);
62         assertEquals(Optional.of(Instant.ofEpochSecond(1659365438)), parsed.gwts);
63         assertEquals(Optional.of(Instant.ofEpochSecond(1659365439)), parsed.ts);
64         assertEquals(24.3, parsed.measurement.getTemperature());
65         assertEquals(100044, parsed.measurement.getPressure());
66         assertEquals(5, parsed.measurement.getDataFormat());
67         assertEquals(53.49, parsed.measurement.getHumidity());
68         assertEquals(0.004, parsed.measurement.getAccelerationX());
69         assertEquals(-0.004, parsed.measurement.getAccelerationY());
70         assertEquals(1.036, parsed.measurement.getAccelerationZ());
71         assertEquals(4, parsed.measurement.getTxPower());
72         assertEquals(2.9770000000000003, parsed.measurement.getBatteryVoltage());
73         assertEquals(66, parsed.measurement.getMovementCounter());
74         assertEquals(205, parsed.measurement.getMeasurementSequenceNumber());
75     }
76
77     @Test
78     public void testInvalidJSON() {
79         assertThrows(JsonSyntaxException.class, () -> {
80             GatewayPayloadParser.parse(bytes(//
81                     "invalid json"));
82         });
83     }
84
85     @Test
86     public void testUnexpectedTypes() {
87         assertThrows(IllegalArgumentException.class, () -> {
88             GatewayPayloadParser.parse(bytes(//
89                     "{\"gw_mac\": \"DE:AD:BE:EF:00:00\","//
90                             + "  \"rssi\": -83,"//
91                             + "  \"aoa\": [],"//
92                             + "  \"gwts\": \"1659365438\","//
93                             + "  \"ts\": \"1659365438\","//
94                             + "  \"data\": 666," // should be hex-string of even length
95                             + "  \"coords\": \"\"" + "}"));
96         });
97     }
98
99     @Test
100     public void testInvalidHex() {
101         assertThrows(IllegalArgumentException.class, () -> {
102             GatewayPayloadParser.parse(bytes(//
103                     "{\"gw_mac\": \"DE:AD:BE:EF:00:00\","//
104                             + "  \"rssi\": -83,"//
105                             + "  \"aoa\": [],"//
106                             + "  \"gwts\": \"1659365438\","//
107                             + "  \"ts\": \"1659365438\","//
108                             + "  \"data\": \"XYZZ\"," // should be hex string
109                             + "  \"coords\": \"\"" + "}"));
110         });
111     }
112
113     @Test
114     public void testUnexpectedTypes3() {
115         assertThrows(JsonSyntaxException.class, () -> {
116             GatewayPayloadParser.parse(bytes(//
117                     """
118                             {"gw_mac": "DE:AD:BE:EF:00:00",\
119                               "rssi": "foobar",\
120                               "aoa": [],\
121                               "gwts": "1659365438",\
122                               "ts": "1659365438",\
123                               "data": "0201061BFF99040512FC5394C37C0004FFFC040CAC364200CDCBB8334C884F",\
124                               "coords": ""\
125                             }\
126                             """));
127         });
128     }
129
130     @Test
131     public void testDataTooShort() {
132         assertThrows(IllegalArgumentException.class, () -> {
133             GatewayPayloadParser.parse(bytes(//
134                     "{\"gw_mac\": \"DE:AD:BE:EF:00:00\","//
135                             + "  \"rssi\": -83," + "  \"aoa\": [],"//
136                             + "  \"gwts\": \"1659365438\","//
137                             + "  \"ts\": \"1659365438\","//
138                             + "  \"data\": \"0201061BFF990405\"," // too short
139                             + "  \"coords\": \"\"" + "}"));
140         });
141     }
142
143     @Test
144     public void testUnexpectedManufacturer() {
145         assertThrows(IllegalArgumentException.class, () -> {
146             GatewayPayloadParser.parse(bytes(//
147                     """
148                             {"gw_mac": "DE:AD:BE:EF:00:00",\
149                               "rssi": -83,\
150                               "aoa": [],\
151                               "gwts": "1659365438",\
152                               "ts": "1659365438",\
153                               "data": "0201061BFF99990512FC5394C37C0004FFFC040CAC364200CDCBB8334C884F",\
154                               "coords": ""\
155                             }\
156                             """));
157         });
158     }
159
160     @Test
161     public void testDataNotBluetoothAdvertisement() {
162         assertThrows(IllegalArgumentException.class, () -> {
163             GatewayPayloadParser.parse(bytes(//
164                     """
165                             {"gw_mac": "DE:AD:BE:EF:00:00",\
166                               "rssi": -83,\
167                               "aoa": [],\
168                               "gwts": "1659365438",\
169                               "ts": "1659365438",\
170                               "data": "0201061BAA99040512FC5394C37C0004FFFC040CAC364200CDCBB8334C884F",\
171                               "coords": ""\
172                             }\
173                             """));
174         });
175     }
176 }