]> git.basschouten.com Git - openhab-addons.git/blob
922c4b83cc83eef4501b37590371d3beffb94f1f
[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.lametrictime.internal.api.local.dto;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16
17 import java.io.FileReader;
18 import java.util.HashMap;
19
20 import org.junit.jupiter.api.BeforeAll;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.lametrictime.internal.api.common.impl.GsonGenerator;
23 import org.openhab.binding.lametrictime.internal.api.test.AbstractTest;
24
25 import com.google.gson.Gson;
26 import com.google.gson.JsonPrimitive;
27
28 /**
29  * widget test.
30  *
31  * @author Gregory Moyer - Initial contribution
32  */
33 public class WidgetTest extends AbstractTest {
34     private static Gson gson;
35
36     @BeforeAll
37     public static void setUpBeforeClass() {
38         gson = GsonGenerator.create(true);
39     }
40
41     @Test
42     @SuppressWarnings("serial")
43     public void testSerialize() throws Exception {
44         Widget widget = new Widget().withPackageName("com.lametric.radio").withIndex(Integer.valueOf(-1))
45                 .withSettings(new HashMap<String, JsonPrimitive>() {
46                     {
47                         put("_title", new JsonPrimitive("Radio"));
48                     }
49                 });
50         assertEquals(readJson("widget.json"), gson.toJson(widget));
51     }
52
53     @Test
54     @SuppressWarnings("serial")
55     public void testDeserialize() throws Exception {
56         try (FileReader reader = new FileReader(getTestDataFile("widget.json"))) {
57             Widget widget = gson.fromJson(reader, Widget.class);
58             assertEquals("com.lametric.radio", widget.getPackageName());
59             assertEquals(Integer.valueOf(-1), widget.getIndex());
60             assertEquals(new HashMap<String, JsonPrimitive>() {
61                 {
62                     put("_title", new JsonPrimitive("Radio"));
63                 }
64             }, widget.getSettings());
65         }
66     }
67 }