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.lametrictime.internal.api.local.dto;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.io.FileReader;
18 import java.util.Iterator;
19 import java.util.SortedMap;
20 import java.util.TreeMap;
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;
27 import com.google.gson.Gson;
32 * @author Gregory Moyer - Initial contribution
34 public class ApplicationTest extends AbstractTest {
35 private static Gson gson;
38 public static void setUpBeforeClass() {
39 gson = GsonGenerator.create(true);
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")
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());}});
54 assertEquals(readJson("application-all.json"), gson.toJson(app));
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));
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());
73 SortedMap<String, Widget> widgets = app.getWidgets();
74 assertNotNull(widgets);
75 assertEquals(1, widgets.size());
76 assertEquals("589ed1b3fcdaa5180bf4848e55ba8061", widgets.keySet().iterator().next());
78 SortedMap<String, Action> actions = app.getActions();
79 assertNotNull(actions);
80 assertEquals(4, actions.size());
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());
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());