]> git.basschouten.com Git - openhab-addons.git/blob
1f03333142f6721974fb0d86f5a7f385f8abb458
[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.surepetcare.internal.data;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.text.ParseException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.surepetcare.internal.SurePetcareConstants;
22 import org.openhab.binding.surepetcare.internal.dto.SurePetcareDevice;
23
24 /**
25  * The {@link SurePetcareDeviceTest} class implements unit test case for {@link SurePetcareDevice}
26  *
27  * @author Rene Scherer - Initial contribution
28  */
29 @NonNullByDefault
30 public class SurePetcareDeviceTest {
31
32     @Test
33     public void testJsonDeserializeHub1() throws ParseException {
34         String testResponse = "{\"id\":296464,\"product_id\":1,\"household_id\":48712,\"name\":\"Home Hub\",\"serial_number\":\"H008-0296432\",\"mac_address\":\"00000491630A0D64\",\"version\":\"NjA=\",\"created_at\":\"2019-04-18T14:45:11+00:00\",\"updated_at\":\"2019-09-30T12:31:52+00:00\",\"control\":{\"led_mode\":4,\"pairing_mode\":0},\"status\":{\"led_mode\":4,\"pairing_mode\":0,\"version\":{\"device\":{\"hardware\":3,\"firmware\":1.772}},\"online\":true}}";
35         SurePetcareDevice response = SurePetcareConstants.GSON.fromJson(testResponse, SurePetcareDevice.class);
36
37         if (response != null) {
38             assertEquals(Long.valueOf(296464L), response.id);
39             assertEquals(Integer.valueOf(1), response.productId);
40             assertEquals(Long.valueOf(48712), response.householdId);
41             assertEquals("Home Hub", response.name);
42             assertEquals("H008-0296432", response.serialNumber);
43             assertEquals("00000491630A0D64", response.macAddress);
44             assertEquals("NjA=", response.version);
45             assertEquals(Integer.valueOf(4), response.control.ledModeId);
46             assertEquals(Integer.valueOf(0), response.control.pairingModeId);
47             assertEquals(Integer.valueOf(4), response.status.ledModeId);
48             assertEquals(Integer.valueOf(0), response.status.pairingModeId);
49             assertEquals("3", response.status.version.device.hardware);
50             assertEquals("1.772", response.status.version.device.firmware);
51         } else {
52             fail("GSON returned null");
53         }
54     }
55
56     @Test
57     public void testJsonDeserializeHub2() throws ParseException {
58         String testResponse = "{\"id\":101797,\"product_id\":1,\"household_id\":21005,\"name\":\"Home Hub\",\"serial_number\":\"H005-0101321\",\"mac_address\":\"0000801F1341F1C7\",\"version\":\"NzAzNg==\",\"created_at\":\"2018-05-18T11:11:59+00:00\",\"updated_at\":\"2020-05-01T07:51:32+00:00\",\"control\":{\"led_mode\":4,\"pairing_mode\":0},\"status\":{\"led_mode\":4,\"pairing_mode\":0,\"version\":{\"device\":{\"hardware\":3,\"firmware\":2.43}},\"online\":true}}";
59         SurePetcareDevice response = SurePetcareConstants.GSON.fromJson(testResponse, SurePetcareDevice.class);
60
61         if (response != null) {
62             assertEquals(Long.valueOf(101797), response.id);
63             assertEquals(Integer.valueOf(1), response.productId);
64             assertEquals(Long.valueOf(21005), response.householdId);
65             assertEquals("Home Hub", response.name);
66             assertEquals("H005-0101321", response.serialNumber);
67             assertEquals("0000801F1341F1C7", response.macAddress);
68             assertEquals("NzAzNg==", response.version);
69             assertEquals(Integer.valueOf(4), response.control.ledModeId);
70             assertEquals(Integer.valueOf(0), response.control.pairingModeId);
71             assertEquals(Integer.valueOf(4), response.status.ledModeId);
72             assertEquals(Integer.valueOf(0), response.status.pairingModeId);
73             assertEquals("3", response.status.version.device.hardware);
74             assertEquals("2.43", response.status.version.device.firmware);
75         } else {
76             fail("GSON returned null");
77         }
78     }
79
80     @Test
81     public void testJsonDeserializeCatFlap() throws ParseException {
82         String testResponse = "{\"id\":318966,\"parent_device_id\":296464,\"product_id\":6,\"household_id\":48712,\"name\":\"Back Door Cat Flap\",\"serial_number\":\"N005-0089709\",\"mac_address\":\"6D5E01CFF9D5B370\",\"index\":0,\"version\":\"MTE5\",\"created_at\":\"2019-05-13T14:09:18+00:00\",\"updated_at\":\"2019-10-01T07:37:20+00:00\",\"pairing_at\":\"2019-09-02T08:24:13+00:00\",\"control\":{\"curfew\":[{\"enabled\":true,\"lock_time\":\"19:30\",\"unlock_time\":\"07:00\"}],\"locking\":0,\"fast_polling\":false},\"parent\":{\"id\":296464,\"product_id\":1,\"household_id\":48712,\"name\":\"Home Hub\",\"serial_number\":\"H008-0296464\",\"mac_address\":\"00000491620A0F60\",\"version\":\"NjE=\",\"created_at\":\"2019-04-18T14:45:11+00:00\",\"updated_at\":\"2019-10-01T07:37:20+00:00\"},\"status\":{\"locking\":{\"mode\":0},\"version\":{\"device\":{\"hardware\":9,\"firmware\":335}},\"battery\":5.771,\"learn_mode\":null,\"online\":true,\"signal\":{\"device_rssi\":-87.25,\"hub_rssi\":-83.5}},\"tags\":[{\"id\":60456,\"index\":0,\"profile\":2,\"version\":\"MA==\",\"created_at\":\"2019-09-02T09:27:17+00:00\",\"updated_at\":\"2019-09-02T09:27:23+00:00\"}]}";
83         SurePetcareDevice response = SurePetcareConstants.GSON.fromJson(testResponse, SurePetcareDevice.class);
84
85         if (response != null) {
86             response.getThingProperties();
87             assertEquals(Long.valueOf(318966), response.id);
88             assertEquals(Integer.valueOf(6), response.productId);
89             assertEquals(Long.valueOf(48712), response.householdId);
90             assertEquals("Back Door Cat Flap", response.name);
91             assertEquals("N005-0089709", response.serialNumber);
92             assertEquals("6D5E01CFF9D5B370", response.macAddress);
93             assertEquals("9", response.status.version.device.hardware);
94             assertEquals("335", response.status.version.device.firmware);
95             assertEquals("MTE5", response.version);
96             assertEquals(Integer.valueOf(0), response.status.locking.modeId);
97         } else {
98             fail("GSON returned null");
99         }
100     }
101
102     @Test
103     public void testJsonDeserializePetFlap() throws ParseException {
104         String testResponse = "{\"id\":318966,\"parent_device_id\":296464,\"product_id\":3,\"household_id\":48712,\"name\":\"Back Door Cat Flap\",\"mac_address\":\"6D5E01CFF9D5B370\",\"index\":0,\"version\":\"MjYxMw==\",\"created_at\":\"2019-03-02T14:54:30+00:00\",\"updated_at\":\"2020-05-01T07:51:32+00:00\",\"pairing_at\":\"2019-06-18T19:54:34+00:00\",\"control\":{\"curfew\":{\"enabled\":true,\"lock_time\":\"19:00\",\"unlock_time\":\"08:00\"},\"fast_polling\":true},\"parent\":{\"id\":101797,\"product_id\":1,\"household_id\":21005,\"name\":\"Salem\",\"serial_number\":\"N005-0089709\",\"mac_address\":\"0000801F1221F1C2\",\"version\":\"NzAzNg==\",\"created_at\":\"2018-05-18T11:11:59+00:00\",\"updated_at\":\"2020-05-01T07:51:32+00:00\"},\"status\":{\"battery\":5.864999999999999,\"locking\":{\"mode\":4,\"curfew\":{\"delay_time\":0,\"lock_time\":\"19:00\",\"permission\":2,\"unlock_time\":\"08:00\",\"locked\":false}},\"version\":{\"lcd\":{\"hardware\":1,\"firmware\":1},\"rf\":{\"hardware\":4,\"firmware\":0.16}},\"learn_mode\":false,\"online\":true,\"signal\":{\"device_rssi\":-88.33333333333333,\"hub_rssi\":-86}},\"tags\":[{\"id\":24725,\"index\":0,\"version\":\"MA==\",\"created_at\":\"2019-06-18T19:54:42+00:00\",\"updated_at\":\"2020-03-11T16:06:58+00:00\"}]}";
105         SurePetcareDevice response = SurePetcareConstants.GSON.fromJson(testResponse, SurePetcareDevice.class);
106
107         if (response != null) {
108             response.getThingProperties();
109             assertEquals(Long.valueOf(318966), response.id);
110             assertEquals(Integer.valueOf(3), response.productId);
111             assertEquals(Long.valueOf(48712), response.householdId);
112             assertEquals("Back Door Cat Flap", response.name);
113             assertNull(response.serialNumber);
114             assertEquals("6D5E01CFF9D5B370", response.macAddress);
115             assertEquals("1", response.status.version.lcd.hardware);
116             assertEquals("1", response.status.version.lcd.firmware);
117             assertEquals("4", response.status.version.rf.hardware);
118             assertEquals("0.16", response.status.version.rf.firmware);
119             assertEquals("MjYxMw==", response.version);
120             assertEquals(Integer.valueOf(4), response.status.locking.modeId);
121         } else {
122             fail("GSON returned null");
123         }
124     }
125 }