]> git.basschouten.com Git - openhab-addons.git/blob
bf3d88a40a1de9329c44883e16048eca72415d4b
[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.salus.internal.rest;
14
15 import static org.assertj.core.api.Assertions.assertThat;
16
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Optional;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23
24 /**
25  * @author Martin GrzeĊ›lowski - Initial contribution
26  */
27 @NonNullByDefault
28 public class GsonMapperTest {
29
30     // Can serialize login parameters to JSON
31     @Test
32     public void testSerializeLoginParametersToJson() {
33         // Given
34         GsonMapper gsonMapper = GsonMapper.INSTANCE;
35         String username = "test@example.com";
36         char[] password = "password".toCharArray();
37         String expectedJson1 = "{\"user\":{\"email\":\"test@example.com\",\"password\":\"password\"}}";
38         String expectedJson2 = "{\"user\":{\"password\":\"password\",\"email\":\"test@example.com\"}}";
39
40         // When
41         String json = gsonMapper.loginParam(username, password);
42
43         // Then
44         assertThat(json).isIn(expectedJson1, expectedJson2);
45     }
46
47     // Can deserialize authentication token from JSON
48     @Test
49     public void testDeserializeAuthenticationTokenFromJson() {
50         // Given
51         GsonMapper gsonMapper = GsonMapper.INSTANCE;
52         String json = "{\"access_token\":\"token\",\"refresh_token\":\"refresh\",\"expires_in\":3600,\"role\":\"admin\"}";
53         AuthToken expectedAuthToken = new AuthToken("token", "refresh", 3600L, "admin");
54
55         // When
56         AuthToken authToken = gsonMapper.authToken(json);
57
58         // Then
59         assertThat(authToken).isEqualTo(expectedAuthToken);
60     }
61
62     // Can parse list of devices from JSON
63     @Test
64     public void testParseListOfDevicesFromJson() {
65         // Given
66         GsonMapper gsonMapper = GsonMapper.INSTANCE;
67         String json = "[{\"device\":{\"dsn\":\"123\",\"product_name\":\"Product 1\"}},{\"device\":{\"dsn\":\"456\",\"product_name\":\"Product 2\"}}]";
68         List<Device> expectedDevices = List.of(new Device("123", "Product 1", Collections.emptyMap()),
69                 new Device("456", "Product 2", Collections.emptyMap()));
70
71         // When
72         List<Device> devices = gsonMapper.parseDevices(json);
73
74         // Then
75         assertThat(devices).isEqualTo(expectedDevices);
76     }
77
78     // Returns empty list when parsing invalid JSON for devices
79     @Test
80     public void testReturnsEmptyListWhenParsingInvalidJsonForDevices() {
81         // Given
82         GsonMapper gsonMapper = GsonMapper.INSTANCE;
83         String json = "invalid json";
84
85         // When
86         List<Device> devices = gsonMapper.parseDevices(json);
87
88         // Then
89         assertThat(devices).isEmpty();
90     }
91
92     // Returns empty list when parsing invalid JSON for device properties
93     @Test
94     public void testReturnsEmptyListWhenParsingInvalidJsonForDeviceProperties() {
95         // Given
96         GsonMapper gsonMapper = GsonMapper.INSTANCE;
97         String json = "invalid json";
98
99         // When
100         List<DeviceProperty<?>> deviceProperties = gsonMapper.parseDeviceProperties(json);
101
102         // Then
103         assertThat(deviceProperties).isEmpty();
104     }
105
106     // Returns empty optional when parsing invalid JSON for datapoint value
107     @Test
108     public void testReturnsEmptyOptionalWhenParsingInvalidJsonForDatapointValue() {
109         // Given
110         GsonMapper gsonMapper = GsonMapper.INSTANCE;
111         String json = "invalid json";
112
113         // When
114         Optional<Object> datapointValue = gsonMapper.datapointValue(json);
115
116         // Then
117         assertThat(datapointValue).isEmpty();
118     }
119 }