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