]> git.basschouten.com Git - openhab-addons.git/blob
7cebb0323b192f4f6b6dae37463afefb92ef5483
[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.Arrays;
22
23 import org.junit.jupiter.api.BeforeAll;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.lametrictime.api.common.impl.GsonGenerator;
26 import org.openhab.binding.lametrictime.api.test.AbstractTest;
27
28 import com.google.gson.Gson;
29
30 public class FrameTest extends AbstractTest
31 {
32     private static Gson gson;
33
34     @BeforeAll
35     public static void setUpBeforeClass()
36     {
37         gson = GsonGenerator.create(true);
38     }
39
40     @Test
41     public void testSerializeSimple() throws Exception
42     {
43         Frame frame = new Frame().withIcon("i87").withText("Hello world!");
44         assertEquals(readJson("frame-simple.json"), gson.toJson(frame));
45     }
46
47     @Test
48     public void testSerializeGoal() throws Exception
49     {
50         Frame frame = new Frame().withIcon("i120").withGoalData(new GoalData());
51         assertEquals(readJson("frame-goal.json"), gson.toJson(frame));
52     }
53
54     @Test
55     public void testSerializeChart() throws Exception
56     {
57         Frame frame = new Frame().withChartData(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
58         assertEquals(readJson("frame-chart.json"), gson.toJson(frame));
59     }
60
61     @Test
62     public void testDeserializeSimple() throws Exception
63     {
64         try (FileReader reader = new FileReader(getTestDataFile("frame-simple.json")))
65         {
66             Frame frame = gson.fromJson(reader, Frame.class);
67             assertEquals("i87", frame.getIcon());
68             assertEquals("Hello world!", frame.getText());
69             assertEquals(null, frame.getGoalData());
70             assertEquals(null, frame.getChartData());
71         }
72     }
73
74     @Test
75     public void testDeserializeGoal() throws Exception
76     {
77         try (FileReader reader = new FileReader(getTestDataFile("frame-goal.json")))
78         {
79             Frame frame = gson.fromJson(reader, Frame.class);
80             assertEquals("i120", frame.getIcon());
81             assertEquals(new GoalData(), frame.getGoalData());
82         }
83     }
84
85     @Test
86     public void testDeserializeChart() throws Exception
87     {
88         try (FileReader reader = new FileReader(getTestDataFile("frame-chart.json")))
89         {
90             Frame frame = gson.fromJson(reader, Frame.class);
91             assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7), frame.getChartData());
92         }
93     }
94 }