2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.salus.internal.rest;
15 import static org.assertj.core.api.Assertions.assertThat;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
25 * @author Martin GrzeĊlowski - Initial contribution
28 public class GsonMapperTest {
30 // Can serialize login parameters to JSON
32 public void testSerializeLoginParametersToJson() {
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\"}}";
41 String json = gsonMapper.loginParam(username, password);
44 assertThat(json).isIn(expectedJson1, expectedJson2);
47 // Can deserialize authentication token from JSON
49 public void testDeserializeAuthenticationTokenFromJson() {
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");
56 AuthToken authToken = gsonMapper.authToken(json);
59 assertThat(authToken).isEqualTo(expectedAuthToken);
62 // Can parse list of devices from JSON
64 public void testParseListOfDevicesFromJson() {
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()));
72 List<Device> devices = gsonMapper.parseDevices(json);
75 assertThat(devices).isEqualTo(expectedDevices);
78 // Returns empty list when parsing invalid JSON for devices
80 public void testReturnsEmptyListWhenParsingInvalidJsonForDevices() {
82 GsonMapper gsonMapper = GsonMapper.INSTANCE;
83 String json = "invalid json";
86 List<Device> devices = gsonMapper.parseDevices(json);
89 assertThat(devices).isEmpty();
92 // Returns empty list when parsing invalid JSON for device properties
94 public void testReturnsEmptyListWhenParsingInvalidJsonForDeviceProperties() {
96 GsonMapper gsonMapper = GsonMapper.INSTANCE;
97 String json = "invalid json";
100 List<DeviceProperty<?>> deviceProperties = gsonMapper.parseDeviceProperties(json);
103 assertThat(deviceProperties).isEmpty();
106 // Returns empty optional when parsing invalid JSON for datapoint value
108 public void testReturnsEmptyOptionalWhenParsingInvalidJsonForDatapointValue() {
110 GsonMapper gsonMapper = GsonMapper.INSTANCE;
111 String json = "invalid json";
114 Optional<Object> datapointValue = gsonMapper.datapointValue(json);
117 assertThat(datapointValue).isEmpty();