]> git.basschouten.com Git - openhab-addons.git/blob
fd40a77e9c85c72b3d31307e45c8982cf36a1cb0
[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.*;
16
17 import java.io.FileReader;
18 import java.util.Iterator;
19 import java.util.SortedMap;
20 import java.util.TreeMap;
21
22 import org.junit.jupiter.api.BeforeAll;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.lametrictime.internal.api.common.impl.GsonGenerator;
25 import org.openhab.binding.lametrictime.internal.api.test.AbstractTest;
26
27 import com.google.gson.Gson;
28
29 /**
30  * application test.
31  *
32  * @author Gregory Moyer - Initial contribution
33  */
34 public class ApplicationTest extends AbstractTest {
35     private static Gson gson;
36
37     @BeforeAll
38     public static void setUpBeforeClass() {
39         gson = GsonGenerator.create(true);
40     }
41
42     @Test
43     @SuppressWarnings("serial")
44     public void testSerializeAllFields() throws Exception {
45         Application app = new Application().withPackageName("com.lametric.radio").withVendor("LaMetric")
46                 .withVersion("1.0.10").withVersionCode("22")
47                 // @formatter:off
48                                            .withWidgets(new TreeMap<String, Widget>(){{put("589ed1b3fcdaa5180bf4848e55ba8061", new Widget());}})
49                                            .withActions(new TreeMap<String, Action>(){{put("radio.next", new Action());
50                                                                                        put("radio.play", new Action());
51                                                                                        put("radio.prev", new Action());
52                                                                                        put("radio.stop", new Action());}});
53                                            // @formatter:on
54         assertEquals(readJson("application-all.json"), gson.toJson(app));
55     }
56
57     @Test
58     public void testSerializeNullLists() throws Exception {
59         Application app = new Application().withPackageName("com.lametric.radio").withVendor("LaMetric")
60                 .withVersion("1.0.10").withVersionCode("22");
61         assertEquals(readJson("application-null-maps.json"), gson.toJson(app));
62     }
63
64     @Test
65     public void testDeserializeAllFields() throws Exception {
66         try (FileReader reader = new FileReader(getTestDataFile("application-all.json"))) {
67             Application app = gson.fromJson(reader, Application.class);
68             assertEquals("com.lametric.radio", app.getPackageName());
69             assertEquals("LaMetric", app.getVendor());
70             assertEquals("1.0.10", app.getVersion());
71             assertEquals("22", app.getVersionCode());
72
73             SortedMap<String, Widget> widgets = app.getWidgets();
74             assertNotNull(widgets);
75             assertEquals(1, widgets.size());
76             assertEquals("589ed1b3fcdaa5180bf4848e55ba8061", widgets.keySet().iterator().next());
77
78             SortedMap<String, Action> actions = app.getActions();
79             assertNotNull(actions);
80             assertEquals(4, actions.size());
81
82             Iterator<String> actionsIter = actions.keySet().iterator();
83             assertEquals("radio.next", actionsIter.next());
84             assertEquals("radio.play", actionsIter.next());
85             assertEquals("radio.prev", actionsIter.next());
86             assertEquals("radio.stop", actionsIter.next());
87         }
88     }
89
90     @Test
91     public void testDeserializeNullLists() throws Exception {
92         try (FileReader reader = new FileReader(getTestDataFile("application-null-maps.json"))) {
93             Application app = gson.fromJson(reader, Application.class);
94             assertEquals("com.lametric.radio", app.getPackageName());
95             assertEquals("LaMetric", app.getVendor());
96             assertEquals("1.0.10", app.getVersion());
97             assertEquals("22", app.getVersionCode());
98             assertNull(app.getWidgets());
99             assertNull(app.getActions());
100         }
101     }
102 }