]> git.basschouten.com Git - openhab-addons.git/blob
2ee46a065f5d4b1fa83b6b0a1c6e7aff80c32584
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.miio.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.Collections;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.miio.internal.basic.Conversions;
23
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
27 import com.google.gson.JsonPrimitive;
28
29 /**
30  * Test case for {@link ConversionsTest}
31  *
32  * @author Marcel Verpaalen - Initial contribution
33  *
34  */
35 @NonNullByDefault
36 public class ConversionsTest {
37
38     @Test
39     public void getJsonElementTest() {
40
41         Map<String, Object> deviceVariables = Collections.emptyMap();
42
43         // test invalid missing element
44         String transformation = "getJsonElement";
45         JsonElement value = new JsonPrimitive("");
46         JsonElement resp = Conversions.execute(transformation, value, deviceVariables);
47         assertNotNull(resp);
48         assertEquals(value, resp);
49
50         // test invalid missing element
51         value = new JsonPrimitive("{\"test\": \"testresponse\"}");
52         resp = Conversions.execute(transformation, value, deviceVariables);
53         assertNotNull(resp);
54         assertEquals(value, resp);
55
56         transformation = "getJsonElement-test";
57
58         // test without deviceVariables
59         resp = Conversions.execute(transformation, value, null);
60         assertNotNull(resp);
61         assertEquals(new JsonPrimitive("testresponse"), resp);
62
63         // test non json
64         value = new JsonPrimitive("some non json value");
65         resp = Conversions.execute(transformation, value, deviceVariables);
66         assertNotNull(resp);
67         assertEquals(value, resp);
68
69         // test non json empty string
70         value = new JsonPrimitive("");
71         resp = Conversions.execute(transformation, value, deviceVariables);
72         assertNotNull(resp);
73         assertEquals(value, resp);
74
75         // test input as jsonString
76         value = new JsonPrimitive("{\"test\": \"testresponse\"}");
77         resp = Conversions.execute(transformation, value, deviceVariables);
78         assertNotNull(resp);
79         assertEquals(new JsonPrimitive("testresponse"), resp);
80
81         // test input as jsonObject
82         value = JsonParser.parseString("{\"test\": \"testresponse\"}");
83         resp = Conversions.execute(transformation, value, deviceVariables);
84         assertNotNull(resp);
85         assertEquals(new JsonPrimitive("testresponse"), resp);
86
87         // test input as jsonString for a number
88         value = new JsonPrimitive("{\"test\": 3}");
89         resp = Conversions.execute(transformation, value, deviceVariables);
90         assertNotNull(resp);
91         assertEquals(new JsonPrimitive(3), resp);
92
93         // test input as jsonString for a array
94         value = new JsonPrimitive("{\"test\": []}");
95         resp = Conversions.execute(transformation, value, deviceVariables);
96         assertNotNull(resp);
97         assertEquals(new JsonArray(), resp);
98
99         // test input as jsonString for a boolean
100         value = new JsonPrimitive("{\"test\": false}");
101         resp = Conversions.execute(transformation, value, deviceVariables);
102         assertNotNull(resp);
103         assertEquals(new JsonPrimitive(false), resp);
104
105         // test input as jsonObject for a number
106         value = JsonParser.parseString("{\"test\": 3}");
107         resp = Conversions.execute(transformation, value, deviceVariables);
108         assertNotNull(resp);
109         assertEquals(new JsonPrimitive(3), resp);
110
111         // test input as jsonString for non-existing element
112         value = new JsonPrimitive("{\"nottest\": \"testresponse\"}");
113         resp = Conversions.execute(transformation, value, deviceVariables);
114         assertNotNull(resp);
115         assertEquals(value, resp);
116
117         // test input as jsonString for non-existing element
118         value = JsonParser.parseString("{\"nottest\": \"testresponse\"}");
119         resp = Conversions.execute(transformation, value, deviceVariables);
120         assertNotNull(resp);
121         assertEquals(value, resp);
122     }
123 }