]> git.basschouten.com Git - openhab-addons.git/blob
2f04c7794fd9d51ba79ed0a35ffc346bb6182ab6
[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 import java.time.LocalTime;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.surepetcare.internal.SurePetcareConstants;
23 import org.openhab.binding.surepetcare.internal.dto.SurePetcareDeviceControl;
24 import org.openhab.binding.surepetcare.internal.dto.SurePetcareDeviceCurfew;
25 import org.openhab.binding.surepetcare.internal.dto.SurePetcareDeviceCurfewList;
26
27 /**
28  * The {@link SurePetcareDeviceControlTest} class implements unit test case for {@link SurePetcareDeviceControl}
29  *
30  * @author Rene Scherer - Initial contribution
31  */
32 @NonNullByDefault
33 public class SurePetcareDeviceControlTest {
34
35     @Test
36     public void testJsonDeserializeCurfewArray() throws ParseException {
37         String testResponse = "{\"curfew\":[{\"enabled\":true,\"lock_time\":\"19:30\",\"unlock_time\":\"07:00\"}],\"locking\":0,\"fast_polling\":false}";
38         SurePetcareDeviceControl response = SurePetcareConstants.GSON.fromJson(testResponse,
39                 SurePetcareDeviceControl.class);
40         if (response != null) {
41             assertEquals(1, response.curfewList.size());
42             assertEquals(Integer.valueOf(0), response.lockingModeId);
43         } else {
44             fail("GSON returned null");
45         }
46     }
47
48     @Test
49     public void testJsonDeserializeSingleCurfew() throws ParseException {
50         String testResponse = "{\"curfew\":{\"enabled\":true,\"lock_time\":\"19:00\",\"unlock_time\":\"08:00\"},\"fast_polling\":true}";
51
52         SurePetcareDeviceControl response = SurePetcareConstants.GSON.fromJson(testResponse,
53                 SurePetcareDeviceControl.class);
54         if (response != null) {
55             assertEquals(1, response.curfewList.size());
56             assertEquals(true, response.fastPolling);
57         } else {
58             fail("GSON returned null");
59         }
60     }
61
62     @Test
63     public void testJsonSerializeLockingMode() throws ParseException {
64         SurePetcareDeviceControl control = new SurePetcareDeviceControl();
65         control.lockingModeId = Integer.valueOf(4);
66
67         String json = SurePetcareConstants.GSON.toJson(control);
68         assertEquals("{\"locking\":4}", json);
69     }
70
71     @Test
72     public void testJsonSerializeCurfew() throws ParseException {
73         SurePetcareDeviceControl control = new SurePetcareDeviceControl();
74         SurePetcareDeviceCurfewList curfews = new SurePetcareDeviceCurfewList();
75         curfews.add(new SurePetcareDeviceCurfew(true, LocalTime.of(19, 30, 00), LocalTime.of(07, 00, 00)));
76         control.curfewList = curfews;
77
78         String json = SurePetcareConstants.GSON.toJson(control);
79         assertEquals("{\"curfew\":[{\"enabled\":true,\"lock_time\":\"19:30\",\"unlock_time\":\"07:00\"}]}", json);
80     }
81 }