2 * Copyright 2017-2018 Gregory Moyer and contributors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openhab.binding.lametrictime.api.local.model;
18 import static org.junit.jupiter.api.Assertions.*;
20 import java.io.FileReader;
21 import java.util.Iterator;
22 import java.util.SortedMap;
23 import java.util.TreeMap;
25 import org.junit.jupiter.api.BeforeAll;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.lametrictime.api.common.impl.GsonGenerator;
28 import org.openhab.binding.lametrictime.api.test.AbstractTest;
30 import com.google.gson.Gson;
32 public class ApplicationTest extends AbstractTest
34 private static Gson gson;
37 public static void setUpBeforeClass()
39 gson = GsonGenerator.create(true);
43 @SuppressWarnings("serial")
44 public void testSerializeAllFields() throws Exception
46 Application app = new Application().withPackageName("com.lametric.radio")
47 .withVendor("LaMetric")
48 .withVersion("1.0.10")
49 .withVersionCode("22")
51 .withWidgets(new TreeMap<String, Widget>(){{put("589ed1b3fcdaa5180bf4848e55ba8061", new Widget());}})
52 .withActions(new TreeMap<String, Action>(){{put("radio.next", new Action());
53 put("radio.play", new Action());
54 put("radio.prev", new Action());
55 put("radio.stop", new Action());}});
57 assertEquals(readJson("application-all.json"), gson.toJson(app));
61 public void testSerializeNullLists() throws Exception
63 Application app = new Application().withPackageName("com.lametric.radio")
64 .withVendor("LaMetric")
65 .withVersion("1.0.10")
66 .withVersionCode("22");
67 assertEquals(readJson("application-null-maps.json"), gson.toJson(app));
71 public void testDeserializeAllFields() throws Exception
73 try (FileReader reader = new FileReader(getTestDataFile("application-all.json")))
75 Application app = gson.fromJson(reader, Application.class);
76 assertEquals("com.lametric.radio", app.getPackageName());
77 assertEquals("LaMetric", app.getVendor());
78 assertEquals("1.0.10", app.getVersion());
79 assertEquals("22", app.getVersionCode());
81 SortedMap<String, Widget> widgets = app.getWidgets();
82 assertNotNull(widgets);
83 assertEquals(1, widgets.size());
84 assertEquals("589ed1b3fcdaa5180bf4848e55ba8061", widgets.keySet().iterator().next());
86 SortedMap<String, Action> actions = app.getActions();
87 assertNotNull(actions);
88 assertEquals(4, actions.size());
90 Iterator<String> actionsIter = actions.keySet().iterator();
91 assertEquals("radio.next", actionsIter.next());
92 assertEquals("radio.play", actionsIter.next());
93 assertEquals("radio.prev", actionsIter.next());
94 assertEquals("radio.stop", actionsIter.next());
99 public void testDeserializeNullLists() throws Exception
101 try (FileReader reader = new FileReader(getTestDataFile("application-null-maps.json")))
103 Application app = gson.fromJson(reader, Application.class);
104 assertEquals("com.lametric.radio", app.getPackageName());
105 assertEquals("LaMetric", app.getVendor());
106 assertEquals("1.0.10", app.getVersion());
107 assertEquals("22", app.getVersionCode());
108 assertNull(app.getWidgets());
109 assertNull(app.getActions());