]> git.basschouten.com Git - openhab-addons.git/blob
902b8c09a365487eca2fb12818b7b095af9ac1d5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.*;
16
17 import java.io.FileReader;
18 import java.util.Iterator;
19 import java.util.SortedMap;
20 import java.util.TreeMap;
21
22 import org.junit.jupiter.api.BeforeAll;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.lametrictime.internal.api.common.impl.GsonGenerator;
25 import org.openhab.binding.lametrictime.internal.api.test.AbstractTest;
26
27 import com.google.gson.Gson;
28
29 /**
30  * action test.
31  *
32  * @author Gregory Moyer - Initial contribution
33  */
34 public class ActionTest extends AbstractTest {
35     private static Gson gson;
36
37     @BeforeAll
38     public static void setUpBeforeClass() {
39         gson = GsonGenerator.create(true);
40     }
41
42     @Test
43     @SuppressWarnings("serial")
44     public void testSerialize() throws Exception {
45         // @formatter:off
46         Action action = new Action().withParameters(new TreeMap<String, Parameter>(){{put("enabled", new BooleanParameter());
47                                                                                       put("time", new StringParameter());}});
48         // @formatter:on
49         assertEquals(readJson("action.json"), gson.toJson(action));
50     }
51
52     @Test
53     public void testDeserialize() throws Exception {
54         try (FileReader reader = new FileReader(getTestDataFile("action.json"))) {
55             Action action = gson.fromJson(reader, Action.class);
56             SortedMap<String, Parameter> parameters = action.getParameters();
57             assertNotNull(parameters);
58             assertEquals(2, parameters.size());
59
60             Iterator<String> parametersIter = parameters.keySet().iterator();
61             assertEquals("enabled", parametersIter.next());
62             assertEquals("time", parametersIter.next());
63         }
64     }
65 }