]> git.basschouten.com Git - openhab-addons.git/blob
a450205d4fd2d97ad73781e08ece4eb3b39ca9f2
[openhab-addons.git] /
1 /**
2  * Copyright 2017-2018 Gregory Moyer and contributors.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openhab.binding.lametrictime.api.local.model;
17
18 import static org.junit.jupiter.api.Assertions.*;
19
20 import java.io.FileReader;
21 import java.util.Iterator;
22 import java.util.SortedMap;
23 import java.util.TreeMap;
24
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;
29
30 import com.google.gson.Gson;
31
32 public class ApplicationTest extends AbstractTest
33 {
34     private static Gson gson;
35
36     @BeforeAll
37     public static void setUpBeforeClass()
38     {
39         gson = GsonGenerator.create(true);
40     }
41
42     @Test
43     @SuppressWarnings("serial")
44     public void testSerializeAllFields() throws Exception
45     {
46         Application app = new Application().withPackageName("com.lametric.radio")
47                                            .withVendor("LaMetric")
48                                            .withVersion("1.0.10")
49                                            .withVersionCode("22")
50                                            // @formatter:off
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());}});
56                                            // @formatter:on
57         assertEquals(readJson("application-all.json"), gson.toJson(app));
58     }
59
60     @Test
61     public void testSerializeNullLists() throws Exception
62     {
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));
68     }
69
70     @Test
71     public void testDeserializeAllFields() throws Exception
72     {
73         try (FileReader reader = new FileReader(getTestDataFile("application-all.json")))
74         {
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());
80
81             SortedMap<String, Widget> widgets = app.getWidgets();
82             assertNotNull(widgets);
83             assertEquals(1, widgets.size());
84             assertEquals("589ed1b3fcdaa5180bf4848e55ba8061", widgets.keySet().iterator().next());
85
86             SortedMap<String, Action> actions = app.getActions();
87             assertNotNull(actions);
88             assertEquals(4, actions.size());
89
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());
95         }
96     }
97
98     @Test
99     public void testDeserializeNullLists() throws Exception
100     {
101         try (FileReader reader = new FileReader(getTestDataFile("application-null-maps.json")))
102         {
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());
110         }
111     }
112 }