]> git.basschouten.com Git - openhab-addons.git/blob
a8c71a2e836eefa00f3aebf7dc39afcefb1e5fd5
[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 ActionTest 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 testSerialize() throws Exception
45     {
46         // @formatter:off
47         Action action = new Action().withParameters(new TreeMap<String, Parameter>(){{put("enabled", new BooleanParameter());
48                                                                                       put("time", new StringParameter());}});
49         // @formatter:on
50         assertEquals(readJson("action.json"), gson.toJson(action));
51     }
52
53     @Test
54     public void testDeserialize() throws Exception
55     {
56         try (FileReader reader = new FileReader(getTestDataFile("action.json")))
57         {
58             Action action = gson.fromJson(reader, Action.class);
59             SortedMap<String, Parameter> parameters = action.getParameters();
60             assertNotNull(parameters);
61             assertEquals(2, parameters.size());
62
63             Iterator<String> parametersIter = parameters.keySet().iterator();
64             assertEquals("enabled", parametersIter.next());
65             assertEquals("time", parametersIter.next());
66         }
67     }
68 }