]> git.basschouten.com Git - openhab-addons.git/blob
2eb3ff9c7d2850d43e705feedac1259a1155780d
[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.digitalstrom.internal.lib.util;
14
15 import static org.hamcrest.MatcherAssert.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.Matchers.contains;
18 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
19
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.OutputChannelEnum;
26
27 import com.google.gson.Gson;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonParser;
30
31 /**
32  * Test class for {@link DSJsonParser}
33  *
34  * @author Rouven Schürch - Initial contribution
35  *
36  */
37 @NonNullByDefault
38 class DSJsonParserTest {
39
40     @Test
41     void testParseSingleOutputchannels() {
42         JsonObject jsonObject = createJsonObject(Arrays.asList(new OutputChannel(OutputChannelEnum.BRIGHTNESS)));
43
44         List<OutputChannelEnum> channels = DSJsonParser.getOutputChannels(jsonObject);
45         assertThat(channels, contains(OutputChannelEnum.BRIGHTNESS));
46     }
47
48     @Test
49     void testParseMultipleOutputchannels() {
50         JsonObject jsonObject = createJsonObject(Arrays.asList(new OutputChannel(OutputChannelEnum.BRIGHTNESS),
51                 new OutputChannel(OutputChannelEnum.AIR_FLAP_POSITION)));
52
53         List<OutputChannelEnum> channels = DSJsonParser.getOutputChannels(jsonObject);
54         assertThat(channels, contains(OutputChannelEnum.BRIGHTNESS, OutputChannelEnum.AIR_FLAP_POSITION));
55     }
56
57     @Test
58     void testParseNoOutputchannels() {
59         JsonObject jsonObject = createJsonObject(Arrays.asList());
60         List<OutputChannelEnum> channels = DSJsonParser.getOutputChannels(jsonObject);
61         assertThat(channels, hasSize(0));
62     }
63
64     private static JsonObject createJsonObject(List<OutputChannel> channels) {
65         JsonModel model = new JsonModel(channels);
66
67         Gson gson = new Gson();
68         String json = gson.toJson(model);
69
70         return JsonParser.parseString(json).getAsJsonObject();
71     }
72 }