Skip to content

Commit beb8c62

Browse files
author
Ravi Nadahar
committed
Create tests for rules and rule templates JSON parsers
Signed-off-by: Ravi Nadahar <[email protected]>
1 parent fd51838 commit beb8c62

File tree

9 files changed

+1924
-4
lines changed

9 files changed

+1924
-4
lines changed

bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/parser/gson/RuleGSONParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import java.io.IOException;
1616
import java.io.InputStreamReader;
17-
import java.util.HashSet;
17+
import java.util.LinkedHashSet;
1818
import java.util.List;
1919
import java.util.Set;
2020

@@ -44,7 +44,7 @@ public class RuleGSONParser extends AbstractGSONParser<Rule> {
4444
public Set<Rule> parse(InputStreamReader reader) throws ParsingException {
4545
JsonReader jr = new JsonReader(reader);
4646
try {
47-
Set<Rule> rules = new HashSet<>();
47+
Set<Rule> rules = new LinkedHashSet<>();
4848
if (jr.hasNext()) {
4949
JsonToken token = jr.peek();
5050
if (JsonToken.BEGIN_ARRAY.equals(token)) {

bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/parser/gson/TemplateGSONParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import java.io.IOException;
1616
import java.io.InputStreamReader;
17-
import java.util.HashSet;
17+
import java.util.LinkedHashSet;
1818
import java.util.List;
1919
import java.util.Set;
2020

@@ -46,7 +46,7 @@ public Set<Template> parse(InputStreamReader reader) throws ParsingException {
4646
try {
4747
if (jr.hasNext()) {
4848
JsonToken token = jr.peek();
49-
Set<Template> templates = new HashSet<>();
49+
Set<Template> templates = new LinkedHashSet<>();
5050
if (JsonToken.BEGIN_ARRAY.equals(token)) {
5151
List<RuleTemplateDTO> templateDtos = gson.fromJson(jr, new TypeToken<List<RuleTemplateDTO>>() {
5252
}.getType());

bundles/org.openhab.core.automation/src/test/java/org/openhab/core/automation/internal/parser/gson/RuleGSONParserTest.java

Lines changed: 353 additions & 0 deletions
Large diffs are not rendered by default.

bundles/org.openhab.core.automation/src/test/java/org/openhab/core/automation/internal/parser/gson/TemplateGSONParserTest.java

Lines changed: 573 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"label": "Basic JSON Rule Template",
3+
"uid": "basic:json-rule-template",
4+
"tags": [
5+
"Basic"
6+
],
7+
"description": "A basic JSON rule template.",
8+
"visibility": "VISIBLE",
9+
"configDescriptions": [
10+
{
11+
"default": "80",
12+
"description": "The start level which will trigger the rule.",
13+
"label": "Start Level",
14+
"name": "startLevel",
15+
"required": true,
16+
"type": "INTEGER",
17+
"min": 40,
18+
"max": 100,
19+
"readOnly": false,
20+
"multiple": false,
21+
"advanced": false,
22+
"verify": false,
23+
"limitToOptions": true,
24+
"options": [],
25+
"filterCriteria": []
26+
}
27+
],
28+
"triggers": [
29+
{
30+
"id": "3",
31+
"configuration": {
32+
"startlevel": "{{startLevel}}"
33+
},
34+
"type": "core.SystemStartlevelTrigger"
35+
},
36+
{
37+
"id": "timeofday",
38+
"configuration": {
39+
"time": "14:05"
40+
},
41+
"type": "timer.TimeOfDayTrigger"
42+
}
43+
],
44+
"conditions": [
45+
{
46+
"inputs": {},
47+
"id": "4",
48+
"configuration": {
49+
"offset": 0
50+
},
51+
"type": "ephemeris.WeekdayCondition"
52+
},
53+
{
54+
"inputs": {},
55+
"id": "5",
56+
"configuration": {
57+
"offset": 2
58+
},
59+
"type": "ephemeris.WeekdayCondition"
60+
}
61+
],
62+
"actions": [
63+
{
64+
"inputs": {},
65+
"id": "1",
66+
"configuration": {
67+
"itemName": "SleepSetTemperature",
68+
"command": "21.0"
69+
},
70+
"type": "core.ItemCommandAction"
71+
},
72+
{
73+
"inputs": {},
74+
"id": "2",
75+
"configuration": {
76+
"sink": "webaudio",
77+
"text": "The sleep temperature has been set"
78+
},
79+
"type": "media.SayAction"
80+
}
81+
]
82+
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
{
2+
"label": "JSON Full Rule Template",
3+
"uid": "test:json-full-rule-template",
4+
"tags": [
5+
"Second Tag",
6+
"First Tag"
7+
],
8+
"description": "The description of the JSON template-based full rule",
9+
"visibility": "VISIBLE",
10+
"configDescriptions": [
11+
{
12+
"default": "A text",
13+
"description": "This is a text parameter.",
14+
"label": "Text parameter",
15+
"name": "textParam",
16+
"required": false,
17+
"type": "TEXT",
18+
"readOnly": false,
19+
"multiple": true,
20+
"multipleLimit": 3,
21+
"advanced": true,
22+
"verify": true,
23+
"limitToOptions": false,
24+
"options": [
25+
{
26+
"label": "First Option",
27+
"value": "Welcome"
28+
},
29+
{
30+
"label": "Second Option",
31+
"value": "Willkommen"
32+
}
33+
],
34+
"filterCriteria": [
35+
{
36+
"value": ".*",
37+
"name": "filter1"
38+
},
39+
{
40+
"value": "a.*",
41+
"name": "filter2"
42+
}
43+
]
44+
},
45+
{
46+
"default": "70",
47+
"description": "This is an integer parameter.",
48+
"label": "Integer parameter",
49+
"name": "integerParam",
50+
"required": true,
51+
"type": "INTEGER",
52+
"min": 60,
53+
"max": 100,
54+
"stepsize": 1,
55+
"readOnly": false,
56+
"multiple": false,
57+
"advanced": false,
58+
"verify": false,
59+
"limitToOptions": true,
60+
"unit": "%",
61+
"unitLabel": "Start Level",
62+
"options": [],
63+
"filterCriteria": []
64+
},
65+
{
66+
"context": "item",
67+
"default": "CurrentPower",
68+
"description": "This is an item parameter",
69+
"label": "Item parameter",
70+
"name": "itemParam",
71+
"required": true,
72+
"type": "TEXT",
73+
"readOnly": false,
74+
"multiple": false,
75+
"advanced": false,
76+
"verify": false,
77+
"limitToOptions": true,
78+
"options": [],
79+
"filterCriteria": []
80+
},
81+
{
82+
"default": "3.25",
83+
"description": "This is a decimal parameter.",
84+
"label": "Decimal parameter",
85+
"name": "decimalParam",
86+
"required": false,
87+
"type": "DECIMAL",
88+
"min": 0.5,
89+
"max": 11,
90+
"stepsize": 0.25,
91+
"readOnly": false,
92+
"multiple": false,
93+
"advanced": true,
94+
"verify": true,
95+
"limitToOptions": true,
96+
"options": [],
97+
"filterCriteria": []
98+
},
99+
{
100+
"default": "true",
101+
"description": "This is a boolean parameter.",
102+
"label": "Boolean parameter",
103+
"name": "booleanParam",
104+
"required": false,
105+
"type": "BOOLEAN",
106+
"readOnly": false,
107+
"multiple": false,
108+
"advanced": false,
109+
"verify": false,
110+
"limitToOptions": true,
111+
"options": [],
112+
"filterCriteria": []
113+
}
114+
],
115+
"triggers": [
116+
{
117+
"id": "first",
118+
"label": "First Trigger",
119+
"description": "The first trigger.",
120+
"configuration": {
121+
"startlevel": "{{integerParam}}"
122+
},
123+
"type": "core.SystemStartlevelTrigger"
124+
},
125+
{
126+
"id": "1",
127+
"label": "Second Trigger",
128+
"description": "The second trigger.",
129+
"configuration": {
130+
"cronExpression": "0 3/30 8 * * ? *"
131+
},
132+
"type": "timer.GenericCronTrigger"
133+
}
134+
],
135+
"conditions": [
136+
{
137+
"inputs": {},
138+
"id": "holiday",
139+
"label": "Is Holiday",
140+
"description": "It must be a holiday.",
141+
"configuration": {},
142+
"type": "ephemeris.HolidayCondition"
143+
},
144+
{
145+
"inputs": {},
146+
"id": "weekday",
147+
"configuration": {},
148+
"type": "ephemeris.WeekdayCondition"
149+
},
150+
{
151+
"inputs": {},
152+
"id": "2",
153+
"label": "Work Hours",
154+
"configuration": {
155+
"startTime": "08:00",
156+
"endTime": "16:00"
157+
},
158+
"type": "core.TimeOfDayCondition"
159+
},
160+
{
161+
"inputs": {},
162+
"id": "3",
163+
"label": "Heating Power Sufficient",
164+
"configuration": {
165+
"itemName": "{{itemParam}}",
166+
"operator": ">",
167+
"state": "50"
168+
},
169+
"type": "core.ItemStateCondition"
170+
}
171+
],
172+
"actions": [
173+
{
174+
"inputs": {},
175+
"id": "greet",
176+
"label": "Greet",
177+
"description": "Greets the person.",
178+
"configuration": {
179+
"volume": 100,
180+
"sink": "enhancedjavasound",
181+
"text": "{{textParam}}"
182+
},
183+
"type": "media.SayAction"
184+
},
185+
{
186+
"inputs": {},
187+
"id": "4",
188+
"label": "Print",
189+
"description": "Gives a warm welcome.",
190+
"configuration": {
191+
"type": "application/x-ruby",
192+
"script": "puts \"Hello and welcome\"\n"
193+
},
194+
"type": "script.ScriptAction"
195+
}
196+
]
197+
}

0 commit comments

Comments
 (0)